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. 4 // 5 // WebSocketHandshake*Handler handles WebSocket handshake request message 6 // from WebKit renderer process, and WebSocket handshake response message 7 // from WebSocket server. 8 // It modifies messages for the following reason: 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 11 // browser process. 12 13 #ifndef NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ 14 #define NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ 15 #pragma once 16 17 #include <string> 18 #include <vector> 19 20 #include "base/memory/ref_counted.h" 21 #include "net/http/http_request_info.h" 22 #include "net/http/http_response_info.h" 23 #include "net/spdy/spdy_framer.h" 24 25 namespace net { 26 27 class WebSocketHandshakeRequestHandler { 28 public: 29 WebSocketHandshakeRequestHandler(); ~WebSocketHandshakeRequestHandler()30 ~WebSocketHandshakeRequestHandler() {} 31 32 // Parses WebSocket handshake request from renderer process. 33 // It assumes a WebSocket handshake request message is given at once, and 34 // no other data is added to the request message. 35 bool ParseRequest(const char* data, int length); 36 37 size_t original_length() const; 38 39 // Appends the header value pair for |name| and |value|, if |name| doesn't 40 // exist. 41 void AppendHeaderIfMissing(const std::string& name, 42 const std::string& value); 43 // Removes the headers that matches (case insensitive). 44 void RemoveHeaders(const char* const headers_to_remove[], 45 size_t headers_to_remove_len); 46 47 // Gets request info to open WebSocket connection. 48 // Also, fill challange data in |challenge|. 49 HttpRequestInfo GetRequestInfo(const GURL& url, std::string* challenge); 50 // Gets request as SpdyHeaderBlock. 51 // Also, fill challenge data in |challenge|. 52 bool GetRequestHeaderBlock(const GURL& url, 53 spdy::SpdyHeaderBlock* headers, 54 std::string* challenge); 55 // Gets WebSocket handshake raw request message to open WebSocket 56 // connection. 57 std::string GetRawRequest(); 58 // Calling raw_length is valid only after GetRawRquest() call. 59 size_t raw_length() const; 60 61 private: 62 std::string status_line_; 63 std::string headers_; 64 std::string key3_; 65 int original_length_; 66 int raw_length_; 67 68 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeRequestHandler); 69 }; 70 71 class WebSocketHandshakeResponseHandler { 72 public: 73 WebSocketHandshakeResponseHandler(); 74 ~WebSocketHandshakeResponseHandler(); 75 76 // Parses WebSocket handshake response from WebSocket server. 77 // Returns number of bytes in |data| used for WebSocket handshake response 78 // message, including response key. If it already got whole WebSocket 79 // handshake response message, returns zero. In other words, 80 // [data + returned value, data + length) will be WebSocket frame data 81 // after handshake response message. 82 // TODO(ukai): fail fast when response gives wrong status code. 83 size_t ParseRawResponse(const char* data, int length); 84 // Returns true if it already parses full handshake response message. 85 bool HasResponse() const; 86 // Parses WebSocket handshake response info given as HttpResponseInfo. 87 bool ParseResponseInfo(const HttpResponseInfo& response_info, 88 const std::string& challenge); 89 // Parses WebSocket handshake response as SpdyHeaderBlock. 90 bool ParseResponseHeaderBlock(const spdy::SpdyHeaderBlock& headers, 91 const std::string& challenge); 92 93 // Gets the headers value. 94 void GetHeaders(const char* const headers_to_get[], 95 size_t headers_to_get_len, 96 std::vector<std::string>* values); 97 // Removes the headers that matches (case insensitive). 98 void RemoveHeaders(const char* const headers_to_remove[], 99 size_t headers_to_remove_len); 100 101 // Gets raw WebSocket handshake response received from WebSocket server. 102 std::string GetRawResponse() const; 103 104 // Gets WebSocket handshake response message sent to renderer process. 105 std::string GetResponse(); 106 107 private: 108 std::string original_; 109 int original_header_length_; 110 std::string status_line_; 111 std::string headers_; 112 std::string header_separator_; 113 std::string key_; 114 115 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeResponseHandler); 116 }; 117 118 } // namespace net 119 120 #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ 121