| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_basic_handshake_stream.h" | 5 #include "net/websockets/websocket_basic_handshake_stream.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 #include "net/websockets/websocket_handshake_request_info.h" | 45 #include "net/websockets/websocket_handshake_request_info.h" |
| 46 #include "net/websockets/websocket_handshake_response_info.h" | 46 #include "net/websockets/websocket_handshake_response_info.h" |
| 47 #include "net/websockets/websocket_stream.h" | 47 #include "net/websockets/websocket_stream.h" |
| 48 | 48 |
| 49 namespace net { | 49 namespace net { |
| 50 | 50 |
| 51 namespace { | 51 namespace { |
| 52 | 52 |
| 53 const char kConnectionErrorStatusLine[] = "HTTP/1.1 503 Connection Error"; | 53 const char kConnectionErrorStatusLine[] = "HTTP/1.1 503 Connection Error"; |
| 54 | 54 |
| 55 // TODO(yhirano): Remove these functions once http://crbug.com/399535 is fixed. | |
| 56 NOINLINE void RunCallbackWithOk(const CompletionCallback& callback, | |
| 57 int result) { | |
| 58 DCHECK_EQ(result, OK); | |
| 59 callback.Run(OK); | |
| 60 } | |
| 61 | |
| 62 NOINLINE void RunCallbackWithInvalidResponseCausedByRedirect( | |
| 63 const CompletionCallback& callback, | |
| 64 int result) { | |
| 65 DCHECK_EQ(result, ERR_INVALID_RESPONSE); | |
| 66 callback.Run(ERR_INVALID_RESPONSE); | |
| 67 } | |
| 68 | |
| 69 NOINLINE void RunCallbackWithInvalidResponse( | |
| 70 const CompletionCallback& callback, | |
| 71 int result) { | |
| 72 DCHECK_EQ(result, ERR_INVALID_RESPONSE); | |
| 73 callback.Run(ERR_INVALID_RESPONSE); | |
| 74 } | |
| 75 | |
| 76 NOINLINE void RunCallback(const CompletionCallback& callback, int result) { | |
| 77 callback.Run(result); | |
| 78 } | |
| 79 | |
| 80 } // namespace | 55 } // namespace |
| 81 | 56 |
| 82 // TODO(ricea): If more extensions are added, replace this with a more general | 57 // TODO(ricea): If more extensions are added, replace this with a more general |
| 83 // mechanism. | 58 // mechanism. |
| 84 struct WebSocketExtensionParams { | 59 struct WebSocketExtensionParams { |
| 85 WebSocketExtensionParams() | 60 WebSocketExtensionParams() |
| 86 : deflate_enabled(false), | 61 : deflate_enabled(false), |
| 87 client_window_bits(15), | 62 client_window_bits(15), |
| 88 deflate_mode(WebSocketDeflater::TAKE_OVER_CONTEXT) {} | 63 deflate_mode(WebSocketDeflater::TAKE_OVER_CONTEXT) {} |
| 89 | 64 |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 // HttpStreamParser uses a weak pointer when reading from the | 431 // HttpStreamParser uses a weak pointer when reading from the |
| 457 // socket, so it won't be called back after being destroyed. The | 432 // socket, so it won't be called back after being destroyed. The |
| 458 // HttpStreamParser is owned by HttpBasicState which is owned by this object, | 433 // HttpStreamParser is owned by HttpBasicState which is owned by this object, |
| 459 // so this use of base::Unretained() is safe. | 434 // so this use of base::Unretained() is safe. |
| 460 int rv = parser()->ReadResponseHeaders( | 435 int rv = parser()->ReadResponseHeaders( |
| 461 base::Bind(&WebSocketBasicHandshakeStream::ReadResponseHeadersCallback, | 436 base::Bind(&WebSocketBasicHandshakeStream::ReadResponseHeadersCallback, |
| 462 base::Unretained(this), | 437 base::Unretained(this), |
| 463 callback)); | 438 callback)); |
| 464 if (rv == ERR_IO_PENDING) | 439 if (rv == ERR_IO_PENDING) |
| 465 return rv; | 440 return rv; |
| 466 bool is_redirect = false; | 441 return ValidateResponse(rv); |
| 467 return ValidateResponse(rv, &is_redirect); | |
| 468 } | 442 } |
| 469 | 443 |
| 470 int WebSocketBasicHandshakeStream::ReadResponseBody( | 444 int WebSocketBasicHandshakeStream::ReadResponseBody( |
| 471 IOBuffer* buf, | 445 IOBuffer* buf, |
| 472 int buf_len, | 446 int buf_len, |
| 473 const CompletionCallback& callback) { | 447 const CompletionCallback& callback) { |
| 474 return parser()->ReadResponseBody(buf, buf_len, callback); | 448 return parser()->ReadResponseBody(buf, buf_len, callback); |
| 475 } | 449 } |
| 476 | 450 |
| 477 void WebSocketBasicHandshakeStream::Close(bool not_reusable) { | 451 void WebSocketBasicHandshakeStream::Close(bool not_reusable) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 } | 545 } |
| 572 | 546 |
| 573 void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting( | 547 void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting( |
| 574 const std::string& key) { | 548 const std::string& key) { |
| 575 handshake_challenge_for_testing_.reset(new std::string(key)); | 549 handshake_challenge_for_testing_.reset(new std::string(key)); |
| 576 } | 550 } |
| 577 | 551 |
| 578 void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( | 552 void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( |
| 579 const CompletionCallback& callback, | 553 const CompletionCallback& callback, |
| 580 int result) { | 554 int result) { |
| 581 bool is_redirect = false; | 555 callback.Run(ValidateResponse(result)); |
| 582 int rv = ValidateResponse(result, &is_redirect); | |
| 583 | |
| 584 // TODO(yhirano): Simplify this statement once http://crbug.com/399535 is | |
| 585 // fixed. | |
| 586 switch (rv) { | |
| 587 case OK: | |
| 588 RunCallbackWithOk(callback, rv); | |
| 589 break; | |
| 590 case ERR_INVALID_RESPONSE: | |
| 591 if (is_redirect) | |
| 592 RunCallbackWithInvalidResponseCausedByRedirect(callback, rv); | |
| 593 else | |
| 594 RunCallbackWithInvalidResponse(callback, rv); | |
| 595 break; | |
| 596 default: | |
| 597 RunCallback(callback, rv); | |
| 598 break; | |
| 599 } | |
| 600 } | 556 } |
| 601 | 557 |
| 602 void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() { | 558 void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() { |
| 603 DCHECK(http_response_info_); | 559 DCHECK(http_response_info_); |
| 604 WebSocketDispatchOnFinishOpeningHandshake(connect_delegate_, | 560 WebSocketDispatchOnFinishOpeningHandshake(connect_delegate_, |
| 605 url_, | 561 url_, |
| 606 http_response_info_->headers, | 562 http_response_info_->headers, |
| 607 http_response_info_->response_time); | 563 http_response_info_->response_time); |
| 608 } | 564 } |
| 609 | 565 |
| 610 int WebSocketBasicHandshakeStream::ValidateResponse(int rv, | 566 int WebSocketBasicHandshakeStream::ValidateResponse(int rv) { |
| 611 bool* is_redirect) { | |
| 612 DCHECK(http_response_info_); | 567 DCHECK(http_response_info_); |
| 613 *is_redirect = false; | |
| 614 // Most net errors happen during connection, so they are not seen by this | 568 // Most net errors happen during connection, so they are not seen by this |
| 615 // method. The histogram for error codes is created in | 569 // method. The histogram for error codes is created in |
| 616 // Delegate::OnResponseStarted in websocket_stream.cc instead. | 570 // Delegate::OnResponseStarted in websocket_stream.cc instead. |
| 617 if (rv >= 0) { | 571 if (rv >= 0) { |
| 618 const HttpResponseHeaders* headers = http_response_info_->headers.get(); | 572 const HttpResponseHeaders* headers = http_response_info_->headers.get(); |
| 619 const int response_code = headers->response_code(); | 573 const int response_code = headers->response_code(); |
| 620 *is_redirect = HttpResponseHeaders::IsRedirectResponseCode(response_code); | |
| 621 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.WebSocket.ResponseCode", response_code); | 574 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.WebSocket.ResponseCode", response_code); |
| 622 switch (response_code) { | 575 switch (response_code) { |
| 623 case HTTP_SWITCHING_PROTOCOLS: | 576 case HTTP_SWITCHING_PROTOCOLS: |
| 624 OnFinishOpeningHandshake(); | 577 OnFinishOpeningHandshake(); |
| 625 return ValidateUpgradeResponse(headers); | 578 return ValidateUpgradeResponse(headers); |
| 626 | 579 |
| 627 // We need to pass these through for authentication to work. | 580 // We need to pass these through for authentication to work. |
| 628 case HTTP_UNAUTHORIZED: | 581 case HTTP_UNAUTHORIZED: |
| 629 case HTTP_PROXY_AUTHENTICATION_REQUIRED: | 582 case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
| 630 return OK; | 583 return OK; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 set_failure_message("Error during WebSocket handshake: " + failure_message); | 645 set_failure_message("Error during WebSocket handshake: " + failure_message); |
| 693 return ERR_INVALID_RESPONSE; | 646 return ERR_INVALID_RESPONSE; |
| 694 } | 647 } |
| 695 | 648 |
| 696 void WebSocketBasicHandshakeStream::set_failure_message( | 649 void WebSocketBasicHandshakeStream::set_failure_message( |
| 697 const std::string& failure_message) { | 650 const std::string& failure_message) { |
| 698 *failure_message_ = failure_message; | 651 *failure_message_ = failure_message; |
| 699 } | 652 } |
| 700 | 653 |
| 701 } // namespace net | 654 } // namespace net |
| OLD | NEW |