1// Copyright (c) 2006-2009 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 <set>
9#include <string>
10#include <vector>
11
12#include "base/scoped_ptr.h"
13#include "net/base/auth.h"
14#include "net/base/completion_callback.h"
15#include "net/http/http_request_info.h"
16#include "net/url_request/url_request_job.h"
17
18namespace net {
19class HttpResponseInfo;
20class HttpTransaction;
21}
22class URLRequestContext;
23
24// A URLRequestJob subclass that is built on top of HttpTransaction.  It
25// provides an implementation for both HTTP and HTTPS.
26class URLRequestHttpJob : public URLRequestJob {
27 public:
28  static URLRequestJob* Factory(URLRequest* request, const std::string& scheme);
29
30 protected:
31  explicit URLRequestHttpJob(URLRequest* request);
32
33  // URLRequestJob methods:
34  virtual void SetUpload(net::UploadData* upload);
35  virtual void SetExtraRequestHeaders(const std::string& headers);
36  virtual void Start();
37  virtual void Kill();
38  virtual net::LoadState GetLoadState() const;
39  virtual uint64 GetUploadProgress() const;
40  virtual bool GetMimeType(std::string* mime_type) const;
41  virtual bool GetCharset(std::string* charset);
42  virtual void GetResponseInfo(net::HttpResponseInfo* info);
43  virtual bool GetResponseCookies(std::vector<std::string>* cookies);
44  virtual int GetResponseCode() const;
45  virtual bool GetContentEncodings(
46      std::vector<Filter::FilterType>* encoding_type);
47  virtual bool IsCachedContent() const { return is_cached_content_; }
48  virtual bool IsSdchResponse() const;
49  virtual bool IsSafeRedirect(const GURL& location);
50  virtual bool NeedsAuth();
51  virtual void GetAuthChallengeInfo(scoped_refptr<net::AuthChallengeInfo>*);
52  virtual void SetAuth(const std::wstring& username,
53                       const std::wstring& password);
54  virtual void CancelAuth();
55  virtual void ContinueWithCertificate(net::X509Certificate* client_cert);
56  virtual void ContinueDespiteLastError();
57  virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read);
58
59  // Shadows URLRequestJob's version of this method so we can grab cookies.
60  void NotifyHeadersComplete();
61
62  void DestroyTransaction();
63  void StartTransaction();
64  void AddExtraHeaders();
65  void AddCookieHeaderAndStart();
66  void SaveCookiesAndNotifyHeadersComplete();
67  void SaveNextCookie();
68  void FetchResponseCookies(const net::HttpResponseInfo* response_info,
69                            std::vector<std::string>* cookies);
70
71  // Process the Strict-Transport-Security header, if one exists.
72  void ProcessStrictTransportSecurityHeader();
73
74  void OnCanGetCookiesCompleted(int result);
75  void OnCanSetCookieCompleted(int result);
76  void OnStartCompleted(int result);
77  void OnReadCompleted(int result);
78
79  bool ShouldTreatAsCertificateError(int result);
80
81  void RestartTransactionWithAuth(const std::wstring& username,
82                                  const std::wstring& password);
83
84  // Keep a reference to the url request context to be sure it's not deleted
85  // before us.
86  scoped_refptr<URLRequestContext> context_;
87
88  net::HttpRequestInfo request_info_;
89  const net::HttpResponseInfo* response_info_;
90
91  std::vector<std::string> response_cookies_;
92  size_t response_cookies_save_index_;
93
94  // Auth states for proxy and origin server.
95  net::AuthState proxy_auth_state_;
96  net::AuthState server_auth_state_;
97
98  std::wstring username_;
99  std::wstring password_;
100
101  net::CompletionCallbackImpl<URLRequestHttpJob> can_get_cookies_callback_;
102  net::CompletionCallbackImpl<URLRequestHttpJob> can_set_cookie_callback_;
103  net::CompletionCallbackImpl<URLRequestHttpJob> start_callback_;
104  net::CompletionCallbackImpl<URLRequestHttpJob> read_callback_;
105
106  bool read_in_progress_;
107
108  // An URL for an SDCH dictionary as suggested in a Get-Dictionary HTTP header.
109  GURL sdch_dictionary_url_;
110
111  scoped_ptr<net::HttpTransaction> transaction_;
112
113  // Indicated if an SDCH dictionary was advertised, and hence an SDCH
114  // compressed response is expected.  We use this to help detect (accidental?)
115  // proxy corruption of a response, which sometimes marks SDCH content as
116  // having no content encoding <oops>.
117  bool sdch_dictionary_advertised_;
118
119  // For SDCH latency experiments, when we are able to do SDCH, we may enable
120  // either an SDCH latency test xor a pass through test.  The following bools
121  // indicate what we decided on for this instance.
122  bool sdch_test_activated_;  // Advertising a dictionary for sdch.
123  bool sdch_test_control_;    // Not even accepting-content sdch.
124
125  // For recording of stats, we need to remember if this is cached content.
126  bool is_cached_content_;
127
128 private:
129  virtual ~URLRequestHttpJob();
130
131  DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob);
132};
133
134#endif  // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_
135