Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Stream a large request and response

Use BeginStreamed when a request or response is too large to hold in memory. This example sends a file with PUT and writes the successful response into a second stream. Set the client's host, port and trusted roots as in Download a page over HTTPS.

The client checks, on every connection, that the server's certificate is issued for the host name it connects to. Give it the DNS name the certificate carries, not the server's IP address: with an address the connection fails although the server's certificate is valid. To connect by address, set ServerName to the name in the certificate - see Download a page over HTTPS.

Transfer two streams

uses
  System.Classes, System.SysUtils, StreamSec.Mobile.HTTPSClient;
function StreamTransfer(aClient: TsmCustomHTTPSClient; const aURL: string;
  aSource, aDestination: TStream; aMaxResponseBytes: Int64;
  aWaitMS: Cardinal): Integer;
var
  lExchange: iHttpStreamedExchange;
begin
  lExchange := aClient.BeginStreamed('PUT', aURL, nil,
    aSource, False, aDestination, False, aMaxResponseBytes);
  try
    if not lExchange.WaitForEnd(aWaitMS) then begin
      if lExchange.Failed then
        raise eHTTPSClientError.Create(lExchange.FailText);
      lExchange.Cancel;
      raise eHTTPSClientError.Create('Timed out waiting for the transfer');
    end;
    Result := lExchange.Response.StatusCode;
  finally
    lExchange.DiscardBody;
  end;
end;
  1. Open the source for reading. The client sends the bytes from its current position to its end, so the source must report its size and position.
  2. Open a separate destination for writing. Its current position is where the response starts. For a file, use a fresh temporary file and publish it only after the transfer finishes with the status your application expects.
  3. Call StreamTransfer with the URL, both streams, the maximum response size in bytes and the time in milliseconds to wait after submission. The two False arguments keep ownership with the caller. Keep the streams open and leave their positions alone until the function returns or raises, then release each in a finally block.

Use files for both bodies

Pass the input path and a separate temporary output path. The function opens the output with fmCreate, which truncates an existing file. Keep any final output at a different path until the transfer succeeds.

function TransferFile(aClient: TsmCustomHTTPSClient;
  const aURL, aInputFile, aTemporaryOutput: string; aMaxResponseBytes: Int64;
  aWaitMS: Cardinal): Integer;
var
  lSource, lDestination: TFileStream;
begin
  lSource := TFileStream.Create(aInputFile, fmOpenRead or fmShareDenyWrite);
  try
    lDestination := TFileStream.Create(aTemporaryOutput, fmCreate);
    try
      Result := StreamTransfer(aClient, aURL, lSource, lDestination,
                               aMaxResponseBytes, aWaitMS);
    finally
      lDestination.Free;
    end;
  finally
    lSource.Free;
  end;
end;

Check the outcome

A 2xx response goes to the destination. The byte limit you pass to BeginStreamed applies even if the server omits Content-Length. Choose a limit that fits the destination. Zero permits a response of any size.

For a redirect or an HTTP error, the function returns the status code, leaves the destination untouched and discards the response body. The client does not follow a redirect or replay the input stream. A failed transfer can leave a partial file. A timeout in this example cancels the exchange.

If the input length is unknown, use the producer interface on BeginUpload. The HTTPS client notes describe its chunk limits and how to retry a chunk when the upload queue is full.

Reference

TsmHTTPSClient · StreamSec.Mobile.HTTPSClient

Related tasks