diff options
author | tyoshino@google.com <tyoshino@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-12 11:19:56 +0000 |
---|---|---|
committer | tyoshino@google.com <tyoshino@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-12 11:19:56 +0000 |
commit | 7ee45785e32196ea2b4b32ef3aed93c4a962aa2f (patch) | |
tree | 345ffffbc492c869288cfe25fd7b7a7592338d0e | |
parent | 250bad43d9d53cefbe36fd53506b2ea45f9c2ae2 (diff) | |
download | chromium_src-7ee45785e32196ea2b4b32ef3aed93c4a962aa2f.zip chromium_src-7ee45785e32196ea2b4b32ef3aed93c4a962aa2f.tar.gz chromium_src-7ee45785e32196ea2b4b32ef3aed93c4a962aa2f.tar.bz2 |
Stop using SplitStringDontTrim in NetLogWebSocketHandshakeParameter::ToValue()
Since key3 may contain some line feeds, we cannot use SplitStringDontTrim.
We use this opportunity to make parser recongize only CR+LF as a separator.
R=ukai
BUG=62918
TEST=Doesn't crash when we open some page using WebSocket and chrome://net-internals.
Review URL: http://codereview.chromium.org/4874001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65933 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/net.gyp | 1 | ||||
-rw-r--r-- | net/websockets/websocket_net_log_params.h | 42 | ||||
-rw-r--r-- | net/websockets/websocket_net_log_params_unittest.cc | 47 |
3 files changed, 72 insertions, 18 deletions
diff --git a/net/net.gyp b/net/net.gyp index 5fa9d6a..a2aff3b 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -971,6 +971,7 @@ 'websockets/websocket_handshake_handler_unittest.cc', 'websockets/websocket_handshake_unittest.cc', 'websockets/websocket_job_unittest.cc', + 'websockets/websocket_net_log_params_unittest.cc', 'websockets/websocket_throttle_unittest.cc', 'websockets/websocket_unittest.cc', ], diff --git a/net/websockets/websocket_net_log_params.h b/net/websockets/websocket_net_log_params.h index a504186..4cd058f 100644 --- a/net/websockets/websocket_net_log_params.h +++ b/net/websockets/websocket_net_log_params.h @@ -28,28 +28,34 @@ class NetLogWebSocketHandshakeParameter : public NetLog::EventParameters { Value* ToValue() const { DictionaryValue* dict = new DictionaryValue(); ListValue* headers = new ListValue(); - std::vector<std::string> lines; - base::SplitStringDontTrim(headers_, '\n', &lines); - for (size_t i = 0; i < lines.size(); ++i) { - if (lines[i] == "\r") { - headers->Append(new StringValue("")); - // Dump WebSocket key3 - std::string key; - i = i + 1; - for (; i < lines.size(); ++i) { - for (size_t j = 0; j < lines[i].length(); ++j) { - key += base::StringPrintf("\\x%02x", lines[i][j] & 0xff); + + size_t last = 0; + size_t headers_size = headers_.size(); + size_t pos = 0; + while (pos <= headers_size) { + if (pos == headers_size || + (headers_[pos] == '\r' && + pos + 1 < headers_size && headers_[pos + 1] == '\n')) { + std::string entry = headers_.substr(last, pos - last); + pos += 2; + last = pos; + + headers->Append(new StringValue(entry)); + + if (entry.empty()) { + // Dump WebSocket key3. + std::string key; + for (; pos < headers_size; ++pos) { + key += base::StringPrintf("\\x%02x", headers_[pos] & 0xff); } - key += "\\x0a"; + headers->Append(new StringValue(key)); + break; } - headers->Append(new StringValue(key)); - break; + } else { + ++pos; } - std::string line = lines[i]; - if (line.length() > 0 && line[line.length() - 1] == '\r') - line = line.substr(0, line.length() - 1); - headers->Append(new StringValue(line)); } + dict->Set("headers", headers); return dict; } diff --git a/net/websockets/websocket_net_log_params_unittest.cc b/net/websockets/websocket_net_log_params_unittest.cc new file mode 100644 index 0000000..7339962 --- /dev/null +++ b/net/websockets/websocket_net_log_params_unittest.cc @@ -0,0 +1,47 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <string> + +#include "base/ref_counted.h" +#include "base/scoped_ptr.h" +#include "base/values.h" +#include "net/websockets/websocket_net_log_params.h" +#include "testing/gtest/include/gtest/gtest.h" + +TEST(NetLogWebSocketHandshakeParameterTest, ToValue) { + ListValue* list = new ListValue(); + list->Append(new StringValue("GET /demo HTTP/1.1")); + list->Append(new StringValue("Host: example.com")); + list->Append(new StringValue("Connection: Upgrade")); + list->Append(new StringValue("Sec-WebSocket-Key2: 12998 5 Y3 1 .P00")); + list->Append(new StringValue("Sec-WebSocket-Protocol: sample")); + list->Append(new StringValue("Upgrade: WebSocket")); + list->Append(new StringValue("Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5")); + list->Append(new StringValue("Origin: http://example.com")); + list->Append(new StringValue("")); + list->Append(new StringValue("\\x00\\x01\\x0a\\x0d\\xff\\xfe\\x0d\\x0a")); + + DictionaryValue expected; + expected.Set("headers", list); + + const std::string key("\x00\x01\x0a\x0d\xff\xfe\x0d\x0a", 8); + const std::string testInput = + "GET /demo HTTP/1.1\r\n" + "Host: example.com\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n" + "Sec-WebSocket-Protocol: sample\r\n" + "Upgrade: WebSocket\r\n" + "Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n" + "Origin: http://example.com\r\n" + "\r\n" + + key; + + scoped_refptr<net::NetLogWebSocketHandshakeParameter> parameter( + new net::NetLogWebSocketHandshakeParameter(testInput)); + scoped_ptr<Value> actual(parameter->ToValue()); + + EXPECT_TRUE(expected.Equals(actual.get())); +} |