Class HttpChannel
- java.lang.Object
-
- org.eclipse.jetty.server.HttpChannel
-
- All Implemented Interfaces:
java.lang.Runnable,HttpOutput.Interceptor
- Direct Known Subclasses:
HttpChannelOverFCGI,HttpChannelOverHttp
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().
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static interfaceHttpChannel.Dispatchablestatic interfaceHttpChannel.ListenerListener forHttpChannelevents.private classHttpChannel.Send100Callbackprivate classHttpChannel.SendCallbackstatic classHttpChannel.TransientListenersDeprecated.
-
Field Summary
Fields Modifier and Type Field Description private HttpChannel.Listener_combinedListenerprivate MetaData.Response_committedMetaDataprivate HttpConfiguration_configurationprivate Connector_connectorprivate EndPoint_endPointprivate java.util.concurrent.Executor_executorprivate long_oldIdleTimeoutprivate Request_requestprivate RequestLog_requestLogprivate java.util.concurrent.atomic.AtomicLong_requestsprivate Response_responseprivate HttpChannelState_stateprivate HttpFields_trailersprivate java.util.function.Supplier<HttpFields>_trailerSupplierprivate java.util.List<HttpChannel.Listener>_transientListenersDeprecated.private HttpTransport_transportprivate long_writtenBytes written after interception (eg after compression)private static LoggerLOGstatic HttpChannel.ListenerNOOP_LISTENER
-
Constructor Summary
Constructors Constructor Description HttpChannel(Connector connector, HttpConfiguration configuration, EndPoint endPoint, HttpTransport transport)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidabort(java.lang.Throwable failure)If a write or similar operation to this channel fails, then this method should be called.booleanaddListener(HttpChannel.Listener listener)Deprecated.voidaddRequestLog(RequestLog requestLog)protected voidcommit(MetaData.Response info)voidcontinue100(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.private voiddispatch(javax.servlet.DispatcherType type, HttpChannel.Dispatchable dispatchable)voidensureConsumeAllOrNotPersistent()protected voidexecute(java.lang.Runnable task)protected java.lang.StringformatAddrOrHost(java.lang.String addr)Format the address or host returned from Request methodsByteBufferPoolgetByteBufferPool()longgetBytesWritten()MetaData.ResponsegetCommittedMetaData()ConnectiongetConnection()ConnectorgetConnector()EndPointgetEndPoint()HttpConfigurationgetHttpConfiguration()HttpTransportgetHttpTransport()longgetIdleTimeout()Get the idle timeout.java.net.InetSocketAddressgetLocalAddress()java.lang.StringgetLocalName()Return the local name of the connected channel.intgetLocalPort()Return the Local Port of the connected channel.HttpOutput.InterceptorgetNextInterceptor()java.net.InetSocketAddressgetRemoteAddress()RequestgetRequest()RequestLoggetRequestLog()longgetRequests()ResponsegetResponse()SchedulergetScheduler()ServergetServer()HostPortgetServerAuthority()HttpChannelStategetState()java.util.List<HttpChannel.Listener>getTransientListeners()Deprecated.booleanhandle()protected voidhandleException(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.booleanisCommitted()booleanisExpecting100Continue()booleanisExpecting102Processing()booleanisOptimizedForDirectBuffers()booleanisPersistent()booleanisRequestCompleted()booleanisResponseCompleted()booleanisSendError()protected HttpInputnewHttpInput(HttpChannelState state)protected HttpOutputnewHttpOutput()private voidnotifyEvent1(java.util.function.Function<HttpChannel.Listener,java.util.function.Consumer<Request>> function, Request request)private voidnotifyEvent2(java.util.function.Function<HttpChannel.Listener,java.util.function.BiConsumer<Request,java.lang.Throwable>> function, Request request, java.lang.Throwable failure)private voidnotifyEvent2(java.util.function.Function<HttpChannel.Listener,java.util.function.BiConsumer<Request,java.nio.ByteBuffer>> function, Request request, java.nio.ByteBuffer content)voidonAsyncWaitForContent()voidonBadMessage(BadMessageException failure)voidonBlockWaitForContent()voidonBlockWaitForContentFailure(java.lang.Throwable failure)voidonCompleted()booleanonContent(HttpInput.Content content)booleanonContentComplete()booleanonEarlyEOF()voidonRequest(MetaData.Request request)booleanonRequestComplete()voidonTrailers(HttpFields trailers)voidrecycle()booleanremoveListener(HttpChannel.Listener listener)Deprecated.voidresetBuffer()Reset the buffers.voidrun()booleansendResponse(MetaData.Response info, java.nio.ByteBuffer content, boolean complete)booleansendResponse(MetaData.Response info, java.nio.ByteBuffer content, boolean complete, Callback callback)voidsendResponseAndComplete()voidsetIdleTimeout(long timeoutMs)Set the idle timeout.voidsetRequestLog(RequestLog requestLog)java.lang.StringtoString()protected java.lang.Throwableunwrap(java.lang.Throwable failure, java.lang.Class<?>... targets)Unwrap failure causes to find target classbooleanuseDirectBuffers()voidwrite(java.nio.ByteBuffer content, boolean complete, Callback callback)Non-Blocking write, committing the response if needed.
-
-
-
Field Detail
-
NOOP_LISTENER
public static HttpChannel.Listener NOOP_LISTENER
-
LOG
private static final Logger LOG
-
_requests
private final java.util.concurrent.atomic.AtomicLong _requests
-
_connector
private final Connector _connector
-
_executor
private final java.util.concurrent.Executor _executor
-
_configuration
private final HttpConfiguration _configuration
-
_endPoint
private final EndPoint _endPoint
-
_transport
private final HttpTransport _transport
-
_state
private final HttpChannelState _state
-
_request
private final Request _request
-
_response
private final Response _response
-
_combinedListener
private final HttpChannel.Listener _combinedListener
-
_transientListeners
@Deprecated private final java.util.List<HttpChannel.Listener> _transientListeners
Deprecated.
-
_trailers
private HttpFields _trailers
-
_trailerSupplier
private final java.util.function.Supplier<HttpFields> _trailerSupplier
-
_committedMetaData
private MetaData.Response _committedMetaData
-
_requestLog
private RequestLog _requestLog
-
_oldIdleTimeout
private long _oldIdleTimeout
-
_written
private long _written
Bytes written after interception (eg after compression)
-
-
Constructor Detail
-
HttpChannel
public HttpChannel(Connector connector, HttpConfiguration configuration, EndPoint endPoint, HttpTransport transport)
-
-
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- Parameters:
addr- The address or host- Returns:
- Default implementation returns
HostPort.normalizeHost(String)
-
newHttpInput
protected HttpInput newHttpInput(HttpChannelState state)
-
newHttpOutput
protected HttpOutput newHttpOutput()
-
getState
public HttpChannelState getState()
-
addListener
@Deprecated public boolean addListener(HttpChannel.Listener listener)
Deprecated.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.TransientListenersas anAbstractConnectorprovided listenerTransient listeners are removed after every request cycle
- Parameters:
listener- the listener to add- Returns:
- true if the listener was added.
-
removeListener
@Deprecated public boolean removeListener(HttpChannel.Listener listener)
Deprecated.
-
getTransientListeners
@Deprecated public java.util.List<HttpChannel.Listener> getTransientListeners()
Deprecated.
-
getBytesWritten
public long getBytesWritten()
-
getRequests
public long getRequests()
- Returns:
- the number of requests handled by this connection
-
getConnector
public Connector getConnector()
-
getHttpTransport
public HttpTransport getHttpTransport()
-
getRequestLog
public RequestLog getRequestLog()
-
setRequestLog
public void setRequestLog(RequestLog requestLog)
-
addRequestLog
public void addRequestLog(RequestLog requestLog)
-
getCommittedMetaData
public MetaData.Response getCommittedMetaData()
-
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.- Returns:
- 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.- Parameters:
timeoutMs- the idle timeout in milliseconds
-
getByteBufferPool
public ByteBufferPool getByteBufferPool()
-
getHttpConfiguration
public HttpConfiguration getHttpConfiguration()
-
isOptimizedForDirectBuffers
public boolean isOptimizedForDirectBuffers()
- Specified by:
isOptimizedForDirectBuffersin interfaceHttpOutput.Interceptor- Returns:
- True if the Interceptor is optimized to receive direct
ByteBuffers in theHttpOutput.Interceptor.write(ByteBuffer, boolean, Callback)method. If false is returned, then passing direct buffers may cause inefficiencies.
-
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.- Returns:
- 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.- Returns:
- the local port, or 0 if unspecified
-
getLocalAddress
public java.net.InetSocketAddress getLocalAddress()
-
getRemoteAddress
public java.net.InetSocketAddress getRemoteAddress()
-
getServerAuthority
public HostPort getServerAuthority()
- Returns:
- return the HttpConfiguration server authority override
-
continue100
public void continue100(int available) throws java.io.IOExceptionIf 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.- Parameters:
available- estimate of the number of bytes that are available- Throws:
java.io.IOException- 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:
runin interfacejava.lang.Runnable
-
handle
public boolean handle()
- Returns:
- True if the channel is ready to continue handling (ie it is not suspended)
-
ensureConsumeAllOrNotPersistent
public void ensureConsumeAllOrNotPersistent()
-
dispatch
private void dispatch(javax.servlet.DispatcherType type, HttpChannel.Dispatchable dispatchable) throws java.io.IOException, javax.servlet.ServletException- Throws:
java.io.IOExceptionjavax.servlet.ServletException
-
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
ErrorHandlermechanisms and the response OutputStream.- Parameters:
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- Parameters:
failure- The throwable to have its causes unwrappedtargets- Exception classes that we should not unwrap- Returns:
- A target throwable or null
-
sendResponseAndComplete
public void sendResponseAndComplete()
-
isExpecting100Continue
public boolean isExpecting100Continue()
-
isExpecting102Processing
public boolean isExpecting102Processing()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
onRequest
public void onRequest(MetaData.Request request)
-
onContent
public boolean onContent(HttpInput.Content content)
-
onContentComplete
public boolean onContentComplete()
-
onTrailers
public void onTrailers(HttpFields trailers)
-
onRequestComplete
public boolean onRequestComplete()
-
onCompleted
public void onCompleted()
-
onEarlyEOF
public boolean onEarlyEOF()
-
onBadMessage
public void onBadMessage(BadMessageException failure)
-
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) throws java.io.IOException
- Throws:
java.io.IOException
-
commit
protected void commit(MetaData.Response info)
-
isCommitted
public boolean isCommitted()
-
isRequestCompleted
public boolean isRequestCompleted()
- Returns:
- True if the request lifecycle is completed
-
isResponseCompleted
public boolean isResponseCompleted()
- Returns:
- 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:
writein interfaceHttpOutput.Interceptor- Parameters:
content- the content buffer to writecomplete- whether the content is complete for the responsecallback- Callback when complete or failed
-
resetBuffer
public void resetBuffer()
Description copied from interface:HttpOutput.InterceptorReset the buffers.If the Interceptor contains buffers then reset them.
- Specified by:
resetBufferin interfaceHttpOutput.Interceptor
-
getNextInterceptor
public HttpOutput.Interceptor getNextInterceptor()
- Specified by:
getNextInterceptorin interfaceHttpOutput.Interceptor- Returns:
- The next Interceptor in the chain or null if this is the last Interceptor in the chain.
-
execute
protected void execute(java.lang.Runnable task)
-
getScheduler
public Scheduler getScheduler()
-
useDirectBuffers
public boolean useDirectBuffers()
- Returns:
- 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).- Parameters:
failure- the failure that caused the abort.
-
notifyEvent1
private void notifyEvent1(java.util.function.Function<HttpChannel.Listener,java.util.function.Consumer<Request>> function, Request request)
-
notifyEvent2
private void notifyEvent2(java.util.function.Function<HttpChannel.Listener,java.util.function.BiConsumer<Request,java.nio.ByteBuffer>> function, Request request, java.nio.ByteBuffer content)
-
notifyEvent2
private void notifyEvent2(java.util.function.Function<HttpChannel.Listener,java.util.function.BiConsumer<Request,java.lang.Throwable>> function, Request request, java.lang.Throwable failure)
-
-