blob: 9b9ce555640d1910b95b709a58449d3d4d28328b [file] [log] [blame]
[email protected]e92fa7e12012-02-16 23:31:221// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]fe2f62a2010-10-01 03:34:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/spdy/spdy_proxy_client_socket.h"
6
7#include <algorithm> // min
diannahu9904e272017-02-03 14:40:088#include <memory>
dchengc7eeda422015-12-26 03:56:489#include <utility>
[email protected]fe2f62a2010-10-01 03:34:0710
[email protected]49639fa2011-12-20 23:22:4111#include "base/bind.h"
12#include "base/bind_helpers.h"
[email protected]aa19cfc2013-05-23 16:41:3813#include "base/callback_helpers.h"
skyostil4891b25b2015-06-11 11:43:4514#include "base/location.h"
[email protected]fe2f62a2010-10-01 03:34:0715#include "base/logging.h"
skyostil4891b25b2015-06-11 11:43:4516#include "base/single_thread_task_runner.h"
[email protected]fc9be5802013-06-11 10:56:5117#include "base/strings/string_util.h"
gabf767595f2016-05-11 18:50:3518#include "base/threading/thread_task_runner_handle.h"
[email protected]f6c63db52013-02-02 00:35:2219#include "base/values.h"
[email protected]fe2f62a2010-10-01 03:34:0720#include "net/base/auth.h"
21#include "net/base/io_buffer.h"
[email protected]fe3b7dc2012-02-03 19:52:0922#include "net/http/http_auth_cache.h"
23#include "net/http/http_auth_handler_factory.h"
rchc5c07de2015-04-08 07:28:1824#include "net/http/http_request_info.h"
[email protected]b104b502010-10-18 20:21:3125#include "net/http/http_response_headers.h"
[email protected]57d2dfa2013-06-24 06:04:1226#include "net/http/proxy_connect_redirect_http_stream.h"
mikecirone8b85c432016-09-08 19:11:0027#include "net/log/net_log_event_type.h"
mikecirone8b85c432016-09-08 19:11:0028#include "net/log/net_log_source_type.h"
[email protected]fe2f62a2010-10-01 03:34:0729#include "net/spdy/spdy_http_utils.h"
[email protected]f89276a72013-07-12 06:41:5430#include "url/gurl.h"
[email protected]fe2f62a2010-10-01 03:34:0731
32namespace net {
33
34SpdyProxyClientSocket::SpdyProxyClientSocket(
[email protected]d26ff352013-05-13 08:48:2835 const base::WeakPtr<SpdyStream>& spdy_stream,
[email protected]fe2f62a2010-10-01 03:34:0736 const std::string& user_agent,
37 const HostPortPair& endpoint,
[email protected]fe2f62a2010-10-01 03:34:0738 const HostPortPair& proxy_server,
tfarina42834112016-09-22 13:38:2039 const NetLogWithSource& source_net_log,
mmenke2a1781d2015-10-07 19:25:3340 HttpAuthController* auth_controller)
[email protected]49639fa2011-12-20 23:22:4141 : next_state_(STATE_DISCONNECTED),
[email protected]fe2f62a2010-10-01 03:34:0742 spdy_stream_(spdy_stream),
[email protected]fe2f62a2010-10-01 03:34:0743 endpoint_(endpoint),
mmenke2a1781d2015-10-07 19:25:3344 auth_(auth_controller),
rchecd3c552015-04-07 20:53:5445 user_agent_(user_agent),
[email protected]ca690b02013-04-17 10:38:4346 user_buffer_len_(0),
[email protected]fe2f62a2010-10-01 03:34:0747 write_buffer_len_(0),
[email protected]57d2dfa2013-06-24 06:04:1248 was_ever_used_(false),
49 redirect_has_load_timing_info_(false),
tfarina42834112016-09-22 13:38:2050 net_log_(NetLogWithSource::Make(spdy_stream->net_log().net_log(),
51 NetLogSourceType::PROXY_CLIENT_SOCKET)),
bnc3d95ca92017-03-29 13:20:3452 source_dependency_(source_net_log.source()),
[email protected]0a428c22014-06-14 02:26:3753 weak_factory_(this),
54 write_callback_weak_factory_(this) {
[email protected]fe2f62a2010-10-01 03:34:0755 request_.method = "CONNECT";
rchecd3c552015-04-07 20:53:5456 request_.url = GURL("https://" + endpoint.ToString());
mikecirone8b85c432016-09-08 19:11:0057 net_log_.BeginEvent(NetLogEventType::SOCKET_ALIVE,
[email protected]f6c63db52013-02-02 00:35:2258 source_net_log.source().ToEventParametersCallback());
59 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:0060 NetLogEventType::HTTP2_PROXY_CLIENT_SESSION,
[email protected]f6c63db52013-02-02 00:35:2261 spdy_stream->net_log().source().ToEventParametersCallback());
62
[email protected]fe2f62a2010-10-01 03:34:0763 spdy_stream_->SetDelegate(this);
64 was_ever_used_ = spdy_stream_->WasEverUsed();
65}
66
67SpdyProxyClientSocket::~SpdyProxyClientSocket() {
68 Disconnect();
mikecirone8b85c432016-09-08 19:11:0069 net_log_.EndEvent(NetLogEventType::SOCKET_ALIVE);
[email protected]fe2f62a2010-10-01 03:34:0770}
71
[email protected]be1a48b2011-01-20 00:12:1372const HttpResponseInfo* SpdyProxyClientSocket::GetConnectResponseInfo() const {
[email protected]90499482013-06-01 00:39:5073 return response_.headers.get() ? &response_ : NULL;
[email protected]be1a48b2011-01-20 00:12:1374}
75
[email protected]c0fe941d2012-02-25 00:15:3276const scoped_refptr<HttpAuthController>&
77SpdyProxyClientSocket::GetAuthController() const {
78 return auth_;
79}
80
81int SpdyProxyClientSocket::RestartWithAuth(const CompletionCallback& callback) {
82 // A SPDY Stream can only handle a single request, so the underlying
83 // stream may not be reused and a new SpdyProxyClientSocket must be
84 // created (possibly on top of the same SPDY Session).
85 next_state_ = STATE_DISCONNECTED;
[email protected]0c5fb722012-02-28 11:50:3586 return OK;
[email protected]c0fe941d2012-02-25 00:15:3287}
88
89bool SpdyProxyClientSocket::IsUsingSpdy() const {
90 return true;
91}
92
bnc6227b26e2016-08-12 02:00:4393NextProto SpdyProxyClientSocket::GetProxyNegotiatedProtocol() const {
bnc4cb660c2016-08-11 19:43:1294 return spdy_stream_->GetNegotiatedProtocol();
[email protected]c0fe941d2012-02-25 00:15:3295}
96
[email protected]511f6f52010-12-17 03:58:2997HttpStream* SpdyProxyClientSocket::CreateConnectResponseStream() {
[email protected]57d2dfa2013-06-24 06:04:1298 return new ProxyConnectRedirectHttpStream(
99 redirect_has_load_timing_info_ ? &redirect_load_timing_info_ : NULL);
[email protected]511f6f52010-12-17 03:58:29100}
101
bnc42331402016-07-25 13:36:15102// Sends a HEADERS frame to the proxy with a CONNECT request
[email protected]fe2f62a2010-10-01 03:34:07103// for the specified endpoint. Waits for the server to send back
bnc42331402016-07-25 13:36:15104// a HEADERS frame. OK will be returned if the status is 200.
[email protected]fe2f62a2010-10-01 03:34:07105// ERR_TUNNEL_CONNECTION_FAILED will be returned for any other status.
106// In any of these cases, Read() may be called to retrieve the HTTP
107// response body. Any other return values should be considered fatal.
[email protected]fe3b7dc2012-02-03 19:52:09108// TODO(rch): handle 407 proxy auth requested correctly, perhaps
109// by creating a new stream for the subsequent request.
[email protected]fe2f62a2010-10-01 03:34:07110// TODO(rch): create a more appropriate error code to disambiguate
111// the HTTPS Proxy tunnel failure from an HTTP Proxy tunnel failure.
[email protected]dbf036f2011-12-06 23:33:24112int SpdyProxyClientSocket::Connect(const CompletionCallback& callback) {
[email protected]83039bb2011-12-09 18:43:55113 DCHECK(read_callback_.is_null());
[email protected]d9da5fe2010-10-13 22:37:16114 if (next_state_ == STATE_OPEN)
[email protected]fe2f62a2010-10-01 03:34:07115 return OK;
116
[email protected]d9da5fe2010-10-13 22:37:16117 DCHECK_EQ(STATE_DISCONNECTED, next_state_);
[email protected]fe2f62a2010-10-01 03:34:07118 next_state_ = STATE_GENERATE_AUTH_TOKEN;
119
120 int rv = DoLoop(OK);
121 if (rv == ERR_IO_PENDING)
122 read_callback_ = callback;
123 return rv;
124}
125
126void SpdyProxyClientSocket::Disconnect() {
[email protected]ca690b02013-04-17 10:38:43127 read_buffer_queue_.Clear();
[email protected]d9da5fe2010-10-13 22:37:16128 user_buffer_ = NULL;
[email protected]ca690b02013-04-17 10:38:43129 user_buffer_len_ = 0;
[email protected]dbf036f2011-12-06 23:33:24130 read_callback_.Reset();
[email protected]d9da5fe2010-10-13 22:37:16131
132 write_buffer_len_ = 0;
[email protected]83039bb2011-12-09 18:43:55133 write_callback_.Reset();
[email protected]0a428c22014-06-14 02:26:37134 write_callback_weak_factory_.InvalidateWeakPtrs();
[email protected]d9da5fe2010-10-13 22:37:16135
136 next_state_ = STATE_DISCONNECTED;
137
[email protected]11fbca0b2013-06-02 23:37:21138 if (spdy_stream_.get()) {
[email protected]fe2f62a2010-10-01 03:34:07139 // This will cause OnClose to be invoked, which takes care of
140 // cleaning up all the internal state.
141 spdy_stream_->Cancel();
[email protected]11fbca0b2013-06-02 23:37:21142 DCHECK(!spdy_stream_.get());
[email protected]f6a78292013-03-09 14:36:34143 }
[email protected]fe2f62a2010-10-01 03:34:07144}
145
146bool SpdyProxyClientSocket::IsConnected() const {
[email protected]194c7a92011-12-03 04:54:18147 return next_state_ == STATE_OPEN;
[email protected]fe2f62a2010-10-01 03:34:07148}
149
150bool SpdyProxyClientSocket::IsConnectedAndIdle() const {
[email protected]ca690b02013-04-17 10:38:43151 return IsConnected() && read_buffer_queue_.IsEmpty() &&
[email protected]5ff0ed32014-02-12 17:48:51152 spdy_stream_->IsOpen();
[email protected]fe2f62a2010-10-01 03:34:07153}
154
tfarina42834112016-09-22 13:38:20155const NetLogWithSource& SpdyProxyClientSocket::NetLog() const {
[email protected]e4be2dd2010-12-14 00:44:39156 return net_log_;
157}
158
[email protected]fe2f62a2010-10-01 03:34:07159void SpdyProxyClientSocket::SetSubresourceSpeculation() {
160 // TODO(rch): what should this implementation be?
161}
162
163void SpdyProxyClientSocket::SetOmniboxSpeculation() {
164 // TODO(rch): what should this implementation be?
165}
166
167bool SpdyProxyClientSocket::WasEverUsed() const {
[email protected]11fbca0b2013-06-02 23:37:21168 return was_ever_used_ || (spdy_stream_.get() && spdy_stream_->WasEverUsed());
[email protected]fe2f62a2010-10-01 03:34:07169}
170
tfarina2846404c2016-12-25 14:31:37171bool SpdyProxyClientSocket::WasAlpnNegotiated() const {
[email protected]2d88e7d2012-07-19 17:55:17172 return false;
173}
174
[email protected]33661e482012-04-03 16:16:26175NextProto SpdyProxyClientSocket::GetNegotiatedProtocol() const {
176 return kProtoUnknown;
177}
178
[email protected]2d88e7d2012-07-19 17:55:17179bool SpdyProxyClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
bnc4cb660c2016-08-11 19:43:12180 return spdy_stream_->GetSSLInfo(ssl_info);
[email protected]2d88e7d2012-07-19 17:55:17181}
182
ttuttle23fdb7b2015-05-15 01:28:03183void SpdyProxyClientSocket::GetConnectionAttempts(
184 ConnectionAttempts* out) const {
185 out->clear();
186}
187
tbansalf82cc8e2015-10-14 20:05:49188int64_t SpdyProxyClientSocket::GetTotalReceivedBytes() const {
189 NOTIMPLEMENTED();
190 return 0;
191}
192
[email protected]fe2f62a2010-10-01 03:34:07193int SpdyProxyClientSocket::Read(IOBuffer* buf, int buf_len,
[email protected]3f55aa12011-12-07 02:03:33194 const CompletionCallback& callback) {
[email protected]83039bb2011-12-09 18:43:55195 DCHECK(read_callback_.is_null());
[email protected]90499482013-06-01 00:39:50196 DCHECK(!user_buffer_.get());
[email protected]3f55aa12011-12-07 02:03:33197
198 if (next_state_ == STATE_DISCONNECTED)
199 return ERR_SOCKET_NOT_CONNECTED;
200
[email protected]ca690b02013-04-17 10:38:43201 if (next_state_ == STATE_CLOSED && read_buffer_queue_.IsEmpty()) {
[email protected]3f55aa12011-12-07 02:03:33202 return 0;
203 }
204
205 DCHECK(next_state_ == STATE_OPEN || next_state_ == STATE_CLOSED);
206 DCHECK(buf);
[email protected]ca690b02013-04-17 10:38:43207 size_t result = PopulateUserReadBuffer(buf->data(), buf_len);
[email protected]3f55aa12011-12-07 02:03:33208 if (result == 0) {
[email protected]ca690b02013-04-17 10:38:43209 user_buffer_ = buf;
210 user_buffer_len_ = static_cast<size_t>(buf_len);
[email protected]3f55aa12011-12-07 02:03:33211 DCHECK(!callback.is_null());
212 read_callback_ = callback;
213 return ERR_IO_PENDING;
214 }
215 user_buffer_ = NULL;
216 return result;
217}
[email protected]fe2f62a2010-10-01 03:34:07218
[email protected]ca690b02013-04-17 10:38:43219size_t SpdyProxyClientSocket::PopulateUserReadBuffer(char* data, size_t len) {
[email protected]09a8d9172013-04-17 19:23:49220 return read_buffer_queue_.Dequeue(data, len);
[email protected]fe2f62a2010-10-01 03:34:07221}
222
223int SpdyProxyClientSocket::Write(IOBuffer* buf, int buf_len,
[email protected]83039bb2011-12-09 18:43:55224 const CompletionCallback& callback) {
225 DCHECK(write_callback_.is_null());
[email protected]194c7a92011-12-03 04:54:18226 if (next_state_ != STATE_OPEN)
[email protected]d9da5fe2010-10-13 22:37:16227 return ERR_SOCKET_NOT_CONNECTED;
228
[email protected]11fbca0b2013-06-02 23:37:21229 DCHECK(spdy_stream_.get());
[email protected]edbfa8c2013-05-29 00:22:33230 spdy_stream_->SendData(buf, buf_len, MORE_DATA_TO_SEND);
mikecirone8b85c432016-09-08 19:11:00231 net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_SENT, buf_len,
232 buf->data());
[email protected]aa19cfc2013-05-23 16:41:38233 write_callback_ = callback;
234 write_buffer_len_ = buf_len;
235 return ERR_IO_PENDING;
[email protected]fe2f62a2010-10-01 03:34:07236}
237
Avi Drissman13fc8932015-12-20 04:40:46238int SpdyProxyClientSocket::SetReceiveBufferSize(int32_t size) {
[email protected]3268023f2011-05-05 00:08:10239 // Since this StreamSocket sits on top of a shared SpdySession, it
[email protected]28b96d1c2014-04-09 12:21:15240 // is not safe for callers to change this underlying socket.
241 return ERR_NOT_IMPLEMENTED;
[email protected]fe2f62a2010-10-01 03:34:07242}
243
Avi Drissman13fc8932015-12-20 04:40:46244int SpdyProxyClientSocket::SetSendBufferSize(int32_t size) {
[email protected]3268023f2011-05-05 00:08:10245 // Since this StreamSocket sits on top of a shared SpdySession, it
[email protected]28b96d1c2014-04-09 12:21:15246 // is not safe for callers to change this underlying socket.
247 return ERR_NOT_IMPLEMENTED;
[email protected]fe2f62a2010-10-01 03:34:07248}
249
[email protected]a3528692012-06-08 00:11:42250int SpdyProxyClientSocket::GetPeerAddress(IPEndPoint* address) const {
[email protected]fe2f62a2010-10-01 03:34:07251 if (!IsConnected())
[email protected]88e03fa2010-10-05 03:09:04252 return ERR_SOCKET_NOT_CONNECTED;
[email protected]fe2f62a2010-10-01 03:34:07253 return spdy_stream_->GetPeerAddress(address);
254}
255
[email protected]e7f74da2011-04-19 23:49:35256int SpdyProxyClientSocket::GetLocalAddress(IPEndPoint* address) const {
257 if (!IsConnected())
258 return ERR_SOCKET_NOT_CONNECTED;
259 return spdy_stream_->GetLocalAddress(address);
260}
261
[email protected]4eddbc732012-08-09 05:40:17262void SpdyProxyClientSocket::LogBlockedTunnelResponse() const {
263 ProxyClientSocket::LogBlockedTunnelResponse(
264 response_.headers->response_code(),
[email protected]4eddbc732012-08-09 05:40:17265 /* is_https_proxy = */ true);
266}
267
[email protected]0a428c22014-06-14 02:26:37268void SpdyProxyClientSocket::RunCallback(const CompletionCallback& callback,
269 int result) const {
270 callback.Run(result);
271}
272
[email protected]fe2f62a2010-10-01 03:34:07273void SpdyProxyClientSocket::OnIOComplete(int result) {
[email protected]d9da5fe2010-10-13 22:37:16274 DCHECK_NE(STATE_DISCONNECTED, next_state_);
[email protected]fe2f62a2010-10-01 03:34:07275 int rv = DoLoop(result);
276 if (rv != ERR_IO_PENDING) {
[email protected]83039bb2011-12-09 18:43:55277 CompletionCallback c = read_callback_;
278 read_callback_.Reset();
279 c.Run(rv);
[email protected]fe2f62a2010-10-01 03:34:07280 }
281}
282
283int SpdyProxyClientSocket::DoLoop(int last_io_result) {
[email protected]d9da5fe2010-10-13 22:37:16284 DCHECK_NE(next_state_, STATE_DISCONNECTED);
[email protected]fe2f62a2010-10-01 03:34:07285 int rv = last_io_result;
286 do {
287 State state = next_state_;
[email protected]d9da5fe2010-10-13 22:37:16288 next_state_ = STATE_DISCONNECTED;
[email protected]fe2f62a2010-10-01 03:34:07289 switch (state) {
290 case STATE_GENERATE_AUTH_TOKEN:
291 DCHECK_EQ(OK, rv);
292 rv = DoGenerateAuthToken();
293 break;
294 case STATE_GENERATE_AUTH_TOKEN_COMPLETE:
295 rv = DoGenerateAuthTokenComplete(rv);
296 break;
297 case STATE_SEND_REQUEST:
298 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00299 net_log_.BeginEvent(
300 NetLogEventType::HTTP_TRANSACTION_TUNNEL_SEND_REQUEST);
[email protected]fe2f62a2010-10-01 03:34:07301 rv = DoSendRequest();
302 break;
303 case STATE_SEND_REQUEST_COMPLETE:
[email protected]d7fd1782011-02-08 19:16:43304 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00305 NetLogEventType::HTTP_TRANSACTION_TUNNEL_SEND_REQUEST, rv);
[email protected]fe2f62a2010-10-01 03:34:07306 rv = DoSendRequestComplete(rv);
[email protected]f6c63db52013-02-02 00:35:22307 if (rv >= 0 || rv == ERR_IO_PENDING) {
308 // Emit extra event so can use the same events as
309 // HttpProxyClientSocket.
310 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00311 NetLogEventType::HTTP_TRANSACTION_TUNNEL_READ_HEADERS);
[email protected]f6c63db52013-02-02 00:35:22312 }
[email protected]fe2f62a2010-10-01 03:34:07313 break;
314 case STATE_READ_REPLY_COMPLETE:
315 rv = DoReadReplyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43316 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00317 NetLogEventType::HTTP_TRANSACTION_TUNNEL_READ_HEADERS, rv);
[email protected]fe2f62a2010-10-01 03:34:07318 break;
319 default:
320 NOTREACHED() << "bad state";
321 rv = ERR_UNEXPECTED;
322 break;
323 }
[email protected]d9da5fe2010-10-13 22:37:16324 } while (rv != ERR_IO_PENDING && next_state_ != STATE_DISCONNECTED &&
325 next_state_ != STATE_OPEN);
[email protected]fe2f62a2010-10-01 03:34:07326 return rv;
327}
328
329int SpdyProxyClientSocket::DoGenerateAuthToken() {
330 next_state_ = STATE_GENERATE_AUTH_TOKEN_COMPLETE;
[email protected]49639fa2011-12-20 23:22:41331 return auth_->MaybeGenerateAuthToken(
332 &request_,
[email protected]f6a78292013-03-09 14:36:34333 base::Bind(&SpdyProxyClientSocket::OnIOComplete,
334 weak_factory_.GetWeakPtr()),
[email protected]49639fa2011-12-20 23:22:41335 net_log_);
[email protected]fe2f62a2010-10-01 03:34:07336}
337
338int SpdyProxyClientSocket::DoGenerateAuthTokenComplete(int result) {
339 DCHECK_NE(ERR_IO_PENDING, result);
340 if (result == OK)
341 next_state_ = STATE_SEND_REQUEST;
342 return result;
343}
344
345int SpdyProxyClientSocket::DoSendRequest() {
346 next_state_ = STATE_SEND_REQUEST_COMPLETE;
347
348 // Add Proxy-Authentication header if necessary.
349 HttpRequestHeaders authorization_headers;
350 if (auth_->HaveAuth()) {
351 auth_->AddAuthorizationHeader(&authorization_headers);
352 }
353
354 std::string request_line;
rchecd3c552015-04-07 20:53:54355 BuildTunnelRequest(endpoint_, authorization_headers, user_agent_,
356 &request_line, &request_.extra_headers);
[email protected]3abacd62012-06-10 20:20:32357
358 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:00359 NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS,
[email protected]3abacd62012-06-10 20:20:32360 base::Bind(&HttpRequestHeaders::NetLogCallback,
rchecd3c552015-04-07 20:53:54361 base::Unretained(&request_.extra_headers), &request_line));
[email protected]fe2f62a2010-10-01 03:34:07362
bnc1d1d86462016-07-20 16:51:55363 SpdyHeaderBlock headers;
bncbca843ba2016-07-14 13:05:48364 CreateSpdyHeadersFromHttpRequest(request_, request_.extra_headers, true,
bnc1d1d86462016-07-20 16:51:55365 &headers);
[email protected]fe2f62a2010-10-01 03:34:07366
dchengc7eeda422015-12-26 03:56:48367 return spdy_stream_->SendRequestHeaders(std::move(headers),
368 MORE_DATA_TO_SEND);
[email protected]fe2f62a2010-10-01 03:34:07369}
370
371int SpdyProxyClientSocket::DoSendRequestComplete(int result) {
372 if (result < 0)
373 return result;
374
bnc42331402016-07-25 13:36:15375 // Wait for HEADERS frame from the server
[email protected]fe2f62a2010-10-01 03:34:07376 next_state_ = STATE_READ_REPLY_COMPLETE;
377 return ERR_IO_PENDING;
378}
379
380int SpdyProxyClientSocket::DoReadReplyComplete(int result) {
381 // We enter this method directly from DoSendRequestComplete, since
bnc42331402016-07-25 13:36:15382 // we are notified by a callback when the HEADERS frame arrives.
[email protected]fe2f62a2010-10-01 03:34:07383
384 if (result < 0)
385 return result;
386
[email protected]fe2f62a2010-10-01 03:34:07387 // Require the "HTTP/1.x" status line for SSL CONNECT.
bncbe0f6af2015-10-15 17:49:56388 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0))
[email protected]fe2f62a2010-10-01 03:34:07389 return ERR_TUNNEL_CONNECTION_FAILED;
390
[email protected]3abacd62012-06-10 20:20:32391 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:00392 NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:32393 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]fe2f62a2010-10-01 03:34:07394
[email protected]4eddbc732012-08-09 05:40:17395 switch (response_.headers->response_code()) {
396 case 200: // OK
397 next_state_ = STATE_OPEN;
398 return OK;
399
400 case 302: // Found / Moved Temporarily
401 // Try to return a sanitized response so we can follow auth redirects.
402 // If we can't, fail the tunnel connection.
ttuttle7933c112015-01-06 00:55:24403 if (!SanitizeProxyRedirect(&response_)) {
[email protected]4eddbc732012-08-09 05:40:17404 LogBlockedTunnelResponse();
405 return ERR_TUNNEL_CONNECTION_FAILED;
406 }
407
ttuttle7933c112015-01-06 00:55:24408 redirect_has_load_timing_info_ =
409 spdy_stream_->GetLoadTimingInfo(&redirect_load_timing_info_);
diannahu9904e272017-02-03 14:40:08410 // Note that this triggers a ERROR_CODE_CANCEL.
ttuttle7933c112015-01-06 00:55:24411 spdy_stream_->DetachDelegate();
412 next_state_ = STATE_DISCONNECTED;
413 return ERR_HTTPS_PROXY_TUNNEL_RESPONSE;
414
[email protected]4eddbc732012-08-09 05:40:17415 case 407: // Proxy Authentication Required
416 next_state_ = STATE_OPEN;
ttuttle7933c112015-01-06 00:55:24417 if (!SanitizeProxyAuth(&response_)) {
418 LogBlockedTunnelResponse();
419 return ERR_TUNNEL_CONNECTION_FAILED;
420 }
[email protected]90499482013-06-01 00:39:50421 return HandleProxyAuthChallenge(auth_.get(), &response_, net_log_);
[email protected]4eddbc732012-08-09 05:40:17422
423 default:
424 // Ignore response to avoid letting the proxy impersonate the target
425 // server. (See http://crbug.com/137891.)
426 LogBlockedTunnelResponse();
427 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]511f6f52010-12-17 03:58:29428 }
[email protected]fe2f62a2010-10-01 03:34:07429}
430
431// SpdyStream::Delegate methods:
432// Called when SYN frame has been sent.
433// Returns true if no more data to be sent after SYN frame.
bnc4c214312016-11-28 16:49:15434void SpdyProxyClientSocket::OnHeadersSent() {
[email protected]fe2f62a2010-10-01 03:34:07435 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE);
436
[email protected]d46715c2013-04-15 00:21:42437 OnIOComplete(OK);
[email protected]fe2f62a2010-10-01 03:34:07438}
439
bnc4c214312016-11-28 16:49:15440void SpdyProxyClientSocket::OnHeadersReceived(
[email protected]6d116e1a2013-06-24 07:42:15441 const SpdyHeaderBlock& response_headers) {
[email protected]d08358502010-12-03 22:04:03442 // If we've already received the reply, existing headers are too late.
443 // TODO(mbelshe): figure out a way to make HEADERS frames useful after the
444 // initial response.
445 if (next_state_ != STATE_READ_REPLY_COMPLETE)
bnc4c214312016-11-28 16:49:15446 return;
[email protected]fe2f62a2010-10-01 03:34:07447
[email protected]d08358502010-12-03 22:04:03448 // Save the response
bnc4c214312016-11-28 16:49:15449 const bool headers_valid =
450 SpdyHeadersToHttpResponse(response_headers, &response_);
451 DCHECK(headers_valid);
[email protected]fe2f62a2010-10-01 03:34:07452
[email protected]6d116e1a2013-06-24 07:42:15453 OnIOComplete(OK);
[email protected]fe2f62a2010-10-01 03:34:07454}
455
[email protected]ca690b02013-04-17 10:38:43456// Called when data is received or on EOF (if |buffer| is NULL).
danakjaee3e1ec2016-04-16 00:23:18457void SpdyProxyClientSocket::OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) {
[email protected]ca690b02013-04-17 10:38:43458 if (buffer) {
mikecirone8b85c432016-09-08 19:11:00459 net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED,
[email protected]ca690b02013-04-17 10:38:43460 buffer->GetRemainingSize(),
461 buffer->GetRemainingData());
dchengc7eeda422015-12-26 03:56:48462 read_buffer_queue_.Enqueue(std::move(buffer));
[email protected]ca690b02013-04-17 10:38:43463 } else {
mikecirone8b85c432016-09-08 19:11:00464 net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED, 0,
465 NULL);
[email protected]fe2f62a2010-10-01 03:34:07466 }
467
[email protected]83039bb2011-12-09 18:43:55468 if (!read_callback_.is_null()) {
[email protected]ca690b02013-04-17 10:38:43469 int rv = PopulateUserReadBuffer(user_buffer_->data(), user_buffer_len_);
[email protected]dbf036f2011-12-06 23:33:24470 CompletionCallback c = read_callback_;
471 read_callback_.Reset();
472 user_buffer_ = NULL;
[email protected]ca690b02013-04-17 10:38:43473 user_buffer_len_ = 0;
[email protected]dbf036f2011-12-06 23:33:24474 c.Run(rv);
[email protected]fe2f62a2010-10-01 03:34:07475 }
476}
477
[email protected]aa19cfc2013-05-23 16:41:38478void SpdyProxyClientSocket::OnDataSent() {
[email protected]83039bb2011-12-09 18:43:55479 DCHECK(!write_callback_.is_null());
[email protected]fe2f62a2010-10-01 03:34:07480
[email protected]aa19cfc2013-05-23 16:41:38481 int rv = write_buffer_len_;
482 write_buffer_len_ = 0;
[email protected]89d4d792014-04-06 16:27:15483
484 // Proxy write callbacks result in deep callback chains. Post to allow the
485 // stream's write callback chain to unwind (see crbug.com/355511).
skyostil4891b25b2015-06-11 11:43:45486 base::ThreadTaskRunnerHandle::Get()->PostTask(
487 FROM_HERE, base::Bind(&SpdyProxyClientSocket::RunCallback,
488 write_callback_weak_factory_.GetWeakPtr(),
tzik2ac7d802016-02-04 01:57:17489 base::ResetAndReturn(&write_callback_), rv));
[email protected]fe2f62a2010-10-01 03:34:07490}
491
xunjieli294da722015-08-11 19:15:02492void SpdyProxyClientSocket::OnTrailers(const SpdyHeaderBlock& trailers) {
493 // |spdy_stream_| is of type SPDY_BIDIRECTIONAL_STREAM, so trailers are
494 // combined with response headers and this method will not be calld.
495 NOTREACHED();
496}
497
[email protected]fe2f62a2010-10-01 03:34:07498void SpdyProxyClientSocket::OnClose(int status) {
[email protected]fe2f62a2010-10-01 03:34:07499 was_ever_used_ = spdy_stream_->WasEverUsed();
[email protected]d26ff352013-05-13 08:48:28500 spdy_stream_.reset();
[email protected]d9da5fe2010-10-13 22:37:16501
502 bool connecting = next_state_ != STATE_DISCONNECTED &&
503 next_state_ < STATE_OPEN;
504 if (next_state_ == STATE_OPEN)
505 next_state_ = STATE_CLOSED;
506 else
507 next_state_ = STATE_DISCONNECTED;
508
[email protected]6af4e412011-11-29 23:39:18509 base::WeakPtr<SpdyProxyClientSocket> weak_ptr = weak_factory_.GetWeakPtr();
[email protected]83039bb2011-12-09 18:43:55510 CompletionCallback write_callback = write_callback_;
511 write_callback_.Reset();
[email protected]d9da5fe2010-10-13 22:37:16512 write_buffer_len_ = 0;
[email protected]d9da5fe2010-10-13 22:37:16513
514 // If we're in the middle of connecting, we need to make sure
515 // we invoke the connect callback.
516 if (connecting) {
[email protected]83039bb2011-12-09 18:43:55517 DCHECK(!read_callback_.is_null());
518 CompletionCallback read_callback = read_callback_;
519 read_callback_.Reset();
520 read_callback.Run(status);
521 } else if (!read_callback_.is_null()) {
[email protected]6af4e412011-11-29 23:39:18522 // If we have a read_callback_, the we need to make sure we call it back.
danakjaee3e1ec2016-04-16 00:23:18523 OnDataReceived(std::unique_ptr<SpdyBuffer>());
[email protected]d9da5fe2010-10-13 22:37:16524 }
[email protected]6af4e412011-11-29 23:39:18525 // This may have been deleted by read_callback_, so check first.
[email protected]11fbca0b2013-06-02 23:37:21526 if (weak_ptr.get() && !write_callback.is_null())
[email protected]83039bb2011-12-09 18:43:55527 write_callback.Run(ERR_CONNECTION_CLOSED);
[email protected]fe2f62a2010-10-01 03:34:07528}
529
bnc3d95ca92017-03-29 13:20:34530NetLogSource SpdyProxyClientSocket::source_dependency() const {
531 return source_dependency_;
532}
533
[email protected]fe2f62a2010-10-01 03:34:07534} // namespace net