public class HttpClientConnection extends Object
This is a low level API that may be needed in rare use cases.
Constructor and Description |
---|
HttpClientConnection(TcpConnection tcpConnection)
Create an HTTP connection, on top of the TCP connection.
|
Instance Methods | |
---|---|
Async<Void> |
send(HttpRequest request)
Send a request over this connection.
|
Async<HttpResponse> |
receive()
Receive the next non-1xx response.
|
Async<HttpResponse> |
receive0()
Receive the next response, which could be 1xx.
|
boolean |
isLastResponse()
Whether the "last" response is received.
|
TcpConnection |
getTcpConnection()
Get the underlying TCP connection.
|
Async<Void> |
close()
Close the connection and free resources.
|
public HttpClientConnection(TcpConnection tcpConnection)
public Async<Void> send(HttpRequest request)
This action completes when the request (including the body, if any) is completely written to the underlying TCP connection.
If this action fails (e.g. due to cancel
),
the connection outbound is corrupt, and further requests will fail too.
It's not necessary to wait for the completion of this action, or the receipt of the corresponding response, before sending another request. This method can be used for request pipelining.
It's not necessary to wait for the completion of this action before calling receive()/receive0(); request and response can be transmitted concurrently over this connection. Although, most servers will read the entirety of the request before sending the response.
If the request contains "Expect: 100-continue", and the server responds that the request body should not be sent, this action may fail (because the request is not completely written); nevertheless the corresponding receive()/receive0() could still succeed.
public Async<HttpResponse> receive()
This action calls receive0()
repeatedly
until a non-1xx response is received.
Intermediary 1xx responses are skipped.
public Async<HttpResponse> receive0()
This action completes as soon as the head of the response is completely received.
This method can only be called if the corresponding request has been
queued previously by send(request)
,
because, unfortunately, response framing is dependent on the request.
One request is corresponded to zero or more 1xx responses, and then one non-1xx response.
Concurrent pending receive0() is not allowed. This method can only be called after the previous response is received, and its body is drained.
If this action fails (e.g. due to cancel
),
the connection inbound is corrupt, and further receive0() will fail too.
isLastResponse()
public boolean isLastResponse()
A response could be the last one on the connection for various reasons, e.g. it contains a "Connection: close" header.
A 1xx response cannot be the "last" response.
Once the "last" response is received, the connection cannot be used for further requests/responses.
After receive()/receive0() succeeds, application should check isLastResponse(); if it's true, the connection should be closed; a new connection is needed for further requests/responses.
public TcpConnection getTcpConnection()
It's not safe to read from and write to the TCP connection, unless it's certain that there's no outstanding requests/responses on the HTTP connection.