HTTP Compression

Supported by

TsgcWebSocketHTTPServer

HTTPCompression compresses HTTP/1.1 responses with gzip or deflate before they are sent to the client, when the client advertises support for it using the Accept-Encoding request header. It applies both to static files served from DocumentRoot and to responses you build yourself in OnCommandGet. It is disabled by default, so existing applications are not affected until you turn it on.

This is a different feature from Compression, which compresses WebSocket messages using the PerMessage_Deflate protocol. HTTPCompression only affects plain HTTP/1.1 requests and responses; it has no effect on WebSocket traffic, and Compression has no effect on HTTP responses.

To enable it, set:

HTTPCompression / Enabled

You can also tune it:

Level — compression level from 0 (none) to 9 (maximum); default 6.

MinSize — responses smaller than this, in bytes, are not compressed; default 1024.

Algorithms — which encodings the server is allowed to use; default both gzip and deflate. When the client accepts both, gzip is preferred.

ContentTypes — list of content types that are eligible for compression, wildcards like "text/*" are supported; defaults to the common text, JSON, JavaScript, XML and SVG types.

The server negotiates the encoding from the request's Accept-Encoding header, honoring a quality value of 0 as a refusal (for example "gzip;q=0"). It never compresses a response that already has a Content-Encoding, a 204/304/1xx status, a Server-Sent Events stream, or a body that would not end up smaller. When it does compress, it sets the response's Content-Encoding header and adds Vary: Accept-Encoding so caches key on the encoding correctly.

HTTPCompression currently applies to HTTP/1.1 responses only; HTTP/2 responses are not compressed by this property. If you compress content that mixes a secret (such as a CSRF token) with attacker-influenced data over a TLS connection, be aware that response compression is what makes the BREACH class of attack possible; this is a general property of HTTP compression, not something specific to this implementation.


oServer := TsgcWebSocketHTTPServer.Create(nil);
oServer.DocumentRoot := 'C:\inetpub\wwwroot';
oServer.HTTPCompression.Enabled := true;
oServer.HTTPCompression.MinSize := 1024;
oServer.Active := true;