Package fi.iki.elonen
Class NanoHTTPD.Response
- java.lang.Object
-
- fi.iki.elonen.NanoHTTPD.Response
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
InternalRewrite
- Enclosing class:
- NanoHTTPD
public static class NanoHTTPD.Response extends java.lang.Object implements java.io.CloseableHTTP response. Return one of these from serve().
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classNanoHTTPD.Response.ChunkedOutputStreamOutput 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 interfaceNanoHTTPD.Response.IStatusstatic classNanoHTTPD.Response.StatusSome HTTP response status codes
-
Field Summary
Fields Modifier and Type Field Description private booleanchunkedTransferUse chunkedTransferprivate longcontentLengthprivate java.io.InputStreamdataData of the response, may be null.private booleanencodeAsGzipprivate java.util.Map<java.lang.String,java.lang.String>headerHeaders for the HTTP response.private booleankeepAliveprivate java.util.Map<java.lang.String,java.lang.String>lowerCaseHeadercopy of the header map with all the keys lowercase for faster searching.private java.lang.StringmimeTypeMIME type of content, e.g.private NanoHTTPD.MethodrequestMethodThe request method that spawned this response.private NanoHTTPD.Response.IStatusstatusHTTP status code after processing, e.g.
-
Constructor Summary
Constructors Modifier Constructor Description protectedResponse(NanoHTTPD.Response.IStatus status, java.lang.String mimeType, java.io.InputStream data, long totalBytes)Creates a fixed length response if totalBytes>=0, otherwise chunked.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddHeader(java.lang.String name, java.lang.String value)Adds given line to the header.voidclose()voidcloseConnection(boolean close)Indicate to close the connection after the Response has been sent.java.io.InputStreamgetData()java.lang.StringgetHeader(java.lang.String name)java.lang.StringgetMimeType()NanoHTTPD.MethodgetRequestMethod()NanoHTTPD.Response.IStatusgetStatus()booleanisCloseConnection()protected voidprintHeader(java.io.PrintWriter pw, java.lang.String key, java.lang.String value)protected voidsend(java.io.OutputStream outputStream)Sends given response to the socket.private voidsendBody(java.io.OutputStream outputStream, long pending)Sends the body to the specified OutputStream.private voidsendBodyWithCorrectEncoding(java.io.OutputStream outputStream, long pending)private voidsendBodyWithCorrectTransferAndEncoding(java.io.OutputStream outputStream, long pending)protected longsendContentLengthHeaderIfNotAlreadyPresent(java.io.PrintWriter pw, long defaultSize)voidsetChunkedTransfer(boolean chunkedTransfer)voidsetData(java.io.InputStream data)voidsetGzipEncoding(boolean encodeAsGzip)voidsetKeepAlive(boolean useKeepAlive)voidsetMimeType(java.lang.String mimeType)voidsetRequestMethod(NanoHTTPD.Method requestMethod)voidsetStatus(NanoHTTPD.Response.IStatus status)
-
-
-
Field Detail
-
status
private NanoHTTPD.Response.IStatus status
HTTP status code after processing, e.g. "200 OK", Status.OK
-
mimeType
private java.lang.String mimeType
MIME type of content, e.g. "text/html"
-
data
private java.io.InputStream data
Data of the response, may be null.
-
contentLength
private long contentLength
-
header
private final java.util.Map<java.lang.String,java.lang.String> header
Headers for the HTTP response. Use addHeader() to add lines. the lowercase map is automatically kept up to date.
-
lowerCaseHeader
private final java.util.Map<java.lang.String,java.lang.String> lowerCaseHeader
copy of the header map with all the keys lowercase for faster searching.
-
requestMethod
private NanoHTTPD.Method requestMethod
The request method that spawned this response.
-
chunkedTransfer
private boolean chunkedTransfer
Use chunkedTransfer
-
encodeAsGzip
private boolean encodeAsGzip
-
keepAlive
private boolean keepAlive
-
-
Constructor Detail
-
Response
protected Response(NanoHTTPD.Response.IStatus status, java.lang.String mimeType, java.io.InputStream data, long totalBytes)
Creates a fixed length response if totalBytes>=0, otherwise chunked.
-
-
Method Detail
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
addHeader
public void addHeader(java.lang.String name, java.lang.String value)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
public java.io.InputStream getData()
-
getHeader
public java.lang.String getHeader(java.lang.String name)
-
getMimeType
public java.lang.String getMimeType()
-
getRequestMethod
public NanoHTTPD.Method getRequestMethod()
-
getStatus
public NanoHTTPD.Response.IStatus getStatus()
-
setGzipEncoding
public void setGzipEncoding(boolean encodeAsGzip)
-
setKeepAlive
public void setKeepAlive(boolean useKeepAlive)
-
send
protected void send(java.io.OutputStream outputStream)
Sends given response to the socket.
-
printHeader
protected void printHeader(java.io.PrintWriter pw, java.lang.String key, java.lang.String value)
-
sendContentLengthHeaderIfNotAlreadyPresent
protected long sendContentLengthHeaderIfNotAlreadyPresent(java.io.PrintWriter pw, long defaultSize)
-
sendBodyWithCorrectTransferAndEncoding
private void sendBodyWithCorrectTransferAndEncoding(java.io.OutputStream outputStream, long pending) throws java.io.IOException- Throws:
java.io.IOException
-
sendBodyWithCorrectEncoding
private void sendBodyWithCorrectEncoding(java.io.OutputStream outputStream, long pending) throws java.io.IOException- Throws:
java.io.IOException
-
sendBody
private void sendBody(java.io.OutputStream outputStream, long pending) throws java.io.IOExceptionSends 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:
java.io.IOException- if something goes wrong while sending the data.
-
setChunkedTransfer
public void setChunkedTransfer(boolean chunkedTransfer)
-
setData
public void setData(java.io.InputStream data)
-
setMimeType
public void setMimeType(java.lang.String mimeType)
-
setRequestMethod
public void setRequestMethod(NanoHTTPD.Method requestMethod)
-
setStatus
public void setStatus(NanoHTTPD.Response.IStatus status)
-
-