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 class
Output 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 interface
static enum
Some HTTP response status codes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
Use chunkedTransferprivate long
private InputStream
Data of the response, may be null.private boolean
Headers for the HTTP response.private boolean
copy of the header map with all the keys lowercase for faster searching.private String
MIME type of content, e.g.private NanoHTTPD.Method
The request method that spawned this response.private NanoHTTPD.Response.IStatus
HTTP status code after processing, e.g. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Response
(NanoHTTPD.Response.IStatus status, String mimeType, InputStream data, long totalBytes) Creates a fixed length response if totalBytes>=0, otherwise chunked. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds given line to the header.void
close()
void
closeConnection
(boolean close) Indicate to close the connection after the Response has been sent.getData()
boolean
protected void
printHeader
(PrintWriter pw, String key, String value) protected void
send
(OutputStream outputStream) Sends given response to the socket.private void
sendBody
(OutputStream outputStream, long pending) Sends the body to the specified OutputStream.private void
sendBodyWithCorrectEncoding
(OutputStream outputStream, long pending) private void
sendBodyWithCorrectTransferAndEncoding
(OutputStream outputStream, long pending) protected long
sendContentLengthHeaderIfNotAlreadyPresent
(PrintWriter pw, long defaultSize) void
setChunkedTransfer
(boolean chunkedTransfer) void
setData
(InputStream data) void
setGzipEncoding
(boolean encodeAsGzip) void
setKeepAlive
(boolean useKeepAlive) void
setMimeType
(String mimeType) void
setRequestMethod
(NanoHTTPD.Method requestMethod) void
setStatus
(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:
close
in interfaceAutoCloseable
- Specified by:
close
in 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
-true
to hint connection closing,false
to let connection be closed by client.
-
isCloseConnection
public boolean isCloseConnection()- Returns:
true
if 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
-