Class RequestDecompressionPolicy

java.lang.Object
com.soklet.RequestDecompressionPolicy

@ThreadSafe public final class RequestDecompressionPolicy extends Object
Policy used by the standard HTTP server to decide whether and how gzip-compressed request bodies (Content-Encoding: gzip or x-gzip, per RFC 9110, Section 8.4) are transparently decompressed before request handling. Only a single gzip/x-gzip coding is supported; coding chains (e.g. identity, gzip) are rejected with 415 as sanctioned by RFC 9110, Section 15.5.16.

Decompression is opt-in and disabled by default; see HttpServer.Builder#requestDecompressionPolicy(RequestDecompressionPolicy). When disabled, request bodies are passed to handlers exactly as received, compressed or not.

When enabled:

  • A request with Content-Encoding: gzip or x-gzip has its body decompressed; the Content-Encoding header is removed and Content-Length is updated to the decompressed size, so handlers observe a self-consistent uncompressed request. The original encoded payload size remains available through Request.getEncodedBodySizeInBytes() for telemetry and diagnostics.
  • A request with an unsupported Content-Encoding (or multiple codings) is rejected with 415 Unsupported Media Type.
  • A request whose body cannot be decompressed is rejected with 400 Bad Request.
  • A request whose decompressed body exceeds getMaximumDecompressedBodySizeInBytes() (or, when unset, the server's maximumRequestSizeInBytes) or expands beyond getMaximumCompressionRatio() is rejected through the normal 413 Content Too Large marshaling path. These limits guard against decompression bombs; decompression aborts as soon as a limit is exceeded.
  • Content-Encoding: identity, requests without Content-Encoding, and bodyless gzip/x-gzip requests are passed through unchanged.

This applies to the standard HTTP server only. The SSE and MCP servers do not decompress request bodies, and the Simulator exercises handlers directly without transport-level decompression.

Author:
Mark Allen
  • Method Details

    • disabledInstance

      Acquires a policy that disables request decompression (the default): request bodies are passed to handlers exactly as received.
      Returns:
      a disabled request decompression policy
    • fromDefaults

      Acquires a policy that enables gzip request decompression with default limits: the decompressed body is capped by the server's maximumRequestSizeInBytes and a 100:1 compression ratio.
      Returns:
      a default request decompression policy
    • builder

      Acquires a builder for an enabled request decompression policy with custom limits.
      Returns:
      a request decompression policy builder
    • isEnabled

      Is request decompression enabled?
      Returns:
      true if gzip request bodies should be decompressed, false otherwise
    • getMaximumDecompressedBodySizeInBytes

      The maximum permitted decompressed body size in bytes, if customized.

      When empty, the server's maximumRequestSizeInBytes applies to the decompressed body.

      Returns:
      the maximum decompressed body size, or Optional.empty() to use the server's request size limit
    • getMaximumCompressionRatio

      The maximum permitted expansion ratio between decompressed and compressed body sizes.

      A request is rejected with 413 Content Too Large once its decompressed size exceeds (compressed size × ratio) + 8 KB; the additive allowance keeps legitimately small compressed bodies from tripping the ratio check.

      Returns:
      the maximum compression ratio (default 100)
    • toString

      Overrides:
      toString in class Object