Class HttpServer.Response
- Enclosing class:
HttpServer
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpServer.ResponseA response with headers but no body, for 304 and for HEAD.static HttpServer.ResponseA response whose body is a file.intAdds a response header and returns this Response.static HttpServer.Responsestatic HttpServer.ResponseA JSON response serialised straight into the connection's write buffer.static HttpServer.Responsestream(int status, String contentType, HttpServer.BodyWriter writer) A response generated as it is downloaded.static HttpServer.Response
-
Constructor Details
-
Response
-
Response
A body AND application headers, which nothing public could express.
The constructor above always passed null for them, empty() takes headers but discards the body, and file() wants a descriptor -- so a handler returning JSON with a Set-Cookie, a CORS header or a cache directive had no supported way to say so, even though both protocol writers send extra headers. Server-owned names are still refused at write time.
-
-
Method Details
-
stream
public static HttpServer.Response stream(int status, String contentType, HttpServer.BodyWriter writer) A response generated as it is downloaded. Its length is unknown: HTTP/1.1 uses chunked framing, HTTP/1.0 closes after the body, and HTTP/2 uses DATA frames with flow control. A generation failure aborts the response; headers have already been sent, so it cannot be replaced with a successful error body. -
file
public static HttpServer.Response file(int status, String contentType, int fd, long offset, long length, Map extraHeaders) A response whose body is a file. The server sends it with sendfile where the platform has it, so the bytes never enter user space, and CLOSES the descriptor when it is done -- a handler that returned one must not. -
text
-
json
-
jsonValue
A JSON response serialised straight into the connection's write buffer.
Prefer this to json(status, Json.write(value)): that builds a StringBuilder, grows its char[], copies it into a String and encodes that to bytes, for a document about to go to a socket and be discarded. Deliberately a DIFFERENT NAME rather than an overload taking Object -- an overload would bind a String-typed variable to this method and double-encode it, which is exactly the kind of thing that is found in production rather than in review.
-
empty
A response with headers but no body, for 304 and for HEAD. -
getStatus
public int getStatus() -
header
Adds a response header and returns this Response. A copy is made rather than writing into the map the Response was built with, which may be a caller's constant; a header the server owns -- Date, Content-Length -- is refused when the response is written.
-