[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 1 | // Copyright 2013 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 | #ifndef NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_ |
| 6 | #define NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_ |
| 7 | |
| 8 | // This file is included from net/http files. |
| 9 | // Since net/http can be built without linking net/websockets code, |
| 10 | // this file must not introduce any link-time dependencies on websockets. |
| 11 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 12 | #include <memory> |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 13 | #include <string> |
Bence Béky | b28709c2 | 2018-03-06 13:03:44 | [diff] [blame^] | 14 | #include <vector> |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 15 | |
tfarina | ea94afc23 | 2015-10-20 04:23:36 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
[email protected] | f4533ba | 2013-11-28 09:35:41 | [diff] [blame] | 18 | #include "base/supports_user_data.h" |
bnc | 81c46c1f | 2016-10-04 16:25:59 | [diff] [blame] | 19 | #include "net/base/net_export.h" |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 20 | #include "net/http/http_stream.h" |
[email protected] | f4533ba | 2013-11-28 09:35:41 | [diff] [blame] | 21 | #include "net/url_request/websocket_handshake_userdata_key.h" |
Bence Béky | b28709c2 | 2018-03-06 13:03:44 | [diff] [blame^] | 22 | #include "net/websockets/websocket_deflate_parameters.h" |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 23 | #include "net/websockets/websocket_stream.h" |
| 24 | |
| 25 | namespace net { |
| 26 | |
| 27 | class ClientSocketHandle; |
Bence Béky | 46bfbc1 | 2018-02-22 19:28:20 | [diff] [blame] | 28 | class SpdySession; |
Bence Béky | b28709c2 | 2018-03-06 13:03:44 | [diff] [blame^] | 29 | class HttpRequestHeaders; |
| 30 | class HttpResponseHeaders; |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 31 | |
| 32 | // WebSocketHandshakeStreamBase is the base class of |
| 33 | // WebSocketBasicHandshakeStream. net/http code uses this interface to handle |
| 34 | // WebSocketBasicHandshakeStream when it needs to be treated differently from |
| 35 | // HttpStreamBase. |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 36 | class NET_EXPORT WebSocketHandshakeStreamBase : public HttpStream { |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 37 | public: |
Bence Béky | b28709c2 | 2018-03-06 13:03:44 | [diff] [blame^] | 38 | WebSocketHandshakeStreamBase() = default; |
| 39 | ~WebSocketHandshakeStreamBase() override = default; |
| 40 | |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 41 | // An object that stores data needed for the creation of a |
| 42 | // WebSocketBasicHandshakeStream object. A new CreateHelper is used for each |
| 43 | // WebSocket connection. |
| 44 | class NET_EXPORT_PRIVATE CreateHelper : public base::SupportsUserData::Data { |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 45 | public: |
[email protected] | f4533ba | 2013-11-28 09:35:41 | [diff] [blame] | 46 | // Returns a key to use to lookup this object in a URLRequest object. It is |
| 47 | // different from any other key that is supplied to |
| 48 | // URLRequest::SetUserData(). |
| 49 | static const void* DataKey() { return kWebSocketHandshakeUserDataKey; } |
| 50 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 51 | ~CreateHelper() override {} |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 52 | |
[email protected] | 7e841a5 | 2013-11-22 09:04:21 | [diff] [blame] | 53 | // Create a WebSocketBasicHandshakeStream. This is called after the |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 54 | // underlying connection has been established but before any handshake data |
[email protected] | efa9e73 | 2013-11-29 02:55:05 | [diff] [blame] | 55 | // has been transferred. This can be called more than once in the case that |
| 56 | // HTTP authentication is needed. |
bnc | 615cf2f | 2017-05-19 18:53:26 | [diff] [blame] | 57 | virtual std::unique_ptr<WebSocketHandshakeStreamBase> CreateBasicStream( |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 58 | std::unique_ptr<ClientSocketHandle> connection, |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 59 | bool using_proxy) = 0; |
Bence Béky | 46bfbc1 | 2018-02-22 19:28:20 | [diff] [blame] | 60 | |
| 61 | // Create a WebSocketHttp2HandshakeStream. This is called after the |
| 62 | // underlying HTTP/2 connection has been established but before the stream |
| 63 | // has been opened. This cannot be called more than once. |
| 64 | virtual std::unique_ptr<WebSocketHandshakeStreamBase> CreateHttp2Stream( |
| 65 | base::WeakPtr<SpdySession> session) = 0; |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 66 | }; |
| 67 | |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 68 | // After the handshake has completed, this method creates a WebSocketStream |
| 69 | // (of the appropriate type) from the WebSocketHandshakeStreamBase object. |
| 70 | // The WebSocketHandshakeStreamBase object is unusable after Upgrade() has |
| 71 | // been called. |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 72 | virtual std::unique_ptr<WebSocketStream> Upgrade() = 0; |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 73 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 74 | void SetRequestHeadersCallback(RequestHeadersCallback callback) override {} |
| 75 | |
Bence Béky | b28709c2 | 2018-03-06 13:03:44 | [diff] [blame^] | 76 | static std::string MultipleHeaderValuesMessage( |
| 77 | const std::string& header_name); |
| 78 | |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 79 | protected: |
Bence Béky | b28709c2 | 2018-03-06 13:03:44 | [diff] [blame^] | 80 | // TODO(ricea): If more extensions are added, replace this with a more general |
| 81 | // mechanism. |
| 82 | struct WebSocketExtensionParams { |
| 83 | bool deflate_enabled = false; |
| 84 | WebSocketDeflateParameters deflate_parameters; |
| 85 | }; |
| 86 | |
| 87 | static void AddVectorHeaderIfNonEmpty(const char* name, |
| 88 | const std::vector<std::string>& value, |
| 89 | HttpRequestHeaders* headers); |
| 90 | |
| 91 | static bool ValidateSubProtocol( |
| 92 | const HttpResponseHeaders* headers, |
| 93 | const std::vector<std::string>& requested_sub_protocols, |
| 94 | std::string* sub_protocol, |
| 95 | std::string* failure_message); |
| 96 | |
| 97 | static bool ValidateExtensions(const HttpResponseHeaders* headers, |
| 98 | std::string* accepted_extensions_descriptor, |
| 99 | std::string* failure_message, |
| 100 | WebSocketExtensionParams* params); |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 101 | |
| 102 | private: |
| 103 | DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeStreamBase); |
| 104 | }; |
| 105 | |
| 106 | } // namespace net |
| 107 | |
| 108 | #endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_ |