| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_H_ | 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_H_ |
| 6 #define NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_H_ | 6 #define NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/scoped_ptr.h" |
| 11 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
| 12 | 13 |
| 13 namespace net { | 14 namespace net { |
| 14 | 15 |
| 15 class HttpResponseHeaders; | 16 class HttpResponseHeaders; |
| 16 | 17 |
| 17 class WebSocketHandshake { | 18 class WebSocketHandshake { |
| 18 public: | 19 public: |
| 19 static const int kWebSocketPort; | 20 static const int kWebSocketPort; |
| 20 static const int kSecureWebSocketPort; | 21 static const int kSecureWebSocketPort; |
| 21 static const char kServerHandshakeHeader[]; | |
| 22 static const size_t kServerHandshakeHeaderLength; | |
| 23 static const char kUpgradeHeader[]; | |
| 24 static const size_t kUpgradeHeaderLength; | |
| 25 static const char kConnectionHeader[]; | |
| 26 static const size_t kConnectionHeaderLength; | |
| 27 | 22 |
| 28 enum Mode { | 23 enum Mode { |
| 29 MODE_INCOMPLETE, MODE_NORMAL, MODE_FAILED, MODE_CONNECTED | 24 MODE_INCOMPLETE, MODE_NORMAL, MODE_FAILED, MODE_CONNECTED |
| 30 }; | 25 }; |
| 31 WebSocketHandshake(const GURL& url, | 26 WebSocketHandshake(const GURL& url, |
| 32 const std::string& origin, | 27 const std::string& origin, |
| 33 const std::string& location, | 28 const std::string& location, |
| 34 const std::string& protocol); | 29 const std::string& protocol); |
| 35 ~WebSocketHandshake(); | 30 virtual ~WebSocketHandshake(); |
| 36 | 31 |
| 37 bool is_secure() const; | 32 bool is_secure() const; |
| 38 // Creates the client handshake message from |this|. | 33 // Creates the client handshake message from |this|. |
| 39 std::string CreateClientHandshakeMessage() const; | 34 virtual std::string CreateClientHandshakeMessage(); |
| 40 | 35 |
| 41 // Reads server handshake message in |len| of |data|, updates |mode_| and | 36 // Reads server handshake message in |len| of |data|, updates |mode_| and |
| 42 // returns number of bytes of the server handshake message. | 37 // returns number of bytes of the server handshake message. |
| 43 // Once connection is established, |mode_| will be MODE_CONNECTED. | 38 // Once connection is established, |mode_| will be MODE_CONNECTED. |
| 44 // If connection establishment failed, |mode_| will be MODE_FAILED. | 39 // If connection establishment failed, |mode_| will be MODE_FAILED. |
| 45 // Returns negative if the server handshake message is incomplete. | 40 // Returns negative if the server handshake message is incomplete. |
| 46 int ReadServerHandshake(const char* data, size_t len); | 41 virtual int ReadServerHandshake(const char* data, size_t len); |
| 47 Mode mode() const { return mode_; } | 42 Mode mode() const { return mode_; } |
| 48 | 43 |
| 49 private: | 44 protected: |
| 50 // Processes server handshake message, parsed as |headers|, and updates | 45 std::string GetResourceName() const; |
| 51 // |ws_origin_|, |ws_location_| and |ws_protocol_|. | 46 std::string GetHostFieldValue() const; |
| 52 // Returns true if it's ok. | 47 std::string GetOriginFieldValue() const; |
| 53 // Returns false otherwise (e.g. duplicate WebSocket-Origin: header, etc.) | |
| 54 bool ProcessHeaders(const HttpResponseHeaders& headers); | |
| 55 | 48 |
| 56 // Checks |ws_origin_|, |ws_location_| and |ws_protocol_| are valid | 49 // Gets the value of the specified header. |
| 57 // against |origin_|, |location_| and |protocol_|. | 50 // It assures only one header of |name| in |headers|. |
| 58 // Returns true if it's ok. | 51 // Returns true iff single header of |name| is found in |headers| |
| 59 // Returns false otherwise (e.g. origin mismatch, etc.) | 52 // and |value| is filled with the value. |
| 60 bool CheckResponseHeaders() const; | 53 // Returns false otherwise. |
| 54 static bool GetSingleHeader(const HttpResponseHeaders& headers, |
| 55 const std::string& name, |
| 56 std::string* value); |
| 61 | 57 |
| 62 GURL url_; | 58 GURL url_; |
| 63 // Handshake messages that the client is going to send out. | 59 // Handshake messages that the client is going to send out. |
| 64 std::string origin_; | 60 std::string origin_; |
| 65 std::string location_; | 61 std::string location_; |
| 66 std::string protocol_; | 62 std::string protocol_; |
| 67 | 63 |
| 68 Mode mode_; | 64 Mode mode_; |
| 69 | 65 |
| 70 // Handshake messages that server sent. | 66 // Handshake messages that server sent. |
| 71 std::string ws_origin_; | 67 std::string ws_origin_; |
| 72 std::string ws_location_; | 68 std::string ws_location_; |
| 73 std::string ws_protocol_; | 69 std::string ws_protocol_; |
| 74 | 70 |
| 71 private: |
| 72 friend class WebSocketHandshakeTest; |
| 73 |
| 74 class Parameter { |
| 75 public: |
| 76 static const int kKey3Size = 8; |
| 77 static const int kExpectedResponseSize = 16; |
| 78 Parameter(); |
| 79 ~Parameter(); |
| 80 |
| 81 void GenerateKeys(); |
| 82 const std::string& GetSecWebSocketKey1() const { return key_1_; } |
| 83 const std::string& GetSecWebSocketKey2() const { return key_2_; } |
| 84 const std::string& GetKey3() const { return key_3_; } |
| 85 |
| 86 void GetExpectedResponse(uint8* expected) const; |
| 87 |
| 88 private: |
| 89 friend class WebSocketHandshakeTest; |
| 90 |
| 91 // Set random number generator. |rand| should return a random number |
| 92 // between min and max (inclusive). |
| 93 static void SetRandomNumberGenerator( |
| 94 uint32 (*rand)(uint32 min, uint32 max)); |
| 95 void GenerateSecWebSocketKey(uint32* number, std::string* key); |
| 96 void GenerateKey3(); |
| 97 |
| 98 uint32 number_1_; |
| 99 uint32 number_2_; |
| 100 std::string key_1_; |
| 101 std::string key_2_; |
| 102 std::string key_3_; |
| 103 |
| 104 static uint32 (*rand_)(uint32 min, uint32 max); |
| 105 }; |
| 106 |
| 107 virtual bool ProcessHeaders(const HttpResponseHeaders& headers); |
| 108 virtual bool CheckResponseHeaders() const; |
| 109 |
| 110 scoped_ptr<Parameter> parameter_; |
| 111 |
| 75 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshake); | 112 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshake); |
| 76 }; | 113 }; |
| 77 | 114 |
| 78 } // namespace net | 115 } // namespace net |
| 79 | 116 |
| 80 #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_H_ | 117 #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_H_ |
| OLD | NEW |