Class Request

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

public final class Request extends Object
Immutable metadata for an in-memory HTTP request. Accepted paths are compared verbatim: case, trailing slashes, and percent-encoding are preserved and nothing is decoded. Paths that could address a different resource after normalization or decoding are rejected instead of normalized; see Request(String, String).

The query is kept raw, without the leading ?; an absent and an empty query are both "". It is validated when the request is created (see Request(String, String, String, Map, Body)) and decoded on each lookup by query(String) and queryAll(String). Fragments are not modeled.

Headers are an immutable, case-insensitive map with one value per name; transports join repeated fields with ", ". The Body carries the content and its Content-Type. toString() omits the query, header values, body content and remote address, which may hold credentials or personal data.

The remote address is the transport peer: the socket address the connection came from, set by the HTTP listener. It is null for requests created in memory unless a test sets it with withRemoteAddress(InetSocketAddress). Behind a reverse proxy it is the proxy's address; forwarded client addresses are headers and must only be believed from trusted proxies.

The tls flag is true when the transport received the request over a TLS connection, set by the HTTP listener and never derived from a header; see isSecure().

This is a final class so that adding metadata does not change a record pattern's shape. Use the named accessors and withers. Equality compares all current metadata and the body's content type and bytes. Header names compare case-insensitively, as they do during lookup.

  • Field Details

    • MAX_QUERY_LENGTH

      public static final int MAX_QUERY_LENGTH
      Longest accepted raw query, in characters.
      See Also:
    • MAX_QUERY_PARAMETERS

      public static final int MAX_QUERY_PARAMETERS
      Most parameters an accepted query may contain; empty &-separated pairs do not count.
      See Also:
  • Constructor Details

    • Request

      public Request(String method, String path)
      Creates a request without headers or body, validating the path as described by the canonical constructor.
      Parameters:
      method - HTTP token
      path - absolute raw path
      Throws:
      IllegalArgumentException - for an invalid method
      InvalidRequestPathException - for a rejected path
    • Request

      public Request(String method, String path, Map<String,String> headers, Body body)
      Creates a request without a query, validating it as described by the canonical constructor.
      Parameters:
      method - HTTP token
      path - absolute raw path
      headers - header fields
      body - request content
      Throws:
      IllegalArgumentException - for an invalid method or header
      InvalidRequestPathException - for a rejected path
    • Request

      public Request(String method, String path, String query, Map<String,String> headers, Body body)
      Creates a request without a known remote address, validating it as described by the canonical constructor.
      Parameters:
      method - HTTP token
      path - absolute raw path
      query - raw query without the leading ?; "" when absent
      headers - header fields
      body - request content
      Throws:
      IllegalArgumentException - for an invalid method, query or header
      InvalidRequestPathException - for a rejected path
    • Request

      public Request(String method, String path, String query, Map<String,String> headers, Body body, InetSocketAddress remoteAddress)
      Creates a request received over a plain connection, validating it as described by the canonical constructor.
      Parameters:
      method - HTTP token
      path - absolute raw path
      query - raw query without the leading ?; "" when absent
      headers - header fields
      body - request content
      remoteAddress - resolved transport peer address, or null when unknown
      Throws:
      IllegalArgumentException - for an invalid method, query, header or an unresolved remote address
      InvalidRequestPathException - for a rejected path
    • Request

      public Request(String method, String path, String query, Map<String,String> headers, Body body, InetSocketAddress remoteAddress, boolean tls)
      Creates and validates the request. The path is either * or an absolute path. * (the asterisk-form of OPTIONS *) is accepted only with the method OPTIONS and an empty query; it addresses the server rather than a resource and is never routed. A path must start with / and contain only RFC 3986 path characters, well-formed percent-escapes, and non-ASCII characters other than controls and spaces. It is rejected when it contains an empty segment (//; a single trailing slash is allowed), a . or .. segment, a backslash, a NUL or other control character, a malformed percent-escape, or an encoded dot, slash, backslash, or NUL (%2E, %2F, %5C, %00 in either case).

      The query may contain RFC 3986 query characters (pchar, / and ?) and non-ASCII characters other than controls and spaces, and every percent-escape must be well-formed. It holds &-separated name=value pairs; empty pairs are ignored, and each name and value must percent-decode (with + as a space) to well-formed UTF-8. At most MAX_QUERY_LENGTH characters and MAX_QUERY_PARAMETERS parameters are accepted. Rejection messages never contain the query.

      Header names must be HTTP tokens and values must not contain control characters other than horizontal tab. A remote address, when present, must be resolved (carry an IP address), so that no code ever triggers a name lookup for it.

      Parameters:
      method - HTTP token
      path - absolute raw path
      query - raw query without the leading ?; "" when absent
      headers - header fields
      body - request content
      remoteAddress - resolved transport peer address, or null when unknown
      tls - whether the connection uses TLS
      Throws:
      IllegalArgumentException - for an invalid method, query, header or an unresolved remote address
      InvalidRequestPathException - for a rejected path
  • Method Details

    • method

      public String method()
      Returns the HTTP method.
      Returns:
      the case-sensitive method token
    • path

      public String path()
      Returns the request path.
      Returns:
      the raw absolute path, or * for OPTIONS *
    • query

      public String query()
      Returns the raw query.
      Returns:
      the query without ?, or an empty string
    • headers

      public Map<String,String> headers()
      Returns the request headers.
      Returns:
      an immutable, case-insensitive map
    • body

      public Body body()
      Returns the request body.
      Returns:
      immutable request content, never null
    • remoteAddress

      public InetSocketAddress remoteAddress()
      Returns the transport peer.
      Returns:
      the resolved address, or null for an unknown peer
    • tls

      public boolean tls()
      Returns the transport TLS flag.
      Returns:
      whether this request arrived over TLS
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • header

      public Optional<String> header(String name)
      Returns a header value, matching the name case-insensitively.
      Parameters:
      name - header name
      Returns:
      value, if present
    • query

      public Optional<String> query(String name)
      Returns the first value of a query parameter. Names and values are percent-decoded once as UTF-8, with + decoded as a space (%2B is a literal plus), and names match exactly after decoding. A parameter without = has the value "".
      Parameters:
      name - decoded parameter name
      Returns:
      first value, if present
    • queryAll

      public List<String> queryAll(String name)
      Returns every value of a query parameter in request order, decoded as by query(String).
      Parameters:
      name - decoded parameter name
      Returns:
      immutable values; empty when the parameter is absent
    • withHeaders

      public Request withHeaders(Map<String,String> headers)
      Returns a copy with the supplied headers replacing the current ones.
      Parameters:
      headers - header fields
      Returns:
      request with the headers
    • withBody

      public Request withBody(Body body)
      Returns a copy with the supplied body.
      Parameters:
      body - request content
      Returns:
      request with the body
    • withRemoteAddress

      public Request withRemoteAddress(InetSocketAddress remoteAddress)
      Returns a copy with the supplied transport peer address. Transports call this with the connection's peer; tests use it to simulate a client or a proxy.
      Parameters:
      remoteAddress - resolved peer address, or null for unknown
      Returns:
      request with the remote address
      Throws:
      IllegalArgumentException - for an unresolved address
    • withTls

      public Request withTls(boolean tls)
      Returns a copy that records whether the carrying connection uses TLS. Transports call this for connections they secured; tests use it to simulate HTTPS.
      Parameters:
      tls - whether the connection uses TLS
      Returns:
      request with the flag
    • isSecure

      public boolean isSecure()
      Reports whether the request arrived over a TLS connection to this server. This describes the hop from the peer only: behind a reverse proxy that terminates TLS it is false even though the client used HTTPS, and X-Forwarded-Proto is a header anyone can send. Use TrustedProxies in axiom-security to decide the client-facing scheme.
      Returns:
      true when the transport connection is TLS
    • scheme

      public String scheme()
      Returns the scheme of the transport connection.
      Returns:
      https when isSecure(), otherwise http
    • toString

      public String toString()
      Describes the request without its query, header values, body content or remote address.
      Overrides:
      toString in class Object
      Returns:
      method, path, header names and body summary
    • get

      public static Request get(String path)
      Creates a GET request for the supplied path.
      Parameters:
      path - absolute raw path
      Returns:
      a GET request
    • fromTarget

      public static Request fromTarget(String method, String target)
      Creates a request from an HTTP origin-form request target (path[?query]), or from the asterisk-form * of an OPTIONS request. The path is validated as by Request(String, String) and the query, which is retained, as by the canonical constructor. Absolute-form targets (http://host/path) are rejected here; the HTTP listener reduces them to origin-form, after checking them against Host, before calling this method. Transports and the test client use this instead of splitting the target themselves.
      Parameters:
      method - HTTP token
      target - origin-form request target, or * for OPTIONS
      Returns:
      a request for the target's path and query
      Throws:
      InvalidRequestPathException - for a rejected path
      IllegalArgumentException - for an invalid method or query