Upstream version 7.36.149.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 CopyFragmentOnRedirect(const GURL& location) const OVERRIDE;
111   virtual bool IsSafeRedirect(const GURL& location) OVERRIDE;
112   virtual bool NeedsAuth() OVERRIDE;
113   virtual void GetAuthChallengeInfo(scoped_refptr<AuthChallengeInfo>*) OVERRIDE;
114   virtual void SetAuth(const AuthCredentials& credentials) OVERRIDE;
115   virtual void CancelAuth() OVERRIDE;
116   virtual void ContinueWithCertificate(X509Certificate* client_cert) OVERRIDE;
117   virtual void ContinueDespiteLastError() OVERRIDE;
118   virtual void ResumeNetworkStart() OVERRIDE;
119   virtual bool ReadRawData(IOBuffer* buf, int buf_size,
120                            int* bytes_read) OVERRIDE;
121   virtual void StopCaching() OVERRIDE;
122   virtual bool GetFullRequestHeaders(
123       HttpRequestHeaders* headers) const OVERRIDE;
124   virtual int64 GetTotalReceivedBytes() const OVERRIDE;
125   virtual void DoneReading() OVERRIDE;
126   virtual void DoneReadingRedirectResponse() OVERRIDE;
127
128   virtual HostPortPair GetSocketAddress() const OVERRIDE;
129   virtual void NotifyURLRequestDestroyed() OVERRIDE;
130
131   void RecordTimer();
132   void ResetTimer();
133
134   virtual void UpdatePacketReadTimes() OVERRIDE;
135   void RecordPacketStats(FilterContext::StatisticSelector statistic) const;
136
137   void RecordCompressionHistograms();
138   bool IsCompressibleContent() const;
139
140   // Starts the transaction if extensions using the webrequest API do not
141   // object.
142   void StartTransaction();
143   // If |result| is net::OK, calls StartTransactionInternal. Otherwise notifies
144   // cancellation.
145   void MaybeStartTransactionInternal(int result);
146   void StartTransactionInternal();
147
148   void RecordPerfHistograms(CompletionCause reason);
149   void DoneWithRequest(CompletionCause reason);
150
151   // Callback functions for Cookie Monster
152   void DoLoadCookies();
153   void CheckCookiePolicyAndLoad(const CookieList& cookie_list);
154   void OnCookiesLoaded(const std::string& cookie_line);
155   void DoStartTransaction();
156
157   // See the implementation for a description of save_next_cookie_running and
158   // callback_pending.
159   void OnCookieSaved(scoped_refptr<SharedBoolean> save_next_cookie_running,
160                      scoped_refptr<SharedBoolean> callback_pending,
161                      bool cookie_status);
162
163   // Some servers send the body compressed, but specify the content length as
164   // the uncompressed size. If this is the case, we return true in order
165   // to request to work around this non-adherence to the HTTP standard.
166   // |rv| is the standard return value of a read function indicating the number
167   // of bytes read or, if negative, an error code.
168   bool ShouldFixMismatchedContentLength(int rv) const;
169
170   // Returns the effective response headers, considering that they may be
171   // overridden by |override_response_headers_|.
172   HttpResponseHeaders* GetResponseHeaders() const;
173
174   RequestPriority priority_;
175
176   HttpRequestInfo request_info_;
177   const HttpResponseInfo* response_info_;
178
179   std::vector<std::string> response_cookies_;
180   size_t response_cookies_save_index_;
181   base::Time response_date_;
182
183   // Auth states for proxy and origin server.
184   AuthState proxy_auth_state_;
185   AuthState server_auth_state_;
186   AuthCredentials auth_credentials_;
187
188   CompletionCallback start_callback_;
189   CompletionCallback notify_before_headers_sent_callback_;
190
191   bool read_in_progress_;
192
193   // An URL for an SDCH dictionary as suggested in a Get-Dictionary HTTP header.
194   GURL sdch_dictionary_url_;
195
196   scoped_ptr<HttpTransaction> transaction_;
197
198   // This is used to supervise traffic and enforce exponential
199   // back-off.  May be NULL.
200   scoped_refptr<URLRequestThrottlerEntryInterface> throttling_entry_;
201
202   // Indicated if an SDCH dictionary was advertised, and hence an SDCH
203   // compressed response is expected.  We use this to help detect (accidental?)
204   // proxy corruption of a response, which sometimes marks SDCH content as
205   // having no content encoding <oops>.
206   bool sdch_dictionary_advertised_;
207
208   // For SDCH latency experiments, when we are able to do SDCH, we may enable
209   // either an SDCH latency test xor a pass through test.  The following bools
210   // indicate what we decided on for this instance.
211   bool sdch_test_activated_;  // Advertising a dictionary for sdch.
212   bool sdch_test_control_;    // Not even accepting-content sdch.
213
214   // For recording of stats, we need to remember if this is cached content.
215   bool is_cached_content_;
216
217   base::Time request_creation_time_;
218
219   // Data used for statistics gathering. This data is only used for histograms
220   // and is not required. It is only gathered if packet_timing_enabled_ == true.
221   //
222   // TODO(jar): improve the quality of the gathered info by gathering most times
223   // at a lower point in the network stack, assuring we have actual packet
224   // boundaries, rather than approximations.  Also note that input byte count
225   // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
226   // true packet arrival times in such cases.
227
228   // Enable recording of packet arrival times for histogramming.
229   bool packet_timing_enabled_;
230   bool done_;  // True when we are done doing work.
231
232   // The number of bytes that have been accounted for in packets (where some of
233   // those packets may possibly have had their time of arrival recorded).
234   int64 bytes_observed_in_packets_;
235
236   // The request time may not be available when we are being destroyed, so we
237   // snapshot it early on.
238   base::Time request_time_snapshot_;
239
240   // Since we don't save all packet times in packet_times_, we save the
241   // last time for use in histograms.
242   base::Time final_packet_time_;
243
244   // The start time for the job, ignoring re-starts.
245   base::TimeTicks start_time_;
246
247   // When the transaction finished reading the request headers.
248   base::TimeTicks receive_headers_end_;
249
250   scoped_ptr<HttpFilterContext> filter_context_;
251   base::WeakPtrFactory<URLRequestHttpJob> weak_factory_;
252
253   CompletionCallback on_headers_received_callback_;
254
255   // We allow the network delegate to modify a copy of the response headers.
256   // This prevents modifications of headers that are shared with the underlying
257   // layers of the network stack.
258   scoped_refptr<HttpResponseHeaders> override_response_headers_;
259
260   // The network delegate can mark a URL as safe for redirection.
261   // The reference fragment of the original URL is not appended to the redirect
262   // URL when the redirect URL is equal to |allowed_unsafe_redirect_url_|.
263   GURL allowed_unsafe_redirect_url_;
264
265   // Flag used to verify that |this| is not deleted while we are awaiting
266   // a callback from the NetworkDelegate. Used as a fail-fast mechanism.
267   // True if we are waiting a callback and
268   // NetworkDelegate::NotifyURLRequestDestroyed has not been called, yet,
269   // to inform the NetworkDelegate that it may not call back.
270   bool awaiting_callback_;
271
272   const HttpUserAgentSettings* http_user_agent_settings_;
273
274   DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob);
275 };
276
277 }  // namespace net
278
279 #endif  // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_