blob: d8abbc40725efe3c9dca8190bf2e2ec997a5bcd2 [file] [log] [blame]
[email protected]a9cf2b92013-10-30 12:08:491// 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
danakj9c5cab52016-04-16 00:54:3312#include <memory>
[email protected]96868202014-01-09 10:38:0413#include <string>
Bence Békyb28709c22018-03-06 13:03:4414#include <vector>
[email protected]96868202014-01-09 10:38:0415
tfarinaea94afc232015-10-20 04:23:3616#include "base/macros.h"
[email protected]a9cf2b92013-10-30 12:08:4917#include "base/memory/weak_ptr.h"
[email protected]f4533ba2013-11-28 09:35:4118#include "base/supports_user_data.h"
bnc81c46c1f2016-10-04 16:25:5919#include "net/base/net_export.h"
yhiranoa7e05bb2014-11-06 05:40:3920#include "net/http/http_stream.h"
[email protected]f4533ba2013-11-28 09:35:4121#include "net/url_request/websocket_handshake_userdata_key.h"
Bence Békyb28709c22018-03-06 13:03:4422#include "net/websockets/websocket_deflate_parameters.h"
[email protected]a9cf2b92013-10-30 12:08:4923#include "net/websockets/websocket_stream.h"
24
25namespace net {
26
27class ClientSocketHandle;
Bence Béky46bfbc12018-02-22 19:28:2028class SpdySession;
Bence Békyb28709c22018-03-06 13:03:4429class HttpRequestHeaders;
30class HttpResponseHeaders;
[email protected]a9cf2b92013-10-30 12:08:4931
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.
yhiranoa7e05bb2014-11-06 05:40:3936class NET_EXPORT WebSocketHandshakeStreamBase : public HttpStream {
[email protected]a9cf2b92013-10-30 12:08:4937 public:
Bence Békyb28709c22018-03-06 13:03:4438 WebSocketHandshakeStreamBase() = default;
39 ~WebSocketHandshakeStreamBase() override = default;
40
[email protected]efa9e732013-11-29 02:55:0541 // 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]a9cf2b92013-10-30 12:08:4945 public:
[email protected]f4533ba2013-11-28 09:35:4146 // 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
dchengb03027d2014-10-21 12:00:2051 ~CreateHelper() override {}
[email protected]a9cf2b92013-10-30 12:08:4952
[email protected]7e841a52013-11-22 09:04:2153 // Create a WebSocketBasicHandshakeStream. This is called after the
[email protected]a9cf2b92013-10-30 12:08:4954 // underlying connection has been established but before any handshake data
[email protected]efa9e732013-11-29 02:55:0555 // has been transferred. This can be called more than once in the case that
56 // HTTP authentication is needed.
bnc615cf2f2017-05-19 18:53:2657 virtual std::unique_ptr<WebSocketHandshakeStreamBase> CreateBasicStream(
danakj9c5cab52016-04-16 00:54:3358 std::unique_ptr<ClientSocketHandle> connection,
[email protected]a9cf2b92013-10-30 12:08:4959 bool using_proxy) = 0;
Bence Béky46bfbc12018-02-22 19:28:2060
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]a9cf2b92013-10-30 12:08:4966 };
67
[email protected]a9cf2b92013-10-30 12:08:4968 // 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.
danakj9c5cab52016-04-16 00:54:3372 virtual std::unique_ptr<WebSocketStream> Upgrade() = 0;
[email protected]a9cf2b92013-10-30 12:08:4973
Andrey Kosyakov83a6eee2017-08-14 19:20:0474 void SetRequestHeadersCallback(RequestHeadersCallback callback) override {}
75
Bence Békyb28709c22018-03-06 13:03:4476 static std::string MultipleHeaderValuesMessage(
77 const std::string& header_name);
78
[email protected]a9cf2b92013-10-30 12:08:4979 protected:
Bence Békyb28709c22018-03-06 13:03:4480 // 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]a9cf2b92013-10-30 12:08:49101
102 private:
103 DISALLOW_COPY_AND_ASSIGN(WebSocketHandshakeStreamBase);
104};
105
106} // namespace net
107
108#endif // NET_WEBSOCKETS_WEBSOCKET_HANDSHAKE_STREAM_BASE_H_