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