OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 // WebSocketHandshake*Handler handles WebSocket handshake request message | 5 // WebSocketHandshake*Handler handles WebSocket handshake request message |
6 // from WebKit renderer process, and WebSocket handshake response message | 6 // from WebKit renderer process, and WebSocket handshake response message |
7 // from WebSocket server. | 7 // from WebSocket server. |
8 // It modifies messages for the following reason: | 8 // It modifies messages for the following reason: |
9 // - We don't trust WebKit renderer process, so we'll not expose HttpOnly | 9 // - We don't trust WebKit renderer process, so we'll not expose HttpOnly |
10 // cookies to the renderer process, so handles HttpOnly cookies in | 10 // cookies to the renderer process, so handles HttpOnly cookies in |
11 // browser process. | 11 // browser process. |
| 12 // |
| 13 // The classes below support two styles of handshake: handshake based |
| 14 // on hixie-76 draft and one based on hybi-04 draft. The critical difference |
| 15 // between these two is how they pass challenge and response values. Hixie-76 |
| 16 // based handshake appends a few bytes of binary data after header fields of |
| 17 // handshake request and response. These data are called "key3" (for request) |
| 18 // or "response key" (for response). On the other hand, handshake based on |
| 19 // hybi-04 and later drafts put challenge and response values into handshake |
| 20 // header fields, thus we do not need to send or receive extra bytes after |
| 21 // handshake headers. |
| 22 // |
| 23 // While we are working on updating WebSocket implementation in WebKit to |
| 24 // conform to the latest procotol draft, we need to accept both styles of |
| 25 // handshake. After we land the protocol changes in WebKit, we will be able to |
| 26 // drop codes handling old-style handshake. |
12 | 27 |
13 #ifndef NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ | 28 #ifndef NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ |
14 #define NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ | 29 #define NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ |
15 #pragma once | 30 #pragma once |
16 | 31 |
17 #include <string> | 32 #include <string> |
18 #include <vector> | 33 #include <vector> |
19 | 34 |
20 #include "base/memory/ref_counted.h" | 35 #include "base/memory/ref_counted.h" |
21 #include "net/http/http_request_info.h" | 36 #include "net/http/http_request_info.h" |
(...skipping 16 matching lines...) Expand all Loading... |
38 | 53 |
39 // Appends the header value pair for |name| and |value|, if |name| doesn't | 54 // Appends the header value pair for |name| and |value|, if |name| doesn't |
40 // exist. | 55 // exist. |
41 void AppendHeaderIfMissing(const std::string& name, | 56 void AppendHeaderIfMissing(const std::string& name, |
42 const std::string& value); | 57 const std::string& value); |
43 // Removes the headers that matches (case insensitive). | 58 // Removes the headers that matches (case insensitive). |
44 void RemoveHeaders(const char* const headers_to_remove[], | 59 void RemoveHeaders(const char* const headers_to_remove[], |
45 size_t headers_to_remove_len); | 60 size_t headers_to_remove_len); |
46 | 61 |
47 // Gets request info to open WebSocket connection. | 62 // Gets request info to open WebSocket connection. |
48 // Also, fill challange data in |challenge|. | 63 // Fills challange data (concatenation of key1, 2 and 3 for hybi-03 and |
| 64 // earlier, or Sec-WebSocket-Key header value for hybi-04 and later) |
| 65 // in |challenge|. |
49 HttpRequestInfo GetRequestInfo(const GURL& url, std::string* challenge); | 66 HttpRequestInfo GetRequestInfo(const GURL& url, std::string* challenge); |
50 // Gets request as SpdyHeaderBlock. | 67 // Gets request as SpdyHeaderBlock. |
51 // Also, fill challenge data in |challenge|. | 68 // Also, fills challenge data in |challenge|. |
52 bool GetRequestHeaderBlock(const GURL& url, | 69 bool GetRequestHeaderBlock(const GURL& url, |
53 spdy::SpdyHeaderBlock* headers, | 70 spdy::SpdyHeaderBlock* headers, |
54 std::string* challenge); | 71 std::string* challenge); |
55 // Gets WebSocket handshake raw request message to open WebSocket | 72 // Gets WebSocket handshake raw request message to open WebSocket |
56 // connection. | 73 // connection. |
57 std::string GetRawRequest(); | 74 std::string GetRawRequest(); |
58 // Calling raw_length is valid only after GetRawRquest() call. | 75 // Calling raw_length is valid only after GetRawRquest() call. |
59 size_t raw_length() const; | 76 size_t raw_length() const; |
60 | 77 |
| 78 // Returns the value of Sec-WebSocket-Version or Sec-WebSocket-Draft header |
| 79 // (the latter is an old name of the former). Returns 0 if both headers were |
| 80 // absent, which means the handshake was based on hybi-00 (= hixie-76). |
| 81 // Should only be called after the handshake has been parsed. |
| 82 int protocol_version() const; |
| 83 |
61 private: | 84 private: |
62 std::string status_line_; | 85 std::string status_line_; |
63 std::string headers_; | 86 std::string headers_; |
64 std::string key3_; | 87 std::string key3_; |
65 int original_length_; | 88 int original_length_; |
66 int raw_length_; | 89 int raw_length_; |
| 90 int protocol_version_; // "-1" means we haven't parsed the handshake yet. |
67 | 91 |
68 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeRequestHandler); | 92 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeRequestHandler); |
69 }; | 93 }; |
70 | 94 |
71 class WebSocketHandshakeResponseHandler { | 95 class WebSocketHandshakeResponseHandler { |
72 public: | 96 public: |
73 WebSocketHandshakeResponseHandler(); | 97 WebSocketHandshakeResponseHandler(); |
74 ~WebSocketHandshakeResponseHandler(); | 98 ~WebSocketHandshakeResponseHandler(); |
75 | 99 |
| 100 // Set WebSocket protocol version before parsing the response. |
| 101 // Default is 0 (hybi-00, which is same as hixie-76). |
| 102 int protocol_version() const; |
| 103 void set_protocol_version(int protocol_version); |
| 104 |
76 // Parses WebSocket handshake response from WebSocket server. | 105 // Parses WebSocket handshake response from WebSocket server. |
77 // Returns number of bytes in |data| used for WebSocket handshake response | 106 // Returns number of bytes in |data| used for WebSocket handshake response |
78 // message, including response key. If it already got whole WebSocket | 107 // message, including response key. If it already got whole WebSocket |
79 // handshake response message, returns zero. In other words, | 108 // handshake response message, returns zero. In other words, |
80 // [data + returned value, data + length) will be WebSocket frame data | 109 // [data + returned value, data + length) will be WebSocket frame data |
81 // after handshake response message. | 110 // after handshake response message. |
82 // TODO(ukai): fail fast when response gives wrong status code. | 111 // TODO(ukai): fail fast when response gives wrong status code. |
83 size_t ParseRawResponse(const char* data, int length); | 112 size_t ParseRawResponse(const char* data, int length); |
84 // Returns true if it already parses full handshake response message. | 113 // Returns true if it already parses full handshake response message. |
85 bool HasResponse() const; | 114 bool HasResponse() const; |
(...skipping 12 matching lines...) Expand all Loading... |
98 void RemoveHeaders(const char* const headers_to_remove[], | 127 void RemoveHeaders(const char* const headers_to_remove[], |
99 size_t headers_to_remove_len); | 128 size_t headers_to_remove_len); |
100 | 129 |
101 // Gets raw WebSocket handshake response received from WebSocket server. | 130 // Gets raw WebSocket handshake response received from WebSocket server. |
102 std::string GetRawResponse() const; | 131 std::string GetRawResponse() const; |
103 | 132 |
104 // Gets WebSocket handshake response message sent to renderer process. | 133 // Gets WebSocket handshake response message sent to renderer process. |
105 std::string GetResponse(); | 134 std::string GetResponse(); |
106 | 135 |
107 private: | 136 private: |
| 137 // Returns the length of response key. This function will return 0 |
| 138 // if the specified WebSocket protocol version does not require |
| 139 // sending response key. |
| 140 size_t GetResponseKeySize() const; |
| 141 |
108 std::string original_; | 142 std::string original_; |
109 int original_header_length_; | 143 int original_header_length_; |
110 std::string status_line_; | 144 std::string status_line_; |
111 std::string headers_; | 145 std::string headers_; |
112 std::string header_separator_; | 146 std::string header_separator_; |
113 std::string key_; | 147 std::string key_; |
| 148 int protocol_version_; |
114 | 149 |
115 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeResponseHandler); | 150 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeResponseHandler); |
116 }; | 151 }; |
117 | 152 |
118 } // namespace net | 153 } // namespace net |
119 | 154 |
120 #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ | 155 #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ |
OLD | NEW |