blob: 3256c04b18ee43cadf308ff99617650ae66d217d [file] [log] [blame]
[email protected]5a3b4d32011-03-17 01:24:051// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]175adac2008-07-30 17:28:045#include "net/url_request/url_request_http_job.h"
initial.commit586acc5fe2008-07-26 22:42:526
[email protected]4ed2755f2008-12-15 09:01:337#include "base/base_switches.h"
8#include "base/command_line.h"
[email protected]39ce5c02008-08-22 04:03:449#include "base/compiler_specific.h"
[email protected]60889422008-09-23 01:18:1610#include "base/file_util.h"
11#include "base/file_version_info.h"
initial.commit586acc5fe2008-07-26 22:42:5212#include "base/message_loop.h"
[email protected]8684a8812011-03-22 13:59:3813#include "base/metrics/field_trial.h"
[email protected]ec23f522011-02-22 21:01:3814#include "base/metrics/histogram.h"
[email protected]5b90b5d2009-04-30 23:06:0115#include "base/rand_util.h"
initial.commit586acc5fe2008-07-26 22:42:5216#include "base/string_util.h"
[email protected]dd29bcd72011-03-24 00:03:4417#include "base/time.h"
[email protected]a9cea7542009-05-20 04:30:2318#include "net/base/cert_status_flags.h"
[email protected]9349cfb2010-08-31 18:00:5319#include "net/base/cookie_store.h"
[email protected]423041b2008-10-27 17:39:2820#include "net/base/filter.h"
[email protected]6d81b482011-02-22 19:47:1921#include "net/base/host_port_pair.h"
[email protected]b8430722008-09-17 20:05:4422#include "net/base/load_flags.h"
[email protected]dd29bcd72011-03-24 00:03:4423#include "net/base/mime_util.h"
initial.commit586acc5fe2008-07-26 22:42:5224#include "net/base/net_errors.h"
25#include "net/base/net_util.h"
[email protected]636eccd2011-06-28 12:28:0126#include "net/base/network_delegate.h"
[email protected]60889422008-09-23 01:18:1627#include "net/base/sdch_manager.h"
[email protected]0b45559b2009-06-12 21:45:1128#include "net/base/ssl_cert_request_info.h"
[email protected]ee1edb472011-05-05 23:31:4629#include "net/base/ssl_config_service.h"
[email protected]6d81b482011-02-22 19:47:1930#include "net/base/transport_security_state.h"
[email protected]87c99b6a2011-05-13 20:06:4831#include "net/http/http_mac_signature.h"
[email protected]8c76ae22010-04-20 22:15:4332#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2133#include "net/http/http_response_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/http/http_response_info.h"
35#include "net/http/http_transaction.h"
36#include "net/http/http_transaction_factory.h"
[email protected]0757e7702009-03-27 04:00:2237#include "net/http/http_util.h"
[email protected]be4d55fe2010-06-01 13:40:0238#include "net/url_request/https_prober.h"
initial.commit586acc5fe2008-07-26 22:42:5239#include "net/url_request/url_request.h"
[email protected]319d9e6f2009-02-18 19:47:2140#include "net/url_request/url_request_context.h"
initial.commit586acc5fe2008-07-26 22:42:5241#include "net/url_request/url_request_error_job.h"
[email protected]06965e02009-09-04 21:36:4242#include "net/url_request/url_request_redirect_job.h"
[email protected]6b3f9642010-11-25 02:29:0643#include "net/url_request/url_request_throttler_header_adapter.h"
44#include "net/url_request/url_request_throttler_manager.h"
initial.commit586acc5fe2008-07-26 22:42:5245
[email protected]8c76ae22010-04-20 22:15:4346static const char kAvailDictionaryHeader[] = "Avail-Dictionary";
47
[email protected]dd29bcd72011-03-24 00:03:4448// When histogramming results related to SDCH and/or an SDCH latency test, the
49// number of packets for which we need to record arrival times so as to
50// calculate interpacket latencies. We currently are only looking at the
51// first few packets, as we're monitoring the impact of the initial TCP
52// congestion window on stalling of transmissions.
53static const size_t kSdchPacketHistogramCount = 5;
54
[email protected]4f5656c62010-12-13 10:47:0955namespace net {
56
57namespace {
58
[email protected]87c99b6a2011-05-13 20:06:4859void AddAuthorizationHeader(
60 const std::vector<CookieStore::CookieInfo>& cookie_infos,
61 HttpRequestInfo* request_info) {
62 const GURL& url = request_info->url;
63 const std::string& method = request_info->method;
64 std::string request_uri = HttpUtil::PathForRequest(url);
65 const std::string& host = url.host();
66 int port = url.EffectiveIntPort();
67 for (size_t i = 0; i < cookie_infos.size(); ++i) {
68 HttpMacSignature signature;
69 if (!signature.AddStateInfo(cookie_infos[i].name,
[email protected]c416fa82011-05-16 22:18:4170 cookie_infos[i].creation_date,
[email protected]87c99b6a2011-05-13 20:06:4871 cookie_infos[i].mac_key,
72 cookie_infos[i].mac_algorithm)) {
73 continue;
74 }
75 if (!signature.AddHttpInfo(method, request_uri, host, port))
76 continue;
77 request_info->extra_headers.SetHeader(
78 HttpRequestHeaders::kAuthorization,
79 signature.GenerateAuthorizationHeader());
80 return; // Only add the first valid header.
81 }
82}
83
[email protected]4f5656c62010-12-13 10:47:0984class HTTPSProberDelegateImpl : public HTTPSProberDelegate {
85 public:
86 HTTPSProberDelegateImpl(const std::string& host, int max_age,
87 bool include_subdomains,
88 TransportSecurityState* sts)
89 : host_(host),
90 max_age_(max_age),
91 include_subdomains_(include_subdomains),
92 sts_(sts) { }
93
94 virtual void ProbeComplete(bool result) {
95 if (result) {
96 base::Time current_time(base::Time::Now());
97 base::TimeDelta max_age_delta = base::TimeDelta::FromSeconds(max_age_);
98
99 TransportSecurityState::DomainState domain_state;
100 domain_state.expiry = current_time + max_age_delta;
101 domain_state.mode =
102 TransportSecurityState::DomainState::MODE_OPPORTUNISTIC;
103 domain_state.include_subdomains = include_subdomains_;
104
105 sts_->EnableHost(host_, domain_state);
106 }
107
108 delete this;
109 }
110
111 private:
112 const std::string host_;
113 const int max_age_;
114 const bool include_subdomains_;
115 scoped_refptr<TransportSecurityState> sts_;
116};
117
118} // namespace
119
[email protected]2e92354c2011-03-25 20:49:53120class URLRequestHttpJob::HttpFilterContext : public FilterContext {
121 public:
122 explicit HttpFilterContext(URLRequestHttpJob* job);
123 virtual ~HttpFilterContext();
124
125 // FilterContext implementation.
126 virtual bool GetMimeType(std::string* mime_type) const;
127 virtual bool GetURL(GURL* gurl) const;
128 virtual base::Time GetRequestTime() const;
129 virtual bool IsCachedContent() const;
130 virtual bool IsDownload() const;
131 virtual bool IsSdchResponse() const;
132 virtual int64 GetByteReadCount() const;
133 virtual int GetResponseCode() const;
134 virtual void RecordPacketStats(StatisticSelector statistic) const;
135
[email protected]46668fe52011-05-04 19:03:23136 // Method to allow us to reset filter context for a response that should have
137 // been SDCH encoded when there is an update due to an explicit HTTP header.
138 void ResetSdchResponseToFalse();
139
[email protected]2e92354c2011-03-25 20:49:53140 private:
141 URLRequestHttpJob* job_;
142
143 DISALLOW_COPY_AND_ASSIGN(HttpFilterContext);
144};
145
[email protected]fc01f232011-03-17 19:06:01146URLRequestHttpJob::HttpFilterContext::HttpFilterContext(URLRequestHttpJob* job)
147 : job_(job) {
148 DCHECK(job_);
149}
150
151URLRequestHttpJob::HttpFilterContext::~HttpFilterContext() {
152}
153
154bool URLRequestHttpJob::HttpFilterContext::GetMimeType(
155 std::string* mime_type) const {
156 return job_->GetMimeType(mime_type);
157}
158
159bool URLRequestHttpJob::HttpFilterContext::GetURL(GURL* gurl) const {
160 if (!job_->request())
161 return false;
162 *gurl = job_->request()->url();
163 return true;
164}
165
166base::Time URLRequestHttpJob::HttpFilterContext::GetRequestTime() const {
167 return job_->request() ? job_->request()->request_time() : base::Time();
168}
169
170bool URLRequestHttpJob::HttpFilterContext::IsCachedContent() const {
[email protected]dd29bcd72011-03-24 00:03:44171 return job_->is_cached_content_;
[email protected]fc01f232011-03-17 19:06:01172}
173
174bool URLRequestHttpJob::HttpFilterContext::IsDownload() const {
175 return (job_->request_info_.load_flags & LOAD_IS_DOWNLOAD) != 0;
176}
177
[email protected]46668fe52011-05-04 19:03:23178void URLRequestHttpJob::HttpFilterContext::ResetSdchResponseToFalse() {
179 DCHECK(job_->sdch_dictionary_advertised_);
180 job_->sdch_dictionary_advertised_ = false;
181}
182
[email protected]fc01f232011-03-17 19:06:01183bool URLRequestHttpJob::HttpFilterContext::IsSdchResponse() const {
184 return job_->sdch_dictionary_advertised_;
185}
186
187int64 URLRequestHttpJob::HttpFilterContext::GetByteReadCount() const {
188 return job_->filter_input_byte_count();
189}
190
191int URLRequestHttpJob::HttpFilterContext::GetResponseCode() const {
192 return job_->GetResponseCode();
193}
194
195void URLRequestHttpJob::HttpFilterContext::RecordPacketStats(
196 StatisticSelector statistic) const {
197 job_->RecordPacketStats(statistic);
198}
199
initial.commit586acc5fe2008-07-26 22:42:52200// TODO(darin): make sure the port blocking code is not lost
initial.commit586acc5fe2008-07-26 22:42:52201// static
[email protected]4f5656c62010-12-13 10:47:09202URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request,
203 const std::string& scheme) {
initial.commit586acc5fe2008-07-26 22:42:52204 DCHECK(scheme == "http" || scheme == "https");
205
[email protected]bcb84f8b2009-08-31 16:20:14206 int port = request->url().IntPort();
[email protected]4f5656c62010-12-13 10:47:09207 if (!IsPortAllowedByDefault(port) && !IsPortAllowedByOverride(port))
208 return new URLRequestErrorJob(request, ERR_UNSAFE_PORT);
initial.commit586acc5fe2008-07-26 22:42:52209
210 if (!request->context() ||
211 !request->context()->http_transaction_factory()) {
212 NOTREACHED() << "requires a valid context";
[email protected]4f5656c62010-12-13 10:47:09213 return new URLRequestErrorJob(request, ERR_INVALID_ARGUMENT);
initial.commit586acc5fe2008-07-26 22:42:52214 }
215
[email protected]4f5656c62010-12-13 10:47:09216 TransportSecurityState::DomainState domain_state;
[email protected]90b153012009-09-10 18:35:16217 if (scheme == "http" &&
[email protected]326e6792009-12-11 21:04:42218 request->context()->transport_security_state() &&
219 request->context()->transport_security_state()->IsEnabledForHost(
[email protected]b7f9fb22011-04-09 20:28:47220 &domain_state,
221 request->url().host(),
[email protected]2b838132011-05-05 22:00:24222 SSLConfigService::IsSNIAvailable(
223 request->context()->ssl_config_service()))) {
[email protected]326e6792009-12-11 21:04:42224 if (domain_state.mode ==
[email protected]4f5656c62010-12-13 10:47:09225 TransportSecurityState::DomainState::MODE_STRICT) {
[email protected]326e6792009-12-11 21:04:42226 DCHECK_EQ(request->url().scheme(), "http");
227 url_canon::Replacements<char> replacements;
228 static const char kNewScheme[] = "https";
229 replacements.SetScheme(kNewScheme,
230 url_parse::Component(0, strlen(kNewScheme)));
231 GURL new_location = request->url().ReplaceComponents(replacements);
232 return new URLRequestRedirectJob(request, new_location);
233 } else {
234 // TODO(agl): implement opportunistic HTTPS upgrade.
235 }
[email protected]06965e02009-09-04 21:36:42236 }
[email protected]4ed2755f2008-12-15 09:01:33237
[email protected]175adac2008-07-30 17:28:04238 return new URLRequestHttpJob(request);
initial.commit586acc5fe2008-07-26 22:42:52239}
240
[email protected]5394e422011-01-20 22:07:43241
[email protected]4f5656c62010-12-13 10:47:09242URLRequestHttpJob::URLRequestHttpJob(URLRequest* request)
243 : URLRequestJob(request),
initial.commit586acc5fe2008-07-26 22:42:52244 response_info_(NULL),
[email protected]34602282010-02-03 22:14:15245 response_cookies_save_index_(0),
[email protected]4f5656c62010-12-13 10:47:09246 proxy_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
247 server_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
[email protected]34602282010-02-03 22:14:15248 ALLOW_THIS_IN_INITIALIZER_LIST(start_callback_(
249 this, &URLRequestHttpJob::OnStartCompleted)),
250 ALLOW_THIS_IN_INITIALIZER_LIST(read_callback_(
251 this, &URLRequestHttpJob::OnReadCompleted)),
[email protected]636eccd2011-06-28 12:28:01252 ALLOW_THIS_IN_INITIALIZER_LIST(notify_before_headers_sent_callback_(
253 this, &URLRequestHttpJob::NotifyBeforeSendHeadersCallback)),
[email protected]3589e552008-08-20 23:11:34254 read_in_progress_(false),
[email protected]2aecf7382009-06-17 04:14:27255 transaction_(NULL),
[email protected]4f5656c62010-12-13 10:47:09256 throttling_entry_(URLRequestThrottlerManager::GetInstance()->
[email protected]6b3f9642010-11-25 02:29:06257 RegisterRequestUrl(request->url())),
[email protected]5b90b5d2009-04-30 23:06:01258 sdch_dictionary_advertised_(false),
259 sdch_test_activated_(false),
[email protected]d8fd5132009-05-15 01:06:53260 sdch_test_control_(false),
[email protected]00e48bf2010-12-03 06:15:42261 is_cached_content_(false),
[email protected]ec23f522011-02-22 21:01:38262 request_creation_time_(),
[email protected]dd29bcd72011-03-24 00:03:44263 packet_timing_enabled_(false),
[email protected]bbaea8f2011-06-24 00:11:01264 done_(false),
[email protected]dd29bcd72011-03-24 00:03:44265 bytes_observed_in_packets_(0),
266 packet_times_(),
267 request_time_snapshot_(),
268 final_packet_time_(),
269 observed_packet_count_(0),
[email protected]2e92354c2011-03-25 20:49:53270 ALLOW_THIS_IN_INITIALIZER_LIST(
271 filter_context_(new HttpFilterContext(this))),
[email protected]00e48bf2010-12-03 06:15:42272 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
[email protected]ec23f522011-02-22 21:01:38273 ResetTimer();
initial.commit586acc5fe2008-07-26 22:42:52274}
275
[email protected]175adac2008-07-30 17:28:04276void URLRequestHttpJob::NotifyHeadersComplete() {
initial.commit586acc5fe2008-07-26 22:42:52277 DCHECK(!response_info_);
278
279 response_info_ = transaction_->GetResponseInfo();
280
[email protected]d8fd5132009-05-15 01:06:53281 // Save boolean, as we'll need this info at destruction time, and filters may
282 // also need this info.
283 is_cached_content_ = response_info_->was_cached;
284
[email protected]6b3f9642010-11-25 02:29:06285 if (!is_cached_content_) {
[email protected]4f5656c62010-12-13 10:47:09286 URLRequestThrottlerHeaderAdapter response_adapter(
[email protected]6b3f9642010-11-25 02:29:06287 response_info_->headers);
[email protected]2fd33ee92011-03-25 22:30:21288 throttling_entry_->UpdateWithResponse(request_info_.url.host(),
289 &response_adapter);
[email protected]6b3f9642010-11-25 02:29:06290 }
291
[email protected]77f6fb432009-09-05 14:21:09292 ProcessStrictTransportSecurityHeader();
[email protected]a9cea7542009-05-20 04:30:23293
[email protected]fe219872008-09-23 02:17:00294 if (SdchManager::Global() &&
295 SdchManager::Global()->IsInSupportedDomain(request_->url())) {
[email protected]c7bef94c2011-06-21 18:05:51296 static const std::string name = "Get-Dictionary";
[email protected]60889422008-09-23 01:18:16297 std::string url_text;
298 void* iter = NULL;
299 // TODO(jar): We need to not fetch dictionaries the first time they are
300 // seen, but rather wait until we can justify their usefulness.
301 // For now, we will only fetch the first dictionary, which will at least
302 // require multiple suggestions before we get additional ones for this site.
303 // Eventually we should wait until a dictionary is requested several times
304 // before we even download it (so that we don't waste memory or bandwidth).
[email protected]c7bef94c2011-06-21 18:05:51305 if (response_info_->headers->EnumerateHeader(&iter, name, &url_text)) {
[email protected]d55ad15d2009-02-17 19:40:50306 // request_->url() won't be valid in the destructor, so we use an
307 // alternate copy.
[email protected]dd29bcd72011-03-24 00:03:44308 DCHECK_EQ(request_->url(), request_info_.url);
[email protected]d55ad15d2009-02-17 19:40:50309 // Resolve suggested URL relative to request url.
310 sdch_dictionary_url_ = request_info_.url.Resolve(url_text);
[email protected]60889422008-09-23 01:18:16311 }
312 }
313
[email protected]0757e7702009-03-27 04:00:22314 // The HTTP transaction may be restarted several times for the purposes
315 // of sending authorization information. Each time it restarts, we get
316 // notified of the headers completion so that we can update the cookie store.
317 if (transaction_->IsReadyToRestartForAuth()) {
318 DCHECK(!response_info_->auth_challenge.get());
[email protected]87a09a92011-07-14 15:50:50319 // TODO(battre): This breaks the webrequest API for
320 // URLRequestTestHTTP.BasicAuthWithCookies
321 // where OnBeforeSendHeaders -> OnRequestSent -> OnBeforeSendHeaders
322 // occurs.
[email protected]13c8a092010-07-29 06:15:44323 RestartTransactionWithAuth(string16(), string16());
[email protected]0757e7702009-03-27 04:00:22324 return;
325 }
326
[email protected]4f5656c62010-12-13 10:47:09327 URLRequestJob::NotifyHeadersComplete();
initial.commit586acc5fe2008-07-26 22:42:52328}
329
[email protected]85c1dce2011-07-06 12:01:29330void URLRequestHttpJob::NotifyDone(const URLRequestStatus& status) {
[email protected]bbaea8f2011-06-24 00:11:01331 DoneWithRequest(FINISHED);
[email protected]dd29bcd72011-03-24 00:03:44332 URLRequestJob::NotifyDone(status);
333}
334
[email protected]175adac2008-07-30 17:28:04335void URLRequestHttpJob::DestroyTransaction() {
[email protected]c6a4eb92010-03-03 23:51:19336 DCHECK(transaction_.get());
initial.commit586acc5fe2008-07-26 22:42:52337
[email protected]bbaea8f2011-06-24 00:11:01338 DoneWithRequest(ABORTED);
[email protected]af4876d2008-10-21 23:10:57339 transaction_.reset();
initial.commit586acc5fe2008-07-26 22:42:52340 response_info_ = NULL;
[email protected]fa4332d2010-11-23 09:59:09341 context_ = NULL;
initial.commit586acc5fe2008-07-26 22:42:52342}
343
[email protected]175adac2008-07-30 17:28:04344void URLRequestHttpJob::StartTransaction() {
[email protected]636eccd2011-06-28 12:28:01345 if (request_->context() && request_->context()->network_delegate()) {
346 int rv = request_->context()->network_delegate()->NotifyBeforeSendHeaders(
347 request_, &notify_before_headers_sent_callback_,
348 &request_info_.extra_headers);
349 // If an extension blocks the request, we rely on the callback to
350 // StartTransactionInternal().
351 if (rv == ERR_IO_PENDING) {
352 request_->net_log().BeginEvent(
353 NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL);
354 return;
355 }
356 }
357 StartTransactionInternal();
358}
359
360void URLRequestHttpJob::NotifyBeforeSendHeadersCallback(int result) {
361 request_->net_log().EndEvent(
362 NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL);
363
364 if (result == OK) {
365 StartTransactionInternal();
366 } else {
367 // TODO(battre): Allow passing information of the extension that canceled
368 // the event.
369 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
370 make_scoped_refptr(new NetLogStringParameter("source", "delegate")));
371 NotifyCanceled();
372 }
373}
374
375void URLRequestHttpJob::StartTransactionInternal() {
initial.commit586acc5fe2008-07-26 22:42:52376 // NOTE: This method assumes that request_info_ is already setup properly.
377
[email protected]34602282010-02-03 22:14:15378 // If we already have a transaction, then we should restart the transaction
379 // with auth provided by username_ and password_.
initial.commit586acc5fe2008-07-26 22:42:52380
[email protected]99c07902010-08-17 18:59:52381 int rv;
[email protected]6b3f9642010-11-25 02:29:06382
[email protected]34602282010-02-03 22:14:15383 if (transaction_.get()) {
[email protected]99c07902010-08-17 18:59:52384 rv = transaction_->RestartWithAuth(username_, password_, &start_callback_);
[email protected]34602282010-02-03 22:14:15385 username_.clear();
386 password_.clear();
387 } else {
388 DCHECK(request_->context());
389 DCHECK(request_->context()->http_transaction_factory());
initial.commit586acc5fe2008-07-26 22:42:52390
[email protected]99c07902010-08-17 18:59:52391 rv = request_->context()->http_transaction_factory()->CreateTransaction(
392 &transaction_);
[email protected]4f5656c62010-12-13 10:47:09393 if (rv == OK) {
[email protected]227b0e82011-03-25 21:11:53394 if (!URLRequestThrottlerManager::GetInstance()->enforce_throttling() ||
395 !throttling_entry_->IsDuringExponentialBackoff()) {
396 rv = transaction_->Start(
397 &request_info_, &start_callback_, request_->net_log());
[email protected]bbaea8f2011-06-24 00:11:01398 start_time_ = base::TimeTicks::Now();
[email protected]227b0e82011-03-25 21:11:53399 } else {
400 // Special error code for the exponential back-off module.
401 rv = ERR_TEMPORARILY_THROTTLED;
402 }
[email protected]fa4332d2010-11-23 09:59:09403 // Make sure the context is alive for the duration of the
404 // transaction.
405 context_ = request_->context();
[email protected]34602282010-02-03 22:14:15406 }
initial.commit586acc5fe2008-07-26 22:42:52407 }
408
[email protected]4f5656c62010-12-13 10:47:09409 if (rv == ERR_IO_PENDING)
[email protected]34602282010-02-03 22:14:15410 return;
411
initial.commit586acc5fe2008-07-26 22:42:52412 // The transaction started synchronously, but we need to notify the
[email protected]4f5656c62010-12-13 10:47:09413 // URLRequest delegate via the message loop.
[email protected]00e48bf2010-12-03 06:15:42414 MessageLoop::current()->PostTask(
415 FROM_HERE,
416 method_factory_.NewRunnableMethod(
417 &URLRequestHttpJob::OnStartCompleted, rv));
initial.commit586acc5fe2008-07-26 22:42:52418}
419
[email protected]175adac2008-07-30 17:28:04420void URLRequestHttpJob::AddExtraHeaders() {
[email protected]c7bef94c2011-06-21 18:05:51421 // Supply Accept-Encoding field only if it is not already provided.
422 // It should be provided IF the content is known to have restrictions on
423 // potential encoding, such as streaming multi-media.
424 // For details see bug 47381.
425 // TODO(jar, enal): jpeg files etc. should set up a request header if
426 // possible. Right now it is done only by buffered_resource_loader and
427 // simple_data_source.
428 if (!request_info_.extra_headers.HasHeader(
429 HttpRequestHeaders::kAcceptEncoding)) {
430 bool advertise_sdch = SdchManager::Global() &&
431 SdchManager::Global()->IsInSupportedDomain(request_->url());
432 std::string avail_dictionaries;
433 if (advertise_sdch) {
434 SdchManager::Global()->GetAvailDictionaryList(request_->url(),
435 &avail_dictionaries);
[email protected]5b90b5d2009-04-30 23:06:01436
[email protected]c7bef94c2011-06-21 18:05:51437 // The AllowLatencyExperiment() is only true if we've successfully done a
438 // full SDCH compression recently in this browser session for this host.
439 // Note that for this path, there might be no applicable dictionaries,
440 // and hence we can't participate in the experiment.
441 if (!avail_dictionaries.empty() &&
442 SdchManager::Global()->AllowLatencyExperiment(request_->url())) {
443 // We are participating in the test (or control), and hence we'll
444 // eventually record statistics via either SDCH_EXPERIMENT_DECODE or
445 // SDCH_EXPERIMENT_HOLDBACK, and we'll need some packet timing data.
446 packet_timing_enabled_ = true;
447 if (base::RandDouble() < .01) {
448 sdch_test_control_ = true; // 1% probability.
449 advertise_sdch = false;
450 } else {
451 sdch_test_activated_ = true;
452 }
[email protected]5b90b5d2009-04-30 23:06:01453 }
454 }
[email protected]5b90b5d2009-04-30 23:06:01455
[email protected]c7bef94c2011-06-21 18:05:51456 // Supply Accept-Encoding headers first so that it is more likely that they
457 // will be in the first transmitted packet. This can sometimes make it
458 // easier to filter and analyze the streams to assure that a proxy has not
459 // damaged these headers. Some proxies deliberately corrupt Accept-Encoding
460 // headers.
461 if (!advertise_sdch) {
462 // Tell the server what compression formats we support (other than SDCH).
[email protected]8c76ae22010-04-20 22:15:43463 request_info_.extra_headers.SetHeader(
[email protected]c7bef94c2011-06-21 18:05:51464 HttpRequestHeaders::kAcceptEncoding, "gzip,deflate");
465 } else {
466 // Include SDCH in acceptable list.
467 request_info_.extra_headers.SetHeader(
468 HttpRequestHeaders::kAcceptEncoding, "gzip,deflate,sdch");
469 if (!avail_dictionaries.empty()) {
470 request_info_.extra_headers.SetHeader(
471 kAvailDictionaryHeader,
472 avail_dictionaries);
473 sdch_dictionary_advertised_ = true;
474 // Since we're tagging this transaction as advertising a dictionary,
475 // we'll definitely employ an SDCH filter (or tentative sdch filter)
476 // when we get a response. When done, we'll record histograms via
477 // SDCH_DECODE or SDCH_PASSTHROUGH. Hence we need to record packet
478 // arrival times.
479 packet_timing_enabled_ = true;
480 }
[email protected]423041b2008-10-27 17:39:28481 }
[email protected]423041b2008-10-27 17:39:28482 }
483
[email protected]ede96662011-07-14 12:34:18484 const URLRequestContext* context = request_->context();
initial.commit586acc5fe2008-07-26 22:42:52485 if (context) {
[email protected]09a362d32009-09-24 18:01:33486 // Only add default Accept-Language and Accept-Charset if the request
487 // didn't have them specified.
[email protected]6dae6b82011-03-30 00:35:34488 if (!context->accept_language().empty()) {
489 request_info_.extra_headers.SetHeaderIfMissing(
490 HttpRequestHeaders::kAcceptLanguage,
491 context->accept_language());
492 }
493 if (!context->accept_charset().empty()) {
494 request_info_.extra_headers.SetHeaderIfMissing(
495 HttpRequestHeaders::kAcceptCharset,
496 context->accept_charset());
497 }
initial.commit586acc5fe2008-07-26 22:42:52498 }
initial.commit586acc5fe2008-07-26 22:42:52499}
500
[email protected]34602282010-02-03 22:14:15501void URLRequestHttpJob::AddCookieHeaderAndStart() {
502 // No matter what, we want to report our status as IO pending since we will
503 // be notifying our consumer asynchronously via OnStartCompleted.
504 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
[email protected]861fcd52009-08-26 02:33:46505
[email protected]ed24fad2011-05-10 22:44:01506 // If the request was destroyed, then there is no more work to do.
507 if (!request_)
508 return;
[email protected]34602282010-02-03 22:14:15509
[email protected]ed24fad2011-05-10 22:44:01510 bool allow = true;
[email protected]a83dd332011-07-13 10:41:01511 if ((request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES) ||
512 !CanGetCookies()) {
[email protected]ed24fad2011-05-10 22:44:01513 allow = false;
[email protected]0757e7702009-03-27 04:00:22514 }
[email protected]34602282010-02-03 22:14:15515
[email protected]9025016c2011-05-12 15:51:23516 if (request_->context()->cookie_store() && allow) {
517 CookieOptions options;
518 options.set_include_httponly();
[email protected]87c99b6a2011-05-13 20:06:48519 std::string cookie_line;
520 std::vector<CookieStore::CookieInfo> cookie_infos;
521 request_->context()->cookie_store()->GetCookiesWithInfo(
522 request_->url(), options, &cookie_line, &cookie_infos);
523 if (!cookie_line.empty()) {
[email protected]9025016c2011-05-12 15:51:23524 request_info_.extra_headers.SetHeader(
[email protected]87c99b6a2011-05-13 20:06:48525 HttpRequestHeaders::kCookie, cookie_line);
[email protected]9025016c2011-05-12 15:51:23526 }
[email protected]87c99b6a2011-05-13 20:06:48527 if (URLRequest::AreMacCookiesEnabled())
528 AddAuthorizationHeader(cookie_infos, &request_info_);
[email protected]9025016c2011-05-12 15:51:23529 }
530 // We may have been canceled within CanGetCookies.
531 if (GetStatus().is_success()) {
532 StartTransaction();
533 } else {
534 NotifyCanceled();
535 }
[email protected]0757e7702009-03-27 04:00:22536}
537
[email protected]34602282010-02-03 22:14:15538void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete() {
539 DCHECK(transaction_.get());
initial.commit586acc5fe2008-07-26 22:42:52540
[email protected]4f5656c62010-12-13 10:47:09541 const HttpResponseInfo* response_info = transaction_->GetResponseInfo();
[email protected]34602282010-02-03 22:14:15542 DCHECK(response_info);
543
544 response_cookies_.clear();
545 response_cookies_save_index_ = 0;
546
547 FetchResponseCookies(response_info, &response_cookies_);
548
549 // Now, loop over the response cookies, and attempt to persist each.
550 SaveNextCookie();
551}
552
553void URLRequestHttpJob::SaveNextCookie() {
554 if (response_cookies_save_index_ == response_cookies_.size()) {
555 response_cookies_.clear();
556 response_cookies_save_index_ = 0;
557 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
558 NotifyHeadersComplete();
559 return;
560 }
561
562 // No matter what, we want to report our status as IO pending since we will
563 // be notifying our consumer asynchronously via OnStartCompleted.
564 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
565
[email protected]ed24fad2011-05-10 22:44:01566 CookieOptions options;
[email protected]7c75b4c2011-07-02 14:38:05567 if (!(request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) &&
[email protected]a83dd332011-07-13 10:41:01568 request_->context()->cookie_store()) {
[email protected]ed24fad2011-05-10 22:44:01569 CookieOptions options;
570 options.set_include_httponly();
[email protected]a83dd332011-07-13 10:41:01571 if (CanSetCookie(
572 response_cookies_[response_cookies_save_index_], &options)) {
[email protected]ed24fad2011-05-10 22:44:01573 request_->context()->cookie_store()->SetCookieWithOptions(
574 request_->url(), response_cookies_[response_cookies_save_index_],
575 options);
576 }
[email protected]34602282010-02-03 22:14:15577 }
578
[email protected]9025016c2011-05-12 15:51:23579 response_cookies_save_index_++;
580 // We may have been canceled within OnSetCookie.
581 if (GetStatus().is_success()) {
582 SaveNextCookie();
583 } else {
584 NotifyCanceled();
585 }
[email protected]34602282010-02-03 22:14:15586}
587
588void URLRequestHttpJob::FetchResponseCookies(
[email protected]4f5656c62010-12-13 10:47:09589 const HttpResponseInfo* response_info,
[email protected]34602282010-02-03 22:14:15590 std::vector<std::string>* cookies) {
initial.commit586acc5fe2008-07-26 22:42:52591 std::string name = "Set-Cookie";
592 std::string value;
593
594 void* iter = NULL;
[email protected]2adf2882010-09-27 08:30:37595 while (response_info->headers->EnumerateHeader(&iter, name, &value)) {
596 if (!value.empty())
597 cookies->push_back(value);
598 }
initial.commit586acc5fe2008-07-26 22:42:52599}
[email protected]a9cea7542009-05-20 04:30:23600
[email protected]77f6fb432009-09-05 14:21:09601void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() {
[email protected]a9cea7542009-05-20 04:30:23602 DCHECK(response_info_);
603
[email protected]ede96662011-07-14 12:34:18604 const URLRequestContext* ctx = request_->context();
[email protected]326e6792009-12-11 21:04:42605 if (!ctx || !ctx->transport_security_state())
[email protected]a9cea7542009-05-20 04:30:23606 return;
607
[email protected]326e6792009-12-11 21:04:42608 const bool https = response_info_->ssl_info.is_valid();
609 const bool valid_https =
[email protected]4f5656c62010-12-13 10:47:09610 https && !IsCertStatusError(response_info_->ssl_info.cert_status);
[email protected]326e6792009-12-11 21:04:42611
[email protected]77f6fb432009-09-05 14:21:09612 std::string name = "Strict-Transport-Security";
[email protected]a9cea7542009-05-20 04:30:23613 std::string value;
614
[email protected]326e6792009-12-11 21:04:42615 int max_age;
616 bool include_subdomains;
617
[email protected]a9cea7542009-05-20 04:30:23618 void* iter = NULL;
[email protected]77f6fb432009-09-05 14:21:09619 while (response_info_->headers->EnumerateHeader(&iter, name, &value)) {
[email protected]4f5656c62010-12-13 10:47:09620 const bool ok = TransportSecurityState::ParseHeader(
[email protected]326e6792009-12-11 21:04:42621 value, &max_age, &include_subdomains);
622 if (!ok)
623 continue;
624 // We will only accept strict mode if we saw the header from an HTTPS
625 // connection with no certificate problems.
626 if (!valid_https)
627 continue;
628 base::Time current_time(base::Time::Now());
629 base::TimeDelta max_age_delta = base::TimeDelta::FromSeconds(max_age);
630
[email protected]4f5656c62010-12-13 10:47:09631 TransportSecurityState::DomainState domain_state;
[email protected]326e6792009-12-11 21:04:42632 domain_state.expiry = current_time + max_age_delta;
[email protected]4f5656c62010-12-13 10:47:09633 domain_state.mode = TransportSecurityState::DomainState::MODE_STRICT;
[email protected]326e6792009-12-11 21:04:42634 domain_state.include_subdomains = include_subdomains;
635
636 ctx->transport_security_state()->EnableHost(request_info_.url.host(),
637 domain_state);
638 }
639
640 // TODO(agl): change this over when we have fixed things at the server end.
641 // The string should be "Opportunistic-Transport-Security";
642 name = "X-Bodge-Transport-Security";
643
644 while (response_info_->headers->EnumerateHeader(&iter, name, &value)) {
[email protected]4f5656c62010-12-13 10:47:09645 const bool ok = TransportSecurityState::ParseHeader(
[email protected]326e6792009-12-11 21:04:42646 value, &max_age, &include_subdomains);
647 if (!ok)
648 continue;
649 // If we saw an opportunistic request over HTTPS, then clearly we can make
650 // HTTPS connections to the host so we should remember this.
651 if (https) {
652 base::Time current_time(base::Time::Now());
653 base::TimeDelta max_age_delta = base::TimeDelta::FromSeconds(max_age);
654
[email protected]4f5656c62010-12-13 10:47:09655 TransportSecurityState::DomainState domain_state;
[email protected]326e6792009-12-11 21:04:42656 domain_state.expiry = current_time + max_age_delta;
657 domain_state.mode =
[email protected]4f5656c62010-12-13 10:47:09658 TransportSecurityState::DomainState::MODE_SPDY_ONLY;
[email protected]326e6792009-12-11 21:04:42659 domain_state.include_subdomains = include_subdomains;
660
661 ctx->transport_security_state()->EnableHost(request_info_.url.host(),
662 domain_state);
663 continue;
664 }
665
666 if (!request())
667 break;
668
669 // At this point, we have a request for opportunistic encryption over HTTP.
670 // In this case we need to probe to check that we can make HTTPS
671 // connections to that host.
[email protected]4f5656c62010-12-13 10:47:09672 HTTPSProber* const prober = HTTPSProber::GetInstance();
[email protected]326e6792009-12-11 21:04:42673 if (prober->HaveProbed(request_info_.url.host()) ||
674 prober->InFlight(request_info_.url.host())) {
675 continue;
676 }
677
[email protected]4f5656c62010-12-13 10:47:09678 HTTPSProberDelegateImpl* delegate =
679 new HTTPSProberDelegateImpl(request_info_.url.host(), max_age,
680 include_subdomains,
681 ctx->transport_security_state());
[email protected]326e6792009-12-11 21:04:42682 if (!prober->ProbeHost(request_info_.url.host(), request()->context(),
683 delegate)) {
684 delete delegate;
685 }
[email protected]77f6fb432009-09-05 14:21:09686 }
[email protected]a9cea7542009-05-20 04:30:23687}
[email protected]4f5656c62010-12-13 10:47:09688
[email protected]5394e422011-01-20 22:07:43689void URLRequestHttpJob::OnStartCompleted(int result) {
[email protected]ec23f522011-02-22 21:01:38690 RecordTimer();
691
[email protected]5394e422011-01-20 22:07:43692 // If the request was destroyed, then there is no more work to do.
[email protected]a83dd332011-07-13 10:41:01693 if (!request_)
[email protected]5394e422011-01-20 22:07:43694 return;
695
696 // If the transaction was destroyed, then the job was cancelled, and
697 // we can just ignore this notification.
698 if (!transaction_.get())
699 return;
700
701 // Clear the IO_PENDING status
702 SetStatus(URLRequestStatus());
703
[email protected]381e8852011-04-14 14:30:58704 // Take care of any mandates for public key pinning.
705 // TODO(agl): we might have an issue here where a request for foo.example.com
706 // merges into a SPDY connection to www.example.com, and gets a different
707 // certificate.
708 const SSLInfo& ssl_info = transaction_->GetResponseInfo()->ssl_info;
709 if (result == OK &&
710 ssl_info.is_valid() &&
[email protected]a84396e2011-04-28 20:26:58711 ssl_info.is_issued_by_known_root &&
[email protected]381e8852011-04-14 14:30:58712 context_->transport_security_state()) {
713 TransportSecurityState::DomainState domain_state;
[email protected]dee9ae92011-04-26 03:58:30714 if (context_->transport_security_state()->HasPinsForHost(
[email protected]381e8852011-04-14 14:30:58715 &domain_state,
716 request_->url().host(),
[email protected]2b838132011-05-05 22:00:24717 SSLConfigService::IsSNIAvailable(
718 context_->ssl_config_service()))) {
[email protected]a84396e2011-04-28 20:26:58719 if (!domain_state.IsChainOfPublicKeysPermitted(
720 ssl_info.public_key_hashes)) {
721 result = ERR_CERT_INVALID;
722 UMA_HISTOGRAM_BOOLEAN("Net.CertificatePinSuccess", false);
723 } else {
724 UMA_HISTOGRAM_BOOLEAN("Net.CertificatePinSuccess", true);
725 }
[email protected]381e8852011-04-14 14:30:58726 }
727 }
728
[email protected]5394e422011-01-20 22:07:43729 if (result == OK) {
730 SaveCookiesAndNotifyHeadersComplete();
731 } else if (ShouldTreatAsCertificateError(result)) {
732 // We encountered an SSL certificate error. Ask our delegate to decide
733 // what we should do.
734 // TODO(wtc): also pass ssl_info.cert_status, or just pass the whole
735 // ssl_info.
[email protected]a83dd332011-07-13 10:41:01736 NotifySSLCertificateError(
737 result, transaction_->GetResponseInfo()->ssl_info.cert);
[email protected]5394e422011-01-20 22:07:43738 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
[email protected]a83dd332011-07-13 10:41:01739 NotifyCertificateRequested(
740 transaction_->GetResponseInfo()->cert_request_info);
[email protected]5394e422011-01-20 22:07:43741 } else {
742 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
743 }
744}
745
746void URLRequestHttpJob::OnReadCompleted(int result) {
747 read_in_progress_ = false;
748
[email protected]85c1dce2011-07-06 12:01:29749 if (ShouldFixMismatchedContentLength(result))
750 result = 0;
751
[email protected]5394e422011-01-20 22:07:43752 if (result == 0) {
753 NotifyDone(URLRequestStatus());
754 } else if (result < 0) {
755 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
756 } else {
757 // Clear the IO_PENDING status
758 SetStatus(URLRequestStatus());
759 }
760
761 NotifyReadComplete(result);
762}
763
764bool URLRequestHttpJob::ShouldTreatAsCertificateError(int result) {
765 if (!IsCertificateError(result))
766 return false;
767
[email protected]047c2722011-03-28 15:07:22768 // Revocation check failures are always certificate errors, even if the host
769 // is using Strict-Transport-Security.
[email protected]9e72e8c2011-03-28 15:03:16770 if (result == ERR_CERT_UNABLE_TO_CHECK_REVOCATION)
771 return true;
772
[email protected]5394e422011-01-20 22:07:43773 // Check whether our context is using Strict-Transport-Security.
774 if (!context_->transport_security_state())
775 return true;
776
777 TransportSecurityState::DomainState domain_state;
778 // TODO(agl): don't ignore opportunistic mode.
779 const bool r = context_->transport_security_state()->IsEnabledForHost(
[email protected]2b838132011-05-05 22:00:24780 &domain_state, request_info_.url.host(),
781 SSLConfigService::IsSNIAvailable(context_->ssl_config_service()));
[email protected]5394e422011-01-20 22:07:43782
783 return !r || domain_state.mode ==
784 TransportSecurityState::DomainState::MODE_OPPORTUNISTIC;
785}
786
787void URLRequestHttpJob::RestartTransactionWithAuth(
788 const string16& username,
789 const string16& password) {
790 username_ = username;
791 password_ = password;
792
793 // These will be reset in OnStartCompleted.
794 response_info_ = NULL;
795 response_cookies_.clear();
796
[email protected]ec23f522011-02-22 21:01:38797 ResetTimer();
798
[email protected]5394e422011-01-20 22:07:43799 // Update the cookies, since the cookie store may have been updated from the
800 // headers in the 401/407. Since cookies were already appended to
801 // extra_headers, we need to strip them out before adding them again.
802 request_info_.extra_headers.RemoveHeader(
803 HttpRequestHeaders::kCookie);
804
805 AddCookieHeaderAndStart();
806}
807
808void URLRequestHttpJob::SetUpload(UploadData* upload) {
809 DCHECK(!transaction_.get()) << "cannot change once started";
810 request_info_.upload_data = upload;
811}
812
813void URLRequestHttpJob::SetExtraRequestHeaders(
814 const HttpRequestHeaders& headers) {
815 DCHECK(!transaction_.get()) << "cannot change once started";
816 request_info_.extra_headers.CopyFrom(headers);
817}
818
819void URLRequestHttpJob::Start() {
820 DCHECK(!transaction_.get());
821
822 // Ensure that we do not send username and password fields in the referrer.
823 GURL referrer(request_->GetSanitizedReferrer());
824
825 request_info_.url = request_->url();
[email protected]5394e422011-01-20 22:07:43826 request_info_.method = request_->method();
827 request_info_.load_flags = request_->load_flags();
828 request_info_.priority = request_->priority();
[email protected]4875ba12011-03-30 22:31:51829 request_info_.request_id = request_->identifier();
[email protected]5394e422011-01-20 22:07:43830
[email protected]c10450102011-06-27 09:06:16831 // Strip Referer from request_info_.extra_headers to prevent, e.g., plugins
832 // from overriding headers that are controlled using other means. Otherwise a
833 // plugin could set a referrer although sending the referrer is inhibited.
834 request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kReferer);
835
836 // Our consumer should have made sure that this is a safe referrer. See for
837 // instance WebCore::FrameLoader::HideReferrer.
838 if (referrer.is_valid()) {
839 request_info_.extra_headers.SetHeader(HttpRequestHeaders::kReferer,
840 referrer.spec());
841 }
842
[email protected]5394e422011-01-20 22:07:43843 if (request_->context()) {
[email protected]50ba404b2011-01-21 13:38:45844 request_info_.extra_headers.SetHeaderIfMissing(
[email protected]5394e422011-01-20 22:07:43845 HttpRequestHeaders::kUserAgent,
846 request_->context()->GetUserAgent(request_->url()));
847 }
848
849 AddExtraHeaders();
850 AddCookieHeaderAndStart();
851}
852
853void URLRequestHttpJob::Kill() {
854 if (!transaction_.get())
855 return;
856
857 DestroyTransaction();
858 URLRequestJob::Kill();
859}
860
861LoadState URLRequestHttpJob::GetLoadState() const {
862 return transaction_.get() ?
863 transaction_->GetLoadState() : LOAD_STATE_IDLE;
864}
865
866uint64 URLRequestHttpJob::GetUploadProgress() const {
867 return transaction_.get() ? transaction_->GetUploadProgress() : 0;
868}
869
870bool URLRequestHttpJob::GetMimeType(std::string* mime_type) const {
871 DCHECK(transaction_.get());
872
873 if (!response_info_)
874 return false;
875
876 return response_info_->headers->GetMimeType(mime_type);
877}
878
879bool URLRequestHttpJob::GetCharset(std::string* charset) {
880 DCHECK(transaction_.get());
881
882 if (!response_info_)
883 return false;
884
885 return response_info_->headers->GetCharset(charset);
886}
887
888void URLRequestHttpJob::GetResponseInfo(HttpResponseInfo* info) {
889 DCHECK(request_);
890 DCHECK(transaction_.get());
891
892 if (response_info_)
893 *info = *response_info_;
894}
895
896bool URLRequestHttpJob::GetResponseCookies(
897 std::vector<std::string>* cookies) {
898 DCHECK(transaction_.get());
899
900 if (!response_info_)
901 return false;
902
903 // TODO(darin): Why are we extracting response cookies again? Perhaps we
904 // should just leverage response_cookies_.
905
906 cookies->clear();
907 FetchResponseCookies(response_info_, cookies);
908 return true;
909}
910
911int URLRequestHttpJob::GetResponseCode() const {
912 DCHECK(transaction_.get());
913
914 if (!response_info_)
915 return -1;
916
917 return response_info_->headers->response_code();
918}
919
[email protected]5a3b4d32011-03-17 01:24:05920Filter* URLRequestHttpJob::SetupFilter() const {
[email protected]5394e422011-01-20 22:07:43921 DCHECK(transaction_.get());
922 if (!response_info_)
[email protected]5a3b4d32011-03-17 01:24:05923 return NULL;
[email protected]5394e422011-01-20 22:07:43924
[email protected]5a3b4d32011-03-17 01:24:05925 std::vector<Filter::FilterType> encoding_types;
[email protected]5394e422011-01-20 22:07:43926 std::string encoding_type;
927 void* iter = NULL;
928 while (response_info_->headers->EnumerateHeader(&iter, "Content-Encoding",
929 &encoding_type)) {
[email protected]5a3b4d32011-03-17 01:24:05930 encoding_types.push_back(Filter::ConvertEncodingToType(encoding_type));
[email protected]5394e422011-01-20 22:07:43931 }
932
[email protected]46668fe52011-05-04 19:03:23933 if (filter_context_->IsSdchResponse()) {
934 // We are wary of proxies that discard or damage SDCH encoding. If a server
935 // explicitly states that this is not SDCH content, then we can correct our
936 // assumption that this is an SDCH response, and avoid the need to recover
937 // as though the content is corrupted (when we discover it is not SDCH
938 // encoded).
939 std::string sdch_response_status;
940 iter = NULL;
941 while (response_info_->headers->EnumerateHeader(&iter, "X-Sdch-Encode",
942 &sdch_response_status)) {
943 if (sdch_response_status == "0") {
944 filter_context_->ResetSdchResponseToFalse();
945 break;
946 }
947 }
948 }
949
[email protected]5394e422011-01-20 22:07:43950 // Even if encoding types are empty, there is a chance that we need to add
951 // some decoding, as some proxies strip encoding completely. In such cases,
952 // we may need to add (for example) SDCH filtering (when the context suggests
953 // it is appropriate).
[email protected]2e92354c2011-03-25 20:49:53954 Filter::FixupEncodingTypes(*filter_context_, &encoding_types);
[email protected]5394e422011-01-20 22:07:43955
[email protected]5a3b4d32011-03-17 01:24:05956 return !encoding_types.empty()
[email protected]2e92354c2011-03-25 20:49:53957 ? Filter::Factory(encoding_types, *filter_context_) : NULL;
[email protected]5394e422011-01-20 22:07:43958}
959
[email protected]5394e422011-01-20 22:07:43960bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) {
961 // We only allow redirects to certain "safe" protocols. This does not
962 // restrict redirects to externally handled protocols. Our consumer would
963 // need to take care of those.
964
965 if (!URLRequest::IsHandledURL(location))
966 return true;
967
968 static const char* kSafeSchemes[] = {
969 "http",
970 "https",
971 "ftp"
972 };
973
974 for (size_t i = 0; i < arraysize(kSafeSchemes); ++i) {
975 if (location.SchemeIs(kSafeSchemes[i]))
976 return true;
977 }
978
979 return false;
980}
981
982bool URLRequestHttpJob::NeedsAuth() {
983 int code = GetResponseCode();
984 if (code == -1)
985 return false;
986
987 // Check if we need either Proxy or WWW Authentication. This could happen
988 // because we either provided no auth info, or provided incorrect info.
989 switch (code) {
990 case 407:
991 if (proxy_auth_state_ == AUTH_STATE_CANCELED)
992 return false;
993 proxy_auth_state_ = AUTH_STATE_NEED_AUTH;
994 return true;
995 case 401:
996 if (server_auth_state_ == AUTH_STATE_CANCELED)
997 return false;
998 server_auth_state_ = AUTH_STATE_NEED_AUTH;
999 return true;
1000 }
1001 return false;
1002}
1003
1004void URLRequestHttpJob::GetAuthChallengeInfo(
1005 scoped_refptr<AuthChallengeInfo>* result) {
1006 DCHECK(transaction_.get());
1007 DCHECK(response_info_);
1008
1009 // sanity checks:
1010 DCHECK(proxy_auth_state_ == AUTH_STATE_NEED_AUTH ||
1011 server_auth_state_ == AUTH_STATE_NEED_AUTH);
1012 DCHECK(response_info_->headers->response_code() == 401 ||
1013 response_info_->headers->response_code() == 407);
1014
1015 *result = response_info_->auth_challenge;
1016}
1017
1018void URLRequestHttpJob::SetAuth(const string16& username,
1019 const string16& password) {
1020 DCHECK(transaction_.get());
1021
1022 // Proxy gets set first, then WWW.
1023 if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) {
1024 proxy_auth_state_ = AUTH_STATE_HAVE_AUTH;
1025 } else {
[email protected]dd29bcd72011-03-24 00:03:441026 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
[email protected]5394e422011-01-20 22:07:431027 server_auth_state_ = AUTH_STATE_HAVE_AUTH;
1028 }
1029
1030 RestartTransactionWithAuth(username, password);
1031}
1032
1033void URLRequestHttpJob::CancelAuth() {
1034 // Proxy gets set first, then WWW.
1035 if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) {
1036 proxy_auth_state_ = AUTH_STATE_CANCELED;
1037 } else {
[email protected]dd29bcd72011-03-24 00:03:441038 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
[email protected]5394e422011-01-20 22:07:431039 server_auth_state_ = AUTH_STATE_CANCELED;
1040 }
1041
1042 // These will be reset in OnStartCompleted.
1043 response_info_ = NULL;
1044 response_cookies_.clear();
1045
[email protected]ec23f522011-02-22 21:01:381046 ResetTimer();
1047
[email protected]5394e422011-01-20 22:07:431048 // OK, let the consumer read the error page...
1049 //
1050 // Because we set the AUTH_STATE_CANCELED flag, NeedsAuth will return false,
1051 // which will cause the consumer to receive OnResponseStarted instead of
1052 // OnAuthRequired.
1053 //
1054 // We have to do this via InvokeLater to avoid "recursing" the consumer.
1055 //
1056 MessageLoop::current()->PostTask(
1057 FROM_HERE,
1058 method_factory_.NewRunnableMethod(
1059 &URLRequestHttpJob::OnStartCompleted, OK));
1060}
1061
1062void URLRequestHttpJob::ContinueWithCertificate(
1063 X509Certificate* client_cert) {
1064 DCHECK(transaction_.get());
1065
1066 DCHECK(!response_info_) << "should not have a response yet";
1067
[email protected]ec23f522011-02-22 21:01:381068 ResetTimer();
1069
[email protected]5394e422011-01-20 22:07:431070 // No matter what, we want to report our status as IO pending since we will
1071 // be notifying our consumer asynchronously via OnStartCompleted.
1072 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1073
1074 int rv = transaction_->RestartWithCertificate(client_cert, &start_callback_);
1075 if (rv == ERR_IO_PENDING)
1076 return;
1077
1078 // The transaction started synchronously, but we need to notify the
1079 // URLRequest delegate via the message loop.
1080 MessageLoop::current()->PostTask(
1081 FROM_HERE,
1082 method_factory_.NewRunnableMethod(
1083 &URLRequestHttpJob::OnStartCompleted, rv));
1084}
1085
1086void URLRequestHttpJob::ContinueDespiteLastError() {
1087 // If the transaction was destroyed, then the job was cancelled.
1088 if (!transaction_.get())
1089 return;
1090
1091 DCHECK(!response_info_) << "should not have a response yet";
1092
[email protected]ec23f522011-02-22 21:01:381093 ResetTimer();
1094
[email protected]5394e422011-01-20 22:07:431095 // No matter what, we want to report our status as IO pending since we will
1096 // be notifying our consumer asynchronously via OnStartCompleted.
1097 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1098
1099 int rv = transaction_->RestartIgnoringLastError(&start_callback_);
1100 if (rv == ERR_IO_PENDING)
1101 return;
1102
1103 // The transaction started synchronously, but we need to notify the
1104 // URLRequest delegate via the message loop.
1105 MessageLoop::current()->PostTask(
1106 FROM_HERE,
1107 method_factory_.NewRunnableMethod(
1108 &URLRequestHttpJob::OnStartCompleted, rv));
1109}
1110
[email protected]85c1dce2011-07-06 12:01:291111bool URLRequestHttpJob::ShouldFixMismatchedContentLength(int rv) const {
1112 // Some servers send the body compressed, but specify the content length as
1113 // the uncompressed size. Although this violates the HTTP spec we want to
1114 // support it (as IE and FireFox do), but *only* for an exact match.
1115 // See http://crbug.com/79694.
1116 if (rv == net::ERR_CONNECTION_CLOSED) {
1117 if (request_ && request_->response_headers()) {
1118 int64 expected_length = request_->response_headers()->GetContentLength();
1119 VLOG(1) << __FUNCTION__ << "() "
1120 << "\"" << request_->url().spec() << "\""
1121 << " content-length = " << expected_length
1122 << " pre total = " << prefilter_bytes_read()
1123 << " post total = " << postfilter_bytes_read();
1124 if (postfilter_bytes_read() == expected_length) {
1125 // Clear the error.
1126 return true;
1127 }
1128 }
1129 }
1130 return false;
1131}
1132
[email protected]5394e422011-01-20 22:07:431133bool URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size,
1134 int *bytes_read) {
1135 DCHECK_NE(buf_size, 0);
1136 DCHECK(bytes_read);
1137 DCHECK(!read_in_progress_);
1138
1139 int rv = transaction_->Read(buf, buf_size, &read_callback_);
[email protected]85c1dce2011-07-06 12:01:291140
1141 if (ShouldFixMismatchedContentLength(rv))
1142 rv = 0;
1143
[email protected]5394e422011-01-20 22:07:431144 if (rv >= 0) {
1145 *bytes_read = rv;
[email protected]bbaea8f2011-06-24 00:11:011146 if (!rv)
1147 DoneWithRequest(FINISHED);
[email protected]5394e422011-01-20 22:07:431148 return true;
1149 }
1150
1151 if (rv == ERR_IO_PENDING) {
1152 read_in_progress_ = true;
1153 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1154 } else {
1155 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
1156 }
1157
1158 return false;
1159}
1160
1161void URLRequestHttpJob::StopCaching() {
1162 if (transaction_.get())
1163 transaction_->StopCaching();
1164}
1165
[email protected]6d81b482011-02-22 19:47:191166HostPortPair URLRequestHttpJob::GetSocketAddress() const {
1167 return response_info_ ? response_info_->socket_address : HostPortPair();
1168}
1169
[email protected]5394e422011-01-20 22:07:431170URLRequestHttpJob::~URLRequestHttpJob() {
1171 DCHECK(!sdch_test_control_ || !sdch_test_activated_);
[email protected]dd29bcd72011-03-24 00:03:441172 if (!is_cached_content_) {
[email protected]5394e422011-01-20 22:07:431173 if (sdch_test_control_)
[email protected]fc01f232011-03-17 19:06:011174 RecordPacketStats(FilterContext::SDCH_EXPERIMENT_HOLDBACK);
[email protected]5394e422011-01-20 22:07:431175 if (sdch_test_activated_)
[email protected]fc01f232011-03-17 19:06:011176 RecordPacketStats(FilterContext::SDCH_EXPERIMENT_DECODE);
[email protected]5394e422011-01-20 22:07:431177 }
[email protected]dd29bcd72011-03-24 00:03:441178 // Make sure SDCH filters are told to emit histogram data while
1179 // filter_context_ is still alive.
[email protected]5394e422011-01-20 22:07:431180 DestroyFilters();
1181
1182 if (sdch_dictionary_url_.is_valid()) {
1183 // Prior to reaching the destructor, request_ has been set to a NULL
1184 // pointer, so request_->url() is no longer valid in the destructor, and we
1185 // use an alternate copy |request_info_.url|.
1186 SdchManager* manager = SdchManager::Global();
1187 // To be extra safe, since this is a "different time" from when we decided
1188 // to get the dictionary, we'll validate that an SdchManager is available.
1189 // At shutdown time, care is taken to be sure that we don't delete this
1190 // globally useful instance "too soon," so this check is just defensive
1191 // coding to assure that IF the system is shutting down, we don't have any
1192 // problem if the manager was deleted ahead of time.
1193 if (manager) // Defensive programming.
1194 manager->FetchDictionary(request_info_.url, sdch_dictionary_url_);
1195 }
[email protected]bbaea8f2011-06-24 00:11:011196 DoneWithRequest(ABORTED);
[email protected]5394e422011-01-20 22:07:431197}
1198
[email protected]ec23f522011-02-22 21:01:381199void URLRequestHttpJob::RecordTimer() {
1200 if (request_creation_time_.is_null()) {
1201 NOTREACHED()
1202 << "The same transaction shouldn't start twice without new timing.";
1203 return;
1204 }
1205
[email protected]320a29f12011-03-21 14:47:411206 base::TimeDelta to_start = base::Time::Now() - request_creation_time_;
[email protected]ec23f522011-02-22 21:01:381207 request_creation_time_ = base::Time();
[email protected]8684a8812011-03-22 13:59:381208
1209 static const bool use_prefetch_histogram =
[email protected]edafd4c2011-05-10 17:18:531210 base::FieldTrialList::TrialExists("Prefetch");
[email protected]8684a8812011-03-22 13:59:381211
[email protected]ec23f522011-02-22 21:01:381212 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte", to_start);
[email protected]8684a8812011-03-22 13:59:381213 if (use_prefetch_histogram) {
1214 UMA_HISTOGRAM_MEDIUM_TIMES(
1215 base::FieldTrial::MakeName("Net.HttpTimeToFirstByte",
1216 "Prefetch"),
1217 to_start);
1218 }
1219
[email protected]61a99dd82011-05-24 19:19:471220 const bool is_prerender = !!(request_info_.load_flags & LOAD_PRERENDERING);
[email protected]8684a8812011-03-22 13:59:381221 if (is_prerender) {
1222 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte_Prerender",
1223 to_start);
1224 if (use_prefetch_histogram) {
1225 UMA_HISTOGRAM_MEDIUM_TIMES(
1226 base::FieldTrial::MakeName("Net.HttpTimeToFirstByte_Prerender",
1227 "Prefetch"),
1228 to_start);
1229 }
1230 } else {
1231 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte_NonPrerender",
1232 to_start);
1233 if (use_prefetch_histogram) {
1234 UMA_HISTOGRAM_MEDIUM_TIMES(
1235 base::FieldTrial::MakeName("Net.HttpTimeToFirstByte_NonPrerender",
1236 "Prefetch"),
1237 to_start);
1238 }
1239 }
[email protected]ec23f522011-02-22 21:01:381240}
1241
1242void URLRequestHttpJob::ResetTimer() {
1243 if (!request_creation_time_.is_null()) {
1244 NOTREACHED()
1245 << "The timer was reset before it was recorded.";
1246 return;
1247 }
1248 request_creation_time_ = base::Time::Now();
1249}
1250
[email protected]dd29bcd72011-03-24 00:03:441251void URLRequestHttpJob::UpdatePacketReadTimes() {
1252 if (!packet_timing_enabled_)
1253 return;
1254
1255 if (filter_input_byte_count() <= bytes_observed_in_packets_) {
1256 DCHECK_EQ(filter_input_byte_count(), bytes_observed_in_packets_);
1257 return; // No new bytes have arrived.
1258 }
1259
1260 if (!bytes_observed_in_packets_)
1261 request_time_snapshot_ = request_ ? request_->request_time() : base::Time();
1262
1263 final_packet_time_ = base::Time::Now();
1264 const size_t kTypicalPacketSize = 1430;
1265 while (filter_input_byte_count() > bytes_observed_in_packets_) {
1266 ++observed_packet_count_;
1267 if (packet_times_.size() < kSdchPacketHistogramCount) {
1268 packet_times_.push_back(final_packet_time_);
1269 DCHECK_EQ(static_cast<size_t>(observed_packet_count_),
1270 packet_times_.size());
1271 }
1272 bytes_observed_in_packets_ += kTypicalPacketSize;
1273 }
1274 // Since packets may not be full, we'll remember the number of bytes we've
1275 // accounted for in packets thus far.
1276 bytes_observed_in_packets_ = filter_input_byte_count();
1277}
1278
1279void URLRequestHttpJob::RecordPacketStats(
1280 FilterContext::StatisticSelector statistic) const {
1281 if (!packet_timing_enabled_ || (final_packet_time_ == base::Time()))
1282 return;
1283
1284 base::TimeDelta duration = final_packet_time_ - request_time_snapshot_;
1285 switch (statistic) {
1286 case FilterContext::SDCH_DECODE: {
1287 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Decode_Latency_F_a", duration,
1288 base::TimeDelta::FromMilliseconds(20),
1289 base::TimeDelta::FromMinutes(10), 100);
1290 UMA_HISTOGRAM_COUNTS_100("Sdch3.Network_Decode_Packets_b",
1291 static_cast<int>(observed_packet_count_));
1292 UMA_HISTOGRAM_CUSTOM_COUNTS("Sdch3.Network_Decode_Bytes_Processed_b",
1293 static_cast<int>(bytes_observed_in_packets_), 500, 100000, 100);
1294 if (packet_times_.empty())
1295 return;
1296 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Decode_1st_To_Last_a",
1297 final_packet_time_ - packet_times_[0],
1298 base::TimeDelta::FromMilliseconds(20),
1299 base::TimeDelta::FromMinutes(10), 100);
1300
1301 DCHECK_GT(kSdchPacketHistogramCount, 4u);
1302 if (packet_times_.size() <= 4)
1303 return;
1304 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Decode_1st_To_2nd_c",
1305 packet_times_[1] - packet_times_[0],
1306 base::TimeDelta::FromMilliseconds(1),
1307 base::TimeDelta::FromSeconds(10), 100);
1308 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Decode_2nd_To_3rd_c",
1309 packet_times_[2] - packet_times_[1],
1310 base::TimeDelta::FromMilliseconds(1),
1311 base::TimeDelta::FromSeconds(10), 100);
1312 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Decode_3rd_To_4th_c",
1313 packet_times_[3] - packet_times_[2],
1314 base::TimeDelta::FromMilliseconds(1),
1315 base::TimeDelta::FromSeconds(10), 100);
1316 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Decode_4th_To_5th_c",
1317 packet_times_[4] - packet_times_[3],
1318 base::TimeDelta::FromMilliseconds(1),
1319 base::TimeDelta::FromSeconds(10), 100);
1320 return;
1321 }
1322 case FilterContext::SDCH_PASSTHROUGH: {
1323 // Despite advertising a dictionary, we handled non-sdch compressed
1324 // content.
1325 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Pass-through_Latency_F_a",
1326 duration,
1327 base::TimeDelta::FromMilliseconds(20),
1328 base::TimeDelta::FromMinutes(10), 100);
1329 UMA_HISTOGRAM_COUNTS_100("Sdch3.Network_Pass-through_Packets_b",
1330 observed_packet_count_);
1331 if (packet_times_.empty())
1332 return;
1333 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Pass-through_1st_To_Last_a",
1334 final_packet_time_ - packet_times_[0],
1335 base::TimeDelta::FromMilliseconds(20),
1336 base::TimeDelta::FromMinutes(10), 100);
1337 DCHECK_GT(kSdchPacketHistogramCount, 4u);
1338 if (packet_times_.size() <= 4)
1339 return;
1340 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Pass-through_1st_To_2nd_c",
1341 packet_times_[1] - packet_times_[0],
1342 base::TimeDelta::FromMilliseconds(1),
1343 base::TimeDelta::FromSeconds(10), 100);
1344 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Pass-through_2nd_To_3rd_c",
1345 packet_times_[2] - packet_times_[1],
1346 base::TimeDelta::FromMilliseconds(1),
1347 base::TimeDelta::FromSeconds(10), 100);
1348 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Pass-through_3rd_To_4th_c",
1349 packet_times_[3] - packet_times_[2],
1350 base::TimeDelta::FromMilliseconds(1),
1351 base::TimeDelta::FromSeconds(10), 100);
1352 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Network_Pass-through_4th_To_5th_c",
1353 packet_times_[4] - packet_times_[3],
1354 base::TimeDelta::FromMilliseconds(1),
1355 base::TimeDelta::FromSeconds(10), 100);
1356 return;
1357 }
1358
1359 case FilterContext::SDCH_EXPERIMENT_DECODE: {
1360 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Experiment_Decode",
1361 duration,
1362 base::TimeDelta::FromMilliseconds(20),
1363 base::TimeDelta::FromMinutes(10), 100);
1364 // We already provided interpacket histograms above in the SDCH_DECODE
1365 // case, so we don't need them here.
1366 return;
1367 }
1368 case FilterContext::SDCH_EXPERIMENT_HOLDBACK: {
1369 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Experiment_Holdback",
1370 duration,
1371 base::TimeDelta::FromMilliseconds(20),
1372 base::TimeDelta::FromMinutes(10), 100);
1373 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Experiment_Holdback_1st_To_Last_a",
1374 final_packet_time_ - packet_times_[0],
1375 base::TimeDelta::FromMilliseconds(20),
1376 base::TimeDelta::FromMinutes(10), 100);
1377
1378 DCHECK_GT(kSdchPacketHistogramCount, 4u);
1379 if (packet_times_.size() <= 4)
1380 return;
1381 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Experiment_Holdback_1st_To_2nd_c",
1382 packet_times_[1] - packet_times_[0],
1383 base::TimeDelta::FromMilliseconds(1),
1384 base::TimeDelta::FromSeconds(10), 100);
1385 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Experiment_Holdback_2nd_To_3rd_c",
1386 packet_times_[2] - packet_times_[1],
1387 base::TimeDelta::FromMilliseconds(1),
1388 base::TimeDelta::FromSeconds(10), 100);
1389 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Experiment_Holdback_3rd_To_4th_c",
1390 packet_times_[3] - packet_times_[2],
1391 base::TimeDelta::FromMilliseconds(1),
1392 base::TimeDelta::FromSeconds(10), 100);
1393 UMA_HISTOGRAM_CLIPPED_TIMES("Sdch3.Experiment_Holdback_4th_To_5th_c",
1394 packet_times_[4] - packet_times_[3],
1395 base::TimeDelta::FromMilliseconds(1),
1396 base::TimeDelta::FromSeconds(10), 100);
1397 return;
1398 }
1399 default:
1400 NOTREACHED();
1401 return;
1402 }
1403}
1404
1405// The common type of histogram we use for all compression-tracking histograms.
1406#define COMPRESSION_HISTOGRAM(name, sample) \
1407 do { \
1408 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.Compress." name, sample, \
1409 500, 1000000, 100); \
1410 } while(0)
1411
1412void URLRequestHttpJob::RecordCompressionHistograms() {
1413 DCHECK(request_);
1414 if (!request_)
1415 return;
1416
1417 if (is_cached_content_ || // Don't record cached content
1418 !GetStatus().is_success() || // Don't record failed content
1419 !IsCompressibleContent() || // Only record compressible content
1420 !prefilter_bytes_read()) // Zero-byte responses aren't useful.
1421 return;
1422
1423 // Miniature requests aren't really compressible. Don't count them.
1424 const int kMinSize = 16;
1425 if (prefilter_bytes_read() < kMinSize)
1426 return;
1427
1428 // Only record for http or https urls.
1429 bool is_http = request_->url().SchemeIs("http");
1430 bool is_https = request_->url().SchemeIs("https");
1431 if (!is_http && !is_https)
1432 return;
1433
1434 int compressed_B = prefilter_bytes_read();
1435 int decompressed_B = postfilter_bytes_read();
1436 bool was_filtered = HasFilter();
1437
1438 // We want to record how often downloaded resources are compressed.
1439 // But, we recognize that different protocols may have different
1440 // properties. So, for each request, we'll put it into one of 3
1441 // groups:
1442 // a) SSL resources
1443 // Proxies cannot tamper with compression headers with SSL.
1444 // b) Non-SSL, loaded-via-proxy resources
1445 // In this case, we know a proxy might have interfered.
1446 // c) Non-SSL, loaded-without-proxy resources
1447 // In this case, we know there was no explicit proxy. However,
1448 // it is possible that a transparent proxy was still interfering.
1449 //
1450 // For each group, we record the same 3 histograms.
1451
1452 if (is_https) {
1453 if (was_filtered) {
1454 COMPRESSION_HISTOGRAM("SSL.BytesBeforeCompression", compressed_B);
1455 COMPRESSION_HISTOGRAM("SSL.BytesAfterCompression", decompressed_B);
1456 } else {
1457 COMPRESSION_HISTOGRAM("SSL.ShouldHaveBeenCompressed", decompressed_B);
1458 }
1459 return;
1460 }
1461
1462 if (request_->was_fetched_via_proxy()) {
1463 if (was_filtered) {
1464 COMPRESSION_HISTOGRAM("Proxy.BytesBeforeCompression", compressed_B);
1465 COMPRESSION_HISTOGRAM("Proxy.BytesAfterCompression", decompressed_B);
1466 } else {
1467 COMPRESSION_HISTOGRAM("Proxy.ShouldHaveBeenCompressed", decompressed_B);
1468 }
1469 return;
1470 }
1471
1472 if (was_filtered) {
1473 COMPRESSION_HISTOGRAM("NoProxy.BytesBeforeCompression", compressed_B);
1474 COMPRESSION_HISTOGRAM("NoProxy.BytesAfterCompression", decompressed_B);
1475 } else {
1476 COMPRESSION_HISTOGRAM("NoProxy.ShouldHaveBeenCompressed", decompressed_B);
1477 }
1478}
1479
1480bool URLRequestHttpJob::IsCompressibleContent() const {
1481 std::string mime_type;
1482 return GetMimeType(&mime_type) &&
1483 (IsSupportedJavascriptMimeType(mime_type.c_str()) ||
1484 IsSupportedNonImageMimeType(mime_type.c_str()));
1485}
1486
[email protected]bbaea8f2011-06-24 00:11:011487void URLRequestHttpJob::RecordPerfHistograms(CompletionCause reason) {
1488 if (start_time_.is_null())
1489 return;
1490
1491 base::TimeDelta total_time = base::TimeTicks::Now() - start_time_;
1492 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTime", total_time);
1493
1494 if (reason == FINISHED) {
1495 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeSuccess", total_time);
1496 } else {
1497 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCancel", total_time);
1498 }
1499
1500 static bool cache_experiment = false;
1501 if (!cache_experiment)
1502 cache_experiment = base::FieldTrialList::TrialExists("CacheListSize");
1503 if (cache_experiment) {
1504 UMA_HISTOGRAM_TIMES(
1505 base::FieldTrial::MakeName("Net.HttpJob.TotalTime", "CacheListSize"),
1506 total_time);
1507 if (reason == FINISHED) {
1508 UMA_HISTOGRAM_TIMES(
1509 base::FieldTrial::MakeName("Net.HttpJob.TotalTimeSuccess",
1510 "CacheListSize"),
1511 total_time);
1512 } else {
1513 UMA_HISTOGRAM_TIMES(
1514 base::FieldTrial::MakeName("Net.HttpJob.TotalTimeCancel",
1515 "CacheListSize"),
1516 total_time);
1517 }
1518 }
1519
1520 start_time_ = base::TimeTicks();
1521}
1522
1523void URLRequestHttpJob::DoneWithRequest(CompletionCause reason) {
1524 if (done_)
1525 return;
1526 done_ = true;
1527
1528 RecordPerfHistograms(reason);
1529 if (reason == FINISHED)
1530 RecordCompressionHistograms();
1531}
1532
[email protected]4f5656c62010-12-13 10:47:091533} // namespace net