From 3e97ce7c5f9638b50a2ca96de076fa4239f4f5c5 Mon Sep 17 00:00:00 2001 From: klzgrad Date: Sat, 7 Mar 2020 18:14:51 +0800 Subject: [PATCH] Handle payload data immediately after HTTP headers In HttpProxySocket there can be data immediately after HTTP headers, as in the case of fast HTTP CONNECT. Instead of reporting an error, handle this case by returning the data after HTTP headers in the next Read() call. --- src/net/tools/naive/http_proxy_socket.cc | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/net/tools/naive/http_proxy_socket.cc b/src/net/tools/naive/http_proxy_socket.cc index 6d2053e52e..549b69e2ad 100644 --- a/src/net/tools/naive/http_proxy_socket.cc +++ b/src/net/tools/naive/http_proxy_socket.cc @@ -142,6 +142,19 @@ int HttpProxySocket::Read(IOBuffer* buf, DCHECK(!user_callback_); DCHECK(callback); + if (!buffer_.empty()) { + int data_len = buffer_.size(); + if (data_len <= buf_len) { + std::memcpy(buf->data(), buffer_.data(), data_len); + buffer_.clear(); + } else { + std::memcpy(buf->data(), buffer_.data(), buf_len); + buffer_ = buffer_.substr(buf_len); + } + was_ever_used_ = true; + return OK; + } + int rv = transport_->Read( buf, buf_len, base::BindOnce(&HttpProxySocket::OnReadWriteComplete, @@ -263,9 +276,6 @@ int HttpProxySocket::DoHeaderReadComplete(int result) { next_state_ = STATE_HEADER_READ; return OK; } - if (header_end + 4 != buffer_.size()) { - return ERR_INVALID_ARGUMENT; - } // HttpProxyClientSocket uses CONNECT for all endpoints. auto first_line_end = buffer_.find("\r\n"); @@ -283,6 +293,8 @@ int HttpProxySocket::DoHeaderReadComplete(int result) { request_endpoint_ = HostPortPair::FromString( buffer_.substr(first_space + 1, second_space - (first_space + 1))); + buffer_ = buffer_.substr(header_end + 4); + next_state_ = STATE_HEADER_WRITE; return OK; }