Class Request
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intLongest accepted raw query, in characters.static final intMost parameters an accepted query may contain; empty&-separated pairs do not count. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a request without headers or body, validating the path as described by the canonical constructor.Creates a request without a known remote address, validating it as described by the canonical constructor.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.Request(String method, String path, String query, Map<String, String> headers, Body body, InetSocketAddress remoteAddress, boolean tls) Creates and validates the request.Creates a request without a query, validating it as described by the canonical constructor. -
Method Summary
Modifier and TypeMethodDescriptionbody()Returns the request body.booleanstatic RequestfromTarget(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.static RequestCreates a GET request for the supplied path.inthashCode()Returns a header value, matching the name case-insensitively.headers()Returns the request headers.booleanisSecure()Reports whether the request arrived over a TLS connection to this server.method()Returns the HTTP method.path()Returns the request path.query()Returns the raw query.Returns the first value of a query parameter.Returns every value of a query parameter in request order, decoded as byquery(String).Returns the transport peer.scheme()Returns the scheme of the transport connection.booleantls()Returns the transport TLS flag.toString()Describes the request without its query, header values, body content or remote address.Returns a copy with the supplied body.withHeaders(Map<String, String> headers) Returns a copy with the supplied headers replacing the current ones.withRemoteAddress(InetSocketAddress remoteAddress) Returns a copy with the supplied transport peer address.withTls(boolean tls) Returns a copy that records whether the carrying connection uses TLS.
-
Field Details
-
MAX_QUERY_LENGTH
public static final int MAX_QUERY_LENGTHLongest accepted raw query, in characters.- See Also:
-
MAX_QUERY_PARAMETERS
public static final int MAX_QUERY_PARAMETERSMost parameters an accepted query may contain; empty&-separated pairs do not count.- See Also:
-
-
Constructor Details
-
Request
Creates a request without headers or body, validating the path as described by the canonical constructor.- Parameters:
method- HTTP tokenpath- absolute raw path- Throws:
IllegalArgumentException- for an invalid methodInvalidRequestPathException- for a rejected path
-
Request
Creates a request without a query, validating it as described by the canonical constructor.- Parameters:
method- HTTP tokenpath- absolute raw pathheaders- header fieldsbody- request content- Throws:
IllegalArgumentException- for an invalid method or headerInvalidRequestPathException- for a rejected path
-
Request
Creates a request without a known remote address, validating it as described by the canonical constructor.- Parameters:
method- HTTP tokenpath- absolute raw pathquery- raw query without the leading?;""when absentheaders- header fieldsbody- request content- Throws:
IllegalArgumentException- for an invalid method, query or headerInvalidRequestPathException- 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 tokenpath- absolute raw pathquery- raw query without the leading?;""when absentheaders- header fieldsbody- request contentremoteAddress- resolved transport peer address, ornullwhen unknown- Throws:
IllegalArgumentException- for an invalid method, query, header or an unresolved remote addressInvalidRequestPathException- 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 ofOPTIONS *) is accepted only with the methodOPTIONSand 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,%00in 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&-separatedname=valuepairs; empty pairs are ignored, and each name and value must percent-decode (with+as a space) to well-formed UTF-8. At mostMAX_QUERY_LENGTHcharacters andMAX_QUERY_PARAMETERSparameters 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 tokenpath- absolute raw pathquery- raw query without the leading?;""when absentheaders- header fieldsbody- request contentremoteAddress- resolved transport peer address, ornullwhen unknowntls- whether the connection uses TLS- Throws:
IllegalArgumentException- for an invalid method, query, header or an unresolved remote addressInvalidRequestPathException- for a rejected path
-
-
Method Details
-
method
Returns the HTTP method.- Returns:
- the case-sensitive method token
-
path
Returns the request path.- Returns:
- the raw absolute path, or
*forOPTIONS *
-
query
Returns the raw query.- Returns:
- the query without
?, or an empty string
-
headers
Returns the request headers.- Returns:
- an immutable, case-insensitive map
-
body
Returns the request body.- Returns:
- immutable request content, never null
-
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
-
hashCode
public int hashCode() -
header
Returns a header value, matching the name case-insensitively.- Parameters:
name- header name- Returns:
- value, if present
-
query
Returns the first value of a query parameter. Names and values are percent-decoded once as UTF-8, with+decoded as a space (%2Bis 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
Returns every value of a query parameter in request order, decoded as byquery(String).- Parameters:
name- decoded parameter name- Returns:
- immutable values; empty when the parameter is absent
-
withHeaders
Returns a copy with the supplied headers replacing the current ones.- Parameters:
headers- header fields- Returns:
- request with the headers
-
withBody
Returns a copy with the supplied body.- Parameters:
body- request content- Returns:
- request with the body
-
withRemoteAddress
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, ornullfor unknown- Returns:
- request with the remote address
- Throws:
IllegalArgumentException- for an unresolved address
-
withTls
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, andX-Forwarded-Protois a header anyone can send. UseTrustedProxiesin axiom-security to decide the client-facing scheme.- Returns:
- true when the transport connection is TLS
-
scheme
Returns the scheme of the transport connection.- Returns:
httpswhenisSecure(), otherwisehttp
-
toString
Describes the request without its query, header values, body content or remote address. -
get
Creates a GET request for the supplied path.- Parameters:
path- absolute raw path- Returns:
- a GET request
-
fromTarget
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 byRequest(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 againstHost, before calling this method. Transports and the test client use this instead of splitting the target themselves.- Parameters:
method- HTTP tokentarget- origin-form request target, or*for OPTIONS- Returns:
- a request for the target's path and query
- Throws:
InvalidRequestPathException- for a rejected pathIllegalArgumentException- for an invalid method or query
-