mirror of
https://github.com/klzgrad/naiveproxy.git
synced 2024-11-24 06:16:30 +03:00
h2: Add support for HTTP/2 CONNECT Fast Open
SpdyProxyClientSocket waits for 200 OK before returning OK for Connect. Change that behavior to returning OK immediately after CONNECT header. This feature is enabled by a "fastopen" header via the proxy delegate. Design notes: The current approach is better than the obvious TCP Fast Open style fake Connect(). Fast Open should not be used for preconnects as preconnects need actual connections set up. The Naive client does not use preconnects per se (using "...RawConnect") but the user agent will use preconnects and the Naive client has to infer that. Hence there is a need to check the incoming socket for available bytes right before Connect() and configure whether a socket should be connected with Fast Open. But fake Connect() make it difficult to check the incoming socket because it immediately returns and there is not enough time for the first read of the incoming socket to arrive. To check for preconnects it is best to push the first read of the incoming socket to as late as possible. The other (wrong) way of doing that is to pass in an early read callback and call it immediately after sending HEADERS and then send the available bytes right there. This way is wrong because it does not work with late binding, which assumes Connect() is idempotent and causes sockets opened in this way to be potentially bound to the wrong socket requests. The current approach is to return OK in Connect() right after sending HEADERS before getting the reply, which is to be received later. If the reply is received during a subsequent Read() and the reply indicates an error, the error is returned to the callback of the Read(); otherwise the error is ignored with the connection disconnected and subsequent Read() and Write() should discover the disconnection.
This commit is contained in:
parent
d679fdd41b
commit
9bc0f85987
@ -50,6 +50,8 @@ SpdyProxyClientSocket::SpdyProxyClientSocket(
|
||||
user_buffer_len_(0),
|
||||
write_buffer_len_(0),
|
||||
was_ever_used_(false),
|
||||
use_fastopen_(false),
|
||||
read_headers_pending_(false),
|
||||
net_log_(NetLogWithSource::Make(spdy_stream->net_log().net_log(),
|
||||
NetLogSourceType::PROXY_CLIENT_SOCKET)),
|
||||
source_dependency_(source_net_log.source()) {
|
||||
@ -332,6 +334,20 @@ int SpdyProxyClientSocket::DoLoop(int last_io_result) {
|
||||
rv = DoReadReplyComplete(rv);
|
||||
net_log_.EndEventWithNetErrorCode(
|
||||
NetLogEventType::HTTP_TRANSACTION_TUNNEL_READ_HEADERS, rv);
|
||||
if (use_fastopen_ && read_headers_pending_) {
|
||||
read_headers_pending_ = false;
|
||||
if (rv < 0) {
|
||||
// read_callback_ cannot be called.
|
||||
if (!read_callback_)
|
||||
rv = ERR_IO_PENDING;
|
||||
// read_callback_ will be called with this error and be reset.
|
||||
// Further data after that will be ignored.
|
||||
next_state_ = STATE_DISCONNECTED;
|
||||
} else {
|
||||
// Does not call read_callback_ from here if headers are OK.
|
||||
rv = ERR_IO_PENDING;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
NOTREACHED() << "bad state";
|
||||
@ -372,6 +388,10 @@ int SpdyProxyClientSocket::DoSendRequest() {
|
||||
HttpRequestHeaders proxy_delegate_headers;
|
||||
proxy_delegate_->OnBeforeTunnelRequest(proxy_server_,
|
||||
&proxy_delegate_headers);
|
||||
if (proxy_delegate_headers.HasHeader("fastopen")) {
|
||||
proxy_delegate_headers.RemoveHeader("fastopen");
|
||||
use_fastopen_ = true;
|
||||
}
|
||||
request_.extra_headers.MergeFrom(proxy_delegate_headers);
|
||||
}
|
||||
|
||||
@ -394,6 +414,12 @@ int SpdyProxyClientSocket::DoSendRequestComplete(int result) {
|
||||
if (result < 0)
|
||||
return result;
|
||||
|
||||
if (use_fastopen_) {
|
||||
read_headers_pending_ = true;
|
||||
next_state_ = STATE_OPEN;
|
||||
return OK;
|
||||
}
|
||||
|
||||
// Wait for HEADERS frame from the server
|
||||
next_state_ = STATE_READ_REPLY_COMPLETE;
|
||||
return ERR_IO_PENDING;
|
||||
@ -455,6 +481,10 @@ void SpdyProxyClientSocket::OnEarlyHintsReceived(
|
||||
void SpdyProxyClientSocket::OnHeadersReceived(
|
||||
const spdy::Http2HeaderBlock& response_headers,
|
||||
const spdy::Http2HeaderBlock* pushed_request_headers) {
|
||||
if (use_fastopen_ && read_headers_pending_ && next_state_ == STATE_OPEN) {
|
||||
next_state_ = STATE_READ_REPLY_COMPLETE;
|
||||
}
|
||||
|
||||
// If we've already received the reply, existing headers are too late.
|
||||
// TODO(mbelshe): figure out a way to make HEADERS frames useful after the
|
||||
// initial response.
|
||||
|
@ -180,6 +180,9 @@ class NET_EXPORT_PRIVATE SpdyProxyClientSocket : public ProxyClientSocket,
|
||||
// True if the transport socket has ever sent data.
|
||||
bool was_ever_used_;
|
||||
|
||||
bool use_fastopen_;
|
||||
bool read_headers_pending_;
|
||||
|
||||
const NetLogWithSource net_log_;
|
||||
const NetLogSource source_dependency_;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user