diff options
author | yhirano@chromium.org <yhirano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-05 12:28:50 +0000 |
---|---|---|
committer | yhirano@chromium.org <yhirano@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-05 12:28:50 +0000 |
commit | e5760f5299273e26119852db031e2f9882bc81e3 (patch) | |
tree | 6bde72d42f0fae6686b725aefee1fc6f9c73058c /net | |
parent | dcd32998a65d03bccf83097cf8e44881e0f9ca56 (diff) | |
download | chromium_src-e5760f5299273e26119852db031e2f9882bc81e3.zip chromium_src-e5760f5299273e26119852db031e2f9882bc81e3.tar.gz chromium_src-e5760f5299273e26119852db031e2f9882bc81e3.tar.bz2 |
[WebSocket] Add Cache-Control to the request header.
In order to suppress cache on servers, added LOAD_BYPASS_CACHE to
the load flags.
Additionally, added some messages on opening handshake failure.
BUG=339373
Review URL: https://codereview.chromium.org/152483003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@248971 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/websockets/websocket_basic_handshake_stream.cc | 77 | ||||
-rw-r--r-- | net/websockets/websocket_basic_handshake_stream.h | 8 | ||||
-rw-r--r-- | net/websockets/websocket_handshake_stream_create_helper_test.cc | 2 | ||||
-rw-r--r-- | net/websockets/websocket_stream.cc | 1 | ||||
-rw-r--r-- | net/websockets/websocket_stream_test.cc | 30 | ||||
-rw-r--r-- | net/websockets/websocket_test_util.cc | 2 |
6 files changed, 64 insertions, 56 deletions
diff --git a/net/websockets/websocket_basic_handshake_stream.cc b/net/websockets/websocket_basic_handshake_stream.cc index 808383b..e3008b8 100644 --- a/net/websockets/websocket_basic_handshake_stream.cc +++ b/net/websockets/websocket_basic_handshake_stream.cc @@ -234,7 +234,7 @@ bool ValidatePerMessageDeflateExtension(const WebSocketExtension& extension, the_strings_server_and_client_must_be_the_same_length); typedef std::vector<WebSocketExtension::Parameter> ParameterVector; - DCHECK(extension.name() == "permessage-deflate"); + DCHECK_EQ("permessage-deflate", extension.name()); const ParameterVector& parameters = extension.parameters(); std::set<std::string> seen_names; for (ParameterVector::const_iterator it = parameters.begin(); @@ -297,7 +297,7 @@ bool ValidateExtensions(const HttpResponseHeaders* headers, // code. bool seen_permessage_deflate = false; while (headers->EnumerateHeader( - &state, websockets::kSecWebSocketExtensions, &value)) { + &state, websockets::kSecWebSocketExtensions, &value)) { WebSocketExtensionParser parser; parser.Parse(value); if (parser.has_error()) { @@ -315,7 +315,7 @@ bool ValidateExtensions(const HttpResponseHeaders* headers, } seen_permessage_deflate = true; if (!ValidatePerMessageDeflateExtension( - parser.extension(), failure_message, params)) + parser.extension(), failure_message, params)) return false; } else { *failure_message = @@ -416,10 +416,7 @@ int WebSocketBasicHandshakeStream::ReadResponseHeaders( callback)); if (rv == ERR_IO_PENDING) return rv; - if (rv == OK) - return ValidateResponse(); - OnFinishOpeningHandshake(); - return rv; + return ValidateResponse(rv); } const HttpResponseInfo* WebSocketBasicHandshakeStream::GetResponseInfo() const { @@ -526,11 +523,7 @@ std::string WebSocketBasicHandshakeStream::GetFailureMessage() const { void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( const CompletionCallback& callback, int result) { - if (result == OK) - result = ValidateResponse(); - else - OnFinishOpeningHandshake(); - callback.Run(result); + callback.Run(ValidateResponse(result)); } void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() { @@ -546,44 +539,50 @@ void WebSocketBasicHandshakeStream::OnFinishOpeningHandshake() { connect_delegate_->OnFinishOpeningHandshake(response.Pass()); } -int WebSocketBasicHandshakeStream::ValidateResponse() { +int WebSocketBasicHandshakeStream::ValidateResponse(int rv) { DCHECK(http_response_info_); - const scoped_refptr<HttpResponseHeaders>& headers = - http_response_info_->headers; - - switch (headers->response_code()) { - case HTTP_SWITCHING_PROTOCOLS: - OnFinishOpeningHandshake(); - return ValidateUpgradeResponse(headers); - - // We need to pass these through for authentication to work. - case HTTP_UNAUTHORIZED: - case HTTP_PROXY_AUTHENTICATION_REQUIRED: - return OK; - - // Other status codes are potentially risky (see the warnings in the - // WHATWG WebSocket API spec) and so are dropped by default. - default: - failure_message_ = base::StringPrintf("Unexpected status code: %d", - headers->response_code()); - OnFinishOpeningHandshake(); - return ERR_INVALID_RESPONSE; + const HttpResponseHeaders* headers = http_response_info_->headers.get(); + if (rv >= 0) { + switch (headers->response_code()) { + case HTTP_SWITCHING_PROTOCOLS: + OnFinishOpeningHandshake(); + return ValidateUpgradeResponse(headers); + + // We need to pass these through for authentication to work. + case HTTP_UNAUTHORIZED: + case HTTP_PROXY_AUTHENTICATION_REQUIRED: + return OK; + + // Other status codes are potentially risky (see the warnings in the + // WHATWG WebSocket API spec) and so are dropped by default. + default: + failure_message_ = base::StringPrintf( + "Error during WebSocket handshake: Unexpected response code: %d", + headers->response_code()); + OnFinishOpeningHandshake(); + return ERR_INVALID_RESPONSE; + } + } else { + failure_message_ = + std::string("Error during WebSocket handshake: ") + ErrorToString(rv); + OnFinishOpeningHandshake(); + return rv; } } int WebSocketBasicHandshakeStream::ValidateUpgradeResponse( - const scoped_refptr<HttpResponseHeaders>& headers) { + const HttpResponseHeaders* headers) { extension_params_.reset(new WebSocketExtensionParams); - if (ValidateUpgrade(headers.get(), &failure_message_) && - ValidateSecWebSocketAccept(headers.get(), + if (ValidateUpgrade(headers, &failure_message_) && + ValidateSecWebSocketAccept(headers, handshake_challenge_response_, &failure_message_) && - ValidateConnection(headers.get(), &failure_message_) && - ValidateSubProtocol(headers.get(), + ValidateConnection(headers, &failure_message_) && + ValidateSubProtocol(headers, requested_sub_protocols_, &sub_protocol_, &failure_message_) && - ValidateExtensions(headers.get(), + ValidateExtensions(headers, requested_extensions_, &extensions_, &failure_message_, diff --git a/net/websockets/websocket_basic_handshake_stream.h b/net/websockets/websocket_basic_handshake_stream.h index 71e835f..eae4872 100644 --- a/net/websockets/websocket_basic_handshake_stream.h +++ b/net/websockets/websocket_basic_handshake_stream.h @@ -86,14 +86,12 @@ class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream void OnFinishOpeningHandshake(); - // Validates the response from the server and returns OK or - // ERR_INVALID_RESPONSE. - int ValidateResponse(); + // Validates the response and sends the finished handshake event. + int ValidateResponse(int rv); // Check that the headers are well-formed for a 101 response, and returns // OK if they are, otherwise returns ERR_INVALID_RESPONSE. - int ValidateUpgradeResponse( - const scoped_refptr<HttpResponseHeaders>& headers); + int ValidateUpgradeResponse(const HttpResponseHeaders* headers); HttpStreamParser* parser() const { return state_.parser(); } diff --git a/net/websockets/websocket_handshake_stream_create_helper_test.cc b/net/websockets/websocket_handshake_stream_create_helper_test.cc index 89e7cc3..652f1bc 100644 --- a/net/websockets/websocket_handshake_stream_create_helper_test.cc +++ b/net/websockets/websocket_handshake_stream_create_helper_test.cc @@ -107,6 +107,8 @@ class WebSocketHandshakeStreamCreateHelperTest : public ::testing::Test { HttpRequestHeaders headers; headers.SetHeader("Host", "localhost"); headers.SetHeader("Connection", "Upgrade"); + headers.SetHeader("Pragma", "no-cache"); + headers.SetHeader("Cache-Control", "no-cache"); headers.SetHeader("Upgrade", "websocket"); headers.SetHeader("Origin", origin); headers.SetHeader("Sec-WebSocket-Version", "13"); diff --git a/net/websockets/websocket_stream.cc b/net/websockets/websocket_stream.cc index 2a6edb2b..507ed9d 100644 --- a/net/websockets/websocket_stream.cc +++ b/net/websockets/websocket_stream.cc @@ -167,6 +167,7 @@ scoped_ptr<WebSocketStreamRequest> CreateAndConnectStreamWithCreateHelper( WebSocketHandshakeStreamBase::CreateHelper::DataKey(), create_helper.release()); request->url_request()->SetLoadFlags(LOAD_DISABLE_CACHE | + LOAD_BYPASS_CACHE | LOAD_DO_NOT_PROMPT_FOR_LOGIN); request->url_request()->Start(); return request.PassAs<WebSocketStreamRequest>(); diff --git a/net/websockets/websocket_stream_test.cc b/net/websockets/websocket_stream_test.cc index 6de9647..77d8ba3 100644 --- a/net/websockets/websocket_stream_test.cc +++ b/net/websockets/websocket_stream_test.cc @@ -251,23 +251,26 @@ TEST_F(WebSocketStreamCreateTest, HandshakeInfo) { EXPECT_EQ(GURL("ws://localhost/"), response_info_->url); EXPECT_EQ(101, response_info_->status_code); EXPECT_EQ("Switching Protocols", response_info_->status_text); - EXPECT_EQ(10u, request_headers.size()); + ASSERT_EQ(12u, request_headers.size()); EXPECT_EQ(HeaderKeyValuePair("Host", "localhost"), request_headers[0]); EXPECT_EQ(HeaderKeyValuePair("Connection", "Upgrade"), request_headers[1]); - EXPECT_EQ(HeaderKeyValuePair("Upgrade", "websocket"), request_headers[2]); - EXPECT_EQ(HeaderKeyValuePair("Origin", "http://localhost/"), + EXPECT_EQ(HeaderKeyValuePair("Pragma", "no-cache"), request_headers[2]); + EXPECT_EQ(HeaderKeyValuePair("Cache-Control", "no-cache"), request_headers[3]); + EXPECT_EQ(HeaderKeyValuePair("Upgrade", "websocket"), request_headers[4]); + EXPECT_EQ(HeaderKeyValuePair("Origin", "http://localhost/"), + request_headers[5]); EXPECT_EQ(HeaderKeyValuePair("Sec-WebSocket-Version", "13"), - request_headers[4]); - EXPECT_EQ(HeaderKeyValuePair("User-Agent", ""), request_headers[5]); - EXPECT_EQ(HeaderKeyValuePair("Accept-Encoding", "gzip,deflate"), request_headers[6]); + EXPECT_EQ(HeaderKeyValuePair("User-Agent", ""), request_headers[7]); + EXPECT_EQ(HeaderKeyValuePair("Accept-Encoding", "gzip,deflate"), + request_headers[8]); EXPECT_EQ(HeaderKeyValuePair("Accept-Language", "en-us,fr"), - request_headers[7]); - EXPECT_EQ("Sec-WebSocket-Key", request_headers[8].first); + request_headers[9]); + EXPECT_EQ("Sec-WebSocket-Key", request_headers[10].first); EXPECT_EQ(HeaderKeyValuePair("Sec-WebSocket-Extensions", "permessage-deflate; client_max_window_bits"), - request_headers[9]); + request_headers[11]); std::vector<HeaderKeyValuePair> response_headers = ToVector(*response_info_->headers); @@ -651,7 +654,8 @@ TEST_F(WebSocketStreamCreateTest, InvalidStatusCode) { kInvalidStatusCodeResponse); RunUntilIdle(); EXPECT_TRUE(has_failed()); - EXPECT_EQ("Unexpected status code: 200", failure_message()); + EXPECT_EQ("Error during WebSocket handshake: Unexpected response code: 200", + failure_message()); } // Redirects are not followed (according to the WHATWG WebSocket API, which @@ -673,7 +677,8 @@ TEST_F(WebSocketStreamCreateTest, RedirectsRejected) { kRedirectResponse); RunUntilIdle(); EXPECT_TRUE(has_failed()); - EXPECT_EQ("Unexpected status code: 302", failure_message()); + EXPECT_EQ("Error during WebSocket handshake: Unexpected response code: 302", + failure_message()); } // Malformed responses should be rejected. HttpStreamParser will accept just @@ -696,7 +701,8 @@ TEST_F(WebSocketStreamCreateTest, MalformedResponse) { kMalformedResponse); RunUntilIdle(); EXPECT_TRUE(has_failed()); - EXPECT_EQ("Unexpected status code: 200", failure_message()); + EXPECT_EQ("Error during WebSocket handshake: Unexpected response code: 200", + failure_message()); } // Upgrade header must be present. diff --git a/net/websockets/websocket_test_util.cc b/net/websockets/websocket_test_util.cc index da030c5..8146cc3 100644 --- a/net/websockets/websocket_test_util.cc +++ b/net/websockets/websocket_test_util.cc @@ -37,6 +37,8 @@ std::string WebSocketStandardRequest(const std::string& path, "GET %s HTTP/1.1\r\n" "Host: localhost\r\n" "Connection: Upgrade\r\n" + "Pragma: no-cache\r\n" + "Cache-Control: no-cache\r\n" "Upgrade: websocket\r\n" "Origin: %s\r\n" "Sec-WebSocket-Version: 13\r\n" |