1 // Copyright 2013 The Chromium Authors 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_BASIC_HANDSHAKE_STREAM_H_ 6 #define NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <set> 12 #include <string> 13 #include <vector> 14 15 #include "base/memory/raw_ptr.h" 16 #include "base/strings/string_piece.h" 17 #include "net/base/completion_once_callback.h" 18 #include "net/base/net_export.h" 19 #include "net/http/http_basic_state.h" 20 #include "net/log/net_log_with_source.h" 21 #include "net/websockets/websocket_handshake_stream_base.h" 22 #include "third_party/abseil-cpp/absl/types/optional.h" 23 #include "url/gurl.h" 24 25 namespace net { 26 27 class ClientSocketHandle; 28 class HttpResponseHeaders; 29 class HttpResponseInfo; 30 class HttpStreamParser; 31 class WebSocketEndpointLockManager; 32 struct WebSocketExtensionParams; 33 class WebSocketStreamRequestAPI; 34 35 class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream final 36 : public WebSocketHandshakeStreamBase { 37 public: 38 // |connect_delegate| and |failure_message| must out-live this object. 39 WebSocketBasicHandshakeStream( 40 std::unique_ptr<ClientSocketHandle> connection, 41 WebSocketStream::ConnectDelegate* connect_delegate, 42 bool using_proxy, 43 std::vector<std::string> requested_sub_protocols, 44 std::vector<std::string> requested_extensions, 45 WebSocketStreamRequestAPI* request, 46 WebSocketEndpointLockManager* websocket_endpoint_lock_manager); 47 48 WebSocketBasicHandshakeStream(const WebSocketBasicHandshakeStream&) = delete; 49 WebSocketBasicHandshakeStream& operator=( 50 const WebSocketBasicHandshakeStream&) = delete; 51 52 ~WebSocketBasicHandshakeStream() override; 53 54 // HttpStreamBase methods 55 void RegisterRequest(const HttpRequestInfo* request_info) override; 56 int InitializeStream(bool can_send_early, 57 RequestPriority priority, 58 const NetLogWithSource& net_log, 59 CompletionOnceCallback callback) override; 60 int SendRequest(const HttpRequestHeaders& request_headers, 61 HttpResponseInfo* response, 62 CompletionOnceCallback callback) override; 63 int ReadResponseHeaders(CompletionOnceCallback callback) override; 64 int ReadResponseBody(IOBuffer* buf, 65 int buf_len, 66 CompletionOnceCallback callback) override; 67 void Close(bool not_reusable) override; 68 bool IsResponseBodyComplete() const override; 69 bool IsConnectionReused() const override; 70 void SetConnectionReused() override; 71 bool CanReuseConnection() const override; 72 int64_t GetTotalReceivedBytes() const override; 73 int64_t GetTotalSentBytes() const override; 74 bool GetAlternativeService( 75 AlternativeService* alternative_service) const override; 76 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override; 77 void GetSSLInfo(SSLInfo* ssl_info) override; 78 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override; 79 int GetRemoteEndpoint(IPEndPoint* endpoint) override; 80 void Drain(HttpNetworkSession* session) override; 81 void SetPriority(RequestPriority priority) override; 82 void PopulateNetErrorDetails(NetErrorDetails* details) override; 83 std::unique_ptr<HttpStream> RenewStreamForAuth() override; 84 const std::set<std::string>& GetDnsAliases() const override; 85 base::StringPiece GetAcceptChViaAlps() const override; 86 87 // This is called from the top level once correct handshake response headers 88 // have been received. It creates an appropriate subclass of WebSocketStream 89 // depending on what extensions were negotiated. This object is unusable after 90 // Upgrade() has been called and should be disposed of as soon as possible. 91 std::unique_ptr<WebSocketStream> Upgrade() override; 92 93 base::WeakPtr<WebSocketHandshakeStreamBase> GetWeakPtr() override; 94 95 // Set the value used for the next Sec-WebSocket-Key header 96 // deterministically. The key is only used once, and then discarded. 97 // For tests only. 98 void SetWebSocketKeyForTesting(const std::string& key); 99 100 private: 101 // A wrapper for the ReadResponseHeaders callback that checks whether or not 102 // the connection has been accepted. 103 void ReadResponseHeadersCallback(CompletionOnceCallback callback, int result); 104 105 // Validates the response and sends the finished handshake event. 106 int ValidateResponse(int rv); 107 108 // Check that the headers are well-formed for a 101 response, and returns 109 // OK if they are, otherwise returns ERR_INVALID_RESPONSE. 110 int ValidateUpgradeResponse(const HttpResponseHeaders* headers); 111 112 void OnFailure(const std::string& message, 113 int net_error, 114 absl::optional<int> response_code); 115 parser()116 HttpStreamParser* parser() const { return state_.parser(); } 117 118 HandshakeResult result_ = HandshakeResult::INCOMPLETE; 119 120 // The request URL. 121 GURL url_; 122 123 // HttpBasicState holds most of the handshake-related state. 124 HttpBasicState state_; 125 126 // Owned by another object. 127 // |connect_delegate| will live during the lifetime of this object. 128 const raw_ptr<WebSocketStream::ConnectDelegate, DanglingUntriaged> 129 connect_delegate_; 130 131 // This is stored in SendRequest() for use by ReadResponseHeaders(). 132 raw_ptr<HttpResponseInfo> http_response_info_ = nullptr; 133 134 // The key to be sent in the next Sec-WebSocket-Key header. Usually NULL (the 135 // key is generated on the fly). 136 absl::optional<std::string> handshake_challenge_for_testing_; 137 138 // The required value for the Sec-WebSocket-Accept header. 139 std::string handshake_challenge_response_; 140 141 // The sub-protocols we requested. 142 std::vector<std::string> requested_sub_protocols_; 143 144 // The extensions we requested. 145 std::vector<std::string> requested_extensions_; 146 147 // The sub-protocol selected by the server. 148 std::string sub_protocol_; 149 150 // The extension(s) selected by the server. 151 std::string extensions_; 152 153 // The extension parameters. The class is defined in the implementation file 154 // to avoid including extension-related header files here. 155 std::unique_ptr<WebSocketExtensionParams> extension_params_; 156 157 const raw_ptr<WebSocketStreamRequestAPI, DanglingUntriaged> stream_request_; 158 159 const raw_ptr<WebSocketEndpointLockManager> websocket_endpoint_lock_manager_; 160 161 NetLogWithSource net_log_; 162 163 // The request to send. 164 // Set to null before the response body is read. This is to allow |this| to 165 // be shared for reading and to possibly outlive request_info_'s owner. 166 // Setting to null happens after headers are completely read or upload data 167 // stream is uploaded, whichever is later. 168 raw_ptr<const HttpRequestInfo> request_info_; 169 170 base::WeakPtrFactory<WebSocketBasicHandshakeStream> weak_ptr_factory_{this}; 171 }; 172 173 } // namespace net 174 175 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_HANDSHAKE_STREAM_H_ 176