diff options
author | ukai@chromium.org <ukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-26 07:35:55 +0000 |
---|---|---|
committer | ukai@chromium.org <ukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-26 07:35:55 +0000 |
commit | 511d0a0a31a54e0cc0f15cb1b977dc9f9b20f0d3 (patch) | |
tree | 8a4b3eb672a1a3f4efd172de181892cc72ec7275 /net/websockets/websocket_handshake.h | |
parent | 57771511d6e0f0bfea8aafa6bd6aca1294b08f87 (diff) | |
download | chromium_src-511d0a0a31a54e0cc0f15cb1b977dc9f9b20f0d3.zip chromium_src-511d0a0a31a54e0cc0f15cb1b977dc9f9b20f0d3.tar.gz chromium_src-511d0a0a31a54e0cc0f15cb1b977dc9f9b20f0d3.tar.bz2 |
Implement new websocket handshake based on draft-hixie-thewebsocketprotocol-76
BUG=none
TEST=net_unittests passes
Review URL: http://codereview.chromium.org/1108002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42736 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/websockets/websocket_handshake.h')
-rw-r--r-- | net/websockets/websocket_handshake.h | 79 |
1 files changed, 58 insertions, 21 deletions
diff --git a/net/websockets/websocket_handshake.h b/net/websockets/websocket_handshake.h index 1e94eff..3f64b8b 100644 --- a/net/websockets/websocket_handshake.h +++ b/net/websockets/websocket_handshake.h @@ -8,6 +8,7 @@ #include <string> #include "base/basictypes.h" +#include "base/scoped_ptr.h" #include "googleurl/src/gurl.h" namespace net { @@ -18,12 +19,6 @@ class WebSocketHandshake { public: static const int kWebSocketPort; static const int kSecureWebSocketPort; - static const char kServerHandshakeHeader[]; - static const size_t kServerHandshakeHeaderLength; - static const char kUpgradeHeader[]; - static const size_t kUpgradeHeaderLength; - static const char kConnectionHeader[]; - static const size_t kConnectionHeaderLength; enum Mode { MODE_INCOMPLETE, MODE_NORMAL, MODE_FAILED, MODE_CONNECTED @@ -32,32 +27,33 @@ class WebSocketHandshake { const std::string& origin, const std::string& location, const std::string& protocol); - ~WebSocketHandshake(); + virtual ~WebSocketHandshake(); bool is_secure() const; // Creates the client handshake message from |this|. - std::string CreateClientHandshakeMessage() const; + virtual std::string CreateClientHandshakeMessage(); // Reads server handshake message in |len| of |data|, updates |mode_| and // returns number of bytes of the server handshake message. // Once connection is established, |mode_| will be MODE_CONNECTED. // If connection establishment failed, |mode_| will be MODE_FAILED. // Returns negative if the server handshake message is incomplete. - int ReadServerHandshake(const char* data, size_t len); + virtual int ReadServerHandshake(const char* data, size_t len); Mode mode() const { return mode_; } - private: - // Processes server handshake message, parsed as |headers|, and updates - // |ws_origin_|, |ws_location_| and |ws_protocol_|. - // Returns true if it's ok. - // Returns false otherwise (e.g. duplicate WebSocket-Origin: header, etc.) - bool ProcessHeaders(const HttpResponseHeaders& headers); - - // Checks |ws_origin_|, |ws_location_| and |ws_protocol_| are valid - // against |origin_|, |location_| and |protocol_|. - // Returns true if it's ok. - // Returns false otherwise (e.g. origin mismatch, etc.) - bool CheckResponseHeaders() const; + protected: + std::string GetResourceName() const; + std::string GetHostFieldValue() const; + std::string GetOriginFieldValue() const; + + // Gets the value of the specified header. + // It assures only one header of |name| in |headers|. + // Returns true iff single header of |name| is found in |headers| + // and |value| is filled with the value. + // Returns false otherwise. + static bool GetSingleHeader(const HttpResponseHeaders& headers, + const std::string& name, + std::string* value); GURL url_; // Handshake messages that the client is going to send out. @@ -72,6 +68,47 @@ class WebSocketHandshake { std::string ws_location_; std::string ws_protocol_; + private: + friend class WebSocketHandshakeTest; + + class Parameter { + public: + static const int kKey3Size = 8; + static const int kExpectedResponseSize = 16; + Parameter(); + ~Parameter(); + + void GenerateKeys(); + const std::string& GetSecWebSocketKey1() const { return key_1_; } + const std::string& GetSecWebSocketKey2() const { return key_2_; } + const std::string& GetKey3() const { return key_3_; } + + void GetExpectedResponse(uint8* expected) const; + + private: + friend class WebSocketHandshakeTest; + + // Set random number generator. |rand| should return a random number + // between min and max (inclusive). + static void SetRandomNumberGenerator( + uint32 (*rand)(uint32 min, uint32 max)); + void GenerateSecWebSocketKey(uint32* number, std::string* key); + void GenerateKey3(); + + uint32 number_1_; + uint32 number_2_; + std::string key_1_; + std::string key_2_; + std::string key_3_; + + static uint32 (*rand_)(uint32 min, uint32 max); + }; + + virtual bool ProcessHeaders(const HttpResponseHeaders& headers); + virtual bool CheckResponseHeaders() const; + + scoped_ptr<Parameter> parameter_; + DISALLOW_COPY_AND_ASSIGN(WebSocketHandshake); }; |