xref: /aosp_15_r20/external/cronet/net/http/http_stream_factory.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 #include <vector>
15 
16 #include "base/containers/unique_ptr_adapters.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/memory/raw_ptr.h"
19 #include "net/base/host_port_pair.h"
20 #include "net/base/load_states.h"
21 #include "net/base/net_export.h"
22 #include "net/base/network_anonymization_key.h"
23 #include "net/base/privacy_mode.h"
24 #include "net/base/proxy_server.h"
25 #include "net/base/request_priority.h"
26 #include "net/dns/public/secure_dns_policy.h"
27 #include "net/http/http_request_info.h"
28 #include "net/http/http_server_properties.h"
29 #include "net/http/http_stream_request.h"
30 #include "net/log/net_log_source.h"
31 #include "net/log/net_log_with_source.h"
32 #include "net/proxy_resolution/proxy_info.h"
33 #include "net/socket/socket_tag.h"
34 #include "net/socket/ssl_client_socket.h"
35 #include "net/spdy/spdy_session_key.h"
36 #include "net/ssl/ssl_config.h"
37 #include "net/websockets/websocket_handshake_stream_base.h"
38 
39 namespace net {
40 
41 class HostMappingRules;
42 class HttpNetworkSession;
43 class HttpResponseHeaders;
44 
45 class NET_EXPORT HttpStreamFactory {
46  public:
47   class NET_EXPORT_PRIVATE Job;
48   class NET_EXPORT_PRIVATE JobController;
49   class NET_EXPORT_PRIVATE JobFactory;
50 
51   enum JobType {
52     // Job that will connect via HTTP/1 or HTTP/2. This may be paused for a
53     // while when ALTERNATIVE or DNS_ALPN_H3 job was created.
54     MAIN,
55     // Job that will connect via HTTP/3 iff Chrome has received an Alt-Svc
56     // header from the origin.
57     ALTERNATIVE,
58     // Job that will connect via HTTP/3 iff an "h3" value was found in the ALPN
59     // list of an HTTPS DNS record.
60     DNS_ALPN_H3,
61     // Job that will preconnect. This uses HTTP/3 iff Chrome has received an
62     // Alt-Svc header from the origin. Otherwise, it use HTTP/1 or HTTP/2.
63     PRECONNECT,
64     // Job that will preconnect via HTTP/3 iff an "h3" value was found in the
65     // ALPN list of an HTTPS DNS record.
66     PRECONNECT_DNS_ALPN_H3,
67   };
68 
69   // This is the subset of HttpRequestInfo needed by the HttpStreamFactory
70   // layer. It's separated out largely to avoid dangling pointers when jobs are
71   // orphaned, though it also avoids creating multiple copies of fields that
72   // aren't needed, like HttpRequestHeaders.
73   //
74   // See HttpRequestInfo for description of most fields.
75   struct NET_EXPORT StreamRequestInfo {
76     StreamRequestInfo();
77     explicit StreamRequestInfo(const HttpRequestInfo& http_request_info);
78 
79     StreamRequestInfo(const StreamRequestInfo& other);
80     StreamRequestInfo& operator=(const StreamRequestInfo& other);
81     StreamRequestInfo(StreamRequestInfo&& other);
82     StreamRequestInfo& operator=(StreamRequestInfo&& other);
83 
84     ~StreamRequestInfo();
85 
86     std::string method;
87     NetworkAnonymizationKey network_anonymization_key;
88 
89     // Whether HTTP/1.x can be used. Extracted from
90     // UploadDataStream::AllowHTTP1().
91     bool is_http1_allowed = true;
92 
93     int load_flags = 0;
94     PrivacyMode privacy_mode = PRIVACY_MODE_DISABLED;
95     SecureDnsPolicy secure_dns_policy = SecureDnsPolicy::kAllow;
96     SocketTag socket_tag;
97   };
98 
99   explicit HttpStreamFactory(HttpNetworkSession* session);
100 
101   HttpStreamFactory(const HttpStreamFactory&) = delete;
102   HttpStreamFactory& operator=(const HttpStreamFactory&) = delete;
103 
104   virtual ~HttpStreamFactory();
105 
106   void ProcessAlternativeServices(
107       HttpNetworkSession* session,
108       const net::NetworkAnonymizationKey& network_anonymization_key,
109       const HttpResponseHeaders* headers,
110       const url::SchemeHostPort& http_server);
111 
112   // Request a stream.
113   // Will call delegate->OnStreamReady on successful completion.
114   std::unique_ptr<HttpStreamRequest> RequestStream(
115       const HttpRequestInfo& info,
116       RequestPriority priority,
117       const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs,
118       HttpStreamRequest::Delegate* delegate,
119       bool enable_ip_based_pooling,
120       bool enable_alternative_services,
121       const NetLogWithSource& net_log);
122 
123   // Request a WebSocket handshake stream.
124   // Will call delegate->OnWebSocketHandshakeStreamReady on successful
125   // completion.
126   std::unique_ptr<HttpStreamRequest> RequestWebSocketHandshakeStream(
127       const HttpRequestInfo& info,
128       RequestPriority priority,
129       const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs,
130       HttpStreamRequest::Delegate* delegate,
131       WebSocketHandshakeStreamBase::CreateHelper* create_helper,
132       bool enable_ip_based_pooling,
133       bool enable_alternative_services,
134       const NetLogWithSource& net_log);
135 
136   // Request a BidirectionalStreamImpl.
137   // Will call delegate->OnBidirectionalStreamImplReady on successful
138   // completion.
139   // TODO(https://crbug.com/836823): This method is virtual to avoid cronet_test
140   // failure on iOS that is caused by Network Thread TLS getting the wrong slot.
141   virtual std::unique_ptr<HttpStreamRequest> RequestBidirectionalStreamImpl(
142       const HttpRequestInfo& info,
143       RequestPriority priority,
144       const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs,
145       HttpStreamRequest::Delegate* delegate,
146       bool enable_ip_based_pooling,
147       bool enable_alternative_services,
148       const NetLogWithSource& net_log);
149 
150   // Requests that enough connections for |num_streams| be opened.
151   //
152   // TODO: Make this take StreamRequestInfo instead.
153   void PreconnectStreams(int num_streams, HttpRequestInfo& info);
154 
155   const HostMappingRules* GetHostMappingRules() const;
156 
157  private:
158   FRIEND_TEST_ALL_PREFIXES(HttpStreamRequestTest, SetPriority);
159 
160   friend class HttpStreamFactoryPeer;
161 
162   using JobControllerSet =
163       std::set<std::unique_ptr<JobController>, base::UniquePtrComparator>;
164 
165   url::SchemeHostPort RewriteHost(const url::SchemeHostPort& server);
166 
167   // Values must not be changed or reused.  Keep in sync with identically named
168   // enum in histograms.xml.
169   enum AlternativeServiceType {
170     NO_ALTERNATIVE_SERVICE = 0,
171     QUIC_SAME_DESTINATION = 1,
172     QUIC_DIFFERENT_DESTINATION = 2,
173     NOT_QUIC_SAME_DESTINATION = 3,
174     NOT_QUIC_DIFFERENT_DESTINATION = 4,
175     MAX_ALTERNATIVE_SERVICE_TYPE
176   };
177 
178   std::unique_ptr<HttpStreamRequest> RequestStreamInternal(
179       const HttpRequestInfo& info,
180       RequestPriority priority,
181       const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs,
182       HttpStreamRequest::Delegate* delegate,
183       WebSocketHandshakeStreamBase::CreateHelper* create_helper,
184       HttpStreamRequest::StreamType stream_type,
185       bool is_websocket,
186       bool enable_ip_based_pooling,
187       bool enable_alternative_services,
188       const NetLogWithSource& net_log);
189 
190   // Called when the Preconnect completes. Used for testing.
OnPreconnectsCompleteInternal()191   virtual void OnPreconnectsCompleteInternal() {}
192 
193   // Called when the JobController finishes service. Delete the JobController
194   // from |job_controller_set_|.
195   void OnJobControllerComplete(JobController* controller);
196 
197   const raw_ptr<HttpNetworkSession> session_;
198 
199   // Factory used by job controllers for creating jobs.
200   std::unique_ptr<JobFactory> job_factory_;
201 
202   // All Requests/Preconnects are assigned with a JobController to manage
203   // serving Job(s). JobController might outlive Request when Request
204   // is served while there's some working Job left. JobController will be
205   // deleted from |job_controller_set_| when it determines the completion of
206   // its work.
207   JobControllerSet job_controller_set_;
208 };
209 
210 }  // namespace net
211 
212 #endif  // NET_HTTP_HTTP_STREAM_FACTORY_H_
213