Class HttpChannel

  • All Implemented Interfaces:
    java.lang.Runnable, HttpOutput.Interceptor
    Direct Known Subclasses:
    HttpChannelOverFCGI, HttpChannelOverHttp, HttpChannelOverHTTP2

    public class HttpChannel
    extends java.lang.Object
    implements java.lang.Runnable, HttpOutput.Interceptor
    HttpChannel represents a single endpoint for HTTP semantic processing. The HttpChannel is both an HttpParser.RequestHandler, where it passively receives events from an incoming HTTP request, and a Runnable, where it actively takes control of the request/response life cycle and calls the application (perhaps suspending and resuming with multiple calls to run). The HttpChannel signals the switch from passive mode to active mode by returning true to one of the HttpParser.RequestHandler callbacks. The completion of the active phase is signalled by a call to HttpTransport.completed().
    • Method Detail

      • isSendError

        public boolean isSendError()
      • formatAddrOrHost

        protected java.lang.String formatAddrOrHost​(java.lang.String addr)
        Format the address or host returned from Request methods
        addr - The address or host
        Default implementation returns HostPort.normalizeHost(String)
      • newHttpOutput

        protected HttpOutput newHttpOutput()
      • addListener

        public boolean addListener​(HttpChannel.Listener listener)
        Add a transient Listener to the HttpChannel.

        Listeners added by this method will only be notified if the HttpChannel has been constructed with an instance of HttpChannel.TransientListeners as an AbstractConnector provided listener

        Transient listeners are removed after every request cycle

        listener - the listener to add
        true if the listener was added.
      • removeListener

        public boolean removeListener​(HttpChannel.Listener listener)
      • getTransientListeners

        public java.util.List<HttpChannel.Listener> getTransientListeners()
      • getBytesWritten

        public long getBytesWritten()
      • getRequests

        public long getRequests()
        the number of requests handled by this connection
      • getConnector

        public Connector getConnector()
      • getRequestLog

        public RequestLog getRequestLog()
      • setRequestLog

        public void setRequestLog​(RequestLog requestLog)
      • addRequestLog

        public void addRequestLog​(RequestLog requestLog)
      • getIdleTimeout

        public long getIdleTimeout()
        Get the idle timeout.

        This is implemented as a call to EndPoint.getIdleTimeout(), but may be overridden by channels that have timeouts different from their connections.

        the idle timeout (in milliseconds)
      • setIdleTimeout

        public void setIdleTimeout​(long timeoutMs)
        Set the idle timeout.

        This is implemented as a call to EndPoint.setIdleTimeout(long), but may be overridden by channels that have timeouts different from their connections.

        timeoutMs - the idle timeout in milliseconds
      • getServer

        public Server getServer()
      • getRequest

        public Request getRequest()
      • getResponse

        public Response getResponse()
      • getConnection

        public Connection getConnection()
      • getEndPoint

        public EndPoint getEndPoint()
      • getLocalName

        public java.lang.String getLocalName()

        Return the local name of the connected channel.

        This is the host name after the connector is bound and the connection is accepted.

        Value can be overridden by HttpConfiguration.setLocalAddress(SocketAddress).

        Note: some connectors are not based on IP networking, and default behavior here will result in a null return. Use HttpConfiguration.setLocalAddress(SocketAddress) to set the value to an acceptable host name.

        the local name, or null
      • getLocalPort

        public int getLocalPort()

        Return the Local Port of the connected channel.

        This is the port the connector is bound to and is accepting connections on.

        Value can be overridden by HttpConfiguration.setLocalAddress(SocketAddress).

        Note: some connectors are not based on IP networking, and default behavior here will result in a value of 0 returned. Use HttpConfiguration.setLocalAddress(SocketAddress) to set the value to an acceptable port.

        the local port, or 0 if unspecified
      • getLocalAddress

        public getLocalAddress()
      • getRemoteAddress

        public getRemoteAddress()
      • getServerAuthority

        public HostPort getServerAuthority()
        return the HttpConfiguration server authority override
      • continue100

        public void continue100​(int available)
        If the associated response has the Expect header set to 100 Continue, then accessing the input stream indicates that the handler/servlet is ready for the request body and thus a 100 Continue response is sent.
        available - estimate of the number of bytes that are available
        Throws: - if the InputStream cannot be created
      • recycle

        public void recycle()
      • onAsyncWaitForContent

        public void onAsyncWaitForContent()
      • onBlockWaitForContent

        public void onBlockWaitForContent()
      • onBlockWaitForContentFailure

        public void onBlockWaitForContentFailure​(java.lang.Throwable failure)
      • run

        public void run()
        Specified by:
        run in interface java.lang.Runnable
      • handle

        public boolean handle()
        True if the channel is ready to continue handling (ie it is not suspended)
      • ensureConsumeAllOrNotPersistent

        public void ensureConsumeAllOrNotPersistent()
      • handleException

        protected void handleException​(java.lang.Throwable failure)

        Sends an error 500, performing a special logic to detect whether the request is suspended, to avoid concurrent writes from the application.

        It may happen that the application suspends, and then throws an exception, while an application spawned thread writes the response content; in such case, we attempt to commit the error directly bypassing the ErrorHandler mechanisms and the response OutputStream.

        failure - the Throwable that caused the problem
      • unwrap

        protected java.lang.Throwable unwrap​(java.lang.Throwable failure,
                                             java.lang.Class<?>... targets)
        Unwrap failure causes to find target class
        failure - The throwable to have its causes unwrapped
        targets - Exception classes that we should not unwrap
        A target throwable or null
      • sendResponseAndComplete

        public void sendResponseAndComplete()
      • isExpecting100Continue

        public boolean isExpecting100Continue()
      • isExpecting102Processing

        public boolean isExpecting102Processing()
      • toString

        public java.lang.String toString()
        toString in class java.lang.Object
      • onContentComplete

        public boolean onContentComplete()
      • onTrailers

        public void onTrailers​(HttpFields trailers)
      • onRequestComplete

        public boolean onRequestComplete()
      • onCompleted

        public void onCompleted()
      • onEarlyEOF

        public boolean onEarlyEOF()
      • sendResponse

        public boolean sendResponse​(MetaData.Response info,
                                    java.nio.ByteBuffer content,
                                    boolean complete,
                                    Callback callback)
      • sendResponse

        public boolean sendResponse​(MetaData.Response info,
                                    java.nio.ByteBuffer content,
                                    boolean complete)
      • isCommitted

        public boolean isCommitted()
      • isRequestCompleted

        public boolean isRequestCompleted()
        True if the request lifecycle is completed
      • isResponseCompleted

        public boolean isResponseCompleted()
        True if the response is completely written.
      • isPersistent

        public boolean isPersistent()
      • write

        public void write​(java.nio.ByteBuffer content,
                          boolean complete,
                          Callback callback)

        Non-Blocking write, committing the response if needed.

        Called as last link in HttpOutput.Filter chain
        Specified by:
        write in interface HttpOutput.Interceptor
        content - the content buffer to write
        complete - whether the content is complete for the response
        callback - Callback when complete or failed
      • execute

        protected void execute​(java.lang.Runnable task)
      • getScheduler

        public Scheduler getScheduler()
      • useDirectBuffers

        public boolean useDirectBuffers()
        true if the HttpChannel can efficiently use direct buffer (typically this means it is not over SSL or a multiplexed protocol)
      • abort

        public void abort​(java.lang.Throwable failure)
        If a write or similar operation to this channel fails, then this method should be called.

        The standard implementation calls HttpTransport.abort(Throwable).

        failure - the failure that caused the abort.