Package dev.relism.flash.models
Class Request
java.lang.Object
dev.relism.flash.models.Request
View of an incoming HTTP/1.1 request. Constructed once per connection by
RequestParser
and repositioned (never reallocated) for every request on that connection — never modified by
user code after creation (path/query params are injected once by the router before the
handler runs).
server.get("/users/{id}", (req, res) -> {
String id = req.param("id"); // /users/42 → "42"
String page = req.query("page"); // ?page=2 → "2"
String accept = req.header("Accept"); // first Accept value
byte[] body = req.body().bytes(); // materialise body
InputStream in = req.body().stream(); // zero-copy stream
});
A Request instance is owned by its connection (HTTP/1.1) or its stream (HTTP/2) and is
recycled after the handler returns. Do not retain it — the same instance is repositioned
over the next request's data as soon as this one's handler returns. equals/
hashCode are the inherited identity-based Object versions and are meaningless
across requests (compare two different Requests from the same connection and they may
be == to each other despite describing entirely different requests, at different
points in time). String values returned by path(), header(String),
param(String), query(String) are independent heap copies and are always safe
to retain past the handler.
Dev-mode use-after-recycle guard
WhenFlash.DEV is true, every accessor checks that this instance is still the
one currently being handled; a call after the handler has already returned (e.g. from a
captured reference in an async callback, a CompletableFuture
continuation, or a background thread) throws IllegalStateException immediately,
loudly, and at the exact call site that misused it — instead of silently reading whatever the
next (or a completely different) request happened to reset this instance to. In production
this check is a single boolean field read gated behind a static final flag the
the measured cost.-
Constructor Summary
ConstructorsConstructorDescriptionRequest()Pooled instance, populated later viareset(dev.relism.flash.models.RequestLine, dev.relism.flash.models.RequestBody, java.net.InetSocketAddress, javax.net.ssl.SSLSocket).Request(RequestLine requestLine, byte[] body) Test / manual constructor —remoteAddress()returnsnull,isSecure()isfalse. -
Method Summary
Modifier and TypeMethodDescriptionbody()Returns the request body accessor.voiddrain()Discards unread body bytes; called by the server after each request on keep-alive connections.static RequestforParsed(Request pooled, RequestLine requestLine, RequestBody body, InetSocketAddress remoteAddress, SSLSocket sslSocket) Repositionspooledover a freshly-parsed request.Internal: path parameters injected by the router, ornullif none matched.Internal: the parsed request line (method, path, query, protocol, headers).Returns the first value of headername, ornullif absent.booleanheaderEquals(String name, String value) Internal: case-insensitive header value comparison used by the server keep-alive logic.headers()Returns all header values in declaration order, one entry per header line.Returns all values of headernamein declaration order.booleanisSecure()Whether this request arrived over TLS (HTTPS).method()HTTP method (GET,POST, …).Returns the path parameter namedname, ornull.path()Request path decoded as UTF-8.Returns all query parameters namednamein declaration order.Returns the first query parameter namedname, ornull.voidrecycle()Marks this instance unsafe for further use.Returns the remote socket address of the connected client, ornullfor test-constructed requests.voidsetTrailers(HeaderView trailers) Internal protocol hook that supplies the request's trailer collection.Returns the TLS session for this connection, ornullfor plain HTTP.toString()trailers()Returns request trailers after the body has been consumed completely.
-
Constructor Details
-
Request
public Request()Pooled instance, populated later viareset(dev.relism.flash.models.RequestLine, dev.relism.flash.models.RequestBody, java.net.InetSocketAddress, javax.net.ssl.SSLSocket). One per connection — seeRequestParser. -
Request
Test / manual constructor —remoteAddress()returnsnull,isSecure()isfalse.
-
-
Method Details
-
recycle
public void recycle()Marks this instance unsafe for further use. Called by the connection driver (e.g.Http1Connection) once the handler (and any automatic post-handler work, e.g.drain()) has finished with it, before the connection loop reuses it for the next request —publicbecause the connection driver lives in a different package (matchingRequestLine.reset(dev.relism.flash.http.HttpMethod, dev.relism.fpr.core.ByteView, dev.relism.fpr.core.ByteView, dev.relism.fpr.core.ByteView, dev.relism.flash.models.HeaderView)'s own precedent), not because user code should ever call it. A no-op in production beyond the field write — see the class Javadoc's dev-mode guard section. -
forParsed
public static Request forParsed(Request pooled, RequestLine requestLine, RequestBody body, InetSocketAddress remoteAddress, SSLSocket sslSocket) Repositionspooledover a freshly-parsed request.bodyis already fully configured by the caller (RequestParser, which owns and resets its own pooled method's only job is wiring it,requestLine, and the connection identity fields intopooled. -
setTrailers
Internal protocol hook that supplies the request's trailer collection. -
method
HTTP method (GET,POST, …). -
path
Request path decoded as UTF-8. Includes a leading slash; never includes the query string. Example: a request for/users/42?page=1returns"/users/42". -
header
Returns the first value of headername, ornullif absent. Lookup is case-insensitive ("content-type"and"Content-Type"are equivalent). -
headers
Returns all values of headernamein declaration order. Useful for headers that appear multiple times (e.g.Accept,Cookie). Lookup is case-insensitive. Returns an empty list if the header is absent. -
headers
Returns all header values in declaration order, one entry per header line. Useful for debugging; for targeted access preferheader(String). -
param
Returns the path parameter namedname, ornull. Parameters are declared in the route pattern (e.g./users/{id}) and injected by the router before the handler runs. Returnsnullif this route has no such parameter or the route is not parametric. -
query
Returns the first query parameter namedname, ornull. The query string is parsed lazily on the first call and cached for the request lifetime. For?a=1&a=2, returns"1". -
queries
Returns all query parameters namednamein declaration order. For?tag=a&tag=b, returns["a", "b"]. Returns an empty list if the parameter is absent. -
remoteAddress
Returns the remote socket address of the connected client, ornullfor test-constructed requests.The
InetSocketAddressis the JDK object created duringServerSocket.accept()— no allocation occurs here. To obtain the IP string (lazy, allocates once):InetSocketAddress addr = req.remoteAddress(); if (addr != null) String ip = addr.getAddress().getHostAddress(); -
isSecure
public boolean isSecure()Whether this request arrived over TLS (HTTPS). -
sslSession
Returns the TLS session for this connection, ornullfor plain HTTP. Gives access toSSLSession.getPeerCertificates()(mTLS — the caller's certificate chain, ifTlsConfig.clientAuthrequired or requested one), and toSSLSession.getCipherSuite()/SSLSession.getProtocol()for logging and diagnostics.nullrather than throwing whenisSecure()isfalse— check that first, or just null-check the result. -
body
Returns the request body accessor. UseRequestBody.bytes()to materialise the full body orRequestBody.stream()for zero-copy streaming access. The two modes are mutually exclusive per request. -
trailers
Returns request trailers after the body has been consumed completely.- Throws:
IllegalStateException- when called before the body reaches EOF
-
drain
public void drain()Discards unread body bytes; called by the server after each request on keep-alive connections. -
getRequestLine
Internal: the parsed request line (method, path, query, protocol, headers). -
getPathParams
Internal: path parameters injected by the router, ornullif none matched. -
headerEquals
Internal: case-insensitive header value comparison used by the server keep-alive logic. -
toString
-