Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / url_request / url_request_http_job.h
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/filter/filter.h"
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
26 class HttpResponseHeaders;
27 class HttpResponseInfo;
28 class HttpTransaction;
29 class HttpUserAgentSettings;
30 class UploadDataStream;
31 class URLRequestContext;
32
33 // A URLRequestJob subclass that is built on top of HttpTransaction.  It
34 // provides an implementation for both HTTP and HTTPS.
35 class NET_EXPORT_PRIVATE URLRequestHttpJob : public URLRequestJob {
36  public:
37   static URLRequestJob* Factory(URLRequest* request,
38                                 NetworkDelegate* network_delegate,
39                                 const std::string& scheme);
40
41  protected:
42   URLRequestHttpJob(URLRequest* request,
43                     NetworkDelegate* network_delegate,
44                     const HttpUserAgentSettings* http_user_agent_settings);
45
46   virtual ~URLRequestHttpJob();
47
48   // Overridden from URLRequestJob:
49   virtual void SetPriority(RequestPriority priority) OVERRIDE;
50   virtual void Start() OVERRIDE;
51   virtual void Kill() OVERRIDE;
52
53   RequestPriority priority() const {
54     return priority_;
55   }
56
57  private:
58   enum CompletionCause {
59     ABORTED,
60     FINISHED
61   };
62
63   typedef base::RefCountedData<bool> SharedBoolean;
64
65   class HttpFilterContext;
66   class HttpTransactionDelegateImpl;
67
68   // Shadows URLRequestJob's version of this method so we can grab cookies.
69   void NotifyHeadersComplete();
70
71   // Shadows URLRequestJob's method so we can record histograms.
72   void NotifyDone(const URLRequestStatus& status);
73
74   void DestroyTransaction();
75
76   void AddExtraHeaders();
77   void AddCookieHeaderAndStart();
78   void SaveCookiesAndNotifyHeadersComplete(int result);
79   void SaveNextCookie();
80   void FetchResponseCookies(std::vector<std::string>* cookies);
81
82   // Processes the Strict-Transport-Security header, if one exists.
83   void ProcessStrictTransportSecurityHeader();
84
85   // Processes the Public-Key-Pins header, if one exists.
86   void ProcessPublicKeyPinsHeader();
87
88   // |result| should be net::OK, or the request is canceled.
89   void OnHeadersReceivedCallback(int result);
90   void OnStartCompleted(int result);
91   void OnReadCompleted(int result);
92   void NotifyBeforeSendHeadersCallback(int result);
93
94   void RestartTransactionWithAuth(const AuthCredentials& credentials);
95
96   // Overridden from URLRequestJob:
97   virtual void SetUpload(UploadDataStream* upload) OVERRIDE;
98   virtual void SetExtraRequestHeaders(
99       const HttpRequestHeaders& headers) OVERRIDE;
100   virtual LoadState GetLoadState() const OVERRIDE;
101   virtual UploadProgress GetUploadProgress() const OVERRIDE;
102   virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
103   virtual bool GetCharset(std::string* charset) OVERRIDE;
104   virtual void GetResponseInfo(HttpResponseInfo* info) OVERRIDE;
105   virtual void GetLoadTimingInfo(
106       LoadTimingInfo* load_timing_info) const OVERRIDE;
107   virtual bool GetResponseCookies(std::vector<std::string>* cookies) OVERRIDE;
108   virtual int GetResponseCode() const OVERRIDE;
109   virtual Filter* SetupFilter() const OVERRIDE;
110   virtual bool IsSafeRedirect(const GURL& location) OVERRIDE;
111   virtual bool NeedsAuth() OVERRIDE;
112   virtual void GetAuthChallengeInfo(scoped_refptr<AuthChallengeInfo>*) OVERRIDE;
113   virtual void SetAuth(const AuthCredentials& credentials) OVERRIDE;
114   virtual void CancelAuth() OVERRIDE;
115   virtual void ContinueWithCertificate(X509Certificate* client_cert) OVERRIDE;
116   virtual void ContinueDespiteLastError() OVERRIDE;
117   virtual void ResumeNetworkStart() OVERRIDE;
118   virtual bool ReadRawData(IOBuffer* buf, int buf_size,
119                            int* bytes_read) OVERRIDE;
120   virtual void StopCaching() OVERRIDE;
121   virtual bool GetFullRequestHeaders(
122       HttpRequestHeaders* headers) const OVERRIDE;
123   virtual int64 GetTotalReceivedBytes() const OVERRIDE;
124   virtual void DoneReading() OVERRIDE;
125   virtual HostPortPair GetSocketAddress() const OVERRIDE;
126   virtual void NotifyURLRequestDestroyed() OVERRIDE;
127
128   void RecordTimer();
129   void ResetTimer();
130
131   virtual void UpdatePacketReadTimes() OVERRIDE;
132   void RecordPacketStats(FilterContext::StatisticSelector statistic) const;
133
134   void RecordCompressionHistograms();
135   bool IsCompressibleContent() const;
136
137   // Starts the transaction if extensions using the webrequest API do not
138   // object.
139   void StartTransaction();
140   // If |result| is net::OK, calls StartTransactionInternal. Otherwise notifies
141   // cancellation.
142   void MaybeStartTransactionInternal(int result);
143   void StartTransactionInternal();
144
145   void RecordPerfHistograms(CompletionCause reason);
146   void DoneWithRequest(CompletionCause reason);
147
148   // Callback functions for Cookie Monster
149   void DoLoadCookies();
150   void CheckCookiePolicyAndLoad(const CookieList& cookie_list);
151   void OnCookiesLoaded(const std::string& cookie_line);
152   void DoStartTransaction();
153
154   // See the implementation for a description of save_next_cookie_running and
155   // callback_pending.
156   void OnCookieSaved(scoped_refptr<SharedBoolean> save_next_cookie_running,
157                      scoped_refptr<SharedBoolean> callback_pending,
158                      bool cookie_status);
159
160   // Some servers send the body compressed, but specify the content length as
161   // the uncompressed size. If this is the case, we return true in order
162   // to request to work around this non-adherence to the HTTP standard.
163   // |rv| is the standard return value of a read function indicating the number
164   // of bytes read or, if negative, an error code.
165   bool ShouldFixMismatchedContentLength(int rv) const;
166
167   // Returns the effective response headers, considering that they may be
168   // overridden by |override_response_headers_|.
169   HttpResponseHeaders* GetResponseHeaders() const;
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   const HttpUserAgentSettings* http_user_agent_settings_;
265
266   DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob);
267 };
268
269 }  // namespace net
270
271 #endif  // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_