diff options
Diffstat (limited to 'net/websockets/websocket_handshake_handler.h')
-rw-r--r-- | net/websockets/websocket_handshake_handler.h | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/net/websockets/websocket_handshake_handler.h b/net/websockets/websocket_handshake_handler.h new file mode 100644 index 0000000..8f0cb4b --- /dev/null +++ b/net/websockets/websocket_handshake_handler.h @@ -0,0 +1,110 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// WebSocketHandshake*Handler handles WebSocket handshake request message +// from WebKit renderer process, and WebSocket handshake response message +// from WebSocket server. +// It modifies messages for the following reason: +// - We don't trust WebKit renderer process, so we'll not expose HttpOnly +// cookies to the renderer process, so handles HttpOnly cookies in +// browser process. + +#ifndef NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ +#define NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ + +#include <string> +#include <vector> + +#include "base/ref_counted.h" +#include "net/http/http_request_info.h" +#include "net/http/http_response_info.h" + +namespace net { + +class WebSocketHandshakeRequestHandler { + public: + WebSocketHandshakeRequestHandler(); + ~WebSocketHandshakeRequestHandler() {} + + // Parses WebSocket handshake request from renderer process. + // It assumes a WebSocket handshake request message is given at once, and + // no other data is added to the request message. + bool ParseRequest(const char* data, int length); + + size_t original_length() const; + + // Appends the header value pair for |name| and |value|, if |name| doesn't + // exist. + void AppendHeaderIfMissing(const std::string& name, + const std::string& value); + // Removes the headers that matches (case insensitive). + void RemoveHeaders(const char* const headers_to_remove[], + size_t headers_to_remove_len); + + // Gets request info to open WebSocket connection. + const HttpRequestInfo& GetRequestInfo(const GURL& url); + // Gets WebSocket handshake raw request message to open WebSocket + // connection. + std::string GetRawRequest(); + // Calling raw_length is valid only after GetRawRquest() call. + size_t raw_length() const; + + std::string GetChallenge() const; + + private: + HttpRequestInfo request_info_; + + std::string status_line_; + std::string headers_; + std::string key3_; + int original_length_; + int raw_length_; + + DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeRequestHandler); +}; + +class WebSocketHandshakeResponseHandler { + public: + WebSocketHandshakeResponseHandler(); + ~WebSocketHandshakeResponseHandler() {} + + // Parses WebSocket handshake response from WebSocket server. + // Returns number of bytes in |data| used for WebSocket handshake response + // message, including response key. If it already got whole WebSocket + // handshake response message, returns zero. In other words, + // [data + returned value, data + length) will be WebSocket frame data + // after handshake response message. + // TODO(ukai): fail fast when response gives wrong status code. + size_t ParseRawResponse(const char* data, int length); + // Returns true if it already parses full handshake response message. + bool HasResponse() const; + // Parses WebSocket handshake response info given as HttpResponseInfo. + bool ParseResponseInfo(const HttpResponseInfo& response_info, + WebSocketHandshakeRequestHandler* request_handler); + + // Gets the headers value. + void GetHeaders(const char* const headers_to_get[], + size_t headers_to_get_len, + std::vector<std::string>* values); + // Removes the headers that matches (case insensitive). + void RemoveHeaders(const char* const headers_to_remove[], + size_t headers_to_remove_len); + + // Gets WebSocket handshake response message sent to renderer process. + std::string GetResponse(); + + private: + HttpResponseInfo response_info_; + std::string original_; + int original_header_length_; + std::string status_line_; + std::string headers_; + std::string key_; + + DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeResponseHandler); +}; + +} // namespace net + +#endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_HANDLER_H_ |