WebKit roll 90808:90810
[chromium-blink-merge.git] / net / websockets / websocket_handshake.h
blobb0b00a7595ecf0215e00a6f92edd3c01fae1bc46
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_H_
7 #pragma once
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "googleurl/src/gurl.h"
14 #include "net/base/net_api.h"
16 namespace net {
18 class HttpResponseHeaders;
20 class NET_TEST WebSocketHandshake {
21 public:
22 static const int kWebSocketPort;
23 static const int kSecureWebSocketPort;
25 enum Mode {
26 MODE_INCOMPLETE, MODE_NORMAL, MODE_FAILED, MODE_CONNECTED
28 WebSocketHandshake(const GURL& url,
29 const std::string& origin,
30 const std::string& location,
31 const std::string& protocol);
32 virtual ~WebSocketHandshake();
34 bool is_secure() const;
35 // Creates the client handshake message from |this|.
36 virtual std::string CreateClientHandshakeMessage();
38 // Reads server handshake message in |len| of |data|, updates |mode_| and
39 // returns number of bytes of the server handshake message.
40 // Once connection is established, |mode_| will be MODE_CONNECTED.
41 // If connection establishment failed, |mode_| will be MODE_FAILED.
42 // Returns negative if the server handshake message is incomplete.
43 virtual int ReadServerHandshake(const char* data, size_t len);
44 Mode mode() const { return mode_; }
46 protected:
47 std::string GetResourceName() const;
48 std::string GetHostFieldValue() const;
49 std::string GetOriginFieldValue() const;
51 // Gets the value of the specified header.
52 // It assures only one header of |name| in |headers|.
53 // Returns true iff single header of |name| is found in |headers|
54 // and |value| is filled with the value.
55 // Returns false otherwise.
56 static bool GetSingleHeader(const HttpResponseHeaders& headers,
57 const std::string& name,
58 std::string* value);
60 GURL url_;
61 // Handshake messages that the client is going to send out.
62 std::string origin_;
63 std::string location_;
64 std::string protocol_;
66 Mode mode_;
68 // Handshake messages that server sent.
69 std::string ws_origin_;
70 std::string ws_location_;
71 std::string ws_protocol_;
73 private:
74 friend class WebSocketHandshakeTest;
76 class NET_TEST Parameter {
77 public:
78 static const int kKey3Size = 8;
79 static const int kExpectedResponseSize = 16;
80 Parameter();
81 ~Parameter();
83 void GenerateKeys();
84 const std::string& GetSecWebSocketKey1() const { return key_1_; }
85 const std::string& GetSecWebSocketKey2() const { return key_2_; }
86 const std::string& GetKey3() const { return key_3_; }
88 void GetExpectedResponse(uint8* expected) const;
90 private:
91 friend class WebSocketHandshakeTest;
93 // Set random number generator. |rand| should return a random number
94 // between min and max (inclusive).
95 static void SetRandomNumberGenerator(
96 uint32 (*rand)(uint32 min, uint32 max));
97 void GenerateSecWebSocketKey(uint32* number, std::string* key);
98 void GenerateKey3();
100 uint32 number_1_;
101 uint32 number_2_;
102 std::string key_1_;
103 std::string key_2_;
104 std::string key_3_;
106 static uint32 (*rand_)(uint32 min, uint32 max);
109 virtual bool ProcessHeaders(const HttpResponseHeaders& headers);
110 virtual bool CheckResponseHeaders() const;
112 scoped_ptr<Parameter> parameter_;
114 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshake);
117 } // namespace net
119 #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_H_