blob: e8e5bfeeeb83dffa86ab17096527fe9d58cc3f9d [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
8
[email protected]49639fa2011-12-20 23:22:419#include "base/bind.h"
10#include "base/bind_helpers.h"
[email protected]aa19cfc2013-05-23 16:41:3811#include "base/callback_helpers.h"
[email protected]fe2f62a2010-10-01 03:34:0712#include "base/logging.h"
[email protected]fc9be5802013-06-11 10:56:5113#include "base/strings/string_util.h"
[email protected]f6c63db52013-02-02 00:35:2214#include "base/values.h"
[email protected]fe2f62a2010-10-01 03:34:0715#include "net/base/auth.h"
16#include "net/base/io_buffer.h"
17#include "net/base/net_util.h"
[email protected]fe3b7dc2012-02-03 19:52:0918#include "net/http/http_auth_cache.h"
19#include "net/http/http_auth_handler_factory.h"
rchc5c07de2015-04-08 07:28:1820#include "net/http/http_request_info.h"
[email protected]b104b502010-10-18 20:21:3121#include "net/http/http_response_headers.h"
[email protected]57d2dfa2013-06-24 06:04:1222#include "net/http/proxy_connect_redirect_http_stream.h"
[email protected]fe2f62a2010-10-01 03:34:0723#include "net/spdy/spdy_http_utils.h"
[email protected]f89276a72013-07-12 06:41:5424#include "url/gurl.h"
[email protected]fe2f62a2010-10-01 03:34:0725
26namespace net {
27
28SpdyProxyClientSocket::SpdyProxyClientSocket(
[email protected]d26ff352013-05-13 08:48:2829 const base::WeakPtr<SpdyStream>& spdy_stream,
[email protected]fe2f62a2010-10-01 03:34:0730 const std::string& user_agent,
31 const HostPortPair& endpoint,
[email protected]fe2f62a2010-10-01 03:34:0732 const HostPortPair& proxy_server,
[email protected]f6c63db52013-02-02 00:35:2233 const BoundNetLog& source_net_log,
[email protected]fe3b7dc2012-02-03 19:52:0934 HttpAuthCache* auth_cache,
35 HttpAuthHandlerFactory* auth_handler_factory)
[email protected]49639fa2011-12-20 23:22:4136 : next_state_(STATE_DISCONNECTED),
[email protected]fe2f62a2010-10-01 03:34:0737 spdy_stream_(spdy_stream),
[email protected]fe2f62a2010-10-01 03:34:0738 endpoint_(endpoint),
[email protected]0a428c22014-06-14 02:26:3739 auth_(new HttpAuthController(HttpAuth::AUTH_PROXY,
40 GURL("https://" + proxy_server.ToString()),
41 auth_cache,
42 auth_handler_factory)),
rchecd3c552015-04-07 20:53:5443 user_agent_(user_agent),
[email protected]ca690b02013-04-17 10:38:4344 user_buffer_len_(0),
[email protected]fe2f62a2010-10-01 03:34:0745 write_buffer_len_(0),
[email protected]57d2dfa2013-06-24 06:04:1246 was_ever_used_(false),
47 redirect_has_load_timing_info_(false),
[email protected]f6c63db52013-02-02 00:35:2248 net_log_(BoundNetLog::Make(spdy_stream->net_log().net_log(),
[email protected]0a30cf512014-05-27 20:55:1849 NetLog::SOURCE_PROXY_CLIENT_SOCKET)),
[email protected]0a428c22014-06-14 02:26:3750 weak_factory_(this),
51 write_callback_weak_factory_(this) {
[email protected]fe2f62a2010-10-01 03:34:0752 request_.method = "CONNECT";
rchecd3c552015-04-07 20:53:5453 request_.url = GURL("https://" + endpoint.ToString());
[email protected]f6c63db52013-02-02 00:35:2254 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE,
55 source_net_log.source().ToEventParametersCallback());
56 net_log_.AddEvent(
bncbe32a032015-02-25 17:22:5557 NetLog::TYPE_HTTP2_PROXY_CLIENT_SESSION,
[email protected]f6c63db52013-02-02 00:35:2258 spdy_stream->net_log().source().ToEventParametersCallback());
59
[email protected]fe2f62a2010-10-01 03:34:0760 spdy_stream_->SetDelegate(this);
61 was_ever_used_ = spdy_stream_->WasEverUsed();
62}
63
64SpdyProxyClientSocket::~SpdyProxyClientSocket() {
65 Disconnect();
[email protected]f6c63db52013-02-02 00:35:2266 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE);
[email protected]fe2f62a2010-10-01 03:34:0767}
68
[email protected]be1a48b2011-01-20 00:12:1369const HttpResponseInfo* SpdyProxyClientSocket::GetConnectResponseInfo() const {
[email protected]90499482013-06-01 00:39:5070 return response_.headers.get() ? &response_ : NULL;
[email protected]be1a48b2011-01-20 00:12:1371}
72
[email protected]c0fe941d2012-02-25 00:15:3273const scoped_refptr<HttpAuthController>&
74SpdyProxyClientSocket::GetAuthController() const {
75 return auth_;
76}
77
78int SpdyProxyClientSocket::RestartWithAuth(const CompletionCallback& callback) {
79 // A SPDY Stream can only handle a single request, so the underlying
80 // stream may not be reused and a new SpdyProxyClientSocket must be
81 // created (possibly on top of the same SPDY Session).
82 next_state_ = STATE_DISCONNECTED;
[email protected]0c5fb722012-02-28 11:50:3583 return OK;
[email protected]c0fe941d2012-02-25 00:15:3284}
85
86bool SpdyProxyClientSocket::IsUsingSpdy() const {
87 return true;
88}
89
[email protected]8e3c78cb2012-03-31 03:58:4690NextProto SpdyProxyClientSocket::GetProtocolNegotiated() const {
[email protected]c0fe941d2012-02-25 00:15:3291 // Save the negotiated protocol
92 SSLInfo ssl_info;
93 bool was_npn_negotiated;
[email protected]8e3c78cb2012-03-31 03:58:4694 NextProto protocol_negotiated;
[email protected]c0fe941d2012-02-25 00:15:3295 spdy_stream_->GetSSLInfo(&ssl_info, &was_npn_negotiated,
96 &protocol_negotiated);
97 return protocol_negotiated;
98}
99
[email protected]511f6f52010-12-17 03:58:29100HttpStream* SpdyProxyClientSocket::CreateConnectResponseStream() {
[email protected]57d2dfa2013-06-24 06:04:12101 return new ProxyConnectRedirectHttpStream(
102 redirect_has_load_timing_info_ ? &redirect_load_timing_info_ : NULL);
[email protected]511f6f52010-12-17 03:58:29103}
104
[email protected]fe2f62a2010-10-01 03:34:07105// Sends a SYN_STREAM frame to the proxy with a CONNECT request
106// for the specified endpoint. Waits for the server to send back
107// a SYN_REPLY frame. OK will be returned if the status is 200.
108// ERR_TUNNEL_CONNECTION_FAILED will be returned for any other status.
109// In any of these cases, Read() may be called to retrieve the HTTP
110// response body. Any other return values should be considered fatal.
[email protected]fe3b7dc2012-02-03 19:52:09111// TODO(rch): handle 407 proxy auth requested correctly, perhaps
112// by creating a new stream for the subsequent request.
[email protected]fe2f62a2010-10-01 03:34:07113// TODO(rch): create a more appropriate error code to disambiguate
114// the HTTPS Proxy tunnel failure from an HTTP Proxy tunnel failure.
[email protected]dbf036f2011-12-06 23:33:24115int SpdyProxyClientSocket::Connect(const CompletionCallback& callback) {
[email protected]83039bb2011-12-09 18:43:55116 DCHECK(read_callback_.is_null());
[email protected]d9da5fe2010-10-13 22:37:16117 if (next_state_ == STATE_OPEN)
[email protected]fe2f62a2010-10-01 03:34:07118 return OK;
119
[email protected]d9da5fe2010-10-13 22:37:16120 DCHECK_EQ(STATE_DISCONNECTED, next_state_);
[email protected]fe2f62a2010-10-01 03:34:07121 next_state_ = STATE_GENERATE_AUTH_TOKEN;
122
123 int rv = DoLoop(OK);
124 if (rv == ERR_IO_PENDING)
125 read_callback_ = callback;
126 return rv;
127}
128
129void SpdyProxyClientSocket::Disconnect() {
[email protected]ca690b02013-04-17 10:38:43130 read_buffer_queue_.Clear();
[email protected]d9da5fe2010-10-13 22:37:16131 user_buffer_ = NULL;
[email protected]ca690b02013-04-17 10:38:43132 user_buffer_len_ = 0;
[email protected]dbf036f2011-12-06 23:33:24133 read_callback_.Reset();
[email protected]d9da5fe2010-10-13 22:37:16134
135 write_buffer_len_ = 0;
[email protected]83039bb2011-12-09 18:43:55136 write_callback_.Reset();
[email protected]0a428c22014-06-14 02:26:37137 write_callback_weak_factory_.InvalidateWeakPtrs();
[email protected]d9da5fe2010-10-13 22:37:16138
139 next_state_ = STATE_DISCONNECTED;
140
[email protected]11fbca0b2013-06-02 23:37:21141 if (spdy_stream_.get()) {
[email protected]fe2f62a2010-10-01 03:34:07142 // This will cause OnClose to be invoked, which takes care of
143 // cleaning up all the internal state.
144 spdy_stream_->Cancel();
[email protected]11fbca0b2013-06-02 23:37:21145 DCHECK(!spdy_stream_.get());
[email protected]f6a78292013-03-09 14:36:34146 }
[email protected]fe2f62a2010-10-01 03:34:07147}
148
149bool SpdyProxyClientSocket::IsConnected() const {
[email protected]194c7a92011-12-03 04:54:18150 return next_state_ == STATE_OPEN;
[email protected]fe2f62a2010-10-01 03:34:07151}
152
153bool SpdyProxyClientSocket::IsConnectedAndIdle() const {
[email protected]ca690b02013-04-17 10:38:43154 return IsConnected() && read_buffer_queue_.IsEmpty() &&
[email protected]5ff0ed32014-02-12 17:48:51155 spdy_stream_->IsOpen();
[email protected]fe2f62a2010-10-01 03:34:07156}
157
[email protected]e4be2dd2010-12-14 00:44:39158const BoundNetLog& SpdyProxyClientSocket::NetLog() const {
159 return net_log_;
160}
161
[email protected]fe2f62a2010-10-01 03:34:07162void SpdyProxyClientSocket::SetSubresourceSpeculation() {
163 // TODO(rch): what should this implementation be?
164}
165
166void SpdyProxyClientSocket::SetOmniboxSpeculation() {
167 // TODO(rch): what should this implementation be?
168}
169
170bool SpdyProxyClientSocket::WasEverUsed() const {
[email protected]11fbca0b2013-06-02 23:37:21171 return was_ever_used_ || (spdy_stream_.get() && spdy_stream_->WasEverUsed());
[email protected]fe2f62a2010-10-01 03:34:07172}
173
[email protected]7f7e92392010-10-26 18:29:29174bool SpdyProxyClientSocket::UsingTCPFastOpen() const {
175 return false;
176}
177
[email protected]2d88e7d2012-07-19 17:55:17178bool SpdyProxyClientSocket::WasNpnNegotiated() const {
179 return false;
180}
181
[email protected]33661e482012-04-03 16:16:26182NextProto SpdyProxyClientSocket::GetNegotiatedProtocol() const {
183 return kProtoUnknown;
184}
185
[email protected]2d88e7d2012-07-19 17:55:17186bool SpdyProxyClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
187 bool was_npn_negotiated;
188 NextProto protocol_negotiated;
189 return spdy_stream_->GetSSLInfo(ssl_info, &was_npn_negotiated,
190 &protocol_negotiated);
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);
[email protected]aa19cfc2013-05-23 16:41:38231 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT,
232 buf_len, buf->data());
233 write_callback_ = callback;
234 write_buffer_len_ = buf_len;
235 return ERR_IO_PENDING;
[email protected]fe2f62a2010-10-01 03:34:07236}
237
[email protected]28b96d1c2014-04-09 12:21:15238int SpdyProxyClientSocket::SetReceiveBufferSize(int32 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
[email protected]28b96d1c2014-04-09 12:21:15244int SpdyProxyClientSocket::SetSendBufferSize(int32 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);
[email protected]f6c63db52013-02-02 00:35:22299 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_SEND_REQUEST);
[email protected]fe2f62a2010-10-01 03:34:07300 rv = DoSendRequest();
301 break;
302 case STATE_SEND_REQUEST_COMPLETE:
[email protected]d7fd1782011-02-08 19:16:43303 net_log_.EndEventWithNetErrorCode(
304 NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_SEND_REQUEST, rv);
[email protected]fe2f62a2010-10-01 03:34:07305 rv = DoSendRequestComplete(rv);
[email protected]f6c63db52013-02-02 00:35:22306 if (rv >= 0 || rv == ERR_IO_PENDING) {
307 // Emit extra event so can use the same events as
308 // HttpProxyClientSocket.
309 net_log_.BeginEvent(
310 NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS);
311 }
[email protected]fe2f62a2010-10-01 03:34:07312 break;
313 case STATE_READ_REPLY_COMPLETE:
314 rv = DoReadReplyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43315 net_log_.EndEventWithNetErrorCode(
316 NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS, rv);
[email protected]fe2f62a2010-10-01 03:34:07317 break;
318 default:
319 NOTREACHED() << "bad state";
320 rv = ERR_UNEXPECTED;
321 break;
322 }
[email protected]d9da5fe2010-10-13 22:37:16323 } while (rv != ERR_IO_PENDING && next_state_ != STATE_DISCONNECTED &&
324 next_state_ != STATE_OPEN);
[email protected]fe2f62a2010-10-01 03:34:07325 return rv;
326}
327
328int SpdyProxyClientSocket::DoGenerateAuthToken() {
329 next_state_ = STATE_GENERATE_AUTH_TOKEN_COMPLETE;
[email protected]49639fa2011-12-20 23:22:41330 return auth_->MaybeGenerateAuthToken(
331 &request_,
[email protected]f6a78292013-03-09 14:36:34332 base::Bind(&SpdyProxyClientSocket::OnIOComplete,
333 weak_factory_.GetWeakPtr()),
[email protected]49639fa2011-12-20 23:22:41334 net_log_);
[email protected]fe2f62a2010-10-01 03:34:07335}
336
337int SpdyProxyClientSocket::DoGenerateAuthTokenComplete(int result) {
338 DCHECK_NE(ERR_IO_PENDING, result);
339 if (result == OK)
340 next_state_ = STATE_SEND_REQUEST;
341 return result;
342}
343
344int SpdyProxyClientSocket::DoSendRequest() {
345 next_state_ = STATE_SEND_REQUEST_COMPLETE;
346
347 // Add Proxy-Authentication header if necessary.
348 HttpRequestHeaders authorization_headers;
349 if (auth_->HaveAuth()) {
350 auth_->AddAuthorizationHeader(&authorization_headers);
351 }
352
353 std::string request_line;
rchecd3c552015-04-07 20:53:54354 BuildTunnelRequest(endpoint_, authorization_headers, user_agent_,
355 &request_line, &request_.extra_headers);
[email protected]3abacd62012-06-10 20:20:32356
357 net_log_.AddEvent(
358 NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS,
359 base::Bind(&HttpRequestHeaders::NetLogCallback,
rchecd3c552015-04-07 20:53:54360 base::Unretained(&request_.extra_headers), &request_line));
[email protected]fe2f62a2010-10-01 03:34:07361
[email protected]0a01d890fc2012-07-18 17:24:05362 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock());
rchecd3c552015-04-07 20:53:54363 CreateSpdyHeadersFromHttpRequest(request_, request_.extra_headers,
[email protected]585df962014-07-01 22:21:23364 spdy_stream_->GetProtocolVersion(), true,
365 headers.get());
[email protected]fe2f62a2010-10-01 03:34:07366 // Reset the URL to be the endpoint of the connection
[email protected]0e861e92012-03-15 18:42:19367 if (spdy_stream_->GetProtocolVersion() > 2) {
368 (*headers)[":path"] = endpoint_.ToString();
369 headers->erase(":scheme");
370 } else {
371 (*headers)["url"] = endpoint_.ToString();
372 headers->erase("scheme");
373 }
[email protected]fe2f62a2010-10-01 03:34:07374
[email protected]528b3452013-05-25 01:28:54375 return spdy_stream_->SendRequestHeaders(headers.Pass(), MORE_DATA_TO_SEND);
[email protected]fe2f62a2010-10-01 03:34:07376}
377
378int SpdyProxyClientSocket::DoSendRequestComplete(int result) {
379 if (result < 0)
380 return result;
381
382 // Wait for SYN_REPLY frame from the server
383 next_state_ = STATE_READ_REPLY_COMPLETE;
384 return ERR_IO_PENDING;
385}
386
387int SpdyProxyClientSocket::DoReadReplyComplete(int result) {
388 // We enter this method directly from DoSendRequestComplete, since
389 // we are notified by a callback when the SYN_REPLY frame arrives
390
391 if (result < 0)
392 return result;
393
[email protected]fe2f62a2010-10-01 03:34:07394 // Require the "HTTP/1.x" status line for SSL CONNECT.
395 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0))
396 return ERR_TUNNEL_CONNECTION_FAILED;
397
[email protected]3abacd62012-06-10 20:20:32398 net_log_.AddEvent(
399 NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS,
400 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]fe2f62a2010-10-01 03:34:07401
[email protected]4eddbc732012-08-09 05:40:17402 switch (response_.headers->response_code()) {
403 case 200: // OK
404 next_state_ = STATE_OPEN;
405 return OK;
406
407 case 302: // Found / Moved Temporarily
408 // Try to return a sanitized response so we can follow auth redirects.
409 // If we can't, fail the tunnel connection.
ttuttle7933c112015-01-06 00:55:24410 if (!SanitizeProxyRedirect(&response_)) {
[email protected]4eddbc732012-08-09 05:40:17411 LogBlockedTunnelResponse();
412 return ERR_TUNNEL_CONNECTION_FAILED;
413 }
414
ttuttle7933c112015-01-06 00:55:24415 redirect_has_load_timing_info_ =
416 spdy_stream_->GetLoadTimingInfo(&redirect_load_timing_info_);
417 // Note that this triggers a RST_STREAM_CANCEL.
418 spdy_stream_->DetachDelegate();
419 next_state_ = STATE_DISCONNECTED;
420 return ERR_HTTPS_PROXY_TUNNEL_RESPONSE;
421
[email protected]4eddbc732012-08-09 05:40:17422 case 407: // Proxy Authentication Required
423 next_state_ = STATE_OPEN;
ttuttle7933c112015-01-06 00:55:24424 if (!SanitizeProxyAuth(&response_)) {
425 LogBlockedTunnelResponse();
426 return ERR_TUNNEL_CONNECTION_FAILED;
427 }
[email protected]90499482013-06-01 00:39:50428 return HandleProxyAuthChallenge(auth_.get(), &response_, net_log_);
[email protected]4eddbc732012-08-09 05:40:17429
430 default:
431 // Ignore response to avoid letting the proxy impersonate the target
432 // server. (See http://crbug.com/137891.)
433 LogBlockedTunnelResponse();
434 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]511f6f52010-12-17 03:58:29435 }
[email protected]fe2f62a2010-10-01 03:34:07436}
437
438// SpdyStream::Delegate methods:
439// Called when SYN frame has been sent.
440// Returns true if no more data to be sent after SYN frame.
[email protected]edbfa8c2013-05-29 00:22:33441void SpdyProxyClientSocket::OnRequestHeadersSent() {
[email protected]fe2f62a2010-10-01 03:34:07442 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE);
443
[email protected]d46715c2013-04-15 00:21:42444 OnIOComplete(OK);
[email protected]fe2f62a2010-10-01 03:34:07445}
446
[email protected]6d116e1a2013-06-24 07:42:15447SpdyResponseHeadersStatus SpdyProxyClientSocket::OnResponseHeadersUpdated(
448 const SpdyHeaderBlock& response_headers) {
[email protected]d08358502010-12-03 22:04:03449 // If we've already received the reply, existing headers are too late.
450 // TODO(mbelshe): figure out a way to make HEADERS frames useful after the
451 // initial response.
452 if (next_state_ != STATE_READ_REPLY_COMPLETE)
[email protected]6d116e1a2013-06-24 07:42:15453 return RESPONSE_HEADERS_ARE_COMPLETE;
[email protected]fe2f62a2010-10-01 03:34:07454
[email protected]d08358502010-12-03 22:04:03455 // Save the response
[email protected]d42dedd02012-04-03 19:42:06456 if (!SpdyHeadersToHttpResponse(
[email protected]6d116e1a2013-06-24 07:42:15457 response_headers, spdy_stream_->GetProtocolVersion(), &response_))
458 return RESPONSE_HEADERS_ARE_INCOMPLETE;
[email protected]fe2f62a2010-10-01 03:34:07459
[email protected]6d116e1a2013-06-24 07:42:15460 OnIOComplete(OK);
461 return RESPONSE_HEADERS_ARE_COMPLETE;
[email protected]fe2f62a2010-10-01 03:34:07462}
463
[email protected]ca690b02013-04-17 10:38:43464// Called when data is received or on EOF (if |buffer| is NULL).
[email protected]6d116e1a2013-06-24 07:42:15465void SpdyProxyClientSocket::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) {
[email protected]ca690b02013-04-17 10:38:43466 if (buffer) {
467 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED,
468 buffer->GetRemainingSize(),
469 buffer->GetRemainingData());
470 read_buffer_queue_.Enqueue(buffer.Pass());
471 } else {
472 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, 0, NULL);
[email protected]fe2f62a2010-10-01 03:34:07473 }
474
[email protected]83039bb2011-12-09 18:43:55475 if (!read_callback_.is_null()) {
[email protected]ca690b02013-04-17 10:38:43476 int rv = PopulateUserReadBuffer(user_buffer_->data(), user_buffer_len_);
[email protected]dbf036f2011-12-06 23:33:24477 CompletionCallback c = read_callback_;
478 read_callback_.Reset();
479 user_buffer_ = NULL;
[email protected]ca690b02013-04-17 10:38:43480 user_buffer_len_ = 0;
[email protected]dbf036f2011-12-06 23:33:24481 c.Run(rv);
[email protected]fe2f62a2010-10-01 03:34:07482 }
483}
484
[email protected]aa19cfc2013-05-23 16:41:38485void SpdyProxyClientSocket::OnDataSent() {
[email protected]83039bb2011-12-09 18:43:55486 DCHECK(!write_callback_.is_null());
[email protected]fe2f62a2010-10-01 03:34:07487
[email protected]aa19cfc2013-05-23 16:41:38488 int rv = write_buffer_len_;
489 write_buffer_len_ = 0;
[email protected]89d4d792014-04-06 16:27:15490
491 // Proxy write callbacks result in deep callback chains. Post to allow the
492 // stream's write callback chain to unwind (see crbug.com/355511).
493 base::MessageLoop::current()->PostTask(
494 FROM_HERE,
[email protected]0a428c22014-06-14 02:26:37495 base::Bind(&SpdyProxyClientSocket::RunCallback,
496 write_callback_weak_factory_.GetWeakPtr(),
497 ResetAndReturn(&write_callback_),
498 rv));
[email protected]fe2f62a2010-10-01 03:34:07499}
500
501void SpdyProxyClientSocket::OnClose(int status) {
[email protected]fe2f62a2010-10-01 03:34:07502 was_ever_used_ = spdy_stream_->WasEverUsed();
[email protected]d26ff352013-05-13 08:48:28503 spdy_stream_.reset();
[email protected]d9da5fe2010-10-13 22:37:16504
505 bool connecting = next_state_ != STATE_DISCONNECTED &&
506 next_state_ < STATE_OPEN;
507 if (next_state_ == STATE_OPEN)
508 next_state_ = STATE_CLOSED;
509 else
510 next_state_ = STATE_DISCONNECTED;
511
[email protected]6af4e412011-11-29 23:39:18512 base::WeakPtr<SpdyProxyClientSocket> weak_ptr = weak_factory_.GetWeakPtr();
[email protected]83039bb2011-12-09 18:43:55513 CompletionCallback write_callback = write_callback_;
514 write_callback_.Reset();
[email protected]d9da5fe2010-10-13 22:37:16515 write_buffer_len_ = 0;
[email protected]d9da5fe2010-10-13 22:37:16516
517 // If we're in the middle of connecting, we need to make sure
518 // we invoke the connect callback.
519 if (connecting) {
[email protected]83039bb2011-12-09 18:43:55520 DCHECK(!read_callback_.is_null());
521 CompletionCallback read_callback = read_callback_;
522 read_callback_.Reset();
523 read_callback.Run(status);
524 } else if (!read_callback_.is_null()) {
[email protected]6af4e412011-11-29 23:39:18525 // If we have a read_callback_, the we need to make sure we call it back.
[email protected]ca690b02013-04-17 10:38:43526 OnDataReceived(scoped_ptr<SpdyBuffer>());
[email protected]d9da5fe2010-10-13 22:37:16527 }
[email protected]6af4e412011-11-29 23:39:18528 // This may have been deleted by read_callback_, so check first.
[email protected]11fbca0b2013-06-02 23:37:21529 if (weak_ptr.get() && !write_callback.is_null())
[email protected]83039bb2011-12-09 18:43:55530 write_callback.Run(ERR_CONNECTION_CLOSED);
[email protected]fe2f62a2010-10-01 03:34:07531}
532
[email protected]fe2f62a2010-10-01 03:34:07533} // namespace net