1 // Copyright (c) 2011 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_HTTP_HTTP_STREAM_FACTORY_H_ 6 #define NET_HTTP_HTTP_STREAM_FACTORY_H_ 7 8 #include <list> 9 #include <string> 10 11 #include "base/memory/ref_counted.h" 12 #include "base/string16.h" 13 #include "net/base/completion_callback.h" 14 #include "net/base/load_states.h" 15 16 class GURL; 17 18 namespace net { 19 20 class BoundNetLog; 21 class HostMappingRules; 22 class HostPortPair; 23 class HttpAlternateProtocols; 24 class HttpAuthController; 25 class HttpNetworkSession; 26 class HttpResponseInfo; 27 class HttpStream; 28 class ProxyInfo; 29 class SSLCertRequestInfo; 30 class SSLInfo; 31 class X509Certificate; 32 struct HttpRequestInfo; 33 struct SSLConfig; 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 HttpStreamRequest { 41 public: 42 // The HttpStreamRequest::Delegate is a set of callback methods for a 43 // HttpStreamRequestJob. Generally, only one of these methods will be 44 // called as a result of a stream request. 45 class Delegate { 46 public: ~Delegate()47 virtual ~Delegate() {} 48 49 // This is the success case. 50 // |stream| is now owned by the delegate. 51 // |used_ssl_config| indicates the actual SSL configuration used for this 52 // stream, since the HttpStreamRequest may have modified the configuration 53 // during stream processing. 54 // |used_proxy_info| indicates the actual ProxyInfo used for this stream, 55 // since the HttpStreamRequest performs the proxy resolution. 56 virtual void OnStreamReady( 57 const SSLConfig& used_ssl_config, 58 const ProxyInfo& used_proxy_info, 59 HttpStream* stream) = 0; 60 61 // This is the failure to create a stream case. 62 // |used_ssl_config| indicates the actual SSL configuration used for this 63 // stream, since the HttpStreamRequest may have modified the configuration 64 // during stream processing. 65 virtual void OnStreamFailed(int status, 66 const SSLConfig& used_ssl_config) = 0; 67 68 // Called when we have a certificate error for the request. 69 // |used_ssl_config| indicates the actual SSL configuration used for this 70 // stream, since the HttpStreamRequest may have modified the configuration 71 // during stream processing. 72 virtual void OnCertificateError(int status, 73 const SSLConfig& used_ssl_config, 74 const SSLInfo& ssl_info) = 0; 75 76 // This is the failure case where we need proxy authentication during 77 // proxy tunnel establishment. For the tunnel case, we were unable to 78 // create the HttpStream, so the caller provides the auth and then resumes 79 // the HttpStreamRequest. 80 // 81 // For the non-tunnel case, the caller will discover the authentication 82 // failure when reading response headers. At that point, he will handle the 83 // authentication failure and restart the HttpStreamRequest entirely. 84 // 85 // Ownership of |auth_controller| and |proxy_response| are owned 86 // by the HttpStreamRequest. |proxy_response| is not guaranteed to be usable 87 // after the lifetime of this callback. The delegate may take a reference 88 // to |auth_controller| if it is needed beyond the lifetime of this 89 // callback. 90 // 91 // |used_ssl_config| indicates the actual SSL configuration used for this 92 // stream, since the HttpStreamRequest may have modified the configuration 93 // during stream processing. 94 virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response, 95 const SSLConfig& used_ssl_config, 96 const ProxyInfo& used_proxy_info, 97 HttpAuthController* auth_controller) = 0; 98 99 // This is the failure for SSL Client Auth 100 // Ownership of |cert_info| is retained by the HttpStreamRequest. The 101 // delegate may take a reference if it needs the cert_info beyond the 102 // lifetime of this callback. 103 virtual void OnNeedsClientAuth(const SSLConfig& used_ssl_config, 104 SSLCertRequestInfo* cert_info) = 0; 105 106 // This is the failure of the CONNECT request through an HTTPS proxy. 107 // Headers can be read from |response_info|, while the body can be read 108 // from |stream|. 109 // 110 // |used_ssl_config| indicates the actual SSL configuration used for this 111 // stream, since the HttpStreamRequest may have modified the configuration 112 // during stream processing. 113 // 114 // |used_proxy_info| indicates the actual ProxyInfo used for this stream, 115 // since the HttpStreamRequest performs the proxy resolution. 116 // 117 // Ownership of |stream| is transferred to the delegate. 118 virtual void OnHttpsProxyTunnelResponse( 119 const HttpResponseInfo& response_info, 120 const SSLConfig& used_ssl_config, 121 const ProxyInfo& used_proxy_info, 122 HttpStream* stream) = 0; 123 }; 124 ~HttpStreamRequest()125 virtual ~HttpStreamRequest() {} 126 127 // When a HttpStream creation process is stalled due to necessity 128 // of Proxy authentication credentials, the delegate OnNeedsProxyAuth 129 // will have been called. It now becomes the delegate's responsibility 130 // to collect the necessary credentials, and then call this method to 131 // resume the HttpStream creation process. 132 virtual int RestartTunnelWithProxyAuth(const string16& username, 133 const string16& password) = 0; 134 135 // Returns the LoadState for the request. 136 virtual LoadState GetLoadState() const = 0; 137 138 // Returns true if TLS/NPN was negotiated for this stream. 139 virtual bool was_npn_negotiated() const = 0; 140 141 // Returns true if this stream is being fetched over SPDY. 142 virtual bool using_spdy() const = 0; 143 }; 144 145 // The HttpStreamFactory defines an interface for creating usable HttpStreams. 146 class HttpStreamFactory { 147 public: 148 virtual ~HttpStreamFactory(); 149 150 void ProcessAlternateProtocol( 151 HttpAlternateProtocols* alternate_protocols, 152 const std::string& alternate_protocol_str, 153 const HostPortPair& http_host_port_pair); 154 155 // Virtual interface methods. 156 157 // Request a stream. 158 // Will callback to the HttpStreamRequestDelegate upon completion. 159 virtual HttpStreamRequest* RequestStream( 160 const HttpRequestInfo& info, 161 const SSLConfig& ssl_config, 162 HttpStreamRequest::Delegate* delegate, 163 const BoundNetLog& net_log) = 0; 164 165 // Requests that enough connections for |num_streams| be opened. 166 virtual void PreconnectStreams(int num_streams, 167 const HttpRequestInfo& info, 168 const SSLConfig& ssl_config, 169 const BoundNetLog& net_log) = 0; 170 171 virtual void AddTLSIntolerantServer(const HostPortPair& server) = 0; 172 virtual bool IsTLSIntolerantServer(const HostPortPair& server) const = 0; 173 174 // Static settings 175 static GURL ApplyHostMappingRules(const GURL& url, HostPortPair* endpoint); 176 177 // Turns spdy on or off. set_spdy_enabled(bool value)178 static void set_spdy_enabled(bool value) { 179 spdy_enabled_ = value; 180 if (value == false) 181 set_next_protos(""); 182 } spdy_enabled()183 static bool spdy_enabled() { return spdy_enabled_; } 184 185 // Controls whether or not we use the Alternate-Protocol header. set_use_alternate_protocols(bool value)186 static void set_use_alternate_protocols(bool value) { 187 use_alternate_protocols_ = value; 188 } use_alternate_protocols()189 static bool use_alternate_protocols() { return use_alternate_protocols_; } 190 191 // Controls whether or not we use ssl when in spdy mode. set_force_spdy_over_ssl(bool value)192 static void set_force_spdy_over_ssl(bool value) { 193 force_spdy_over_ssl_ = value; 194 } force_spdy_over_ssl()195 static bool force_spdy_over_ssl() { 196 return force_spdy_over_ssl_; 197 } 198 199 // Controls whether or not we use spdy without npn. set_force_spdy_always(bool value)200 static void set_force_spdy_always(bool value) { 201 force_spdy_always_ = value; 202 } force_spdy_always()203 static bool force_spdy_always() { return force_spdy_always_; } 204 205 // Add a URL to exclude from forced SPDY. 206 static void add_forced_spdy_exclusion(const std::string& value); 207 // Check if a HostPortPair is excluded from using spdy. 208 static bool HasSpdyExclusion(const HostPortPair& endpoint); 209 210 // Sets the next protocol negotiation value used during the SSL handshake. set_next_protos(const std::string & value)211 static void set_next_protos(const std::string& value) { 212 delete next_protos_; 213 next_protos_ = new std::string(value); 214 } next_protos()215 static const std::string* next_protos() { return next_protos_; } 216 217 // Sets the HttpStreamFactoryImpl into a mode where it can ignore certificate 218 // errors. This is for testing. set_ignore_certificate_errors(bool value)219 static void set_ignore_certificate_errors(bool value) { 220 ignore_certificate_errors_ = value; 221 } ignore_certificate_errors()222 static bool ignore_certificate_errors() { 223 return ignore_certificate_errors_; 224 } 225 226 static void SetHostMappingRules(const std::string& rules); 227 228 protected: 229 HttpStreamFactory(); 230 231 private: 232 static const HostMappingRules& host_mapping_rules(); 233 234 static const HostMappingRules* host_mapping_rules_; 235 static const std::string* next_protos_; 236 static bool spdy_enabled_; 237 static bool use_alternate_protocols_; 238 static bool force_spdy_over_ssl_; 239 static bool force_spdy_always_; 240 static std::list<HostPortPair>* forced_spdy_exclusions_; 241 static bool ignore_certificate_errors_; 242 243 DISALLOW_COPY_AND_ASSIGN(HttpStreamFactory); 244 }; 245 246 } // namespace net 247 248 #endif // NET_HTTP_HTTP_STREAM_FACTORY_H_ 249