Skip to main content

Module compression

Module compression 

Source
Expand description

Response compression policy.

Without a policy the server keeps its long-standing behaviour: it serves a precompressed sibling (.br, .zst, .gz) of a file when the client accepts that encoding. Setting a CompressionPolicy with ServerBuilder::compression_policy adds the checks below, in order; the first that applies decides:

  1. the policy, or the matched route (Route::compression), disables compression: the plain file is served;
  2. the client accepts none of br, zstd or gzip (q=0 counts as not accepted);
  3. the content type is not in CompressionPolicy::compressible_types, which leaves already-compressed formats such as PNG or ZIP alone;
  4. the file is smaller than CompressionPolicy::min_size_bytes;
  5. an accepted precompressed sibling exists: it is served;
  6. with the compression feature and CompressionPolicy::dynamic_gzip, and no Range header, the body is gzip-compressed on the fly.

Each decision is counted; stats returns the counters and the /metrics endpoint prints them.

§Examples

use http_handle::compression::CompressionPolicy;
use http_handle::Server;

let server = Server::builder()
    .address("127.0.0.1:0")
    .document_root(".")
    .compression_policy(CompressionPolicy {
        min_size_bytes: 512,
        ..CompressionPolicy::default()
    })
    .build()
    .expect("server");
assert!(server.compression_policy().is_some());

Structs§

CompressionPolicy
When and how responses are compressed.
CompressionStats
Counts of compression decisions since the process started.

Functions§

stats
Returns the compression decision counters.