Home | History | Annotate | Download | only in url_request
      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 
      5 #ifndef NET_URL_REQUEST_URL_REQUEST_H_
      6 #define NET_URL_REQUEST_URL_REQUEST_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/debug/leak_tracker.h"
     12 #include "base/logging.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/strings/string16.h"
     15 #include "base/supports_user_data.h"
     16 #include "base/threading/non_thread_safe.h"
     17 #include "base/time/time.h"
     18 #include "net/base/auth.h"
     19 #include "net/base/completion_callback.h"
     20 #include "net/base/load_states.h"
     21 #include "net/base/load_timing_info.h"
     22 #include "net/base/net_export.h"
     23 #include "net/base/net_log.h"
     24 #include "net/base/network_delegate.h"
     25 #include "net/base/request_priority.h"
     26 #include "net/base/upload_progress.h"
     27 #include "net/cookies/canonical_cookie.h"
     28 #include "net/cookies/cookie_store.h"
     29 #include "net/http/http_request_headers.h"
     30 #include "net/http/http_response_info.h"
     31 #include "net/url_request/url_request_status.h"
     32 #include "url/gurl.h"
     33 
     34 namespace base {
     35 class Value;
     36 
     37 namespace debug {
     38 class StackTrace;
     39 }  // namespace debug
     40 }  // namespace base
     41 
     42 // Temporary layering violation to allow existing users of a deprecated
     43 // interface.
     44 namespace content {
     45 class AppCacheInterceptor;
     46 }
     47 
     48 namespace net {
     49 
     50 class CookieOptions;
     51 class HostPortPair;
     52 class IOBuffer;
     53 struct LoadTimingInfo;
     54 class SSLCertRequestInfo;
     55 class SSLInfo;
     56 class UploadDataStream;
     57 class URLRequestContext;
     58 class URLRequestJob;
     59 class X509Certificate;
     60 
     61 // This stores the values of the Set-Cookie headers received during the request.
     62 // Each item in the vector corresponds to a Set-Cookie: line received,
     63 // excluding the "Set-Cookie:" part.
     64 typedef std::vector<std::string> ResponseCookies;
     65 
     66 //-----------------------------------------------------------------------------
     67 // A class  representing the asynchronous load of a data stream from an URL.
     68 //
     69 // The lifetime of an instance of this class is completely controlled by the
     70 // consumer, and the instance is not required to live on the heap or be
     71 // allocated in any special way.  It is also valid to delete an URLRequest
     72 // object during the handling of a callback to its delegate.  Of course, once
     73 // the URLRequest is deleted, no further callbacks to its delegate will occur.
     74 //
     75 // NOTE: All usage of all instances of this class should be on the same thread.
     76 //
     77 class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe),
     78                               public base::SupportsUserData {
     79  public:
     80   // Callback function implemented by protocol handlers to create new jobs.
     81   // The factory may return NULL to indicate an error, which will cause other
     82   // factories to be queried.  If no factory handles the request, then the
     83   // default job will be used.
     84   typedef URLRequestJob* (ProtocolFactory)(URLRequest* request,
     85                                            NetworkDelegate* network_delegate,
     86                                            const std::string& scheme);
     87 
     88   // HTTP request/response header IDs (via some preprocessor fun) for use with
     89   // SetRequestHeaderById and GetResponseHeaderById.
     90   enum {
     91 #define HTTP_ATOM(x) HTTP_ ## x,
     92 #include "net/http/http_atom_list.h"
     93 #undef HTTP_ATOM
     94   };
     95 
     96   // Referrer policies (see set_referrer_policy): During server redirects, the
     97   // referrer header might be cleared, if the protocol changes from HTTPS to
     98   // HTTP. This is the default behavior of URLRequest, corresponding to
     99   // CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE. Alternatively, the
    100   // referrer policy can be set to never change the referrer header. This
    101   // behavior corresponds to NEVER_CLEAR_REFERRER. Embedders will want to use
    102   // NEVER_CLEAR_REFERRER when implementing the meta-referrer support
    103   // (http://wiki.whatwg.org/wiki/Meta_referrer) and sending requests with a
    104   // non-default referrer policy. Only the default referrer policy requires
    105   // the referrer to be cleared on transitions from HTTPS to HTTP.
    106   enum ReferrerPolicy {
    107     CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
    108     NEVER_CLEAR_REFERRER,
    109   };
    110 
    111   // This class handles network interception.  Use with
    112   // (Un)RegisterRequestInterceptor.
    113   class NET_EXPORT Interceptor {
    114    public:
    115     virtual ~Interceptor() {}
    116 
    117     // Called for every request made.  Should return a new job to handle the
    118     // request if it should be intercepted, or NULL to allow the request to
    119     // be handled in the normal manner.
    120     virtual URLRequestJob* MaybeIntercept(
    121         URLRequest* request, NetworkDelegate* network_delegate) = 0;
    122 
    123     // Called after having received a redirect response, but prior to the
    124     // the request delegate being informed of the redirect. Can return a new
    125     // job to replace the existing job if it should be intercepted, or NULL
    126     // to allow the normal handling to continue. If a new job is provided,
    127     // the delegate never sees the original redirect response, instead the
    128     // response produced by the intercept job will be returned.
    129     virtual URLRequestJob* MaybeInterceptRedirect(
    130         URLRequest* request,
    131         NetworkDelegate* network_delegate,
    132         const GURL& location);
    133 
    134     // Called after having received a final response, but prior to the
    135     // the request delegate being informed of the response. This is also
    136     // called when there is no server response at all to allow interception
    137     // on dns or network errors. Can return a new job to replace the existing
    138     // job if it should be intercepted, or NULL to allow the normal handling to
    139     // continue. If a new job is provided, the delegate never sees the original
    140     // response, instead the response produced by the intercept job will be
    141     // returned.
    142     virtual URLRequestJob* MaybeInterceptResponse(
    143         URLRequest* request, NetworkDelegate* network_delegate);
    144   };
    145 
    146   // Deprecated interfaces in net::URLRequest. They have been moved to
    147   // URLRequest's private section to prevent new uses. Existing uses are
    148   // explicitly friended here and should be removed over time.
    149   class NET_EXPORT Deprecated {
    150    private:
    151     // TODO(willchan): Kill off these friend declarations.
    152     friend class TestInterceptor;
    153     friend class content::AppCacheInterceptor;
    154 
    155     // TODO(pauljensen): Remove this when AppCacheInterceptor is a
    156     // ProtocolHandler, see crbug.com/161547.
    157     static void RegisterRequestInterceptor(Interceptor* interceptor);
    158     static void UnregisterRequestInterceptor(Interceptor* interceptor);
    159 
    160     DISALLOW_IMPLICIT_CONSTRUCTORS(Deprecated);
    161   };
    162 
    163   // The delegate's methods are called from the message loop of the thread
    164   // on which the request's Start() method is called. See above for the
    165   // ordering of callbacks.
    166   //
    167   // The callbacks will be called in the following order:
    168   //   Start()
    169   //    - OnCertificateRequested* (zero or more calls, if the SSL server and/or
    170   //      SSL proxy requests a client certificate for authentication)
    171   //    - OnSSLCertificateError* (zero or one call, if the SSL server's
    172   //      certificate has an error)
    173   //    - OnReceivedRedirect* (zero or more calls, for the number of redirects)
    174   //    - OnAuthRequired* (zero or more calls, for the number of
    175   //      authentication failures)
    176   //    - OnResponseStarted
    177   //   Read() initiated by delegate
    178   //    - OnReadCompleted* (zero or more calls until all data is read)
    179   //
    180   // Read() must be called at least once. Read() returns true when it completed
    181   // immediately, and false if an IO is pending or if there is an error.  When
    182   // Read() returns false, the caller can check the Request's status() to see
    183   // if an error occurred, or if the IO is just pending.  When Read() returns
    184   // true with zero bytes read, it indicates the end of the response.
    185   //
    186   class NET_EXPORT Delegate {
    187    public:
    188     // Called upon a server-initiated redirect.  The delegate may call the
    189     // request's Cancel method to prevent the redirect from being followed.
    190     // Since there may be multiple chained redirects, there may also be more
    191     // than one redirect call.
    192     //
    193     // When this function is called, the request will still contain the
    194     // original URL, the destination of the redirect is provided in 'new_url'.
    195     // If the delegate does not cancel the request and |*defer_redirect| is
    196     // false, then the redirect will be followed, and the request's URL will be
    197     // changed to the new URL.  Otherwise if the delegate does not cancel the
    198     // request and |*defer_redirect| is true, then the redirect will be
    199     // followed once FollowDeferredRedirect is called on the URLRequest.
    200     //
    201     // The caller must set |*defer_redirect| to false, so that delegates do not
    202     // need to set it if they are happy with the default behavior of not
    203     // deferring redirect.
    204     virtual void OnReceivedRedirect(URLRequest* request,
    205                                     const GURL& new_url,
    206                                     bool* defer_redirect);
    207 
    208     // Called when we receive an authentication failure.  The delegate should
    209     // call request->SetAuth() with the user's credentials once it obtains them,
    210     // or request->CancelAuth() to cancel the login and display the error page.
    211     // When it does so, the request will be reissued, restarting the sequence
    212     // of On* callbacks.
    213     virtual void OnAuthRequired(URLRequest* request,
    214                                 AuthChallengeInfo* auth_info);
    215 
    216     // Called when we receive an SSL CertificateRequest message for client
    217     // authentication.  The delegate should call
    218     // request->ContinueWithCertificate() with the client certificate the user
    219     // selected, or request->ContinueWithCertificate(NULL) to continue the SSL
    220     // handshake without a client certificate.
    221     virtual void OnCertificateRequested(
    222         URLRequest* request,
    223         SSLCertRequestInfo* cert_request_info);
    224 
    225     // Called when using SSL and the server responds with a certificate with
    226     // an error, for example, whose common name does not match the common name
    227     // we were expecting for that host.  The delegate should either do the
    228     // safe thing and Cancel() the request or decide to proceed by calling
    229     // ContinueDespiteLastError().  cert_error is a ERR_* error code
    230     // indicating what's wrong with the certificate.
    231     // If |fatal| is true then the host in question demands a higher level
    232     // of security (due e.g. to HTTP Strict Transport Security, user
    233     // preference, or built-in policy). In this case, errors must not be
    234     // bypassable by the user.
    235     virtual void OnSSLCertificateError(URLRequest* request,
    236                                        const SSLInfo& ssl_info,
    237                                        bool fatal);
    238 
    239     // Called to notify that the request must use the network to complete the
    240     // request and is about to do so. This is called at most once per
    241     // URLRequest, and by default does not defer. If deferred, call
    242     // ResumeNetworkStart() to continue or Cancel() to cancel.
    243     virtual void OnBeforeNetworkStart(URLRequest* request, bool* defer);
    244 
    245     // After calling Start(), the delegate will receive an OnResponseStarted
    246     // callback when the request has completed.  If an error occurred, the
    247     // request->status() will be set.  On success, all redirects have been
    248     // followed and the final response is beginning to arrive.  At this point,
    249     // meta data about the response is available, including for example HTTP
    250     // response headers if this is a request for a HTTP resource.
    251     virtual void OnResponseStarted(URLRequest* request) = 0;
    252 
    253     // Called when the a Read of the response body is completed after an
    254     // IO_PENDING status from a Read() call.
    255     // The data read is filled into the buffer which the caller passed
    256     // to Read() previously.
    257     //
    258     // If an error occurred, request->status() will contain the error,
    259     // and bytes read will be -1.
    260     virtual void OnReadCompleted(URLRequest* request, int bytes_read) = 0;
    261 
    262    protected:
    263     virtual ~Delegate() {}
    264   };
    265 
    266   // TODO(tburkard): we should get rid of this constructor, and have each
    267   // creator of a URLRequest specifically list the cookie store to be used.
    268   // For now, this constructor will use the cookie store in |context|.
    269   URLRequest(const GURL& url,
    270              RequestPriority priority,
    271              Delegate* delegate,
    272              const URLRequestContext* context);
    273 
    274   URLRequest(const GURL& url,
    275              RequestPriority priority,
    276              Delegate* delegate,
    277              const URLRequestContext* context,
    278              CookieStore* cookie_store);
    279 
    280   // If destroyed after Start() has been called but while IO is pending,
    281   // then the request will be effectively canceled and the delegate
    282   // will not have any more of its methods called.
    283   virtual ~URLRequest();
    284 
    285   // Changes the default cookie policy from allowing all cookies to blocking all
    286   // cookies. Embedders that want to implement a more flexible policy should
    287   // change the default to blocking all cookies, and provide a NetworkDelegate
    288   // with the URLRequestContext that maintains the CookieStore.
    289   // The cookie policy default has to be set before the first URLRequest is
    290   // started. Once it was set to block all cookies, it cannot be changed back.
    291   static void SetDefaultCookiePolicyToBlock();
    292 
    293   // Returns true if the scheme can be handled by URLRequest. False otherwise.
    294   static bool IsHandledProtocol(const std::string& scheme);
    295 
    296   // Returns true if the url can be handled by URLRequest. False otherwise.
    297   // The function returns true for invalid urls because URLRequest knows how
    298   // to handle those.
    299   // NOTE: This will also return true for URLs that are handled by
    300   // ProtocolFactories that only work for requests that are scoped to a
    301   // Profile.
    302   static bool IsHandledURL(const GURL& url);
    303 
    304   // The original url is the url used to initialize the request, and it may
    305   // differ from the url if the request was redirected.
    306   const GURL& original_url() const { return url_chain_.front(); }
    307   // The chain of urls traversed by this request.  If the request had no
    308   // redirects, this vector will contain one element.
    309   const std::vector<GURL>& url_chain() const { return url_chain_; }
    310   const GURL& url() const { return url_chain_.back(); }
    311 
    312   // The URL that should be consulted for the third-party cookie blocking
    313   // policy.
    314   //
    315   // WARNING: This URL must only be used for the third-party cookie blocking
    316   //          policy. It MUST NEVER be used for any kind of SECURITY check.
    317   //
    318   //          For example, if a top-level navigation is redirected, the
    319   //          first-party for cookies will be the URL of the first URL in the
    320   //          redirect chain throughout the whole redirect. If it was used for
    321   //          a security check, an attacker might try to get around this check
    322   //          by starting from some page that redirects to the
    323   //          host-to-be-attacked.
    324   const GURL& first_party_for_cookies() const {
    325     return first_party_for_cookies_;
    326   }
    327   // This method may be called before Start() or FollowDeferredRedirect() is
    328   // called.
    329   void set_first_party_for_cookies(const GURL& first_party_for_cookies);
    330 
    331   // The request method, as an uppercase string.  "GET" is the default value.
    332   // The request method may only be changed before Start() is called and
    333   // should only be assigned an uppercase value.
    334   const std::string& method() const { return method_; }
    335   void set_method(const std::string& method);
    336 
    337   // Determines the new method of the request afer following a redirect.
    338   // |method| is the method used to arrive at the redirect,
    339   // |http_status_code| is the status code associated with the redirect.
    340   static std::string ComputeMethodForRedirect(const std::string& method,
    341                                               int http_status_code);
    342 
    343   // The referrer URL for the request.  This header may actually be suppressed
    344   // from the underlying network request for security reasons (e.g., a HTTPS
    345   // URL will not be sent as the referrer for a HTTP request).  The referrer
    346   // may only be changed before Start() is called.
    347   const std::string& referrer() const { return referrer_; }
    348   // Referrer is sanitized to remove URL fragment, user name and password.
    349   void SetReferrer(const std::string& referrer);
    350 
    351   // The referrer policy to apply when updating the referrer during redirects.
    352   // The referrer policy may only be changed before Start() is called.
    353   void set_referrer_policy(ReferrerPolicy referrer_policy);
    354 
    355   // Sets the delegate of the request.  This value may be changed at any time,
    356   // and it is permissible for it to be null.
    357   void set_delegate(Delegate* delegate);
    358 
    359   // Indicates that the request body should be sent using chunked transfer
    360   // encoding. This method may only be called before Start() is called.
    361   void EnableChunkedUpload();
    362 
    363   // Appends the given bytes to the request's upload data to be sent
    364   // immediately via chunked transfer encoding. When all data has been sent,
    365   // call MarkEndOfChunks() to indicate the end of upload data.
    366   //
    367   // This method may be called only after calling EnableChunkedUpload().
    368   void AppendChunkToUpload(const char* bytes,
    369                            int bytes_len,
    370                            bool is_last_chunk);
    371 
    372   // Sets the upload data.
    373   void set_upload(scoped_ptr<UploadDataStream> upload);
    374 
    375   // Gets the upload data.
    376   const UploadDataStream* get_upload() const;
    377 
    378   // Returns true if the request has a non-empty message body to upload.
    379   bool has_upload() const;
    380 
    381   // Set an extra request header by ID or name, or remove one by name.  These
    382   // methods may only be called before Start() is called, or before a new
    383   // redirect in the request chain.
    384   void SetExtraRequestHeaderById(int header_id, const std::string& value,
    385                                  bool overwrite);
    386   void SetExtraRequestHeaderByName(const std::string& name,
    387                                    const std::string& value, bool overwrite);
    388   void RemoveRequestHeaderByName(const std::string& name);
    389 
    390   // Sets all extra request headers.  Any extra request headers set by other
    391   // methods are overwritten by this method.  This method may only be called
    392   // before Start() is called.  It is an error to call it later.
    393   void SetExtraRequestHeaders(const HttpRequestHeaders& headers);
    394 
    395   const HttpRequestHeaders& extra_request_headers() const {
    396     return extra_request_headers_;
    397   }
    398 
    399   // Gets the full request headers sent to the server.
    400   //
    401   // Return true and overwrites headers if it can get the request headers;
    402   // otherwise, returns false and does not modify headers.  (Always returns
    403   // false for request types that don't have headers, like file requests.)
    404   //
    405   // This is guaranteed to succeed if:
    406   //
    407   // 1. A redirect or auth callback is currently running.  Once it ends, the
    408   //    headers may become unavailable as a new request with the new address
    409   //    or credentials is made.
    410   //
    411   // 2. The OnResponseStarted callback is currently running or has run.
    412   bool GetFullRequestHeaders(HttpRequestHeaders* headers) const;
    413 
    414   // Gets the total amount of data received from network after SSL decoding and
    415   // proxy handling.
    416   int64 GetTotalReceivedBytes() const;
    417 
    418   // Returns the current load state for the request. The returned value's
    419   // |param| field is an optional parameter describing details related to the
    420   // load state. Not all load states have a parameter.
    421   LoadStateWithParam GetLoadState() const;
    422 
    423   // Returns a partial representation of the request's state as a value, for
    424   // debugging.  Caller takes ownership of returned value.
    425   base::Value* GetStateAsValue() const;
    426 
    427   // Logs information about the what external object currently blocking the
    428   // request.  LogUnblocked must be called before resuming the request.  This
    429   // can be called multiple times in a row either with or without calling
    430   // LogUnblocked between calls.  |blocked_by| must not be NULL or have length
    431   // 0.
    432   void LogBlockedBy(const char* blocked_by);
    433 
    434   // Just like LogBlockedBy, but also makes GetLoadState return source as the
    435   // |param| in the value returned by GetLoadState.  Calling LogUnblocked or
    436   // LogBlockedBy will clear the load param.  |blocked_by| must not be NULL or
    437   // have length 0.
    438   void LogAndReportBlockedBy(const char* blocked_by);
    439 
    440   // Logs that the request is no longer blocked by the last caller to
    441   // LogBlockedBy.
    442   void LogUnblocked();
    443 
    444   // Returns the current upload progress in bytes. When the upload data is
    445   // chunked, size is set to zero, but position will not be.
    446   UploadProgress GetUploadProgress() const;
    447 
    448   // Get response header(s) by ID or name.  These methods may only be called
    449   // once the delegate's OnResponseStarted method has been called.  Headers
    450   // that appear more than once in the response are coalesced, with values
    451   // separated by commas (per RFC 2616). This will not work with cookies since
    452   // comma can be used in cookie values.
    453   // TODO(darin): add API to enumerate response headers.
    454   void GetResponseHeaderById(int header_id, std::string* value);
    455   void GetResponseHeaderByName(const std::string& name, std::string* value);
    456 
    457   // Get all response headers, \n-delimited and \n\0-terminated.  This includes
    458   // the response status line.  Restrictions on GetResponseHeaders apply.
    459   void GetAllResponseHeaders(std::string* headers);
    460 
    461   // The time when |this| was constructed.
    462   base::TimeTicks creation_time() const { return creation_time_; }
    463 
    464   // The time at which the returned response was requested.  For cached
    465   // responses, this is the last time the cache entry was validated.
    466   const base::Time& request_time() const {
    467     return response_info_.request_time;
    468   }
    469 
    470   // The time at which the returned response was generated.  For cached
    471   // responses, this is the last time the cache entry was validated.
    472   const base::Time& response_time() const {
    473     return response_info_.response_time;
    474   }
    475 
    476   // Indicate if this response was fetched from disk cache.
    477   bool was_cached() const { return response_info_.was_cached; }
    478 
    479   // Returns true if the URLRequest was delivered through a proxy.
    480   bool was_fetched_via_proxy() const {
    481     return response_info_.was_fetched_via_proxy;
    482   }
    483 
    484   // Returns true if the URLRequest was delivered over SPDY.
    485   bool was_fetched_via_spdy() const {
    486     return response_info_.was_fetched_via_spdy;
    487   }
    488 
    489   // Returns the host and port that the content was fetched from.  See
    490   // http_response_info.h for caveats relating to cached content.
    491   HostPortPair GetSocketAddress() const;
    492 
    493   // Get all response headers, as a HttpResponseHeaders object.  See comments
    494   // in HttpResponseHeaders class as to the format of the data.
    495   HttpResponseHeaders* response_headers() const;
    496 
    497   // Get the SSL connection info.
    498   const SSLInfo& ssl_info() const {
    499     return response_info_.ssl_info;
    500   }
    501 
    502   // Gets timing information related to the request.  Events that have not yet
    503   // occurred are left uninitialized.  After a second request starts, due to
    504   // a redirect or authentication, values will be reset.
    505   //
    506   // LoadTimingInfo only contains ConnectTiming information and socket IDs for
    507   // non-cached HTTP responses.
    508   void GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const;
    509 
    510   // Returns the cookie values included in the response, if the request is one
    511   // that can have cookies.  Returns true if the request is a cookie-bearing
    512   // type, false otherwise.  This method may only be called once the
    513   // delegate's OnResponseStarted method has been called.
    514   bool GetResponseCookies(ResponseCookies* cookies);
    515 
    516   // Get the mime type.  This method may only be called once the delegate's
    517   // OnResponseStarted method has been called.
    518   void GetMimeType(std::string* mime_type);
    519 
    520   // Get the charset (character encoding).  This method may only be called once
    521   // the delegate's OnResponseStarted method has been called.
    522   void GetCharset(std::string* charset);
    523 
    524   // Returns the HTTP response code (e.g., 200, 404, and so on).  This method
    525   // may only be called once the delegate's OnResponseStarted method has been
    526   // called.  For non-HTTP requests, this method returns -1.
    527   int GetResponseCode() const;
    528 
    529   // Get the HTTP response info in its entirety.
    530   const HttpResponseInfo& response_info() const { return response_info_; }
    531 
    532   // Access the LOAD_* flags modifying this request (see load_flags.h).
    533   int load_flags() const { return load_flags_; }
    534 
    535   // The new flags may change the IGNORE_LIMITS flag only when called
    536   // before Start() is called, it must only set the flag, and if set,
    537   // the priority of this request must already be MAXIMUM_PRIORITY.
    538   void SetLoadFlags(int flags);
    539 
    540   // Returns true if the request is "pending" (i.e., if Start() has been called,
    541   // and the response has not yet been called).
    542   bool is_pending() const { return is_pending_; }
    543 
    544   // Returns true if the request is in the process of redirecting to a new
    545   // URL but has not yet initiated the new request.
    546   bool is_redirecting() const { return is_redirecting_; }
    547 
    548   // Returns the error status of the request.
    549   const URLRequestStatus& status() const { return status_; }
    550 
    551   // Returns a globally unique identifier for this request.
    552   uint64 identifier() const { return identifier_; }
    553 
    554   // This method is called to start the request.  The delegate will receive
    555   // a OnResponseStarted callback when the request is started.
    556   void Start();
    557 
    558   // This method may be called at any time after Start() has been called to
    559   // cancel the request.  This method may be called many times, and it has
    560   // no effect once the response has completed.  It is guaranteed that no
    561   // methods of the delegate will be called after the request has been
    562   // cancelled, except that this may call the delegate's OnReadCompleted()
    563   // during the call to Cancel itself.
    564   void Cancel();
    565 
    566   // Cancels the request and sets the error to |error| (see net_error_list.h
    567   // for values).
    568   void CancelWithError(int error);
    569 
    570   // Cancels the request and sets the error to |error| (see net_error_list.h
    571   // for values) and attaches |ssl_info| as the SSLInfo for that request.  This
    572   // is useful to attach a certificate and certificate error to a canceled
    573   // request.
    574   void CancelWithSSLError(int error, const SSLInfo& ssl_info);
    575 
    576   // Read initiates an asynchronous read from the response, and must only
    577   // be called after the OnResponseStarted callback is received with a
    578   // successful status.
    579   // If data is available, Read will return true, and the data and length will
    580   // be returned immediately.  If data is not available, Read returns false,
    581   // and an asynchronous Read is initiated.  The Read is finished when
    582   // the caller receives the OnReadComplete callback.  Unless the request was
    583   // cancelled, OnReadComplete will always be called, even if the read failed.
    584   //
    585   // The buf parameter is a buffer to receive the data.  If the operation
    586   // completes asynchronously, the implementation will reference the buffer
    587   // until OnReadComplete is called.  The buffer must be at least max_bytes in
    588   // length.
    589   //
    590   // The max_bytes parameter is the maximum number of bytes to read.
    591   //
    592   // The bytes_read parameter is an output parameter containing the
    593   // the number of bytes read.  A value of 0 indicates that there is no
    594   // more data available to read from the stream.
    595   //
    596   // If a read error occurs, Read returns false and the request->status
    597   // will be set to an error.
    598   bool Read(IOBuffer* buf, int max_bytes, int* bytes_read);
    599 
    600   // If this request is being cached by the HTTP cache, stop subsequent caching.
    601   // Note that this method has no effect on other (simultaneous or not) requests
    602   // for the same resource. The typical example is a request that results in
    603   // the data being stored to disk (downloaded instead of rendered) so we don't
    604   // want to store it twice.
    605   void StopCaching();
    606 
    607   // This method may be called to follow a redirect that was deferred in
    608   // response to an OnReceivedRedirect call.
    609   void FollowDeferredRedirect();
    610 
    611   // This method must be called to resume network communications that were
    612   // deferred in response to an OnBeforeNetworkStart call.
    613   void ResumeNetworkStart();
    614 
    615   // One of the following two methods should be called in response to an
    616   // OnAuthRequired() callback (and only then).
    617   // SetAuth will reissue the request with the given credentials.
    618   // CancelAuth will give up and display the error page.
    619   void SetAuth(const AuthCredentials& credentials);
    620   void CancelAuth();
    621 
    622   // This method can be called after the user selects a client certificate to
    623   // instruct this URLRequest to continue with the request with the
    624   // certificate.  Pass NULL if the user doesn't have a client certificate.
    625   void ContinueWithCertificate(X509Certificate* client_cert);
    626 
    627   // This method can be called after some error notifications to instruct this
    628   // URLRequest to ignore the current error and continue with the request.  To
    629   // cancel the request instead, call Cancel().
    630   void ContinueDespiteLastError();
    631 
    632   // Used to specify the context (cookie store, cache) for this request.
    633   const URLRequestContext* context() const;
    634 
    635   const BoundNetLog& net_log() const { return net_log_; }
    636 
    637   // Returns the expected content size if available
    638   int64 GetExpectedContentSize() const;
    639 
    640   // Returns the priority level for this request.
    641   RequestPriority priority() const { return priority_; }
    642 
    643   // Sets the priority level for this request and any related
    644   // jobs. Must not change the priority to anything other than
    645   // MAXIMUM_PRIORITY if the IGNORE_LIMITS load flag is set.
    646   void SetPriority(RequestPriority priority);
    647 
    648   // Returns true iff this request would be internally redirected to HTTPS
    649   // due to HSTS. If so, |redirect_url| is rewritten to the new HTTPS URL.
    650   bool GetHSTSRedirect(GURL* redirect_url) const;
    651 
    652   // TODO(willchan): Undo this. Only temporarily public.
    653   bool has_delegate() const { return delegate_ != NULL; }
    654 
    655   // NOTE(willchan): This is just temporary for debugging
    656   // http://crbug.com/90971.
    657   // Allows to setting debug info into the URLRequest.
    658   void set_stack_trace(const base::debug::StackTrace& stack_trace);
    659   const base::debug::StackTrace* stack_trace() const;
    660 
    661   void set_received_response_content_length(int64 received_content_length) {
    662     received_response_content_length_ = received_content_length;
    663   }
    664   int64 received_response_content_length() {
    665     return received_response_content_length_;
    666   }
    667 
    668   // Available at NetworkDelegate::NotifyHeadersReceived() time, which is before
    669   // the more general response_info() is available, even though it is a subset.
    670   const HostPortPair& proxy_server() const {
    671     return proxy_server_;
    672   }
    673 
    674  protected:
    675   // Allow the URLRequestJob class to control the is_pending() flag.
    676   void set_is_pending(bool value) { is_pending_ = value; }
    677 
    678   // Allow the URLRequestJob class to set our status too
    679   void set_status(const URLRequestStatus& value) { status_ = value; }
    680 
    681   CookieStore* cookie_store() const { return cookie_store_; }
    682 
    683   // Allow the URLRequestJob to redirect this request.  Returns OK if
    684   // successful, otherwise an error code is returned.
    685   int Redirect(const GURL& location, int http_status_code);
    686 
    687   // Called by URLRequestJob to allow interception when a redirect occurs.
    688   void NotifyReceivedRedirect(const GURL& location, bool* defer_redirect);
    689 
    690   // Called by URLRequestHttpJob (note, only HTTP(S) jobs will call this) to
    691   // allow deferral of network initialization.
    692   void NotifyBeforeNetworkStart(bool* defer);
    693 
    694   // Allow an interceptor's URLRequestJob to restart this request.
    695   // Should only be called if the original job has not started a response.
    696   void Restart();
    697 
    698  private:
    699   friend class URLRequestJob;
    700 
    701   // Registers or unregisters a network interception class.
    702   static void RegisterRequestInterceptor(Interceptor* interceptor);
    703   static void UnregisterRequestInterceptor(Interceptor* interceptor);
    704 
    705   // Initializes the URLRequest. Code shared between the two constructors.
    706   // TODO(tburkard): This can ultimately be folded into a single constructor
    707   // again.
    708   void Init(const GURL& url,
    709             RequestPriority priotity,
    710             Delegate* delegate,
    711             const URLRequestContext* context,
    712             CookieStore* cookie_store);
    713 
    714   // Resumes or blocks a request paused by the NetworkDelegate::OnBeforeRequest
    715   // handler. If |blocked| is true, the request is blocked and an error page is
    716   // returned indicating so. This should only be called after Start is called
    717   // and OnBeforeRequest returns true (signalling that the request should be
    718   // paused).
    719   void BeforeRequestComplete(int error);
    720 
    721   void StartJob(URLRequestJob* job);
    722 
    723   // Restarting involves replacing the current job with a new one such as what
    724   // happens when following a HTTP redirect.
    725   void RestartWithJob(URLRequestJob* job);
    726   void PrepareToRestart();
    727 
    728   // Detaches the job from this request in preparation for this object going
    729   // away or the job being replaced. The job will not call us back when it has
    730   // been orphaned.
    731   void OrphanJob();
    732 
    733   // Cancels the request and set the error and ssl info for this request to the
    734   // passed values.
    735   void DoCancel(int error, const SSLInfo& ssl_info);
    736 
    737   // Called by the URLRequestJob when the headers are received, before any other
    738   // method, to allow caching of load timing information.
    739   void OnHeadersComplete();
    740 
    741   // Notifies the network delegate that the request has been completed.
    742   // This does not imply a successful completion. Also a canceled request is
    743   // considered completed.
    744   void NotifyRequestCompleted();
    745 
    746   // Called by URLRequestJob to allow interception when the final response
    747   // occurs.
    748   void NotifyResponseStarted();
    749 
    750   // These functions delegate to |delegate_| and may only be used if
    751   // |delegate_| is not NULL. See URLRequest::Delegate for the meaning
    752   // of these functions.
    753   void NotifyAuthRequired(AuthChallengeInfo* auth_info);
    754   void NotifyAuthRequiredComplete(NetworkDelegate::AuthRequiredResponse result);
    755   void NotifyCertificateRequested(SSLCertRequestInfo* cert_request_info);
    756   void NotifySSLCertificateError(const SSLInfo& ssl_info, bool fatal);
    757   void NotifyReadCompleted(int bytes_read);
    758 
    759   // These functions delegate to |network_delegate_| if it is not NULL.
    760   // If |network_delegate_| is NULL, cookies can be used unless
    761   // SetDefaultCookiePolicyToBlock() has been called.
    762   bool CanGetCookies(const CookieList& cookie_list) const;
    763   bool CanSetCookie(const std::string& cookie_line,
    764                     CookieOptions* options) const;
    765   bool CanEnablePrivacyMode() const;
    766 
    767   // Called just before calling a delegate that may block a request.
    768   void OnCallToDelegate();
    769   // Called when the delegate lets a request continue.  Also called on
    770   // cancellation.
    771   void OnCallToDelegateComplete();
    772 
    773   // Contextual information used for this request. Cannot be NULL. This contains
    774   // most of the dependencies which are shared between requests (disk cache,
    775   // cookie store, socket pool, etc.)
    776   const URLRequestContext* context_;
    777 
    778   NetworkDelegate* network_delegate_;
    779 
    780   // Tracks the time spent in various load states throughout this request.
    781   BoundNetLog net_log_;
    782 
    783   scoped_refptr<URLRequestJob> job_;
    784   scoped_ptr<UploadDataStream> upload_data_stream_;
    785   std::vector<GURL> url_chain_;
    786   GURL first_party_for_cookies_;
    787   GURL delegate_redirect_url_;
    788   std::string method_;  // "GET", "POST", etc. Should be all uppercase.
    789   std::string referrer_;
    790   ReferrerPolicy referrer_policy_;
    791   HttpRequestHeaders extra_request_headers_;
    792   int load_flags_;  // Flags indicating the request type for the load;
    793                     // expected values are LOAD_* enums above.
    794 
    795   // Never access methods of the |delegate_| directly. Always use the
    796   // Notify... methods for this.
    797   Delegate* delegate_;
    798 
    799   // Current error status of the job. When no error has been encountered, this
    800   // will be SUCCESS. If multiple errors have been encountered, this will be
    801   // the first non-SUCCESS status seen.
    802   URLRequestStatus status_;
    803 
    804   // The HTTP response info, lazily initialized.
    805   HttpResponseInfo response_info_;
    806 
    807   // Tells us whether the job is outstanding. This is true from the time
    808   // Start() is called to the time we dispatch RequestComplete and indicates
    809   // whether the job is active.
    810   bool is_pending_;
    811 
    812   // Indicates if the request is in the process of redirecting to a new
    813   // location.  It is true from the time the headers complete until a
    814   // new request begins.
    815   bool is_redirecting_;
    816 
    817   // Number of times we're willing to redirect.  Used to guard against
    818   // infinite redirects.
    819   int redirect_limit_;
    820 
    821   // Cached value for use after we've orphaned the job handling the
    822   // first transaction in a request involving redirects.
    823   UploadProgress final_upload_progress_;
    824 
    825   // The priority level for this request.  Objects like
    826   // ClientSocketPool use this to determine which URLRequest to
    827   // allocate sockets to first.
    828   RequestPriority priority_;
    829 
    830   // TODO(battre): The only consumer of the identifier_ is currently the
    831   // web request API. We need to match identifiers of requests between the
    832   // web request API and the web navigation API. As the URLRequest does not
    833   // exist when the web navigation API is triggered, the tracking probably
    834   // needs to be done outside of the URLRequest anyway. Therefore, this
    835   // identifier should be deleted here. http://crbug.com/89321
    836   // A globally unique identifier for this request.
    837   const uint64 identifier_;
    838 
    839   // True if this request is currently calling a delegate, or is blocked waiting
    840   // for the URL request or network delegate to resume it.
    841   bool calling_delegate_;
    842 
    843   // An optional parameter that provides additional information about what
    844   // |this| is currently being blocked by.
    845   std::string blocked_by_;
    846   bool use_blocked_by_as_load_param_;
    847 
    848   base::debug::LeakTracker<URLRequest> leak_tracker_;
    849 
    850   // Callback passed to the network delegate to notify us when a blocked request
    851   // is ready to be resumed or canceled.
    852   CompletionCallback before_request_callback_;
    853 
    854   // Safe-guard to ensure that we do not send multiple "I am completed"
    855   // messages to network delegate.
    856   // TODO(battre): Remove this. http://crbug.com/89049
    857   bool has_notified_completion_;
    858 
    859   // Authentication data used by the NetworkDelegate for this request,
    860   // if one is present. |auth_credentials_| may be filled in when calling
    861   // |NotifyAuthRequired| on the NetworkDelegate. |auth_info_| holds
    862   // the authentication challenge being handled by |NotifyAuthRequired|.
    863   AuthCredentials auth_credentials_;
    864   scoped_refptr<AuthChallengeInfo> auth_info_;
    865 
    866   int64 received_response_content_length_;
    867 
    868   base::TimeTicks creation_time_;
    869 
    870   // Timing information for the most recent request.  Its start times are
    871   // populated during Start(), and the rest are populated in OnResponseReceived.
    872   LoadTimingInfo load_timing_info_;
    873 
    874   scoped_ptr<const base::debug::StackTrace> stack_trace_;
    875 
    876   // Keeps track of whether or not OnBeforeNetworkStart has been called yet.
    877   bool notified_before_network_start_;
    878 
    879   // The cookie store to be used for this request.
    880   scoped_refptr<CookieStore> cookie_store_;
    881 
    882   // The proxy server used for this request, if any.
    883   HostPortPair proxy_server_;
    884 
    885   DISALLOW_COPY_AND_ASSIGN(URLRequest);
    886 };
    887 
    888 }  // namespace net
    889 
    890 #endif  // NET_URL_REQUEST_URL_REQUEST_H_
    891