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