Package fi.iki.elonen
Class NanoHTTPD.Response
java.lang.Object
fi.iki.elonen.NanoHTTPD.Response
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
InternalRewrite
- Enclosing class:
NanoHTTPD
HTTP response. Return one of these from serve().
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classOutput stream that will automatically send every write to the wrapped OutputStream according to chunked transfer: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1static interfacestatic enumSome HTTP response status codes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanUse chunkedTransferprivate longprivate InputStreamData of the response, may be null.private booleanHeaders for the HTTP response.private booleancopy of the header map with all the keys lowercase for faster searching.private StringMIME type of content, e.g.private NanoHTTPD.MethodThe request method that spawned this response.private NanoHTTPD.Response.IStatusHTTP status code after processing, e.g. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedResponse(NanoHTTPD.Response.IStatus status, String mimeType, InputStream data, long totalBytes) Creates a fixed length response if totalBytes>=0, otherwise chunked. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds given line to the header.voidclose()voidcloseConnection(boolean close) Indicate to close the connection after the Response has been sent.getData()booleanprotected voidprintHeader(PrintWriter pw, String key, String value) protected voidsend(OutputStream outputStream) Sends given response to the socket.private voidsendBody(OutputStream outputStream, long pending) Sends the body to the specified OutputStream.private voidsendBodyWithCorrectEncoding(OutputStream outputStream, long pending) private voidsendBodyWithCorrectTransferAndEncoding(OutputStream outputStream, long pending) protected longsendContentLengthHeaderIfNotAlreadyPresent(PrintWriter pw, long defaultSize) voidsetChunkedTransfer(boolean chunkedTransfer) voidsetData(InputStream data) voidsetGzipEncoding(boolean encodeAsGzip) voidsetKeepAlive(boolean useKeepAlive) voidsetMimeType(String mimeType) voidsetRequestMethod(NanoHTTPD.Method requestMethod) voidsetStatus(NanoHTTPD.Response.IStatus status)
-
Field Details
-
status
HTTP status code after processing, e.g. "200 OK", Status.OK -
mimeType
MIME type of content, e.g. "text/html" -
data
Data of the response, may be null. -
contentLength
private long contentLength -
header
Headers for the HTTP response. Use addHeader() to add lines. the lowercase map is automatically kept up to date. -
lowerCaseHeader
copy of the header map with all the keys lowercase for faster searching. -
requestMethod
The request method that spawned this response. -
chunkedTransfer
private boolean chunkedTransferUse chunkedTransfer -
encodeAsGzip
private boolean encodeAsGzip -
keepAlive
private boolean keepAlive
-
-
Constructor Details
-
Response
protected Response(NanoHTTPD.Response.IStatus status, String mimeType, InputStream data, long totalBytes) Creates a fixed length response if totalBytes>=0, otherwise chunked.
-
-
Method Details
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
addHeader
Adds given line to the header. -
closeConnection
public void closeConnection(boolean close) Indicate to close the connection after the Response has been sent.- Parameters:
close-trueto hint connection closing,falseto let connection be closed by client.
-
isCloseConnection
public boolean isCloseConnection()- Returns:
trueif connection is to be closed after this Response has been sent.
-
getData
-
getHeader
-
getMimeType
-
getRequestMethod
-
getStatus
-
setGzipEncoding
public void setGzipEncoding(boolean encodeAsGzip) -
setKeepAlive
public void setKeepAlive(boolean useKeepAlive) -
send
Sends given response to the socket. -
printHeader
-
sendContentLengthHeaderIfNotAlreadyPresent
-
sendBodyWithCorrectTransferAndEncoding
private void sendBodyWithCorrectTransferAndEncoding(OutputStream outputStream, long pending) throws IOException - Throws:
IOException
-
sendBodyWithCorrectEncoding
private void sendBodyWithCorrectEncoding(OutputStream outputStream, long pending) throws IOException - Throws:
IOException
-
sendBody
Sends the body to the specified OutputStream. The pending parameter limits the maximum amounts of bytes sent unless it is -1, in which case everything is sent.- Parameters:
outputStream- the OutputStream to send data topending- -1 to send everything, otherwise sets a max limit to the number of bytes sent- Throws:
IOException- if something goes wrong while sending the data.
-
setChunkedTransfer
public void setChunkedTransfer(boolean chunkedTransfer) -
setData
-
setMimeType
-
setRequestMethod
-
setStatus
-