TsgcWebSocketServer_HTTPAPI | Send File Response

Use the FileName property of THttpServerResponse object if you want to send a filename as a response to a HTTP request.

 


procedure OnHTTPRequest(TsgcWSConnection_HTTPAPI *aConnection,
	const THttpServerRequest *aRequestInfo,
	var THttpServerResponse *aResponseInfo)
{
  if (aRequestInfo->Method == "GET") 
  {
    if aRequestInfo->Document == '/test.zip' then
	{
	  aResponseInfo->ResponseNo = 200;
	  aResponseInfo->ContentText = "c:\download\test.zip";
	  aResponseInfo->ContentType = "application/zip";
	}
	else
	{
	  aResponseInfo->ResponseNo = 404;
	}
  }
  else
  {  
    aResponseInfo->ResponseNo = 500;
  }
}

Resumable Downloads

An HTTP 206 Partial Content response is used when a server is fulfilling a request for a specific portion (range) of a resource, instead of sending the entire file. This is commonly used for resumable downloads, media streaming, and large file transfers. 

How it works:

 

Client Requests a Partial Resource: The client (browser, downloader, or media player) sends a Range header specifying the byte range it wants. Example request:

 

GET /video.mp4 HTTP/1.1
Host: example.com
Range: bytes=1000-5000

 

This requests bytes 1000 to 5000 of video.mp4.

 

Server Responds with HTTP 206: If the server supports range requests, it responds with 206 Partial Content and includes a Content-Range header. Example response:

 

HTTP/1.1 206 Partial Content
Content-Range: bytes 1000-5000/1000000
Content-Length: 4001
Content-Type: video/mp4

 

The Content-Range header shows:

 

The range served (1000-5000)

The total size of the file (1000000 bytes).

The Content-Length header is the size of the returned portion (4001 bytes).

 

Client Can Request More Chunks:

The client can send multiple requests for different parts.

This enables resumable downloads and efficient streaming.

 


void __fastcall OnHTTPRequest(TIdContext* AContext, TIdHTTPRequestInfo* ARequestInfo, TIdHTTPResponseInfo* AResponseInfo)
{
    std::unique_ptr<TFileStream> oStream(new TFileStream("test.pdf", fmOpenRead | fmShareDenyWrite));
    std::unique_ptr<TIdEntityRangeStrings> oRanges(new TIdEntityRangeStrings(nullptr));
    try {
        oRanges->Text = ARequestInfo->RawHeaders->Values["Range"];
        AResponseInfo->ContentType = "application/pdf";
        if (oRanges->Count > 0) {
            AResponseInfo->ResponseNo = 206; // Partial Content
            AResponseInfo->AcceptRanges = "bytes";
            AResponseInfo->ContentRangeStart = oRanges->Items[0]->StartPos;
            AResponseInfo->ContentRangeEnd = oRanges->Items[0]->EndPos;
            AResponseInfo->ContentRangeInstanceLength = oStream->Size;
            
            AResponseInfo->ContentStream = new TIdHTTPRangeStream(oStream.release(), 
                AResponseInfo->ContentRangeStart, AResponseInfo->ContentRangeEnd);
        } else {
            AResponseInfo->ResponseNo = 200; // OK
            AResponseInfo->ContentStream = oStream.release();
        }
    } catch (...) {
        // Handle exceptions
    }
}