Class HttpSender
- All Implemented Interfaces:
EventListener, AsyncContentProvider.Listener
- Direct Known Subclasses:
HttpSenderOverFCGI, HttpSenderOverHTTP
HttpSender abstracts the algorithm to send HTTP requests, so that subclasses only implement
the transport-specific code to send requests over the wire, implementing
sendHeaders(HttpExchange, HttpContent, Callback) and
sendContent(HttpExchange, HttpContent, Callback).
HttpSender governs two state machines.
The request state machine is updated by HttpSender as the various steps of sending a request
are executed, see RequestState.
At any point in time, a user thread may abort the request, which may (if the request has not been
completely sent yet) move the request state machine to RequestState#FAILURE.
The request state machine guarantees that the request steps are executed (by I/O threads) only if
the request has not been failed already.
The sender state machine is updated by HttpSender from three sources: deferred content notifications
(via onContent()), 100-continue notifications (via proceed(HttpExchange, Throwable))
and normal request send (via sendContent(HttpExchange, HttpContent, Callback)).
This state machine must guarantee that the request sending is never executed concurrently: only one of
those sources may trigger the call to sendContent(HttpExchange, HttpContent, Callback).
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classprivate classprivate classprivate static enumThe request statesHttpSendergoes through when sending a request.private static enumThe sender statesHttpSendergoes through when sending a request. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final HttpChannelprivate final Callbackprivate HttpContentprivate final IteratingCallbackprivate Throwableprivate final Callbackprotected static final Loggerprivate final AtomicReference<HttpSender.RequestState> private final AtomicReference<HttpSender.SenderState> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanabort(HttpExchange exchange, Throwable failure) private voidanyToFailure(Throwable failure) protected booleanbeginToHeaders(HttpExchange exchange) protected voiddispose()private voidexecuteAbort(HttpExchange exchange, Throwable failure) protected booleanexpects100Continue(Request request) protected HttpChannelprotected HttpExchangeprotected booleanheadersToCommit(HttpExchange exchange) private voidillegalSenderState(HttpSender.SenderState current) booleanisFailed()voidCallback method invoked when content is availablevoidproceed(HttpExchange exchange, Throwable failure) protected booleanqueuedToBegin(HttpExchange exchange) protected voidreset()voidsend(HttpExchange exchange) protected abstract voidsendContent(HttpExchange exchange, HttpContent content, Callback callback) Implementations should send the content at theHttpContentcursor position over the wire.protected abstract voidsendHeaders(HttpExchange exchange, HttpContent content, Callback callback) Implementations should send the HTTP headers over the wire, possibly with some content, in a single write, and notify the givencallbackof the result of this operation.protected booleansomeToContent(HttpExchange exchange, ByteBuffer content) protected booleansomeToSuccess(HttpExchange exchange) private voidterminateRequest(HttpExchange exchange) private voidterminateRequest(HttpExchange exchange, Throwable failure, Result result) toString()private booleanprivate boolean
-
Field Details
-
LOG
-
requestState
-
senderState
-
commitCallback
-
contentCallback
-
lastCallback
-
channel
-
-
failure
-
-
Constructor Details
-
HttpSender
-
-
Method Details
-
getHttpChannel
-
getHttpExchange
-
isFailed
public boolean isFailed() -
onContent
public void onContent()Description copied from interface:AsyncContentProvider.ListenerCallback method invoked when content is available- Specified by:
onContentin interfaceAsyncContentProvider.Listener
-
send
-
expects100Continue
-
queuedToBegin
-
beginToHeaders
-
headersToCommit
-
someToContent
-
someToSuccess
-
anyToFailure
-
executeAbort
-
terminateRequest
-
terminateRequest
-
sendHeaders
Implementations should send the HTTP headers over the wire, possibly with some content, in a single write, and notify the givencallbackof the result of this operation.If there is more content to send, then
sendContent(HttpExchange, HttpContent, Callback)will be invoked.- Parameters:
exchange- the exchange to sendcontent- the content to sendcallback- the callback to notify
-
sendContent
Implementations should send the content at theHttpContentcursor position over the wire.The
HttpContentcursor is advanced by HttpSender at the right time, and if more content needs to be sent, this method is invoked again; subclasses need only to send the content at theHttpContentcursor position.This method is invoked one last time when
HttpContent.isConsumed()is true and therefore there is no actual content to send. This is done to allow subclasses to write "terminal" bytes (such as the terminal chunk when the transfer encoding is chunked) if their protocol needs to.- Parameters:
exchange- the exchange to sendcontent- the content to sendcallback- the callback to notify
-
reset
protected void reset() -
dispose
protected void dispose() -
proceed
-
abort
-
updateRequestState
-
updateSenderState
-
illegalSenderState
-
toString
-