OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/spdy_proxy_client_socket.h" | 5 #include "net/spdy/spdy_proxy_client_socket.h" |
6 | 6 |
7 #include <algorithm> // min | 7 #include <algorithm> // min |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 SpdyProxyClientSocket::SpdyProxyClientSocket( | 23 SpdyProxyClientSocket::SpdyProxyClientSocket( |
24 SpdyStream* spdy_stream, | 24 SpdyStream* spdy_stream, |
25 const std::string& user_agent, | 25 const std::string& user_agent, |
26 const HostPortPair& endpoint, | 26 const HostPortPair& endpoint, |
27 const GURL& url, | 27 const GURL& url, |
28 const HostPortPair& proxy_server, | 28 const HostPortPair& proxy_server, |
29 HttpAuthCache* auth_cache, | 29 HttpAuthCache* auth_cache, |
30 HttpAuthHandlerFactory* auth_handler_factory) | 30 HttpAuthHandlerFactory* auth_handler_factory) |
31 : ALLOW_THIS_IN_INITIALIZER_LIST( | 31 : ALLOW_THIS_IN_INITIALIZER_LIST( |
32 io_callback_(this, &SpdyProxyClientSocket::OnIOComplete)), | 32 io_callback_(this, &SpdyProxyClientSocket::OnIOComplete)), |
33 next_state_(STATE_NONE), | 33 next_state_(STATE_DISCONNECTED), |
34 spdy_stream_(spdy_stream), | 34 spdy_stream_(spdy_stream), |
35 read_callback_(NULL), | 35 read_callback_(NULL), |
36 write_callback_(NULL), | 36 write_callback_(NULL), |
37 endpoint_(endpoint), | 37 endpoint_(endpoint), |
38 auth_( | 38 auth_( |
39 new HttpAuthController(HttpAuth::AUTH_PROXY, | 39 new HttpAuthController(HttpAuth::AUTH_PROXY, |
40 GURL("http://" + proxy_server.ToString()), | 40 GURL("http://" + proxy_server.ToString()), |
41 auth_cache, | 41 auth_cache, |
42 auth_handler_factory)), | 42 auth_handler_factory)), |
43 user_buffer_(NULL), | 43 user_buffer_(NULL), |
(...skipping 19 matching lines...) Expand all Loading... |
63 // a SYN_REPLY frame. OK will be returned if the status is 200. | 63 // a SYN_REPLY frame. OK will be returned if the status is 200. |
64 // ERR_TUNNEL_CONNECTION_FAILED will be returned for any other status. | 64 // ERR_TUNNEL_CONNECTION_FAILED will be returned for any other status. |
65 // In any of these cases, Read() may be called to retrieve the HTTP | 65 // In any of these cases, Read() may be called to retrieve the HTTP |
66 // response body. Any other return values should be considered fatal. | 66 // response body. Any other return values should be considered fatal. |
67 // TODO(rch): handle 407 proxy auth requested correctly, perhaps | 67 // TODO(rch): handle 407 proxy auth requested correctly, perhaps |
68 // by creating a new stream for the subsequent request. | 68 // by creating a new stream for the subsequent request. |
69 // TODO(rch): create a more appropriate error code to disambiguate | 69 // TODO(rch): create a more appropriate error code to disambiguate |
70 // the HTTPS Proxy tunnel failure from an HTTP Proxy tunnel failure. | 70 // the HTTPS Proxy tunnel failure from an HTTP Proxy tunnel failure. |
71 int SpdyProxyClientSocket::Connect(CompletionCallback* callback) { | 71 int SpdyProxyClientSocket::Connect(CompletionCallback* callback) { |
72 DCHECK(!read_callback_); | 72 DCHECK(!read_callback_); |
73 if (next_state_ == STATE_DONE) | 73 if (next_state_ == STATE_OPEN) |
74 return OK; | 74 return OK; |
75 | 75 |
76 DCHECK_EQ(STATE_NONE, next_state_); | 76 DCHECK_EQ(STATE_DISCONNECTED, next_state_); |
77 next_state_ = STATE_GENERATE_AUTH_TOKEN; | 77 next_state_ = STATE_GENERATE_AUTH_TOKEN; |
78 | 78 |
79 int rv = DoLoop(OK); | 79 int rv = DoLoop(OK); |
80 if (rv == ERR_IO_PENDING) | 80 if (rv == ERR_IO_PENDING) |
81 read_callback_ = callback; | 81 read_callback_ = callback; |
82 return rv; | 82 return rv; |
83 } | 83 } |
84 | 84 |
85 void SpdyProxyClientSocket::Disconnect() { | 85 void SpdyProxyClientSocket::Disconnect() { |
86 next_state_ = STATE_NONE; | 86 read_buffer_.clear(); |
| 87 user_buffer_ = NULL; |
| 88 read_callback_ = NULL; |
| 89 |
| 90 write_buffer_len_ = 0; |
| 91 write_bytes_outstanding_ = 0; |
| 92 write_callback_ = NULL; |
| 93 |
| 94 next_state_ = STATE_DISCONNECTED; |
| 95 |
87 if (spdy_stream_) | 96 if (spdy_stream_) |
88 // This will cause OnClose to be invoked, which takes care of | 97 // This will cause OnClose to be invoked, which takes care of |
89 // cleaning up all the internal state. | 98 // cleaning up all the internal state. |
90 spdy_stream_->Cancel(); | 99 spdy_stream_->Cancel(); |
91 } | 100 } |
92 | 101 |
93 bool SpdyProxyClientSocket::IsConnected() const { | 102 bool SpdyProxyClientSocket::IsConnected() const { |
94 return next_state_ == STATE_DONE && spdy_stream_ != NULL && | 103 return next_state_ == STATE_OPEN || next_state_ == STATE_CLOSED; |
95 !spdy_stream_->closed(); | |
96 } | 104 } |
97 | 105 |
98 bool SpdyProxyClientSocket::IsConnectedAndIdle() const { | 106 bool SpdyProxyClientSocket::IsConnectedAndIdle() const { |
99 return IsConnected() && !spdy_stream_->is_idle(); | 107 return IsConnected() && !spdy_stream_->is_idle(); |
100 } | 108 } |
101 | 109 |
102 void SpdyProxyClientSocket::SetSubresourceSpeculation() { | 110 void SpdyProxyClientSocket::SetSubresourceSpeculation() { |
103 // TODO(rch): what should this implementation be? | 111 // TODO(rch): what should this implementation be? |
104 } | 112 } |
105 | 113 |
106 void SpdyProxyClientSocket::SetOmniboxSpeculation() { | 114 void SpdyProxyClientSocket::SetOmniboxSpeculation() { |
107 // TODO(rch): what should this implementation be? | 115 // TODO(rch): what should this implementation be? |
108 } | 116 } |
109 | 117 |
110 bool SpdyProxyClientSocket::WasEverUsed() const { | 118 bool SpdyProxyClientSocket::WasEverUsed() const { |
111 return was_ever_used_ || (spdy_stream_ && spdy_stream_->WasEverUsed()); | 119 return was_ever_used_ || (spdy_stream_ && spdy_stream_->WasEverUsed()); |
112 } | 120 } |
113 | 121 |
114 int SpdyProxyClientSocket::Read(IOBuffer* buf, int buf_len, | 122 int SpdyProxyClientSocket::Read(IOBuffer* buf, int buf_len, |
115 CompletionCallback* callback) { | 123 CompletionCallback* callback) { |
116 DCHECK(!read_callback_); | 124 DCHECK(!read_callback_); |
117 DCHECK(!user_buffer_); | 125 DCHECK(!user_buffer_); |
118 | 126 |
119 if (!spdy_stream_) { | 127 if (next_state_ == STATE_DISCONNECTED) |
| 128 return ERR_SOCKET_NOT_CONNECTED; |
| 129 |
| 130 if (!spdy_stream_ && read_buffer_.empty()) { |
120 if (eof_has_been_read_) | 131 if (eof_has_been_read_) |
121 return ERR_CONNECTION_CLOSED; | 132 return ERR_CONNECTION_CLOSED; |
122 eof_has_been_read_ = true; | 133 eof_has_been_read_ = true; |
123 return 0; | 134 return 0; |
124 } | 135 } |
125 | 136 |
126 DCHECK(next_state_ == STATE_DONE); | 137 DCHECK(next_state_ == STATE_OPEN || next_state_ == STATE_CLOSED); |
127 DCHECK(buf); | 138 DCHECK(buf); |
128 user_buffer_ = new DrainableIOBuffer(buf, buf_len); | 139 user_buffer_ = new DrainableIOBuffer(buf, buf_len); |
129 int result = PopulateUserReadBuffer(); | 140 int result = PopulateUserReadBuffer(); |
130 if (result == 0) { | 141 if (result == 0) { |
131 DCHECK(callback); | 142 DCHECK(callback); |
132 read_callback_ = callback; | 143 read_callback_ = callback; |
133 return ERR_IO_PENDING; | 144 return ERR_IO_PENDING; |
134 } | 145 } |
135 user_buffer_ = NULL; | 146 user_buffer_ = NULL; |
136 return result; | 147 return result; |
137 } | 148 } |
138 | 149 |
139 int SpdyProxyClientSocket::PopulateUserReadBuffer() { | 150 int SpdyProxyClientSocket::PopulateUserReadBuffer() { |
140 if (!user_buffer_) | 151 if (!user_buffer_) |
141 return ERR_IO_PENDING; | 152 return ERR_IO_PENDING; |
142 | 153 |
143 while (!read_buffer_.empty() && user_buffer_->BytesRemaining() > 0) { | 154 while (!read_buffer_.empty() && user_buffer_->BytesRemaining() > 0) { |
144 scoped_refptr<DrainableIOBuffer> data = read_buffer_.front(); | 155 scoped_refptr<DrainableIOBuffer> data = read_buffer_.front(); |
145 const int bytes_to_copy = std::min(user_buffer_->BytesRemaining(), | 156 const int bytes_to_copy = std::min(user_buffer_->BytesRemaining(), |
146 data->BytesRemaining()); | 157 data->BytesRemaining()); |
147 memcpy(user_buffer_->data(), data->data(), bytes_to_copy); | 158 memcpy(user_buffer_->data(), data->data(), bytes_to_copy); |
148 user_buffer_->DidConsume(bytes_to_copy); | 159 user_buffer_->DidConsume(bytes_to_copy); |
149 if (data->BytesRemaining() == 0) { | 160 if (data->BytesRemaining() == bytes_to_copy) { |
150 // Consumed all data from this buffer | 161 // Consumed all data from this buffer |
151 read_buffer_.pop_front(); | 162 read_buffer_.pop_front(); |
152 } else { | 163 } else { |
153 data->DidConsume(bytes_to_copy); | 164 data->DidConsume(bytes_to_copy); |
154 } | 165 } |
155 } | 166 } |
156 | 167 |
157 return user_buffer_->BytesConsumed(); | 168 return user_buffer_->BytesConsumed(); |
158 } | 169 } |
159 | 170 |
160 int SpdyProxyClientSocket::Write(IOBuffer* buf, int buf_len, | 171 int SpdyProxyClientSocket::Write(IOBuffer* buf, int buf_len, |
161 CompletionCallback* callback) { | 172 CompletionCallback* callback) { |
162 DCHECK(!write_callback_); | 173 DCHECK(!write_callback_); |
| 174 if (next_state_ == STATE_DISCONNECTED) |
| 175 return ERR_SOCKET_NOT_CONNECTED; |
| 176 |
163 if (!spdy_stream_) | 177 if (!spdy_stream_) |
164 return ERR_CONNECTION_CLOSED; | 178 return ERR_CONNECTION_CLOSED; |
165 | 179 |
166 write_bytes_outstanding_= buf_len; | 180 write_bytes_outstanding_= buf_len; |
167 if (buf_len <= kMaxSpdyFrameChunkSize) { | 181 if (buf_len <= kMaxSpdyFrameChunkSize) { |
168 int rv = spdy_stream_->WriteStreamData(buf, buf_len, spdy::DATA_FLAG_NONE); | 182 int rv = spdy_stream_->WriteStreamData(buf, buf_len, spdy::DATA_FLAG_NONE); |
169 if (rv == ERR_IO_PENDING) { | 183 if (rv == ERR_IO_PENDING) { |
170 write_callback_ = callback; | 184 write_callback_ = callback; |
171 write_buffer_len_ = buf_len; | 185 write_buffer_len_ = buf_len; |
172 } | 186 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 return false; | 221 return false; |
208 } | 222 } |
209 | 223 |
210 int SpdyProxyClientSocket::GetPeerAddress(AddressList* address) const { | 224 int SpdyProxyClientSocket::GetPeerAddress(AddressList* address) const { |
211 if (!IsConnected()) | 225 if (!IsConnected()) |
212 return ERR_SOCKET_NOT_CONNECTED; | 226 return ERR_SOCKET_NOT_CONNECTED; |
213 return spdy_stream_->GetPeerAddress(address); | 227 return spdy_stream_->GetPeerAddress(address); |
214 } | 228 } |
215 | 229 |
216 void SpdyProxyClientSocket::OnIOComplete(int result) { | 230 void SpdyProxyClientSocket::OnIOComplete(int result) { |
217 DCHECK_NE(STATE_NONE, next_state_); | 231 DCHECK_NE(STATE_DISCONNECTED, next_state_); |
218 int rv = DoLoop(result); | 232 int rv = DoLoop(result); |
219 if (rv != ERR_IO_PENDING) { | 233 if (rv != ERR_IO_PENDING) { |
220 CompletionCallback* c = read_callback_; | 234 CompletionCallback* c = read_callback_; |
221 read_callback_ = NULL; | 235 read_callback_ = NULL; |
222 c->Run(rv); | 236 c->Run(rv); |
223 } | 237 } |
224 } | 238 } |
225 | 239 |
226 int SpdyProxyClientSocket::DoLoop(int last_io_result) { | 240 int SpdyProxyClientSocket::DoLoop(int last_io_result) { |
227 DCHECK_NE(next_state_, STATE_NONE); | 241 DCHECK_NE(next_state_, STATE_DISCONNECTED); |
228 int rv = last_io_result; | 242 int rv = last_io_result; |
229 do { | 243 do { |
230 State state = next_state_; | 244 State state = next_state_; |
231 next_state_ = STATE_NONE; | 245 next_state_ = STATE_DISCONNECTED; |
232 switch (state) { | 246 switch (state) { |
233 case STATE_GENERATE_AUTH_TOKEN: | 247 case STATE_GENERATE_AUTH_TOKEN: |
234 DCHECK_EQ(OK, rv); | 248 DCHECK_EQ(OK, rv); |
235 rv = DoGenerateAuthToken(); | 249 rv = DoGenerateAuthToken(); |
236 break; | 250 break; |
237 case STATE_GENERATE_AUTH_TOKEN_COMPLETE: | 251 case STATE_GENERATE_AUTH_TOKEN_COMPLETE: |
238 rv = DoGenerateAuthTokenComplete(rv); | 252 rv = DoGenerateAuthTokenComplete(rv); |
239 break; | 253 break; |
240 case STATE_SEND_REQUEST: | 254 case STATE_SEND_REQUEST: |
241 DCHECK_EQ(OK, rv); | 255 DCHECK_EQ(OK, rv); |
242 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_SEND_REQUEST, | 256 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_SEND_REQUEST, |
243 NULL); | 257 NULL); |
244 rv = DoSendRequest(); | 258 rv = DoSendRequest(); |
245 break; | 259 break; |
246 case STATE_SEND_REQUEST_COMPLETE: | 260 case STATE_SEND_REQUEST_COMPLETE: |
247 rv = DoSendRequestComplete(rv); | 261 rv = DoSendRequestComplete(rv); |
248 net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_SEND_REQUEST, | 262 net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_SEND_REQUEST, |
249 NULL); | 263 NULL); |
250 break; | 264 break; |
251 case STATE_READ_REPLY_COMPLETE: | 265 case STATE_READ_REPLY_COMPLETE: |
252 rv = DoReadReplyComplete(rv); | 266 rv = DoReadReplyComplete(rv); |
253 net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS, | 267 net_log_.EndEvent(NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS, |
254 NULL); | 268 NULL); |
255 break; | 269 break; |
256 default: | 270 default: |
257 NOTREACHED() << "bad state"; | 271 NOTREACHED() << "bad state"; |
258 rv = ERR_UNEXPECTED; | 272 rv = ERR_UNEXPECTED; |
259 break; | 273 break; |
260 } | 274 } |
261 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE && | 275 } while (rv != ERR_IO_PENDING && next_state_ != STATE_DISCONNECTED && |
262 next_state_ != STATE_DONE); | 276 next_state_ != STATE_OPEN); |
263 return rv; | 277 return rv; |
264 } | 278 } |
265 | 279 |
266 int SpdyProxyClientSocket::DoGenerateAuthToken() { | 280 int SpdyProxyClientSocket::DoGenerateAuthToken() { |
267 next_state_ = STATE_GENERATE_AUTH_TOKEN_COMPLETE; | 281 next_state_ = STATE_GENERATE_AUTH_TOKEN_COMPLETE; |
268 return auth_->MaybeGenerateAuthToken(&request_, &io_callback_, net_log_); | 282 return auth_->MaybeGenerateAuthToken(&request_, &io_callback_, net_log_); |
269 } | 283 } |
270 | 284 |
271 int SpdyProxyClientSocket::DoGenerateAuthTokenComplete(int result) { | 285 int SpdyProxyClientSocket::DoGenerateAuthTokenComplete(int result) { |
272 DCHECK_NE(ERR_IO_PENDING, result); | 286 DCHECK_NE(ERR_IO_PENDING, result); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 return ERR_IO_PENDING; | 329 return ERR_IO_PENDING; |
316 } | 330 } |
317 | 331 |
318 int SpdyProxyClientSocket::DoReadReplyComplete(int result) { | 332 int SpdyProxyClientSocket::DoReadReplyComplete(int result) { |
319 // We enter this method directly from DoSendRequestComplete, since | 333 // We enter this method directly from DoSendRequestComplete, since |
320 // we are notified by a callback when the SYN_REPLY frame arrives | 334 // we are notified by a callback when the SYN_REPLY frame arrives |
321 | 335 |
322 if (result < 0) | 336 if (result < 0) |
323 return result; | 337 return result; |
324 | 338 |
325 next_state_ = STATE_DONE; | |
326 // Require the "HTTP/1.x" status line for SSL CONNECT. | 339 // Require the "HTTP/1.x" status line for SSL CONNECT. |
327 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) | 340 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) |
328 return ERR_TUNNEL_CONNECTION_FAILED; | 341 return ERR_TUNNEL_CONNECTION_FAILED; |
329 | 342 |
| 343 next_state_ = STATE_OPEN; |
330 if (net_log_.IsLoggingAll()) { | 344 if (net_log_.IsLoggingAll()) { |
331 net_log_.AddEvent( | 345 net_log_.AddEvent( |
332 NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, | 346 NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
333 new NetLogHttpResponseParameter(response_.headers)); | 347 new NetLogHttpResponseParameter(response_.headers)); |
334 } | 348 } |
335 | 349 |
336 if (response_.headers->response_code() == 200) | 350 if (response_.headers->response_code() == 200) |
337 return OK; | 351 return OK; |
338 else | 352 else |
339 return ERR_TUNNEL_CONNECTION_FAILED; | 353 return ERR_TUNNEL_CONNECTION_FAILED; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 write_buffer_len_ = 0; | 424 write_buffer_len_ = 0; |
411 write_bytes_outstanding_ = 0; | 425 write_bytes_outstanding_ = 0; |
412 CompletionCallback* c = write_callback_; | 426 CompletionCallback* c = write_callback_; |
413 write_callback_ = NULL; | 427 write_callback_ = NULL; |
414 c->Run(rv); | 428 c->Run(rv); |
415 } | 429 } |
416 } | 430 } |
417 | 431 |
418 void SpdyProxyClientSocket::OnClose(int status) { | 432 void SpdyProxyClientSocket::OnClose(int status) { |
419 DCHECK(spdy_stream_); | 433 DCHECK(spdy_stream_); |
| 434 was_ever_used_ = spdy_stream_->WasEverUsed(); |
| 435 spdy_stream_ = NULL; |
| 436 |
| 437 bool connecting = next_state_ != STATE_DISCONNECTED && |
| 438 next_state_ < STATE_OPEN; |
| 439 if (next_state_ == STATE_OPEN) |
| 440 next_state_ = STATE_CLOSED; |
| 441 else |
| 442 next_state_ = STATE_DISCONNECTED; |
| 443 |
| 444 CompletionCallback* write_callback = write_callback_; |
| 445 write_callback_ = NULL; |
| 446 write_buffer_len_ = 0; |
| 447 write_bytes_outstanding_ = 0; |
| 448 |
420 // If we're in the middle of connecting, we need to make sure | 449 // If we're in the middle of connecting, we need to make sure |
421 // we invoke the connect callback. | 450 // we invoke the connect callback. |
422 CompletionCallback* connect_callback = NULL; | 451 if (connecting) { |
423 if (next_state_ != STATE_NONE && next_state_ != STATE_DONE) { | |
424 DCHECK(read_callback_); | 452 DCHECK(read_callback_); |
425 connect_callback = read_callback_; | 453 CompletionCallback* read_callback = read_callback_; |
| 454 read_callback_ = NULL; |
| 455 read_callback->Run(status); |
| 456 } else if (read_callback_) { |
| 457 // If we have a read_callback, the we need to make sure we call it back |
| 458 OnDataReceived(NULL, 0); |
426 } | 459 } |
427 was_ever_used_ = spdy_stream_->WasEverUsed(); | 460 if (write_callback) |
428 spdy_stream_ = NULL; | 461 write_callback->Run(ERR_CONNECTION_CLOSED); |
429 read_callback_ = NULL; | |
430 write_callback_ = NULL; | |
431 user_buffer_ = NULL; | |
432 read_buffer_.empty(); | |
433 if (connect_callback) | |
434 connect_callback->Run(status); | |
435 } | 462 } |
436 | 463 |
437 } // namespace net | 464 } // namespace net |
OLD | NEW |