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_
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"
18 class HttpResponseHeaders
;
20 class NET_TEST WebSocketHandshake
{
22 static const int kWebSocketPort
;
23 static const int kSecureWebSocketPort
;
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_
; }
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
,
61 // Handshake messages that the client is going to send out.
63 std::string location_
;
64 std::string protocol_
;
68 // Handshake messages that server sent.
69 std::string ws_origin_
;
70 std::string ws_location_
;
71 std::string ws_protocol_
;
74 friend class WebSocketHandshakeTest
;
76 class NET_TEST Parameter
{
78 static const int kKey3Size
= 8;
79 static const int kExpectedResponseSize
= 16;
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;
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
);
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
);
119 #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_H_