1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/websockets/websocket_job.h"
10 #include "base/lazy_instance.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/net_log.h"
14 #include "net/cookies/cookie_store.h"
15 #include "net/http/http_network_session.h"
16 #include "net/http/http_transaction_factory.h"
17 #include "net/http/http_util.h"
18 #include "net/spdy/spdy_session.h"
19 #include "net/spdy/spdy_session_pool.h"
20 #include "net/url_request/url_request_context.h"
21 #include "net/websockets/websocket_handshake_handler.h"
22 #include "net/websockets/websocket_net_log_params.h"
23 #include "net/websockets/websocket_throttle.h"
26 static const int kMaxPendingSendAllowed
= 32768; // 32 kilobytes.
30 // lower-case header names.
31 const char* const kCookieHeaders
[] = {
34 const char* const kSetCookieHeaders
[] = {
35 "set-cookie", "set-cookie2"
38 net::SocketStreamJob
* WebSocketJobFactory(
39 const GURL
& url
, net::SocketStream::Delegate
* delegate
,
40 net::URLRequestContext
* context
, net::CookieStore
* cookie_store
) {
41 net::WebSocketJob
* job
= new net::WebSocketJob(delegate
);
42 job
->InitSocketStream(new net::SocketStream(url
, job
, context
, cookie_store
));
46 class WebSocketJobInitSingleton
{
48 friend struct base::DefaultLazyInstanceTraits
<WebSocketJobInitSingleton
>;
49 WebSocketJobInitSingleton() {
50 net::SocketStreamJob::RegisterProtocolFactory("ws", WebSocketJobFactory
);
51 net::SocketStreamJob::RegisterProtocolFactory("wss", WebSocketJobFactory
);
55 static base::LazyInstance
<WebSocketJobInitSingleton
> g_websocket_job_init
=
56 LAZY_INSTANCE_INITIALIZER
;
58 } // anonymous namespace
63 void WebSocketJob::EnsureInit() {
64 g_websocket_job_init
.Get();
67 WebSocketJob::WebSocketJob(SocketStream::Delegate
* delegate
)
68 : delegate_(delegate
),
71 handshake_request_(new WebSocketHandshakeRequestHandler
),
72 handshake_response_(new WebSocketHandshakeResponseHandler
),
73 started_to_send_handshake_request_(false),
74 handshake_request_sent_(0),
75 response_cookies_save_index_(0),
76 spdy_protocol_version_(0),
77 save_next_cookie_running_(false),
78 callback_pending_(false),
79 weak_ptr_factory_(this),
80 weak_ptr_factory_for_send_pending_(this) {
83 WebSocketJob::~WebSocketJob() {
84 DCHECK_EQ(CLOSED
, state_
);
86 DCHECK(!socket_
.get());
89 void WebSocketJob::Connect() {
90 DCHECK(socket_
.get());
91 DCHECK_EQ(state_
, INITIALIZED
);
96 bool WebSocketJob::SendData(const char* data
, int len
) {
102 return SendHandshakeRequest(data
, len
);
106 scoped_refptr
<IOBufferWithSize
> buffer
= new IOBufferWithSize(len
);
107 memcpy(buffer
->data(), data
, len
);
108 if (current_send_buffer_
.get() || !send_buffer_queue_
.empty()) {
109 send_buffer_queue_
.push_back(buffer
);
112 current_send_buffer_
= new DrainableIOBuffer(buffer
.get(), len
);
113 return SendDataInternal(current_send_buffer_
->data(),
114 current_send_buffer_
->BytesRemaining());
124 void WebSocketJob::Close() {
125 if (state_
== CLOSED
)
129 if (current_send_buffer_
.get()) {
130 // Will close in SendPending.
137 void WebSocketJob::RestartWithAuth(const AuthCredentials
& credentials
) {
139 socket_
->RestartWithAuth(credentials
);
142 void WebSocketJob::DetachDelegate() {
144 WebSocketThrottle::GetInstance()->RemoveFromQueue(this);
146 scoped_refptr
<WebSocketJob
> protect(this);
147 weak_ptr_factory_
.InvalidateWeakPtrs();
148 weak_ptr_factory_for_send_pending_
.InvalidateWeakPtrs();
152 socket_
->DetachDelegate();
154 if (!callback_
.is_null()) {
157 Release(); // Balanced with OnStartOpenConnection().
161 int WebSocketJob::OnStartOpenConnection(
162 SocketStream
* socket
, const CompletionCallback
& callback
) {
163 DCHECK(callback_
.is_null());
166 addresses_
= socket
->address_list();
167 if (!WebSocketThrottle::GetInstance()->PutInQueue(this)) {
168 return ERR_WS_THROTTLE_QUEUE_TOO_LARGE
;
172 int result
= delegate_
->OnStartOpenConnection(socket
, callback
);
173 DCHECK_EQ(OK
, result
);
176 // PutInQueue() may set |waiting_| true for throttling. In this case,
177 // Wakeup() will be called later.
178 callback_
= callback
;
179 AddRef(); // Balanced when callback_ is cleared.
180 return ERR_IO_PENDING
;
182 return TrySpdyStream();
185 void WebSocketJob::OnConnected(
186 SocketStream
* socket
, int max_pending_send_allowed
) {
187 if (state_
== CLOSED
)
189 DCHECK_EQ(CONNECTING
, state_
);
191 delegate_
->OnConnected(socket
, max_pending_send_allowed
);
194 void WebSocketJob::OnSentData(SocketStream
* socket
, int amount_sent
) {
195 DCHECK_NE(INITIALIZED
, state_
);
196 DCHECK_GT(amount_sent
, 0);
197 if (state_
== CLOSED
)
199 if (state_
== CONNECTING
) {
200 OnSentHandshakeRequest(socket
, amount_sent
);
204 DCHECK(state_
== OPEN
|| state_
== CLOSING
);
205 if (!current_send_buffer_
.get()) {
207 << "OnSentData current_send_buffer=NULL amount_sent=" << amount_sent
;
210 current_send_buffer_
->DidConsume(amount_sent
);
211 if (current_send_buffer_
->BytesRemaining() > 0)
214 // We need to report amount_sent of original buffer size, instead of
215 // amount sent to |socket|.
216 amount_sent
= current_send_buffer_
->size();
217 DCHECK_GT(amount_sent
, 0);
218 current_send_buffer_
= NULL
;
219 if (!weak_ptr_factory_for_send_pending_
.HasWeakPtrs()) {
220 base::MessageLoopForIO::current()->PostTask(
222 base::Bind(&WebSocketJob::SendPending
,
223 weak_ptr_factory_for_send_pending_
.GetWeakPtr()));
225 delegate_
->OnSentData(socket
, amount_sent
);
229 void WebSocketJob::OnReceivedData(
230 SocketStream
* socket
, const char* data
, int len
) {
231 DCHECK_NE(INITIALIZED
, state_
);
232 if (state_
== CLOSED
)
234 if (state_
== CONNECTING
) {
235 OnReceivedHandshakeResponse(socket
, data
, len
);
238 DCHECK(state_
== OPEN
|| state_
== CLOSING
);
239 if (delegate_
&& len
> 0)
240 delegate_
->OnReceivedData(socket
, data
, len
);
243 void WebSocketJob::OnClose(SocketStream
* socket
) {
245 WebSocketThrottle::GetInstance()->RemoveFromQueue(this);
247 scoped_refptr
<WebSocketJob
> protect(this);
248 weak_ptr_factory_
.InvalidateWeakPtrs();
250 SocketStream::Delegate
* delegate
= delegate_
;
253 if (!callback_
.is_null()) {
256 Release(); // Balanced with OnStartOpenConnection().
259 delegate
->OnClose(socket
);
262 void WebSocketJob::OnAuthRequired(
263 SocketStream
* socket
, AuthChallengeInfo
* auth_info
) {
265 delegate_
->OnAuthRequired(socket
, auth_info
);
268 void WebSocketJob::OnSSLCertificateError(
269 SocketStream
* socket
, const SSLInfo
& ssl_info
, bool fatal
) {
271 delegate_
->OnSSLCertificateError(socket
, ssl_info
, fatal
);
274 void WebSocketJob::OnError(const SocketStream
* socket
, int error
) {
275 if (delegate_
&& error
!= ERR_PROTOCOL_SWITCHED
)
276 delegate_
->OnError(socket
, error
);
279 void WebSocketJob::OnCreatedSpdyStream(int result
) {
280 DCHECK(spdy_websocket_stream_
.get());
281 DCHECK(socket_
.get());
282 DCHECK_NE(ERR_IO_PENDING
, result
);
284 if (state_
== CLOSED
) {
285 result
= ERR_ABORTED
;
286 } else if (result
== OK
) {
288 result
= ERR_PROTOCOL_SWITCHED
;
290 spdy_websocket_stream_
.reset();
296 void WebSocketJob::OnSentSpdyHeaders() {
297 DCHECK_NE(INITIALIZED
, state_
);
298 if (state_
!= CONNECTING
)
300 size_t original_length
= handshake_request_
->original_length();
301 handshake_request_
.reset();
303 delegate_
->OnSentData(socket_
.get(), original_length
);
306 void WebSocketJob::OnSpdyResponseHeadersUpdated(
307 const SpdyHeaderBlock
& response_headers
) {
308 DCHECK_NE(INITIALIZED
, state_
);
309 if (state_
!= CONNECTING
)
311 // TODO(toyoshim): Fallback to non-spdy connection?
312 handshake_response_
->ParseResponseHeaderBlock(response_headers
,
314 spdy_protocol_version_
);
316 SaveCookiesAndNotifyHeadersComplete();
319 void WebSocketJob::OnSentSpdyData(size_t bytes_sent
) {
320 DCHECK_NE(INITIALIZED
, state_
);
321 DCHECK_NE(CONNECTING
, state_
);
322 if (state_
== CLOSED
)
324 if (!spdy_websocket_stream_
.get())
326 OnSentData(socket_
.get(), static_cast<int>(bytes_sent
));
329 void WebSocketJob::OnReceivedSpdyData(scoped_ptr
<SpdyBuffer
> buffer
) {
330 DCHECK_NE(INITIALIZED
, state_
);
331 DCHECK_NE(CONNECTING
, state_
);
332 if (state_
== CLOSED
)
334 if (!spdy_websocket_stream_
.get())
338 socket_
.get(), buffer
->GetRemainingData(), buffer
->GetRemainingSize());
340 OnReceivedData(socket_
.get(), NULL
, 0);
344 void WebSocketJob::OnCloseSpdyStream() {
345 spdy_websocket_stream_
.reset();
346 OnClose(socket_
.get());
349 bool WebSocketJob::SendHandshakeRequest(const char* data
, int len
) {
350 DCHECK_EQ(state_
, CONNECTING
);
351 if (started_to_send_handshake_request_
)
353 if (!handshake_request_
->ParseRequest(data
, len
))
356 AddCookieHeaderAndSend();
360 void WebSocketJob::AddCookieHeaderAndSend() {
362 if (delegate_
&& !delegate_
->CanGetCookies(socket_
.get(), GetURLForCookies()))
365 if (socket_
.get() && delegate_
&& state_
== CONNECTING
) {
366 handshake_request_
->RemoveHeaders(kCookieHeaders
,
367 arraysize(kCookieHeaders
));
368 if (allow
&& socket_
->cookie_store()) {
369 // Add cookies, including HttpOnly cookies.
370 CookieOptions cookie_options
;
371 cookie_options
.set_include_httponly();
372 socket_
->cookie_store()->GetCookiesWithOptionsAsync(
373 GetURLForCookies(), cookie_options
,
374 base::Bind(&WebSocketJob::LoadCookieCallback
,
375 weak_ptr_factory_
.GetWeakPtr()));
382 void WebSocketJob::LoadCookieCallback(const std::string
& cookie
) {
384 // TODO(tyoshino): Sending cookie means that connection doesn't need
385 // PRIVACY_MODE_ENABLED as cookies may be server-bound and channel id
386 // wouldn't negatively affect privacy anyway. Need to restart connection
387 // or refactor to determine cookie status prior to connecting.
388 handshake_request_
->AppendHeaderIfMissing("Cookie", cookie
);
392 void WebSocketJob::DoSendData() {
393 if (spdy_websocket_stream_
.get()) {
394 scoped_ptr
<SpdyHeaderBlock
> headers(new SpdyHeaderBlock
);
395 handshake_request_
->GetRequestHeaderBlock(
396 socket_
->url(), headers
.get(), &challenge_
, spdy_protocol_version_
);
397 spdy_websocket_stream_
->SendRequest(headers
.Pass());
399 const std::string
& handshake_request
=
400 handshake_request_
->GetRawRequest();
401 handshake_request_sent_
= 0;
402 socket_
->net_log()->AddEvent(
403 NetLog::TYPE_WEB_SOCKET_SEND_REQUEST_HEADERS
,
404 base::Bind(&NetLogWebSocketHandshakeCallback
, &handshake_request
));
405 socket_
->SendData(handshake_request
.data(),
406 handshake_request
.size());
408 // Just buffered in |handshake_request_|.
409 started_to_send_handshake_request_
= true;
412 void WebSocketJob::OnSentHandshakeRequest(
413 SocketStream
* socket
, int amount_sent
) {
414 DCHECK_EQ(state_
, CONNECTING
);
415 handshake_request_sent_
+= amount_sent
;
416 DCHECK_LE(handshake_request_sent_
, handshake_request_
->raw_length());
417 if (handshake_request_sent_
>= handshake_request_
->raw_length()) {
418 // handshake request has been sent.
419 // notify original size of handshake request to delegate.
420 // Reset the handshake_request_ first in case this object is deleted by the
422 size_t original_length
= handshake_request_
->original_length();
423 handshake_request_
.reset();
425 delegate_
->OnSentData(socket
, original_length
);
429 void WebSocketJob::OnReceivedHandshakeResponse(
430 SocketStream
* socket
, const char* data
, int len
) {
431 DCHECK_EQ(state_
, CONNECTING
);
432 if (handshake_response_
->HasResponse()) {
433 // If we already has handshake response, received data should be frame
434 // data, not handshake message.
435 received_data_after_handshake_
.insert(
436 received_data_after_handshake_
.end(), data
, data
+ len
);
440 size_t response_length
= handshake_response_
->ParseRawResponse(data
, len
);
441 if (!handshake_response_
->HasResponse()) {
442 // not yet. we need more data.
445 // handshake message is completed.
446 std::string raw_response
= handshake_response_
->GetRawResponse();
447 socket_
->net_log()->AddEvent(
448 NetLog::TYPE_WEB_SOCKET_READ_RESPONSE_HEADERS
,
449 base::Bind(&NetLogWebSocketHandshakeCallback
, &raw_response
));
450 if (len
- response_length
> 0) {
451 // If we received extra data, it should be frame data.
452 DCHECK(received_data_after_handshake_
.empty());
453 received_data_after_handshake_
.assign(data
+ response_length
, data
+ len
);
455 SaveCookiesAndNotifyHeadersComplete();
458 void WebSocketJob::SaveCookiesAndNotifyHeadersComplete() {
459 // handshake message is completed.
460 DCHECK(handshake_response_
->HasResponse());
462 // Extract cookies from the handshake response into a temporary vector.
463 response_cookies_
.clear();
464 response_cookies_save_index_
= 0;
466 handshake_response_
->GetHeaders(
467 kSetCookieHeaders
, arraysize(kSetCookieHeaders
), &response_cookies_
);
469 // Now, loop over the response cookies, and attempt to persist each.
473 void WebSocketJob::NotifyHeadersComplete() {
474 // Remove cookie headers, with malformed headers preserved.
475 // Actual handshake should be done in Blink.
476 handshake_response_
->RemoveHeaders(
477 kSetCookieHeaders
, arraysize(kSetCookieHeaders
));
478 std::string handshake_response
= handshake_response_
->GetResponse();
479 handshake_response_
.reset();
480 std::vector
<char> received_data(handshake_response
.begin(),
481 handshake_response
.end());
482 received_data
.insert(received_data
.end(),
483 received_data_after_handshake_
.begin(),
484 received_data_after_handshake_
.end());
485 received_data_after_handshake_
.clear();
489 DCHECK(!received_data
.empty());
491 delegate_
->OnReceivedData(
492 socket_
.get(), &received_data
.front(), received_data
.size());
494 WebSocketThrottle::GetInstance()->RemoveFromQueue(this);
497 void WebSocketJob::SaveNextCookie() {
498 if (!socket_
.get() || !delegate_
|| state_
!= CONNECTING
)
501 callback_pending_
= false;
502 save_next_cookie_running_
= true;
504 if (socket_
->cookie_store()) {
505 GURL url_for_cookies
= GetURLForCookies();
507 CookieOptions options
;
508 options
.set_include_httponly();
510 // Loop as long as SetCookieWithOptionsAsync completes synchronously. Since
511 // CookieMonster's asynchronous operation APIs queue the callback to run it
512 // on the thread where the API was called, there won't be race. I.e. unless
513 // the callback is run synchronously, it won't be run in parallel with this
515 while (!callback_pending_
&&
516 response_cookies_save_index_
< response_cookies_
.size()) {
517 std::string cookie
= response_cookies_
[response_cookies_save_index_
];
518 response_cookies_save_index_
++;
520 if (!delegate_
->CanSetCookie(
521 socket_
.get(), url_for_cookies
, cookie
, &options
))
524 callback_pending_
= true;
525 socket_
->cookie_store()->SetCookieWithOptionsAsync(
526 url_for_cookies
, cookie
, options
,
527 base::Bind(&WebSocketJob::OnCookieSaved
,
528 weak_ptr_factory_
.GetWeakPtr()));
532 save_next_cookie_running_
= false;
534 if (callback_pending_
)
537 response_cookies_
.clear();
538 response_cookies_save_index_
= 0;
540 NotifyHeadersComplete();
543 void WebSocketJob::OnCookieSaved(bool cookie_status
) {
544 // Tell the caller of SetCookieWithOptionsAsync() that this completion
545 // callback is invoked.
546 // - If the caller checks callback_pending earlier than this callback, the
547 // caller exits to let this method continue iteration.
548 // - Otherwise, the caller continues iteration.
549 callback_pending_
= false;
551 // Resume SaveNextCookie if the caller of SetCookieWithOptionsAsync() exited
552 // the loop. Otherwise, return.
553 if (save_next_cookie_running_
)
559 GURL
WebSocketJob::GetURLForCookies() const {
560 GURL url
= socket_
->url();
561 std::string scheme
= socket_
->is_secure() ? "https" : "http";
562 url::Replacements
<char> replacements
;
563 replacements
.SetScheme(scheme
.c_str(), url::Component(0, scheme
.length()));
564 return url
.ReplaceComponents(replacements
);
567 const AddressList
& WebSocketJob::address_list() const {
571 int WebSocketJob::TrySpdyStream() {
575 // Check if we have a SPDY session available.
576 HttpTransactionFactory
* factory
=
577 socket_
->context()->http_transaction_factory();
580 scoped_refptr
<HttpNetworkSession
> session
= factory
->GetSession();
581 if (!session
.get() || !session
->params().enable_websocket_over_spdy
)
583 SpdySessionPool
* spdy_pool
= session
->spdy_session_pool();
584 PrivacyMode privacy_mode
= socket_
->privacy_mode();
585 const SpdySessionKey
key(HostPortPair::FromURL(socket_
->url()),
586 socket_
->proxy_server(), privacy_mode
);
587 // Forbid wss downgrade to SPDY without SSL.
588 // TODO(toyoshim): Does it realize the same policy with HTTP?
589 base::WeakPtr
<SpdySession
> spdy_session
=
590 spdy_pool
->FindAvailableSession(key
, *socket_
->net_log());
595 bool was_npn_negotiated
;
596 NextProto protocol_negotiated
= kProtoUnknown
;
597 bool use_ssl
= spdy_session
->GetSSLInfo(
598 &ssl_info
, &was_npn_negotiated
, &protocol_negotiated
);
599 if (socket_
->is_secure() && !use_ssl
)
602 // Create SpdyWebSocketStream.
603 spdy_protocol_version_
= spdy_session
->GetProtocolVersion();
604 spdy_websocket_stream_
.reset(new SpdyWebSocketStream(spdy_session
, this));
606 int result
= spdy_websocket_stream_
->InitializeStream(
607 socket_
->url(), MEDIUM
, *socket_
->net_log());
609 OnConnected(socket_
.get(), kMaxPendingSendAllowed
);
610 return ERR_PROTOCOL_SWITCHED
;
612 if (result
!= ERR_IO_PENDING
) {
613 spdy_websocket_stream_
.reset();
617 return ERR_IO_PENDING
;
620 void WebSocketJob::SetWaiting() {
624 bool WebSocketJob::IsWaiting() const {
628 void WebSocketJob::Wakeup() {
632 DCHECK(!callback_
.is_null());
633 base::MessageLoopForIO::current()->PostTask(
635 base::Bind(&WebSocketJob::RetryPendingIO
,
636 weak_ptr_factory_
.GetWeakPtr()));
639 void WebSocketJob::RetryPendingIO() {
640 int result
= TrySpdyStream();
642 // In the case of ERR_IO_PENDING, CompleteIO() will be called from
643 // OnCreatedSpdyStream().
644 if (result
!= ERR_IO_PENDING
)
648 void WebSocketJob::CompleteIO(int result
) {
649 // |callback_| may be null if OnClose() or DetachDelegate() was called.
650 if (!callback_
.is_null()) {
651 CompletionCallback callback
= callback_
;
653 callback
.Run(result
);
654 Release(); // Balanced with OnStartOpenConnection().
658 bool WebSocketJob::SendDataInternal(const char* data
, int length
) {
659 if (spdy_websocket_stream_
.get())
660 return ERR_IO_PENDING
== spdy_websocket_stream_
->SendData(data
, length
);
662 return socket_
->SendData(data
, length
);
666 void WebSocketJob::CloseInternal() {
667 if (spdy_websocket_stream_
.get())
668 spdy_websocket_stream_
->Close();
673 void WebSocketJob::SendPending() {
674 if (current_send_buffer_
.get())
677 // Current buffer has been sent. Try next if any.
678 if (send_buffer_queue_
.empty()) {
679 // No more data to send.
680 if (state_
== CLOSING
)
685 scoped_refptr
<IOBufferWithSize
> next_buffer
= send_buffer_queue_
.front();
686 send_buffer_queue_
.pop_front();
687 current_send_buffer_
=
688 new DrainableIOBuffer(next_buffer
.get(), next_buffer
->size());
689 SendDataInternal(current_send_buffer_
->data(),
690 current_send_buffer_
->BytesRemaining());