• 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_URL_REQUEST_URL_REQUEST_JOB_H_
6 #define NET_URL_REQUEST_URL_REQUEST_JOB_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <optional>
12 #include <string>
13 #include <vector>
14 
15 #include "base/memory/raw_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "net/base/completion_once_callback.h"
18 #include "net/base/completion_repeating_callback.h"
19 #include "net/base/ip_endpoint.h"
20 #include "net/base/load_states.h"
21 #include "net/base/net_error_details.h"
22 #include "net/base/net_export.h"
23 #include "net/base/privacy_mode.h"
24 #include "net/base/request_priority.h"
25 #include "net/cookies/canonical_cookie.h"
26 #include "net/cookies/cookie_setting_override.h"
27 #include "net/cookies/cookie_util.h"
28 #include "net/filter/source_stream.h"
29 #include "net/http/http_raw_request_headers.h"
30 #include "net/http/http_response_headers.h"
31 #include "net/socket/connection_attempts.h"
32 #include "net/url_request/referrer_policy.h"
33 #include "net/url_request/url_request.h"
34 #include "url/gurl.h"
35 
36 namespace net {
37 
38 class AuthChallengeInfo;
39 class AuthCredentials;
40 class CookieOptions;
41 class HttpRequestHeaders;
42 class HttpResponseInfo;
43 class IOBuffer;
44 struct LoadTimingInfo;
45 class ProxyChain;
46 class SSLCertRequestInfo;
47 class SSLInfo;
48 class SSLPrivateKey;
49 struct TransportInfo;
50 class UploadDataStream;
51 class X509Certificate;
52 
53 class NET_EXPORT URLRequestJob {
54  public:
55   explicit URLRequestJob(URLRequest* request);
56 
57   URLRequestJob(const URLRequestJob&) = delete;
58   URLRequestJob& operator=(const URLRequestJob&) = delete;
59 
60   virtual ~URLRequestJob();
61 
62   // Returns the request that owns this job.
request()63   URLRequest* request() const {
64     return request_;
65   }
66 
67   // Sets the upload data, most requests have no upload data, so this is a NOP.
68   // Job types supporting upload data will override this.
69   virtual void SetUpload(UploadDataStream* upload_data_stream);
70 
71   // Sets extra request headers for Job types that support request
72   // headers. Called once before Start() is called.
73   virtual void SetExtraRequestHeaders(const HttpRequestHeaders& headers);
74 
75   // Sets the priority of the job. Called once before Start() is
76   // called, but also when the priority of the parent request changes.
77   virtual void SetPriority(RequestPriority priority);
78 
79   // If any error occurs while starting the Job, NotifyStartError should be
80   // called asynchronously.
81   // This helps ensure that all errors follow more similar notification code
82   // paths, which should simplify testing.
83   virtual void Start() = 0;
84 
85   // This function MUST somehow call NotifyDone/NotifyCanceled or some requests
86   // will get leaked. Certain callers use that message to know when they can
87   // delete their URLRequest object, even when doing a cancel. The default
88   // Kill implementation calls NotifyCanceled, so it is recommended that
89   // subclasses call URLRequestJob::Kill() after doing any additional work.
90   //
91   // The job should endeavor to stop working as soon as is convenient, but must
92   // not send and complete notifications from inside this function. Instead,
93   // complete notifications (including "canceled") should be sent from a
94   // callback run from the message loop.
95   //
96   // The job is not obliged to immediately stop sending data in response to
97   // this call, nor is it obliged to fail with "canceled" unless not all data
98   // was sent as a result. A typical case would be where the job is almost
99   // complete and can succeed before the canceled notification can be
100   // dispatched (from the message loop).
101   //
102   // The job should be prepared to receive multiple calls to kill it, but only
103   // one notification must be issued.
104   virtual void Kill();
105 
106   // Called to read post-filtered data from this Job, returning the number of
107   // bytes read, 0 when there is no more data, or net error if there was an
108   // error. This is just the backend for URLRequest::Read, see that function for
109   // more info.
110   int Read(IOBuffer* buf, int buf_size);
111 
112   // Get the number of bytes received from network. The values returned by this
113   // will never decrease over the lifetime of the URLRequestJob.
114   virtual int64_t GetTotalReceivedBytes() const;
115 
116   // Get the number of bytes sent over the network. The values returned by this
117   // will never decrease over the lifetime of the URLRequestJob.
118   virtual int64_t GetTotalSentBytes() const;
119 
120   // Get the number of bytes of the body received from network.
121   virtual int64_t GetReceivedBodyBytes() const;
122 
123   // Called to fetch the current load state for the job.
124   virtual LoadState GetLoadState() const;
125 
126   // Called to fetch the charset for this request.  Only makes sense for some
127   // types of requests. Returns true on success.  Calling this on a type that
128   // doesn't have a charset will return false.
129   virtual bool GetCharset(std::string* charset);
130 
131   // Called to get response info.
132   virtual void GetResponseInfo(HttpResponseInfo* info);
133 
134   // This returns the times when events actually occurred, rather than the time
135   // each event blocked the request.  See FixupLoadTimingInfo in url_request.h
136   // for more information on the difference.
137   virtual void GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const;
138 
139   // Gets the remote endpoint that the network stack is currently fetching the
140   // URL from. Returns true and fills in |endpoint| if it is available; returns
141   // false and leaves |endpoint| unchanged if it is unavailable.
142   virtual bool GetTransactionRemoteEndpoint(IPEndPoint* endpoint) const;
143 
144   // Populates the network error details of the most recent origin that the
145   // network stack makes the request to.
146   virtual void PopulateNetErrorDetails(NetErrorDetails* details) const;
147 
148   // Called to determine if this response is a redirect.  Only makes sense
149   // for some types of requests.  This method returns true if the response
150   // is a redirect, and fills in the location param with the URL of the
151   // redirect.  The HTTP status code (e.g., 302) is filled into
152   // |*http_status_code| to signify the type of redirect.
153   // |*insecure_scheme_was_upgraded| is set to true if the scheme of this
154   // request was upgraded to HTTPS due to an 'upgrade-insecure-requests'
155   // policy.
156   //
157   // The caller is responsible for following the redirect by setting up an
158   // appropriate replacement Job. Note that the redirected location may be
159   // invalid, the caller should be sure it can handle this.
160   //
161   // The default implementation inspects the response_info_.
162   virtual bool IsRedirectResponse(GURL* location,
163                                   int* http_status_code,
164                                   bool* insecure_scheme_was_upgraded);
165 
166   // Called to determine if it is okay to copy the reference fragment from the
167   // original URL (if existent) to the redirection target when the redirection
168   // target has no reference fragment.
169   //
170   // The default implementation returns true.
171   virtual bool CopyFragmentOnRedirect(const GURL& location) const;
172 
173   // Called to determine if it is okay to redirect this job to the specified
174   // location.  This may be used to implement protocol-specific restrictions.
175   // If this function returns false, then the URLRequest will fail
176   // reporting ERR_UNSAFE_REDIRECT.
177   virtual bool IsSafeRedirect(const GURL& location);
178 
179   // Called to determine if this response is asking for authentication.  Only
180   // makes sense for some types of requests.  The caller is responsible for
181   // obtaining the credentials passing them to SetAuth.
182   virtual bool NeedsAuth();
183 
184   // Returns a copy of the authentication challenge that came with the server's
185   // response.
186   virtual std::unique_ptr<AuthChallengeInfo> GetAuthChallengeInfo();
187 
188   // Resend the request with authentication credentials.
189   virtual void SetAuth(const AuthCredentials& credentials);
190 
191   // Display the error page without asking for credentials again.
192   virtual void CancelAuth();
193 
194   virtual void ContinueWithCertificate(
195       scoped_refptr<X509Certificate> client_cert,
196       scoped_refptr<SSLPrivateKey> client_private_key);
197 
198   // Continue processing the request ignoring the last error.
199   virtual void ContinueDespiteLastError();
200 
201   // Returns true if the Job is done producing response data and has called
202   // NotifyDone on the request.
is_done()203   bool is_done() const { return done_; }
204 
205   // Get/Set expected content size
expected_content_size()206   int64_t expected_content_size() const { return expected_content_size_; }
set_expected_content_size(const int64_t & size)207   void set_expected_content_size(const int64_t& size) {
208     expected_content_size_ = size;
209   }
210 
211   // Whether we have processed the response for that request yet.
has_response_started()212   bool has_response_started() const { return has_handled_response_; }
213 
214   // The number of bytes read before passing to the filter. This value reflects
215   // bytes read even when there is no filter.
216   // TODO(caseq): this is only virtual because of StreamURLRequestJob.
217   // Consider removing virtual when StreamURLRequestJob is gone.
218   virtual int64_t prefilter_bytes_read() const;
219 
220   // These methods are not applicable to all connections.
221   virtual bool GetMimeType(std::string* mime_type) const;
222   virtual int GetResponseCode() const;
223 
224   // Returns the socket address for the connection.
225   // See url_request.h for details.
226   virtual IPEndPoint GetResponseRemoteEndpoint() const;
227 
228   // Called after a NetworkDelegate has been informed that the URLRequest
229   // will be destroyed. This is used to track that no pending callbacks
230   // exist at destruction time of the URLRequestJob, unless they have been
231   // canceled by an explicit NetworkDelegate::NotifyURLRequestDestroyed() call.
232   virtual void NotifyURLRequestDestroyed();
233 
234   // Returns the connection attempts made at the socket layer in the course of
235   // executing the URLRequestJob. Should be called after the job has failed or
236   // the response headers have been received.
237   virtual ConnectionAttempts GetConnectionAttempts() const;
238 
239   // Sets a callback that will be invoked each time the request is about to
240   // be actually sent and will receive actual request headers that are about
241   // to hit the wire, including SPDY/QUIC internal headers.
SetRequestHeadersCallback(RequestHeadersCallback callback)242   virtual void SetRequestHeadersCallback(RequestHeadersCallback callback) {}
243 
244   // Sets a callback that will be invoked each time the response is received
245   // from the remote party with the actual response headers received.
SetResponseHeadersCallback(ResponseHeadersCallback callback)246   virtual void SetResponseHeadersCallback(ResponseHeadersCallback callback) {}
247 
248   // Sets a callback that will be invoked each time a 103 Early Hints response
249   // is received from the remote party with the actual response headers
250   // received.
SetEarlyResponseHeadersCallback(ResponseHeadersCallback callback)251   virtual void SetEarlyResponseHeadersCallback(
252       ResponseHeadersCallback callback) {}
253 
254   // Set a callback that will be invoked when a matching shared dictionary is
255   // available to determine whether it is allowed to use the dictionary.
SetIsSharedDictionaryReadAllowedCallback(base::RepeatingCallback<bool ()> callback)256   virtual void SetIsSharedDictionaryReadAllowedCallback(
257       base::RepeatingCallback<bool()> callback) {}
258 
259   // Causes the current transaction always close its active socket on
260   // destruction. Does not close H2/H3 sessions.
261   virtual void CloseConnectionOnDestruction();
262 
263   // Returns true if the request should be retried after activating Storage
264   // Access.
265   virtual bool NeedsRetryWithStorageAccess();
266 
267   // Set a SharedDictionaryGetter which will be used to get a shared
268   // dictionary for this request.
SetSharedDictionaryGetter(SharedDictionaryGetter dictionary_getter)269   virtual void SetSharedDictionaryGetter(
270       SharedDictionaryGetter dictionary_getter) {}
271 
272   // Given |policy|, |original_referrer|, and |destination|, returns the
273   // referrer URL mandated by |request|'s referrer policy.
274   //
275   // If |same_origin_out_for_metrics| is non-null, saves to
276   // |*same_origin_out_for_metrics| whether |original_referrer| and
277   // |destination| are cross-origin.
278   // (This allows reporting in a UMA whether the request is same-origin, without
279   // recomputing that information.)
280   static GURL ComputeReferrerForPolicy(
281       ReferrerPolicy policy,
282       const GURL& original_referrer,
283       const GURL& destination,
284       bool* same_origin_out_for_metrics = nullptr);
285 
286  protected:
287   // Notifies the job that we are connected.
288   int NotifyConnected(const TransportInfo& info,
289                       CompletionOnceCallback callback);
290 
291   // Notifies the job that a certificate is requested.
292   void NotifyCertificateRequested(SSLCertRequestInfo* cert_request_info);
293 
294   // Notifies the job about an SSL certificate error.
295   void NotifySSLCertificateError(int net_error,
296                                  const SSLInfo& ssl_info,
297                                  bool fatal);
298 
299   // Delegates to URLRequest.
300   bool CanSetCookie(const net::CanonicalCookie& cookie,
301                     CookieOptions* options,
302                     const net::FirstPartySetMetadata& first_party_set_metadata,
303                     CookieInclusionStatus* inclusion_status) const;
304 
305   // Notifies the job that headers have been received.
306   void NotifyHeadersComplete();
307 
308   // Called when the final set headers have been received (no more redirects to
309   // follow, and no more auth challenges that will be responded to).
310   void NotifyFinalHeadersReceived();
311 
312   // Notifies the request that a start error has occurred.
313   // NOTE: Must not be called synchronously from |Start|.
314   void NotifyStartError(int net_error);
315 
316   // Used as an asynchronous callback for Kill to notify the URLRequest
317   // that we were canceled.
318   void NotifyCanceled();
319 
320   // See corresponding functions in url_request.h.
321   void OnCallToDelegate(NetLogEventType type);
322   void OnCallToDelegateComplete();
323 
324   // Called to read raw (pre-filtered) data from this Job. Reads at most
325   // |buf_size| bytes into |buf|.
326   // Possible return values:
327   //   >= 0: Read completed synchronously. Return value is the number of bytes
328   //         read. 0 means eof.
329   //   ERR_IO_PENDING: Read pending asynchronously.
330   //                   When the read completes, |ReadRawDataComplete| should be
331   //                   called.
332   //   Any other negative number: Read failed synchronously. Return value is a
333   //                   network error code.
334   // This method might hold onto a reference to |buf| (by incrementing the
335   // refcount) until the method completes or is cancelled.
336   virtual int ReadRawData(IOBuffer* buf, int buf_size);
337 
338   // Called to tell the job that a filter has successfully reached the end of
339   // the stream.
340   virtual void DoneReading();
341 
342   // Called to tell the job that the body won't be read because it's a redirect.
343   // This is needed so that redirect headers can be cached even though their
344   // bodies are never read.
345   virtual void DoneReadingRedirectResponse();
346 
347   // Called to tell the job that the body won't be read (and headers won't be
348   // cached) because we're going to retry the request.
349   virtual void DoneReadingRetryResponse();
350 
351   // Called to set up a SourceStream chain for this request.
352   // Subclasses should return the appropriate last SourceStream of the chain,
353   // or nullptr on error.
354   virtual std::unique_ptr<SourceStream> SetUpSourceStream();
355 
356   // Set the proxy chain that was used, if any.
357   void SetProxyChain(const ProxyChain& proxy_chain);
358 
359   // The number of bytes read after passing through the filter. This value
360   // reflects bytes read even when there is no filter.
postfilter_bytes_read()361   int64_t postfilter_bytes_read() const { return postfilter_bytes_read_; }
362 
363   // Turns an integer result code into an Error and a count of bytes read.
364   // The semantics are:
365   //   |result| >= 0: |*error| == OK, |*count| == |result|
366   //   |result| < 0: |*error| = |result|, |*count| == 0
367   static void ConvertResultToError(int result, Error* error, int* count);
368 
369   // Completion callback for raw reads. See |ReadRawData| for details.
370   // |bytes_read| is either >= 0 to indicate a successful read and count of
371   // bytes read, or < 0 to indicate an error.
372   // On return, |this| may be deleted.
373   void ReadRawDataComplete(int bytes_read);
374 
375   // The request that initiated this job. This value will never be nullptr.
376   const raw_ptr<URLRequest> request_;
377 
378  private:
379   class URLRequestJobSourceStream;
380 
381   // Helper method used to perform tasks after reading from |source_stream_| is
382   // completed. |synchronous| true if the read completed synchronously.
383   // See the documentation for |Read| above for the contract of this method.
384   void SourceStreamReadComplete(bool synchronous, int result);
385 
386   // Invokes ReadRawData and records bytes read if the read completes
387   // synchronously.
388   int ReadRawDataHelper(IOBuffer* buf,
389                         int buf_size,
390                         CompletionOnceCallback callback);
391 
392   // Returns OK if |new_url| is a valid redirect target and an error code
393   // otherwise.
394   int CanFollowRedirect(const GURL& new_url);
395 
396   // Called after every raw read. If |bytes_read| is > 0, this indicates
397   // a successful read of |bytes_read| unfiltered bytes. If |bytes_read|
398   // is 0, this indicates that there is no additional data to read.
399   // If |bytes_read| is negative, no bytes were read.
400   void GatherRawReadStats(int bytes_read);
401 
402   // Updates the profiling info and notifies observers that an additional
403   // |bytes_read| unfiltered bytes have been read for this job.
404   void RecordBytesRead(int bytes_read);
405 
406   // OnDone marks that request is done. It is really a glorified
407   // set_status, but also does internal state checking and job tracking. It
408   // should be called once per request, when the job is finished doing all IO.
409   //
410   // If |notify_done| is true, will notify the URLRequest if there was an error
411   // asynchronously.  Otherwise, the caller will need to do this itself,
412   // possibly through a synchronous return value.
413   // TODO(mmenke):  Remove |notify_done|, and make caller handle notification.
414   void OnDone(int net_error, bool notify_done);
415 
416   // Takes care of the notification initiated by OnDone() to avoid re-entering
417   // the URLRequest::Delegate.
418   void NotifyDone();
419 
420   // Indicates that the job is done producing data, either it has completed
421   // all the data or an error has been encountered. Set exclusively by
422   // NotifyDone so that it is kept in sync with the request.
423   bool done_ = false;
424 
425   // Number of raw network bytes read from job subclass.
426   int64_t prefilter_bytes_read_ = 0;
427 
428   // Number of bytes after applying |source_stream_| filters.
429   int64_t postfilter_bytes_read_ = 0;
430 
431   // The first SourceStream of the SourceStream chain used.
432   std::unique_ptr<SourceStream> source_stream_;
433 
434   // Keep a reference to the buffer passed in via URLRequestJob::Read() so it
435   // doesn't get destroyed when the read has not completed.
436   scoped_refptr<IOBuffer> pending_read_buffer_;
437 
438   // We keep a pointer to the read buffer while asynchronous reads are
439   // in progress, so we are able to pass those bytes to job observers.
440   scoped_refptr<IOBuffer> raw_read_buffer_;
441 
442   // Used by HandleResponseIfNecessary to track whether we've sent the
443   // OnResponseStarted callback and potentially redirect callbacks as well.
444   bool has_handled_response_ = false;
445 
446   // Expected content size
447   int64_t expected_content_size_ = -1;
448 
449   // Non-null if ReadRawData() returned ERR_IO_PENDING, and the read has not
450   // completed.
451   CompletionOnceCallback read_raw_callback_;
452 
453   base::WeakPtrFactory<URLRequestJob> weak_factory_{this};
454 };
455 
456 }  // namespace net
457 
458 #endif  // NET_URL_REQUEST_URL_REQUEST_JOB_H_
459