Lably
Skip to content

HTTP compression

Capability that can be built into web servers and web clients

  • 7 min read
  • 5 chapters

Related on Lably

Through Web server, HTTP and Java (programming language)

HTTP data is compressed before it is sent from the server: compliant browsers will announce what methods are supported to the server before downloading the correct format; browsers that do not support compliant compression method will download uncompressed data. The most common compression schemes include gzip and Brotli; a full list of available schemes is maintained by the IANA.

There are two different ways compression can be done in HTTP. At a lower level, a Transfer-Encoding header field may indicate the payload of an HTTP message is compressed. At a higher level, a Content-Encoding header field may indicate that a resource being transferred, cached, or otherwise referenced is compressed. Compression using Content-Encoding is more widely supported than Transfer-Encoding, and some browsers do not advertise support for Transfer-Encoding compression to avoid triggering bugs in servers.

Compression scheme negotiation

The negotiation is done in two steps, described in RFC 2616 and RFC 9110:

1. The web client advertises which compression schemes it supports by including a list of tokens in the HTTP request. For Content-Encoding, the list is in a field called Accept-Encoding; for Transfer-Encoding, the field is called TE.

<span id="mwJQ"></span><span class="nf" id="mwJg">GET</span> <span class="nn" id="mwJw">/encrypted-area</span> <span class="kr" id="mwKA">HTTP</span><span class="o" id="mwKQ">/</span><span class="m" id="mwKg">1.1</span> <span class="na" id="mwKw">Host</span><span class="o" id="mwLA">:</span> <span class="l" id="mwLQ">www.example.com</span> <span class="hll" id="mwLg"><span class="na" id="mwLw">Accept-Encoding</span><span class="o" id="mwMA">:</span> <span class="l" id="mwMQ">gzip, deflate</span> </span>

2. If the server supports one or more compression schemes, the outgoing data may be compressed by one or more methods supported by both parties. If this is the case, the server will add a Content-Encoding or Transfer-Encoding field in the HTTP response with the used schemes, separated by commas.

<span id="mwNw"></span><span class="kr" id="mwOA">HTTP</span><span class="o" id="mwOQ">/</span><span class="m" id="mwOg">1.1</span> <span class="m" id="mwOw">200</span> <span class="ne" id="mwPA">OK</span> <span class="na" id="mwPQ">Date</span><span class="o" id="mwPg">:</span> <span class="l" id="mwPw">mon, 26 June 2016 22:38:34 GMT</span> <span class="na" id="mwQA">Server</span><span class="o" id="mwQQ">:</span> <span class="l" id="mwQg">Apache/1.3.3.7 (Unix) (Red-Hat/Linux)</span> <span class="na" id="mwQw">Last-Modified</span><span class="o" id="mwRA">:</span> <span class="l" id="mwRQ">Wed, 08 Jan 2003 23:11:55 GMT</span> <span class="na" id="mwRg">Accept-Ranges</span><span class="o" id="mwRw">:</span> <span class="l" id="mwSA">bytes</span> <span class="na" id="mwSQ">Content-Length</span><span class="o" id="mwSg">:</span> <span class="l" id="mwSw">438</span> <span class="na" id="mwTA">Connection</span><span class="o" id="mwTQ">:</span> <span class="l" id="mwTg">close</span> <span class="na" id="mwTw">Content-Type</span><span class="o" id="mwUA">:</span> <span class="l" id="mwUQ">text/html; charset=UTF-8</span> <span class="hll" id="mwUg"><span class="na" id="mwUw">Content-Encoding</span><span class="o" id="mwVA">:</span> <span class="l" id="mwVQ">gzip</span> </span>

The web server is by no means obligated to use any compression method – this depends on the internal settings of the web server and also may depend on the internal architecture of the website in question.

Sign in to keep reading

4 more chapters are free with a Lably account.

More in Computing and communication