Package fi.iki.elonen

Class 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.Closeable
    HTTP response. Return one of these from serve().
    • Field Detail

      • 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:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.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 - 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

        public java.io.InputStream getData()
      • getHeader

        public java.lang.String getHeader​(java.lang.String name)
      • getMimeType

        public java.lang.String getMimeType()
      • 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.IOException
        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 to
        pending - -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)