Class Request

java.lang.Object
dev.relism.flash.models.Request

public class Request extends Object
Immutable view of an incoming HTTP/1.1 request. Constructed by RequestParser and passed directly to route handlers; never modified 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
 });
 
  • Constructor Details

    • Request

      public Request(RequestLine requestLine, byte[] body)
      Test / manual constructor — remoteAddress() returns null.
  • Method Details

    • forParsed

      public static Request forParsed(RequestLine requestLine, InputStream stream, long contentLength, byte[] headerBuf, int bodyStart, int preBufLen, InetSocketAddress remoteAddress)
    • method

      public HttpMethod method()
      HTTP method (GET, POST, …).
    • path

      public String path()
      Request path decoded as UTF-8. Includes a leading slash; never includes the query string. Example: a request for /users/42?page=1 returns "/users/42".
    • header

      public String header(String name)
      Returns the first value of header name, or null if absent. Lookup is case-insensitive ("content-type" and "Content-Type" are equivalent).
    • headers

      public List<String> headers(String name)
      Returns all values of header name in 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

      public List<String> headers()
      Returns all header values in declaration order, one entry per header line. Useful for debugging; for targeted access prefer header(String).
    • param

      public String param(String name)
      Returns the path parameter named name, or null. Parameters are declared in the route pattern (e.g. /users/{id}) and injected by the router before the handler runs. Returns null if this route has no such parameter or the route is not parametric.
    • query

      public String query(String name)
      Returns the first query parameter named name, or null. The query string is parsed lazily on the first call and cached for the request lifetime. For ?a=1&a=2, returns "1".
    • queries

      public List<String> queries(String name)
      Returns all query parameters named name in declaration order. For ?tag=a&tag=b, returns ["a", "b"]. Returns an empty list if the parameter is absent.
    • remoteAddress

      public InetSocketAddress remoteAddress()
      Returns the remote socket address of the connected client, or null for test-constructed requests.

      The InetSocketAddress is the JDK object created during ServerSocket.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();
       
    • body

      public RequestBody body()
      Returns the request body accessor. Use RequestBody.bytes() to materialise the full body or RequestBody.stream() for zero-copy streaming access. The two modes are mutually exclusive per request.
    • drain

      public void drain()
      Discards unread body bytes; called by the server after each request on keep-alive connections.
    • headerEquals

      public boolean headerEquals(String name, String value)
      Internal: case-insensitive header value comparison used by the server keep-alive logic.