Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(546)

Side by Side Diff: net/websockets/websocket_handshake_handler.h

Issue 6823075: Accept new WebSocket handshake format (hybi-04 and later). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Revise comments. Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 // True if the parsed handshake conforms to hybi-04 or later, which means
79 // it has Sec-WebSocket-Key header and does not have "key3" part after
80 // the handshake headers.
81 bool is_hybi04_handshake() const;
ukai 2011/04/13 04:09:00 maybe, HasSecWebSocketKey() or HasKey3Data() ? or
Yuta Kitamura 2011/04/13 11:23:50 Used Sec-WebSocket-Version. Done.
82
61 private: 83 private:
62 std::string status_line_; 84 std::string status_line_;
63 std::string headers_; 85 std::string headers_;
64 std::string key3_; 86 std::string key3_;
65 int original_length_; 87 int original_length_;
66 int raw_length_; 88 int raw_length_;
89 bool is_hybi04_handshake_;
67 90
68 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeRequestHandler); 91 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeRequestHandler);
69 }; 92 };
70 93
71 class WebSocketHandshakeResponseHandler { 94 class WebSocketHandshakeResponseHandler {
72 public: 95 public:
73 WebSocketHandshakeResponseHandler(); 96 WebSocketHandshakeResponseHandler();
74 ~WebSocketHandshakeResponseHandler(); 97 ~WebSocketHandshakeResponseHandler();
75 98
99 // Set true if the format of handshake request was hybi-04 or newer
100 // before parsing the response. Default is false.
101 bool expect_hybi04_handshake() const;
102 void set_expect_hybi04_handshake(bool expect_hybi04_handshake);
103
76 // Parses WebSocket handshake response from WebSocket server. 104 // Parses WebSocket handshake response from WebSocket server.
77 // Returns number of bytes in |data| used for WebSocket handshake response 105 // Returns number of bytes in |data| used for WebSocket handshake response
78 // message, including response key. If it already got whole WebSocket 106 // message, including response key. If it already got whole WebSocket
79 // handshake response message, returns zero. In other words, 107 // handshake response message, returns zero. In other words,
80 // [data + returned value, data + length) will be WebSocket frame data 108 // [data + returned value, data + length) will be WebSocket frame data
81 // after handshake response message. 109 // after handshake response message.
82 // TODO(ukai): fail fast when response gives wrong status code. 110 // TODO(ukai): fail fast when response gives wrong status code.
83 size_t ParseRawResponse(const char* data, int length); 111 size_t ParseRawResponse(const char* data, int length);
84 // Returns true if it already parses full handshake response message. 112 // Returns true if it already parses full handshake response message.
85 bool HasResponse() const; 113 bool HasResponse() const;
(...skipping 12 matching lines...) Expand all
98 void RemoveHeaders(const char* const headers_to_remove[], 126 void RemoveHeaders(const char* const headers_to_remove[],
99 size_t headers_to_remove_len); 127 size_t headers_to_remove_len);
100 128
101 // Gets raw WebSocket handshake response received from WebSocket server. 129 // Gets raw WebSocket handshake response received from WebSocket server.
102 std::string GetRawResponse() const; 130 std::string GetRawResponse() const;
103 131
104 // Gets WebSocket handshake response message sent to renderer process. 132 // Gets WebSocket handshake response message sent to renderer process.
105 std::string GetResponse(); 133 std::string GetResponse();
106 134
107 private: 135 private:
136 // Returns the length of response key. This function will return 0
137 // if |expect_hybi04_handshake_| is set to true.
138 size_t GetResponseKeySize() const;
139
108 std::string original_; 140 std::string original_;
109 int original_header_length_; 141 int original_header_length_;
110 std::string status_line_; 142 std::string status_line_;
111 std::string headers_; 143 std::string headers_;
112 std::string header_separator_; 144 std::string header_separator_;
113 std::string key_; 145 std::string key_;
146 bool expect_hybi04_handshake_;
114 147
115 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeResponseHandler); 148 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeResponseHandler);
116 }; 149 };
117 150
118 } // namespace net 151 } // namespace net
119 152
120 #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ 153 #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_
OLDNEW
« no previous file with comments | « no previous file | net/websockets/websocket_handshake_handler.cc » ('j') | net/websockets/websocket_handshake_handler.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698