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