1 // Copyright 2012 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_HTTP_HTTP_STREAM_REQUEST_H_ 6 #define NET_HTTP_HTTP_STREAM_REQUEST_H_ 7 8 #include <memory> 9 10 #include "base/memory/raw_ptr.h" 11 #include "net/base/load_states.h" 12 #include "net/base/net_error_details.h" 13 #include "net/base/net_export.h" 14 #include "net/base/request_priority.h" 15 #include "net/http/http_response_info.h" 16 #include "net/log/net_log_source.h" 17 #include "net/log/net_log_with_source.h" 18 #include "net/proxy_resolution/proxy_info.h" 19 #include "net/socket/connection_attempts.h" 20 #include "net/socket/next_proto.h" 21 #include "net/spdy/spdy_session_key.h" 22 #include "net/spdy/spdy_session_pool.h" 23 #include "net/ssl/ssl_config.h" 24 #include "net/ssl/ssl_info.h" 25 #include "net/websockets/websocket_handshake_stream_base.h" 26 #include "url/gurl.h" 27 28 namespace net { 29 30 class BidirectionalStreamImpl; 31 class HttpAuthController; 32 class HttpStream; 33 class SSLCertRequestInfo; 34 35 // The HttpStreamRequest is the client's handle to the worker object which 36 // handles the creation of an HttpStream. While the HttpStream is being 37 // created, this object is the creator's handle for interacting with the 38 // HttpStream creation process. The request is cancelled by deleting it, after 39 // which no callbacks will be invoked. 40 class NET_EXPORT_PRIVATE HttpStreamRequest { 41 public: 42 // Indicates which type of stream is requested. 43 enum StreamType { 44 BIDIRECTIONAL_STREAM, 45 HTTP_STREAM, 46 }; 47 48 // The HttpStreamRequest::Delegate is a set of callback methods for a 49 // HttpStreamRequestJob. Generally, only one of these methods will be 50 // called as a result of a stream request. 51 class NET_EXPORT_PRIVATE Delegate { 52 public: 53 virtual ~Delegate() = default; 54 55 // This is the success case for RequestStream. 56 // |stream| is now owned by the delegate. 57 // |used_ssl_config| indicates the actual SSL configuration used for this 58 // stream, since the HttpStreamRequest may have modified the configuration 59 // during stream processing. 60 // |used_proxy_info| indicates the actual ProxyInfo used for this stream, 61 // since the HttpStreamRequest performs the proxy resolution. 62 virtual void OnStreamReady(const SSLConfig& used_ssl_config, 63 const ProxyInfo& used_proxy_info, 64 std::unique_ptr<HttpStream> stream) = 0; 65 66 // This is the success case for RequestWebSocketHandshakeStream. 67 // |stream| is now owned by the delegate. 68 // |used_ssl_config| indicates the actual SSL configuration used for this 69 // stream, since the HttpStreamRequest may have modified the configuration 70 // during stream processing. 71 // |used_proxy_info| indicates the actual ProxyInfo used for this stream, 72 // since the HttpStreamRequest performs the proxy resolution. 73 virtual void OnWebSocketHandshakeStreamReady( 74 const SSLConfig& used_ssl_config, 75 const ProxyInfo& used_proxy_info, 76 std::unique_ptr<WebSocketHandshakeStreamBase> stream) = 0; 77 78 virtual void OnBidirectionalStreamImplReady( 79 const SSLConfig& used_ssl_config, 80 const ProxyInfo& used_proxy_info, 81 std::unique_ptr<BidirectionalStreamImpl> stream) = 0; 82 83 // This is the failure to create a stream case. 84 // |used_ssl_config| indicates the actual SSL configuration used for this 85 // stream, since the HttpStreamRequest may have modified the configuration 86 // during stream processing. 87 // |used_proxy_info| indicates the actual ProxyInfo used for this stream, 88 // since the HttpStreamRequest performs the proxy resolution. 89 virtual void OnStreamFailed(int status, 90 const NetErrorDetails& net_error_details, 91 const SSLConfig& used_ssl_config, 92 const ProxyInfo& used_proxy_info, 93 ResolveErrorInfo resolve_error_info) = 0; 94 95 // Called when we have a certificate error for the request. 96 // |used_ssl_config| indicates the actual SSL configuration used for this 97 // stream, since the HttpStreamRequest may have modified the configuration 98 // during stream processing. 99 virtual void OnCertificateError(int status, 100 const SSLConfig& used_ssl_config, 101 const SSLInfo& ssl_info) = 0; 102 103 // This is the failure case where we need proxy authentication during 104 // proxy tunnel establishment. For the tunnel case, we were unable to 105 // create the HttpStream, so the caller provides the auth and then resumes 106 // the HttpStreamRequest. 107 // 108 // For the non-tunnel case, the caller will discover the authentication 109 // failure when reading response headers. At that point, it will handle the 110 // authentication failure and restart the HttpStreamRequest entirely. 111 // 112 // Ownership of |auth_controller| and |proxy_response| are owned 113 // by the HttpStreamRequest. |proxy_response| is not guaranteed to be usable 114 // after the lifetime of this callback. The delegate may take a reference 115 // to |auth_controller| if it is needed beyond the lifetime of this 116 // callback. 117 // 118 // |used_ssl_config| indicates the actual SSL configuration used for this 119 // stream, since the HttpStreamRequest may have modified the configuration 120 // during stream processing. 121 virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response, 122 const SSLConfig& used_ssl_config, 123 const ProxyInfo& used_proxy_info, 124 HttpAuthController* auth_controller) = 0; 125 126 // This is the failure for SSL Client Auth 127 // Ownership of |cert_info| is retained by the HttpStreamRequest. The 128 // delegate may take a reference if it needs the cert_info beyond the 129 // lifetime of this callback. 130 virtual void OnNeedsClientAuth(const SSLConfig& used_ssl_config, 131 SSLCertRequestInfo* cert_info) = 0; 132 133 // Called when finding all QUIC alternative services are marked broken for 134 // the origin in this request which advertises supporting QUIC. 135 virtual void OnQuicBroken() = 0; 136 }; 137 138 class NET_EXPORT_PRIVATE Helper { 139 public: 140 virtual ~Helper() = default; 141 142 // Returns the LoadState for Request. 143 virtual LoadState GetLoadState() const = 0; 144 145 // Called when Request is destructed. 146 virtual void OnRequestComplete() = 0; 147 148 // Called to resume the HttpStream creation process when necessary 149 // Proxy authentication credentials are collected. 150 virtual int RestartTunnelWithProxyAuth() = 0; 151 152 // Called when the priority of transaction changes. 153 virtual void SetPriority(RequestPriority priority) = 0; 154 }; 155 156 // Request will notify |job_controller| when it's destructed. 157 // Thus |job_controller| is valid for the lifetime of the |this| Request. 158 HttpStreamRequest(const GURL& url, 159 Helper* helper, 160 HttpStreamRequest::Delegate* delegate, 161 WebSocketHandshakeStreamBase::CreateHelper* 162 websocket_handshake_stream_create_helper, 163 const NetLogWithSource& net_log, 164 StreamType stream_type); 165 166 HttpStreamRequest(const HttpStreamRequest&) = delete; 167 HttpStreamRequest& operator=(const HttpStreamRequest&) = delete; 168 169 ~HttpStreamRequest(); 170 171 // When a HttpStream creation process is stalled due to necessity 172 // of Proxy authentication credentials, the delegate OnNeedsProxyAuth 173 // will have been called. It now becomes the delegate's responsibility 174 // to collect the necessary credentials, and then call this method to 175 // resume the HttpStream creation process. 176 int RestartTunnelWithProxyAuth(); 177 178 // Called when the priority of the parent transaction changes. 179 void SetPriority(RequestPriority priority); 180 181 // Marks completion of the request. Must be called before OnStreamReady(). 182 void Complete(bool was_alpn_negotiated, 183 NextProto negotiated_protocol, 184 AlternateProtocolUsage alternate_protocol_usage, 185 bool using_spdy); 186 187 // Called by |helper_| to record connection attempts made by the socket 188 // layer in an attached Job for this stream request. 189 void AddConnectionAttempts(const ConnectionAttempts& attempts); 190 191 // Returns the LoadState for the request. 192 LoadState GetLoadState() const; 193 194 // Returns true if TLS/ALPN was negotiated for this stream. 195 bool was_alpn_negotiated() const; 196 197 // Protocol negotiated with the server. 198 NextProto negotiated_protocol() const; 199 200 // The reason why Chrome uses a specific transport protocol for HTTP 201 // semantics. 202 AlternateProtocolUsage alternate_protocol_usage() const; 203 204 // Returns true if this stream is being fetched over SPDY. 205 bool using_spdy() const; 206 207 // Returns socket-layer connection attempts made for this stream request. 208 const ConnectionAttempts& connection_attempts() const; 209 210 // Returns the WebSocketHandshakeStreamBase::CreateHelper for this stream 211 // request. 212 WebSocketHandshakeStreamBase::CreateHelper* 213 websocket_handshake_stream_create_helper() const; 214 215 // The GURL from the HttpRequestInfo the started the Request. url()216 const GURL& url() const { return url_; } 217 net_log()218 const NetLogWithSource& net_log() const { return net_log_; } 219 stream_type()220 StreamType stream_type() const { return stream_type_; } 221 completed()222 bool completed() const { return completed_; } 223 224 private: 225 const GURL url_; 226 227 // Unowned. The helper must not be destroyed before this object is. 228 raw_ptr<Helper> helper_; 229 230 const raw_ptr<WebSocketHandshakeStreamBase::CreateHelper> 231 websocket_handshake_stream_create_helper_; 232 const NetLogWithSource net_log_; 233 234 bool completed_ = false; 235 bool was_alpn_negotiated_ = false; 236 // Protocol negotiated with the server. 237 NextProto negotiated_protocol_ = kProtoUnknown; 238 // The reason why Chrome uses a specific transport protocol for HTTP 239 // semantics. 240 AlternateProtocolUsage alternate_protocol_usage_ = 241 AlternateProtocolUsage::ALTERNATE_PROTOCOL_USAGE_UNSPECIFIED_REASON; 242 bool using_spdy_ = false; 243 ConnectionAttempts connection_attempts_; 244 const StreamType stream_type_; 245 }; 246 247 } // namespace net 248 249 #endif // NET_HTTP_HTTP_STREAM_REQUEST_H_ 250