Class HttpReceiver
- Direct Known Subclasses:
HttpReceiverOverFCGI, HttpReceiverOverHTTP
HttpReceiver provides the abstract code to implement the various steps of the receive of HTTP responses.
HttpReceiver maintains a state machine that is updated when the steps of receiving a response are executed.
Subclasses must handle the transport-specific details, for example how to read from the raw socket and how to parse the bytes read from the socket. Then they have to call the methods defined in this class in the following order:
responseBegin(HttpExchange), when the HTTP response data containing the HTTP status code is availableresponseHeader(HttpExchange, HttpField), when an HTTP field is availableresponseHeaders(HttpExchange), when all HTTP headers are availableresponseContent(HttpExchange, ByteBuffer, Callback), when HTTP content is availableresponseSuccess(HttpExchange), when the response is successful
responseFailure(Throwable) to indicate that the response has failed
(for example, because of I/O exceptions).
At any time, user threads may abort the response which will cause responseFailure(Throwable) to be
invoked.
The state machine maintained by this class ensures that the response steps are not executed by an I/O thread if the response has already been failed.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classWraps a list of content listeners, notifies them about content events and tracks individual listener demand to produce a global demand for content.private classImplements the decoding of content, producing decoded buffers only if there is demand for content.private static enumprivate static enumThe request statesHttpReceivergoes through when receiving a response. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final HttpChannelprivate HttpReceiver.ContentListenersprivate HttpReceiver.Decoderprivate longprivate Throwableprotected static final Loggerprivate final AtomicReference<HttpReceiver.ResponseState> private boolean -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanabort(HttpExchange exchange, Throwable failure) private voidcleanup()private booleandecodeResponseContent(ByteBuffer buffer, Callback callback) protected longdemand()(package private) voiddemand(long n) private longdemand(LongUnaryOperator operator) protected voiddispose()Disposes the state of this HttpReceiver.protected HttpChannelprotected HttpDestinationprotected HttpExchangeprotected booleanbooleanisFailed()private booleanplainResponseContent(HttpExchange exchange, ByteBuffer buffer, Callback callback) protected voidreceive()protected voidreset()Resets the state of this HttpReceiver.protected booleanresponseBegin(HttpExchange exchange) Method to be invoked when the response status code is available.protected booleanresponseContent(HttpExchange exchange, ByteBuffer buffer, Callback callback) Method to be invoked when response HTTP content is available.protected booleanresponseFailure(Throwable failure) Method to be invoked when the response is failed.protected booleanresponseHeader(HttpExchange exchange, HttpField field) Method to be invoked when a response HTTP header is available.protected booleanresponseHeaders(HttpExchange exchange) Method to be invoked after all response HTTP headers are available.protected booleanresponseSuccess(HttpExchange exchange) Method to be invoked when the response is successful.protected voidstoreCookie(URI uri, HttpField field) private voidterminateResponse(HttpExchange exchange) private voidterminateResponse(HttpExchange exchange, Result result) toString()private booleanprivate booleanupdateResponseState(HttpReceiver.ResponseState from1, HttpReceiver.ResponseState from2, HttpReceiver.ResponseState to)
-
Field Details
-
LOG
-
responseState
-
channel
-
contentListeners
-
decoder
-
failure
-
demand
private long demand -
stalled
private boolean stalled
-
-
Constructor Details
-
HttpReceiver
-
-
Method Details
-
getHttpChannel
-
demand
void demand(long n) -
demand
protected long demand() -
demand
-
hasDemandOrStall
protected boolean hasDemandOrStall() -
getHttpExchange
-
getHttpDestination
-
isFailed
public boolean isFailed() -
receive
protected void receive() -
responseBegin
Method to be invoked when the response status code is available.Subclasses must have set the response status code on the
Responseobject of theHttpExchangeprior invoking this method.This method takes case of notifying
Response.BeginListeners.- Parameters:
exchange- the HTTP exchange- Returns:
- whether the processing should continue
-
responseHeader
Method to be invoked when a response HTTP header is available.Subclasses must not have added the header to the
Responseobject of theHttpExchangeprior invoking this method.This method takes case of notifying
Response.HeaderListeners and storing cookies.- Parameters:
exchange- the HTTP exchangefield- the response HTTP field- Returns:
- whether the processing should continue
-
storeCookie
-
responseHeaders
Method to be invoked after all response HTTP headers are available.This method takes case of notifying
Response.HeadersListeners.- Parameters:
exchange- the HTTP exchange- Returns:
- whether the processing should continue
-
responseContent
Method to be invoked when response HTTP content is available.This method takes case of decoding the content, if necessary, and notifying
Response.ContentListeners.- Parameters:
exchange- the HTTP exchangebuffer- the response HTTP content buffercallback- the callback- Returns:
- whether the processing should continue
-
plainResponseContent
-
decodeResponseContent
-
responseSuccess
Method to be invoked when the response is successful.This method takes case of notifying
Response.SuccessListeners and possiblyResponse.CompleteListeners (if the exchange is completed).- Parameters:
exchange- the HTTP exchange- Returns:
- whether the response was processed as successful
-
responseFailure
Method to be invoked when the response is failed.This method takes care of notifying
Response.FailureListeners.- Parameters:
failure- the response failure- Returns:
- whether the response was processed as failed
-
terminateResponse
-
terminateResponse
-
reset
protected void reset()Resets the state of this HttpReceiver.Subclasses should override (but remember to call
super) to reset their own state.Either this method or
dispose()is called. -
dispose
protected void dispose()Disposes the state of this HttpReceiver.Subclasses should override (but remember to call
super) to dispose their own state.Either this method or
reset()is called. -
cleanup
private void cleanup() -
abort
-
updateResponseState
private boolean updateResponseState(HttpReceiver.ResponseState from1, HttpReceiver.ResponseState from2, HttpReceiver.ResponseState to) -
updateResponseState
-
toString
-