Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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_HTTP_JOB_H_ |
| 6 | #define NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/compiler_specific.h" |
| 12 | #include "base/memory/scoped_ptr.h" |
| 13 | #include "base/memory/weak_ptr.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 14 | #include "base/time/time.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 15 | #include "net/base/auth.h" |
| 16 | #include "net/base/completion_callback.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 17 | #include "net/base/net_export.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | #include "net/cookies/cookie_store.h" |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 19 | #include "net/filter/filter.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 20 | #include "net/http/http_request_info.h" |
| 21 | #include "net/url_request/url_request_job.h" |
| 22 | #include "net/url_request/url_request_throttler_entry_interface.h" |
| 23 | |
| 24 | namespace net { |
| 25 | |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 26 | class HttpRequestHeaders; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 27 | class HttpResponseHeaders; |
| 28 | class HttpResponseInfo; |
| 29 | class HttpTransaction; |
| 30 | class HttpUserAgentSettings; |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 31 | class ProxyInfo; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 32 | class UploadDataStream; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 33 | class URLRequestContext; |
| 34 | |
| 35 | // A URLRequestJob subclass that is built on top of HttpTransaction. It |
| 36 | // provides an implementation for both HTTP and HTTPS. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 37 | class NET_EXPORT_PRIVATE URLRequestHttpJob : public URLRequestJob { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 38 | public: |
| 39 | static URLRequestJob* Factory(URLRequest* request, |
| 40 | NetworkDelegate* network_delegate, |
| 41 | const std::string& scheme); |
| 42 | |
| 43 | protected: |
| 44 | URLRequestHttpJob(URLRequest* request, |
| 45 | NetworkDelegate* network_delegate, |
| 46 | const HttpUserAgentSettings* http_user_agent_settings); |
| 47 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 48 | virtual ~URLRequestHttpJob(); |
| 49 | |
| 50 | // Overridden from URLRequestJob: |
| 51 | virtual void SetPriority(RequestPriority priority) OVERRIDE; |
| 52 | virtual void Start() OVERRIDE; |
| 53 | virtual void Kill() OVERRIDE; |
| 54 | |
| 55 | RequestPriority priority() const { |
| 56 | return priority_; |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | enum CompletionCause { |
| 61 | ABORTED, |
| 62 | FINISHED |
| 63 | }; |
| 64 | |
| 65 | typedef base::RefCountedData<bool> SharedBoolean; |
| 66 | |
| 67 | class HttpFilterContext; |
| 68 | class HttpTransactionDelegateImpl; |
| 69 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 70 | // Shadows URLRequestJob's version of this method so we can grab cookies. |
| 71 | void NotifyHeadersComplete(); |
| 72 | |
| 73 | // Shadows URLRequestJob's method so we can record histograms. |
| 74 | void NotifyDone(const URLRequestStatus& status); |
| 75 | |
| 76 | void DestroyTransaction(); |
| 77 | |
| 78 | void AddExtraHeaders(); |
| 79 | void AddCookieHeaderAndStart(); |
| 80 | void SaveCookiesAndNotifyHeadersComplete(int result); |
| 81 | void SaveNextCookie(); |
| 82 | void FetchResponseCookies(std::vector<std::string>* cookies); |
| 83 | |
| 84 | // Processes the Strict-Transport-Security header, if one exists. |
| 85 | void ProcessStrictTransportSecurityHeader(); |
| 86 | |
| 87 | // Processes the Public-Key-Pins header, if one exists. |
| 88 | void ProcessPublicKeyPinsHeader(); |
| 89 | |
| 90 | // |result| should be net::OK, or the request is canceled. |
| 91 | void OnHeadersReceivedCallback(int result); |
| 92 | void OnStartCompleted(int result); |
| 93 | void OnReadCompleted(int result); |
| 94 | void NotifyBeforeSendHeadersCallback(int result); |
Ben Murdoch | 116680a | 2014-07-20 18:25:52 -0700 | [diff] [blame] | 95 | void NotifyBeforeSendProxyHeadersCallback( |
| 96 | const ProxyInfo& proxy_info, |
| 97 | HttpRequestHeaders* request_headers); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 98 | |
| 99 | void RestartTransactionWithAuth(const AuthCredentials& credentials); |
| 100 | |
| 101 | // Overridden from URLRequestJob: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 102 | virtual void SetUpload(UploadDataStream* upload) OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 103 | virtual void SetExtraRequestHeaders( |
| 104 | const HttpRequestHeaders& headers) OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 105 | virtual LoadState GetLoadState() const OVERRIDE; |
| 106 | virtual UploadProgress GetUploadProgress() const OVERRIDE; |
| 107 | virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; |
| 108 | virtual bool GetCharset(std::string* charset) OVERRIDE; |
| 109 | virtual void GetResponseInfo(HttpResponseInfo* info) OVERRIDE; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 110 | virtual void GetLoadTimingInfo( |
| 111 | LoadTimingInfo* load_timing_info) const OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 112 | virtual bool GetResponseCookies(std::vector<std::string>* cookies) OVERRIDE; |
| 113 | virtual int GetResponseCode() const OVERRIDE; |
| 114 | virtual Filter* SetupFilter() const OVERRIDE; |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 115 | virtual bool CopyFragmentOnRedirect(const GURL& location) const OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 116 | virtual bool IsSafeRedirect(const GURL& location) OVERRIDE; |
| 117 | virtual bool NeedsAuth() OVERRIDE; |
| 118 | virtual void GetAuthChallengeInfo(scoped_refptr<AuthChallengeInfo>*) OVERRIDE; |
| 119 | virtual void SetAuth(const AuthCredentials& credentials) OVERRIDE; |
| 120 | virtual void CancelAuth() OVERRIDE; |
| 121 | virtual void ContinueWithCertificate(X509Certificate* client_cert) OVERRIDE; |
| 122 | virtual void ContinueDespiteLastError() OVERRIDE; |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 123 | virtual void ResumeNetworkStart() OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 124 | virtual bool ReadRawData(IOBuffer* buf, int buf_size, |
| 125 | int* bytes_read) OVERRIDE; |
| 126 | virtual void StopCaching() OVERRIDE; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 127 | virtual bool GetFullRequestHeaders( |
| 128 | HttpRequestHeaders* headers) const OVERRIDE; |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 129 | virtual int64 GetTotalReceivedBytes() const OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 130 | virtual void DoneReading() OVERRIDE; |
Ben Murdoch | effb81e | 2014-03-31 11:51:25 +0100 | [diff] [blame] | 131 | virtual void DoneReadingRedirectResponse() OVERRIDE; |
| 132 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 133 | virtual HostPortPair GetSocketAddress() const OVERRIDE; |
| 134 | virtual void NotifyURLRequestDestroyed() OVERRIDE; |
| 135 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 136 | void RecordTimer(); |
| 137 | void ResetTimer(); |
| 138 | |
| 139 | virtual void UpdatePacketReadTimes() OVERRIDE; |
| 140 | void RecordPacketStats(FilterContext::StatisticSelector statistic) const; |
| 141 | |
| 142 | void RecordCompressionHistograms(); |
| 143 | bool IsCompressibleContent() const; |
| 144 | |
| 145 | // Starts the transaction if extensions using the webrequest API do not |
| 146 | // object. |
| 147 | void StartTransaction(); |
| 148 | // If |result| is net::OK, calls StartTransactionInternal. Otherwise notifies |
| 149 | // cancellation. |
| 150 | void MaybeStartTransactionInternal(int result); |
| 151 | void StartTransactionInternal(); |
| 152 | |
| 153 | void RecordPerfHistograms(CompletionCause reason); |
| 154 | void DoneWithRequest(CompletionCause reason); |
| 155 | |
| 156 | // Callback functions for Cookie Monster |
| 157 | void DoLoadCookies(); |
| 158 | void CheckCookiePolicyAndLoad(const CookieList& cookie_list); |
| 159 | void OnCookiesLoaded(const std::string& cookie_line); |
| 160 | void DoStartTransaction(); |
| 161 | |
| 162 | // See the implementation for a description of save_next_cookie_running and |
| 163 | // callback_pending. |
| 164 | void OnCookieSaved(scoped_refptr<SharedBoolean> save_next_cookie_running, |
| 165 | scoped_refptr<SharedBoolean> callback_pending, |
| 166 | bool cookie_status); |
| 167 | |
| 168 | // Some servers send the body compressed, but specify the content length as |
| 169 | // the uncompressed size. If this is the case, we return true in order |
| 170 | // to request to work around this non-adherence to the HTTP standard. |
| 171 | // |rv| is the standard return value of a read function indicating the number |
| 172 | // of bytes read or, if negative, an error code. |
| 173 | bool ShouldFixMismatchedContentLength(int rv) const; |
| 174 | |
| 175 | // Returns the effective response headers, considering that they may be |
| 176 | // overridden by |override_response_headers_|. |
| 177 | HttpResponseHeaders* GetResponseHeaders() const; |
| 178 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 179 | RequestPriority priority_; |
| 180 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 181 | HttpRequestInfo request_info_; |
| 182 | const HttpResponseInfo* response_info_; |
| 183 | |
| 184 | std::vector<std::string> response_cookies_; |
| 185 | size_t response_cookies_save_index_; |
| 186 | base::Time response_date_; |
| 187 | |
| 188 | // Auth states for proxy and origin server. |
| 189 | AuthState proxy_auth_state_; |
| 190 | AuthState server_auth_state_; |
| 191 | AuthCredentials auth_credentials_; |
| 192 | |
| 193 | CompletionCallback start_callback_; |
| 194 | CompletionCallback notify_before_headers_sent_callback_; |
| 195 | |
| 196 | bool read_in_progress_; |
| 197 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 198 | scoped_ptr<HttpTransaction> transaction_; |
| 199 | |
| 200 | // This is used to supervise traffic and enforce exponential |
| 201 | // back-off. May be NULL. |
| 202 | scoped_refptr<URLRequestThrottlerEntryInterface> throttling_entry_; |
| 203 | |
| 204 | // Indicated if an SDCH dictionary was advertised, and hence an SDCH |
| 205 | // compressed response is expected. We use this to help detect (accidental?) |
| 206 | // proxy corruption of a response, which sometimes marks SDCH content as |
| 207 | // having no content encoding <oops>. |
| 208 | bool sdch_dictionary_advertised_; |
| 209 | |
| 210 | // For SDCH latency experiments, when we are able to do SDCH, we may enable |
| 211 | // either an SDCH latency test xor a pass through test. The following bools |
| 212 | // indicate what we decided on for this instance. |
| 213 | bool sdch_test_activated_; // Advertising a dictionary for sdch. |
| 214 | bool sdch_test_control_; // Not even accepting-content sdch. |
| 215 | |
| 216 | // For recording of stats, we need to remember if this is cached content. |
| 217 | bool is_cached_content_; |
| 218 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 219 | base::Time request_creation_time_; |
| 220 | |
| 221 | // Data used for statistics gathering. This data is only used for histograms |
| 222 | // and is not required. It is only gathered if packet_timing_enabled_ == true. |
| 223 | // |
| 224 | // TODO(jar): improve the quality of the gathered info by gathering most times |
| 225 | // at a lower point in the network stack, assuring we have actual packet |
| 226 | // boundaries, rather than approximations. Also note that input byte count |
| 227 | // as gathered here is post-SSL, and post-cache-fetch, and does not reflect |
| 228 | // true packet arrival times in such cases. |
| 229 | |
| 230 | // Enable recording of packet arrival times for histogramming. |
| 231 | bool packet_timing_enabled_; |
| 232 | bool done_; // True when we are done doing work. |
| 233 | |
| 234 | // The number of bytes that have been accounted for in packets (where some of |
| 235 | // those packets may possibly have had their time of arrival recorded). |
| 236 | int64 bytes_observed_in_packets_; |
| 237 | |
| 238 | // The request time may not be available when we are being destroyed, so we |
| 239 | // snapshot it early on. |
| 240 | base::Time request_time_snapshot_; |
| 241 | |
| 242 | // Since we don't save all packet times in packet_times_, we save the |
| 243 | // last time for use in histograms. |
| 244 | base::Time final_packet_time_; |
| 245 | |
| 246 | // The start time for the job, ignoring re-starts. |
| 247 | base::TimeTicks start_time_; |
| 248 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 249 | // When the transaction finished reading the request headers. |
| 250 | base::TimeTicks receive_headers_end_; |
| 251 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 252 | scoped_ptr<HttpFilterContext> filter_context_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 253 | |
| 254 | CompletionCallback on_headers_received_callback_; |
| 255 | |
| 256 | // We allow the network delegate to modify a copy of the response headers. |
| 257 | // This prevents modifications of headers that are shared with the underlying |
| 258 | // layers of the network stack. |
| 259 | scoped_refptr<HttpResponseHeaders> override_response_headers_; |
| 260 | |
Ben Murdoch | effb81e | 2014-03-31 11:51:25 +0100 | [diff] [blame] | 261 | // The network delegate can mark a URL as safe for redirection. |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 262 | // The reference fragment of the original URL is not appended to the redirect |
| 263 | // URL when the redirect URL is equal to |allowed_unsafe_redirect_url_|. |
Ben Murdoch | effb81e | 2014-03-31 11:51:25 +0100 | [diff] [blame] | 264 | GURL allowed_unsafe_redirect_url_; |
| 265 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 266 | // Flag used to verify that |this| is not deleted while we are awaiting |
| 267 | // a callback from the NetworkDelegate. Used as a fail-fast mechanism. |
| 268 | // True if we are waiting a callback and |
| 269 | // NetworkDelegate::NotifyURLRequestDestroyed has not been called, yet, |
| 270 | // to inform the NetworkDelegate that it may not call back. |
| 271 | bool awaiting_callback_; |
| 272 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 273 | const HttpUserAgentSettings* http_user_agent_settings_; |
| 274 | |
Torne (Richard Coles) | cedac22 | 2014-06-03 10:58:34 +0100 | [diff] [blame] | 275 | base::WeakPtrFactory<URLRequestHttpJob> weak_factory_; |
| 276 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 277 | DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob); |
| 278 | }; |
| 279 | |
| 280 | } // namespace net |
| 281 | |
| 282 | #endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_ |