Class HttpPreconditions

java.lang.Object
com.jsgalactic.axiom.http.HttpPreconditions

public final class HttpPreconditions extends Object
Opt-in HTTP conditional and range request support (RFC 9110 sections 13 and 14), built on the immutable response values Axiom already produces.

 app.use(HttpPreconditions.conditional());            // evaluates 304, 412, 206 and 416
 app.use(HttpPreconditions.preconditionRequired());   // answers 428 for unsafe methods
 

conditional() calls the rest of the chain and evaluates the request against the response's ETag and Last-Modified fields; the application supplies those validators. Precedence follows RFC 9110 section 13.2.2: If-Match (strong comparison, 412 on failure), then If-Unmodified-Since when If-Match is absent (412), then If-None-Match (weak comparison; 304 for GET and HEAD, 412 otherwise), then If-Modified-Since for GET and HEAD when If-None-Match is absent (304). A mismatching If-Range makes the middleware ignore Range and keep the full 200 response.

Range is parsed strictly and only for GET: at most ByteRanges.MAX_RANGES specifications, suffix and open-ended forms, and integer overflow clamped to unsatisfiability. A single satisfiable range produces 206 with Content-Range; several produce a multipart/byteranges body with the fixed BOUNDARY. A malformed or unsatisfiable range produces 416 with an asterisk Content-Range giving the representation's total length. Every range response carries Accept-Ranges: bytes.

The evaluation applies to buffered responses after encoding: byte arrays, and strings read as UTF-8 bytes. It also applies to streamed responses that declare a total representation length and a slice writer, such as those built by StaticFiles: a range is written from the declared slices, so 206 and 416 need no buffer. An ordinary stream passes through unchanged because it has no total length; applications that need ranges over such a body must slice the data before streaming it. Responses with another status, such as the router's own 404, are also left untouched.

Both middleware are immutable and thread-safe; one instance may serve every request. evaluate(Request, Response) is the same decision as a pure function, for applications that call it directly instead of registering middleware. The status responses it builds are bodiless and keep only the response's cache validators, leaving problem responses to the application.

  • Field Details

    • BOUNDARY

      public static final String BOUNDARY
      Boundary of every multipart range body, chosen so responses are reproducible.
      See Also:
  • Method Details

    • conditional

      public static Middleware conditional()
      Returns the opt-in middleware that evaluates preconditions and ranges. Register it globally or on a route with app.use(...); it makes no change to a response that no precondition or range applies to.
      Returns:
      the shared, stateless middleware
    • preconditionRequired

      public static Middleware preconditionRequired()
      Returns the opt-in middleware that refuses unsafe methods without a precondition. Safe methods (GET, HEAD, OPTIONS and TRACE) and unsafe methods carrying If-Match, If-None-Match or If-Unmodified-Since continue to the rest of the chain. A refusal is a PreconditionRequiredException, so the runtime builds the standard problem response with code precondition_required.
      Returns:
      the shared, stateless middleware
    • evaluate

      public static Response evaluate(Request request, Response response)
      Evaluates the request's preconditions and ranges against a response.

      The response is returned unchanged when it is not successful, is an ordinary stream without a declared length, or has no buffered bytes, and when no conditional field applies. A failed precondition yields a bodiless 412 or 304 response carrying the original cache validators. A GET with a satisfiable range yields 206; with a malformed, excessive or unsatisfiable range it yields a bodiless 416 with an asterisk Content-Range. A mismatching If-Range leaves the whole 200 response intact.

      Parameters:
      request - the request whose conditional fields are evaluated
      response - the response selected for the request
      Returns:
      the response to send, never null