TsgcWebSocketServer_HTTPAPI | Enviar Respuesta de Archivo

Use la propiedad FileName del objeto THttpServerResponse si desea enviar un archivo como respuesta a una solicitud HTTP.

 


procedure OnHTTPRequest(aConnection: TsgcWSConnection_HTTPAPI; 
	const aRequestInfo: THttpServerRequest; 
	var aResponseInfo: THttpServerResponse);
begin
  if aRequestInfo.Method = 'GET' then
  begin
    if aRequestInfo.Document = '/test.zip' then
	begin
	  aResponseInfo.ResponseNo := 200;
	  aResponseInfo.FileName := 'c:\download\test.zip';
	  aResponseInfo.ContentType := 'application/zip';
	end
	else
	  aResponseInfo.ResponseNo := 404;
  end
  else 
    aResponseInfo.ResponseNo := 500;
end;

Descargas Reanudables

Una respuesta HTTP 206 Partial Content se utiliza cuando un servidor satisface una solicitud de una porción específica (rango) de un recurso, en lugar de enviar el archivo completo. Se usa habitualmente para descargas reanudables, streaming de medios y transferencias de archivos de gran tamaño.

Cómo funciona:

 

El cliente solicita un recurso parcial: El cliente (navegador, descargador o reproductor multimedia) envía un encabezado Range que especifica el rango de bytes que desea. Ejemplo de solicitud:

 

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

 

Esto solicita los bytes 1000 al 5000 de video.mp4.

 

El servidor responde con HTTP 206: Si el servidor admite solicitudes de rango, responde con 206 Partial Content e incluye una cabecera Content-Range. Ejemplo de respuesta:

 

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

 

La cabecera Content-Range muestra:

 

El rango servido (1000-5000)

El tamaño total del archivo (1 000 000 bytes).

El encabezado Content-Length indica el tamaño de la porción devuelta (4001 bytes).

 

El cliente puede solicitar más fragmentos:

El cliente puede enviar múltiples solicitudes para diferentes partes.

Esto permite descargas reanudables y streaming eficiente.

 


procedure OnHTTPRequest(aConnection: TsgcWSConnection_HTTPAPI;
const aRequestInfo: THttpServerRequest; var aResponseInfo: THttpServerResponse);
var
  oStream: TFileStream;
  oRanges: TIdEntityRanges;
begin
  oStream := TFileStream.Create('test.pdf', fmOpenRead);
  oRanges := TIdEntityRanges.Create(nil);
  Try
    oRanges.Text := aRequestInfo.Range;
    aResponseInfo.ContentType := 'application/pdf';
    if oRanges.Count > 0 then
    begin
      aResponseInfo.ResponseNo := 206;
      aResponseInfo.AcceptRanges := 'bytes';
      aResponseInfo.ContentRangeStart := oRanges[0].StartPos;
      aResponseInfo.ContentRangeEnd := oRanges[0].EndPos;
      aResponseInfo.ContentRangeInstanceLength := oStream.Size;
      aResponseInfo.ContentStream := TIdHTTPRangeStream.Create(oStream,
        aResponseInfo.ContentRangeStart, aResponseInfo.ContentRangeEnd);
    end
    else
    begin
      aResponseInfo.ResponseNo := 200;
      aResponseInfo.ContentStream := oStream;
    end;
  Finally
    oRanges.Free;
  End;
end;