| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/websockets/websocket_job.h" | 5 #include "net/websockets/websocket_job.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/string_tokenizer.h" | 10 #include "base/string_tokenizer.h" |
| 11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 61 |
| 62 WebSocketJob::WebSocketJob(SocketStream::Delegate* delegate) | 62 WebSocketJob::WebSocketJob(SocketStream::Delegate* delegate) |
| 63 : delegate_(delegate), | 63 : delegate_(delegate), |
| 64 state_(INITIALIZED), | 64 state_(INITIALIZED), |
| 65 waiting_(false), | 65 waiting_(false), |
| 66 callback_(NULL), | 66 callback_(NULL), |
| 67 handshake_request_(new WebSocketHandshakeRequestHandler), | 67 handshake_request_(new WebSocketHandshakeRequestHandler), |
| 68 handshake_response_(new WebSocketHandshakeResponseHandler), | 68 handshake_response_(new WebSocketHandshakeResponseHandler), |
| 69 handshake_request_sent_(0), | 69 handshake_request_sent_(0), |
| 70 response_cookies_save_index_(0), | 70 response_cookies_save_index_(0), |
| 71 ALLOW_THIS_IN_INITIALIZER_LIST(can_get_cookies_callback_( | |
| 72 this, &WebSocketJob::OnCanGetCookiesCompleted)), | |
| 73 ALLOW_THIS_IN_INITIALIZER_LIST(can_set_cookie_callback_( | |
| 74 this, &WebSocketJob::OnCanSetCookieCompleted)), | |
| 75 send_frame_handler_(new WebSocketFrameHandler), | 71 send_frame_handler_(new WebSocketFrameHandler), |
| 76 receive_frame_handler_(new WebSocketFrameHandler) { | 72 receive_frame_handler_(new WebSocketFrameHandler) { |
| 77 } | 73 } |
| 78 | 74 |
| 79 WebSocketJob::~WebSocketJob() { | 75 WebSocketJob::~WebSocketJob() { |
| 80 DCHECK_EQ(CLOSED, state_); | 76 DCHECK_EQ(CLOSED, state_); |
| 81 DCHECK(!delegate_); | 77 DCHECK(!delegate_); |
| 82 DCHECK(!socket_.get()); | 78 DCHECK(!socket_.get()); |
| 83 } | 79 } |
| 84 | 80 |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 if (!handshake_request_->ParseRequest(data, len)) | 270 if (!handshake_request_->ParseRequest(data, len)) |
| 275 return false; | 271 return false; |
| 276 | 272 |
| 277 // handshake message is completed. | 273 // handshake message is completed. |
| 278 AddCookieHeaderAndSend(); | 274 AddCookieHeaderAndSend(); |
| 279 // Just buffered in |handshake_request_|. | 275 // Just buffered in |handshake_request_|. |
| 280 return true; | 276 return true; |
| 281 } | 277 } |
| 282 | 278 |
| 283 void WebSocketJob::AddCookieHeaderAndSend() { | 279 void WebSocketJob::AddCookieHeaderAndSend() { |
| 284 AddRef(); // Balanced in OnCanGetCookiesCompleted | |
| 285 | |
| 286 int policy = OK; | 280 int policy = OK; |
| 287 if (socket_->context()->cookie_policy()) { | 281 if (socket_->context()->cookie_policy()) { |
| 288 GURL url_for_cookies = GetURLForCookies(); | 282 GURL url_for_cookies = GetURLForCookies(); |
| 289 policy = socket_->context()->cookie_policy()->CanGetCookies( | 283 policy = socket_->context()->cookie_policy()->CanGetCookies( |
| 290 url_for_cookies, | 284 url_for_cookies, |
| 291 url_for_cookies, | 285 url_for_cookies); |
| 292 &can_get_cookies_callback_); | |
| 293 if (policy == ERR_IO_PENDING) | |
| 294 return; // Wait for completion callback | |
| 295 } | 286 } |
| 287 DCHECK_NE(ERR_IO_PENDING, policy); |
| 296 OnCanGetCookiesCompleted(policy); | 288 OnCanGetCookiesCompleted(policy); |
| 297 } | 289 } |
| 298 | 290 |
| 299 void WebSocketJob::OnCanGetCookiesCompleted(int policy) { | 291 void WebSocketJob::OnCanGetCookiesCompleted(int policy) { |
| 300 if (socket_ && delegate_ && state_ == CONNECTING) { | 292 if (socket_ && delegate_ && state_ == CONNECTING) { |
| 301 handshake_request_->RemoveHeaders( | 293 handshake_request_->RemoveHeaders( |
| 302 kCookieHeaders, arraysize(kCookieHeaders)); | 294 kCookieHeaders, arraysize(kCookieHeaders)); |
| 303 if (policy == OK) { | 295 if (policy == OK) { |
| 304 // Add cookies, including HttpOnly cookies. | 296 // Add cookies, including HttpOnly cookies. |
| 305 if (socket_->context()->cookie_store()) { | 297 if (socket_->context()->cookie_store()) { |
| 306 CookieOptions cookie_options; | 298 CookieOptions cookie_options; |
| 307 cookie_options.set_include_httponly(); | 299 cookie_options.set_include_httponly(); |
| 308 std::string cookie = | 300 std::string cookie = |
| 309 socket_->context()->cookie_store()->GetCookiesWithOptions( | 301 socket_->context()->cookie_store()->GetCookiesWithOptions( |
| 310 GetURLForCookies(), cookie_options); | 302 GetURLForCookies(), cookie_options); |
| 311 if (!cookie.empty()) | 303 if (!cookie.empty()) |
| 312 handshake_request_->AppendHeaderIfMissing("Cookie", cookie); | 304 handshake_request_->AppendHeaderIfMissing("Cookie", cookie); |
| 313 } | 305 } |
| 314 } | 306 } |
| 315 | 307 |
| 316 const std::string& handshake_request = handshake_request_->GetRawRequest(); | 308 const std::string& handshake_request = handshake_request_->GetRawRequest(); |
| 317 handshake_request_sent_ = 0; | 309 handshake_request_sent_ = 0; |
| 318 socket_->net_log()->AddEvent( | 310 socket_->net_log()->AddEvent( |
| 319 NetLog::TYPE_WEB_SOCKET_SEND_REQUEST_HEADERS, | 311 NetLog::TYPE_WEB_SOCKET_SEND_REQUEST_HEADERS, |
| 320 make_scoped_refptr( | 312 make_scoped_refptr( |
| 321 new NetLogWebSocketHandshakeParameter(handshake_request))); | 313 new NetLogWebSocketHandshakeParameter(handshake_request))); |
| 322 socket_->SendData(handshake_request.data(), | 314 socket_->SendData(handshake_request.data(), |
| 323 handshake_request.size()); | 315 handshake_request.size()); |
| 324 } | 316 } |
| 325 Release(); // Balance AddRef taken in AddCookieHeaderAndSend | |
| 326 } | 317 } |
| 327 | 318 |
| 328 void WebSocketJob::OnSentHandshakeRequest( | 319 void WebSocketJob::OnSentHandshakeRequest( |
| 329 SocketStream* socket, int amount_sent) { | 320 SocketStream* socket, int amount_sent) { |
| 330 DCHECK_EQ(state_, CONNECTING); | 321 DCHECK_EQ(state_, CONNECTING); |
| 331 handshake_request_sent_ += amount_sent; | 322 handshake_request_sent_ += amount_sent; |
| 332 DCHECK_LE(handshake_request_sent_, handshake_request_->raw_length()); | 323 DCHECK_LE(handshake_request_sent_, handshake_request_->raw_length()); |
| 333 if (handshake_request_sent_ >= handshake_request_->raw_length()) { | 324 if (handshake_request_sent_ >= handshake_request_->raw_length()) { |
| 334 // handshake request has been sent. | 325 // handshake request has been sent. |
| 335 // notify original size of handshake request to delegate. | 326 // notify original size of handshake request to delegate. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 delegate_->OnReceivedData( | 398 delegate_->OnReceivedData( |
| 408 socket_, received_data.data(), received_data.size()); | 399 socket_, received_data.data(), received_data.size()); |
| 409 | 400 |
| 410 handshake_response_.reset(); | 401 handshake_response_.reset(); |
| 411 | 402 |
| 412 WebSocketThrottle::GetInstance()->RemoveFromQueue(this); | 403 WebSocketThrottle::GetInstance()->RemoveFromQueue(this); |
| 413 WebSocketThrottle::GetInstance()->WakeupSocketIfNecessary(); | 404 WebSocketThrottle::GetInstance()->WakeupSocketIfNecessary(); |
| 414 return; | 405 return; |
| 415 } | 406 } |
| 416 | 407 |
| 417 AddRef(); // Balanced in OnCanSetCookieCompleted | |
| 418 | |
| 419 int policy = OK; | 408 int policy = OK; |
| 420 if (socket_->context()->cookie_policy()) { | 409 if (socket_->context()->cookie_policy()) { |
| 421 GURL url_for_cookies = GetURLForCookies(); | 410 GURL url_for_cookies = GetURLForCookies(); |
| 422 policy = socket_->context()->cookie_policy()->CanSetCookie( | 411 policy = socket_->context()->cookie_policy()->CanSetCookie( |
| 423 url_for_cookies, | 412 url_for_cookies, |
| 424 url_for_cookies, | 413 url_for_cookies, |
| 425 response_cookies_[response_cookies_save_index_], | 414 response_cookies_[response_cookies_save_index_]); |
| 426 &can_set_cookie_callback_); | |
| 427 if (policy == ERR_IO_PENDING) | |
| 428 return; // Wait for completion callback | |
| 429 } | 415 } |
| 430 | 416 |
| 417 DCHECK_NE(ERR_IO_PENDING, policy); |
| 431 OnCanSetCookieCompleted(policy); | 418 OnCanSetCookieCompleted(policy); |
| 432 } | 419 } |
| 433 | 420 |
| 434 void WebSocketJob::OnCanSetCookieCompleted(int policy) { | 421 void WebSocketJob::OnCanSetCookieCompleted(int policy) { |
| 435 if (socket_ && delegate_ && state_ == CONNECTING) { | 422 if (socket_ && delegate_ && state_ == CONNECTING) { |
| 436 if ((policy == OK || policy == OK_FOR_SESSION_ONLY) && | 423 if ((policy == OK || policy == OK_FOR_SESSION_ONLY) && |
| 437 socket_->context()->cookie_store()) { | 424 socket_->context()->cookie_store()) { |
| 438 CookieOptions options; | 425 CookieOptions options; |
| 439 options.set_include_httponly(); | 426 options.set_include_httponly(); |
| 440 if (policy == OK_FOR_SESSION_ONLY) | 427 if (policy == OK_FOR_SESSION_ONLY) |
| 441 options.set_force_session(); | 428 options.set_force_session(); |
| 442 GURL url_for_cookies = GetURLForCookies(); | 429 GURL url_for_cookies = GetURLForCookies(); |
| 443 socket_->context()->cookie_store()->SetCookieWithOptions( | 430 socket_->context()->cookie_store()->SetCookieWithOptions( |
| 444 url_for_cookies, response_cookies_[response_cookies_save_index_], | 431 url_for_cookies, response_cookies_[response_cookies_save_index_], |
| 445 options); | 432 options); |
| 446 } | 433 } |
| 447 response_cookies_save_index_++; | 434 response_cookies_save_index_++; |
| 448 SaveNextCookie(); | 435 SaveNextCookie(); |
| 449 } | 436 } |
| 450 Release(); // Balance AddRef taken in SaveNextCookie | |
| 451 } | 437 } |
| 452 | 438 |
| 453 GURL WebSocketJob::GetURLForCookies() const { | 439 GURL WebSocketJob::GetURLForCookies() const { |
| 454 GURL url = socket_->url(); | 440 GURL url = socket_->url(); |
| 455 std::string scheme = socket_->is_secure() ? "https" : "http"; | 441 std::string scheme = socket_->is_secure() ? "https" : "http"; |
| 456 url_canon::Replacements<char> replacements; | 442 url_canon::Replacements<char> replacements; |
| 457 replacements.SetScheme(scheme.c_str(), | 443 replacements.SetScheme(scheme.c_str(), |
| 458 url_parse::Component(0, scheme.length())); | 444 url_parse::Component(0, scheme.length())); |
| 459 return url.ReplaceComponents(replacements); | 445 return url.ReplaceComponents(replacements); |
| 460 } | 446 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 socket_->Close(); | 489 socket_->Close(); |
| 504 return; | 490 return; |
| 505 } | 491 } |
| 506 current_buffer_ = new DrainableIOBuffer( | 492 current_buffer_ = new DrainableIOBuffer( |
| 507 send_frame_handler_->GetCurrentBuffer(), | 493 send_frame_handler_->GetCurrentBuffer(), |
| 508 send_frame_handler_->GetCurrentBufferSize()); | 494 send_frame_handler_->GetCurrentBufferSize()); |
| 509 socket_->SendData(current_buffer_->data(), current_buffer_->BytesRemaining()); | 495 socket_->SendData(current_buffer_->data(), current_buffer_->BytesRemaining()); |
| 510 } | 496 } |
| 511 | 497 |
| 512 } // namespace net | 498 } // namespace net |
| OLD | NEW |