http_stream_factory.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright (c) 2012 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. #ifndef NET_HTTP_HTTP_STREAM_FACTORY_H_
  5. #define NET_HTTP_HTTP_STREAM_FACTORY_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include "base/containers/unique_ptr_adapters.h"
  12. #include "base/gtest_prod_util.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/ref_counted.h"
  15. #include "net/base/host_port_pair.h"
  16. #include "net/base/load_states.h"
  17. #include "net/base/net_export.h"
  18. #include "net/base/privacy_mode.h"
  19. #include "net/base/proxy_server.h"
  20. #include "net/base/request_priority.h"
  21. #include "net/http/http_request_info.h"
  22. #include "net/http/http_server_properties.h"
  23. #include "net/http/http_stream_request.h"
  24. #include "net/log/net_log_source.h"
  25. #include "net/log/net_log_with_source.h"
  26. #include "net/proxy_resolution/proxy_info.h"
  27. #include "net/socket/ssl_client_socket.h"
  28. #include "net/spdy/spdy_session_key.h"
  29. #include "net/ssl/ssl_config.h"
  30. #include "net/websockets/websocket_handshake_stream_base.h"
  31. namespace net {
  32. class HostMappingRules;
  33. class HttpNetworkSession;
  34. class HttpResponseHeaders;
  35. class NET_EXPORT HttpStreamFactory {
  36. public:
  37. class NET_EXPORT_PRIVATE Job;
  38. class NET_EXPORT_PRIVATE JobController;
  39. class NET_EXPORT_PRIVATE JobFactory;
  40. enum JobType {
  41. // Job that will connect via HTTP/1 or HTTP/2. This may be paused for a
  42. // while when ALTERNATIVE or DNS_ALPN_H3 job was created.
  43. MAIN,
  44. // Job that will connect via HTTP/3 iff Chrome has received an Alt-Svc
  45. // header from the origin.
  46. ALTERNATIVE,
  47. // Job that will connect via HTTP/3 iff an "h3" value was found in the ALPN
  48. // list of an HTTPS DNS record.
  49. DNS_ALPN_H3,
  50. // Job that will preconnect. This uses HTTP/3 iff Chrome has received an
  51. // Alt-Svc header from the origin. Otherwise, it use HTTP/1 or HTTP/2.
  52. PRECONNECT,
  53. // Job that will preconnect via HTTP/3 iff an "h3" value was found in the
  54. // ALPN list of an HTTPS DNS record.
  55. PRECONNECT_DNS_ALPN_H3,
  56. };
  57. explicit HttpStreamFactory(HttpNetworkSession* session);
  58. HttpStreamFactory(const HttpStreamFactory&) = delete;
  59. HttpStreamFactory& operator=(const HttpStreamFactory&) = delete;
  60. virtual ~HttpStreamFactory();
  61. void ProcessAlternativeServices(
  62. HttpNetworkSession* session,
  63. const net::NetworkIsolationKey& network_isolation_key,
  64. const HttpResponseHeaders* headers,
  65. const url::SchemeHostPort& http_server);
  66. // Request a stream.
  67. // Will call delegate->OnStreamReady on successful completion.
  68. std::unique_ptr<HttpStreamRequest> RequestStream(
  69. const HttpRequestInfo& info,
  70. RequestPriority priority,
  71. const SSLConfig& server_ssl_config,
  72. const SSLConfig& proxy_ssl_config,
  73. HttpStreamRequest::Delegate* delegate,
  74. bool enable_ip_based_pooling,
  75. bool enable_alternative_services,
  76. const NetLogWithSource& net_log);
  77. // Request a WebSocket handshake stream.
  78. // Will call delegate->OnWebSocketHandshakeStreamReady on successful
  79. // completion.
  80. std::unique_ptr<HttpStreamRequest> RequestWebSocketHandshakeStream(
  81. const HttpRequestInfo& info,
  82. RequestPriority priority,
  83. const SSLConfig& server_ssl_config,
  84. const SSLConfig& proxy_ssl_config,
  85. HttpStreamRequest::Delegate* delegate,
  86. WebSocketHandshakeStreamBase::CreateHelper* create_helper,
  87. bool enable_ip_based_pooling,
  88. bool enable_alternative_services,
  89. const NetLogWithSource& net_log);
  90. // Request a BidirectionalStreamImpl.
  91. // Will call delegate->OnBidirectionalStreamImplReady on successful
  92. // completion.
  93. // TODO(https://crbug.com/836823): This method is virtual to avoid cronet_test
  94. // failure on iOS that is caused by Network Thread TLS getting the wrong slot.
  95. virtual std::unique_ptr<HttpStreamRequest> RequestBidirectionalStreamImpl(
  96. const HttpRequestInfo& info,
  97. RequestPriority priority,
  98. const SSLConfig& server_ssl_config,
  99. const SSLConfig& proxy_ssl_config,
  100. HttpStreamRequest::Delegate* delegate,
  101. bool enable_ip_based_pooling,
  102. bool enable_alternative_services,
  103. const NetLogWithSource& net_log);
  104. // Requests that enough connections for |num_streams| be opened.
  105. void PreconnectStreams(int num_streams, const HttpRequestInfo& info);
  106. const HostMappingRules* GetHostMappingRules() const;
  107. private:
  108. FRIEND_TEST_ALL_PREFIXES(HttpStreamRequestTest, SetPriority);
  109. friend class HttpStreamFactoryPeer;
  110. using JobControllerSet =
  111. std::set<std::unique_ptr<JobController>, base::UniquePtrComparator>;
  112. url::SchemeHostPort RewriteHost(const url::SchemeHostPort& server);
  113. // Values must not be changed or reused. Keep in sync with identically named
  114. // enum in histograms.xml.
  115. enum AlternativeServiceType {
  116. NO_ALTERNATIVE_SERVICE = 0,
  117. QUIC_SAME_DESTINATION = 1,
  118. QUIC_DIFFERENT_DESTINATION = 2,
  119. NOT_QUIC_SAME_DESTINATION = 3,
  120. NOT_QUIC_DIFFERENT_DESTINATION = 4,
  121. MAX_ALTERNATIVE_SERVICE_TYPE
  122. };
  123. std::unique_ptr<HttpStreamRequest> RequestStreamInternal(
  124. const HttpRequestInfo& info,
  125. RequestPriority priority,
  126. const SSLConfig& server_ssl_config,
  127. const SSLConfig& proxy_ssl_config,
  128. HttpStreamRequest::Delegate* delegate,
  129. WebSocketHandshakeStreamBase::CreateHelper* create_helper,
  130. HttpStreamRequest::StreamType stream_type,
  131. bool is_websocket,
  132. bool enable_ip_based_pooling,
  133. bool enable_alternative_services,
  134. const NetLogWithSource& net_log);
  135. // Called when the Job detects that the endpoint indicated by the
  136. // Alternate-Protocol does not work. Lets the factory update
  137. // HttpAlternateProtocols with the failure and resets the SPDY session key.
  138. void OnBrokenAlternateProtocol(const Job*, const HostPortPair& origin);
  139. // Called when the Preconnect completes. Used for testing.
  140. virtual void OnPreconnectsCompleteInternal() {}
  141. // Called when the JobController finishes service. Delete the JobController
  142. // from |job_controller_set_|.
  143. void OnJobControllerComplete(JobController* controller);
  144. const raw_ptr<HttpNetworkSession> session_;
  145. // All Requests/Preconnects are assigned with a JobController to manage
  146. // serving Job(s). JobController might outlive Request when Request
  147. // is served while there's some working Job left. JobController will be
  148. // deleted from |job_controller_set_| when it determines the completion of
  149. // its work.
  150. JobControllerSet job_controller_set_;
  151. // Factory used by job controllers for creating jobs.
  152. std::unique_ptr<JobFactory> job_factory_;
  153. };
  154. } // namespace net
  155. #endif // NET_HTTP_HTTP_STREAM_FACTORY_H_