OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/url_request_http_job.h" | 5 #include "net/url_request/url_request_http_job.h" |
6 | 6 |
7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
24 #include "net/http/http_response_info.h" | 24 #include "net/http/http_response_info.h" |
25 #include "net/http/http_transaction.h" | 25 #include "net/http/http_transaction.h" |
26 #include "net/http/http_transaction_factory.h" | 26 #include "net/http/http_transaction_factory.h" |
27 #include "net/http/http_util.h" | 27 #include "net/http/http_util.h" |
28 #include "net/url_request/url_request.h" | 28 #include "net/url_request/url_request.h" |
29 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
30 #include "net/url_request/url_request_error_job.h" | 30 #include "net/url_request/url_request_error_job.h" |
31 #include "net/url_request/url_request_redirect_job.h" | 31 #include "net/url_request/url_request_redirect_job.h" |
32 | 32 |
33 // static | |
34 std::set<int> URLRequestHttpJob::explicitly_allowed_ports_; | |
35 | |
36 // TODO(darin): make sure the port blocking code is not lost | 33 // TODO(darin): make sure the port blocking code is not lost |
37 | |
38 // static | 34 // static |
39 URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request, | 35 URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request, |
40 const std::string& scheme) { | 36 const std::string& scheme) { |
41 DCHECK(scheme == "http" || scheme == "https"); | 37 DCHECK(scheme == "http" || scheme == "https"); |
42 | 38 |
43 int port = request->url().IntPort(); | 39 int port = request->url().IntPort(); |
44 if (!net::IsPortAllowedByDefault(port) && !IsPortAllowedByOverride(port)) | 40 if (!net::IsPortAllowedByDefault(port) && !net::IsPortAllowedByOverride(port)) |
45 return new URLRequestErrorJob(request, net::ERR_UNSAFE_PORT); | 41 return new URLRequestErrorJob(request, net::ERR_UNSAFE_PORT); |
46 | 42 |
47 if (!request->context() || | 43 if (!request->context() || |
48 !request->context()->http_transaction_factory()) { | 44 !request->context()->http_transaction_factory()) { |
49 NOTREACHED() << "requires a valid context"; | 45 NOTREACHED() << "requires a valid context"; |
50 return new URLRequestErrorJob(request, net::ERR_INVALID_ARGUMENT); | 46 return new URLRequestErrorJob(request, net::ERR_INVALID_ARGUMENT); |
51 } | 47 } |
52 | 48 |
53 // We cache the value of the switch because this code path is hit on every | 49 // We cache the value of the switch because this code path is hit on every |
54 // network request. | 50 // network request. |
55 static const bool kForceHTTPS = | 51 static const bool kForceHTTPS = |
56 CommandLine::ForCurrentProcess()->HasSwitch(switches::kForceHTTPS); | 52 CommandLine::ForCurrentProcess()->HasSwitch(switches::kForceHTTPS); |
57 if (kForceHTTPS && scheme == "http" && | 53 if (kForceHTTPS && scheme == "http" && |
58 request->context()->strict_transport_security_state() && | 54 request->context()->strict_transport_security_state() && |
59 request->context()->strict_transport_security_state()->IsEnabledForHost( | 55 request->context()->strict_transport_security_state()->IsEnabledForHost( |
60 request->url().host())) { | 56 request->url().host())) { |
61 DCHECK_EQ(request->url().scheme(), "http"); | 57 DCHECK_EQ(request->url().scheme(), "http"); |
62 url_canon::Replacements<char> replacements; | 58 url_canon::Replacements<char> replacements; |
63 static const char kNewScheme[] = "https"; | 59 static const char kNewScheme[] = "https"; |
64 replacements.SetScheme(kNewScheme, | 60 replacements.SetScheme(kNewScheme, |
65 url_parse::Component(0, strlen(kNewScheme))); | 61 url_parse::Component(0, strlen(kNewScheme))); |
66 GURL new_location = request->url().ReplaceComponents(replacements); | 62 GURL new_location = request->url().ReplaceComponents(replacements); |
67 return new URLRequestRedirectJob(request, new_location); | 63 return new URLRequestRedirectJob(request, new_location); |
68 } | 64 } |
69 | 65 |
70 return new URLRequestHttpJob(request); | 66 return new URLRequestHttpJob(request); |
71 } | 67 } |
72 | 68 |
73 // static | |
74 void URLRequestHttpJob::SetExplicitlyAllowedPorts( | |
75 const std::wstring& allowed_ports) { | |
76 if (allowed_ports.empty()) | |
77 return; | |
78 | |
79 std::set<int> ports; | |
80 size_t last = 0; | |
81 size_t size = allowed_ports.size(); | |
82 // The comma delimiter. | |
83 const std::wstring::value_type kComma = L','; | |
84 | |
85 // Overflow is still possible for evil user inputs. | |
86 for (size_t i = 0; i <= size; ++i) { | |
87 // The string should be composed of only digits and commas. | |
88 if (i != size && !IsAsciiDigit(allowed_ports[i]) && | |
89 (allowed_ports[i] != kComma)) | |
90 return; | |
91 if (i == size || allowed_ports[i] == kComma) { | |
92 size_t length = i - last; | |
93 if (length > 0) | |
94 ports.insert(StringToInt(WideToASCII( | |
95 allowed_ports.substr(last, length)))); | |
96 last = i + 1; | |
97 } | |
98 } | |
99 explicitly_allowed_ports_ = ports; | |
100 } | |
101 | |
102 URLRequestHttpJob::URLRequestHttpJob(URLRequest* request) | 69 URLRequestHttpJob::URLRequestHttpJob(URLRequest* request) |
103 : URLRequestJob(request), | 70 : URLRequestJob(request), |
104 context_(request->context()), | 71 context_(request->context()), |
105 response_info_(NULL), | 72 response_info_(NULL), |
106 proxy_auth_state_(net::AUTH_STATE_DONT_NEED_AUTH), | 73 proxy_auth_state_(net::AUTH_STATE_DONT_NEED_AUTH), |
107 server_auth_state_(net::AUTH_STATE_DONT_NEED_AUTH), | 74 server_auth_state_(net::AUTH_STATE_DONT_NEED_AUTH), |
108 ALLOW_THIS_IN_INITIALIZER_LIST( | 75 ALLOW_THIS_IN_INITIALIZER_LIST( |
109 start_callback_(this, &URLRequestHttpJob::OnStartCompleted)), | 76 start_callback_(this, &URLRequestHttpJob::OnStartCompleted)), |
110 ALLOW_THIS_IN_INITIALIZER_LIST( | 77 ALLOW_THIS_IN_INITIALIZER_LIST( |
111 read_callback_(this, &URLRequestHttpJob::OnReadCompleted)), | 78 read_callback_(this, &URLRequestHttpJob::OnReadCompleted)), |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 &start_callback_); | 338 &start_callback_); |
372 if (rv == net::ERR_IO_PENDING) | 339 if (rv == net::ERR_IO_PENDING) |
373 return; | 340 return; |
374 | 341 |
375 // The transaction started synchronously, but we need to notify the | 342 // The transaction started synchronously, but we need to notify the |
376 // URLRequest delegate via the message loop. | 343 // URLRequest delegate via the message loop. |
377 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 344 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
378 this, &URLRequestHttpJob::OnStartCompleted, rv)); | 345 this, &URLRequestHttpJob::OnStartCompleted, rv)); |
379 } | 346 } |
380 | 347 |
381 // static | |
382 bool URLRequestHttpJob::IsPortAllowedByOverride(int port) { | |
383 if (explicitly_allowed_ports().empty()) | |
384 return false; | |
385 | |
386 std::set<int>::const_iterator it = | |
387 std::find(explicitly_allowed_ports().begin(), | |
388 explicitly_allowed_ports().end(), | |
389 port); | |
390 | |
391 return it != explicitly_allowed_ports().end(); | |
392 } | |
393 | |
394 void URLRequestHttpJob::CancelAuth() { | 348 void URLRequestHttpJob::CancelAuth() { |
395 // Proxy gets set first, then WWW. | 349 // Proxy gets set first, then WWW. |
396 if (proxy_auth_state_ == net::AUTH_STATE_NEED_AUTH) { | 350 if (proxy_auth_state_ == net::AUTH_STATE_NEED_AUTH) { |
397 proxy_auth_state_ = net::AUTH_STATE_CANCELED; | 351 proxy_auth_state_ = net::AUTH_STATE_CANCELED; |
398 } else { | 352 } else { |
399 DCHECK(server_auth_state_ == net::AUTH_STATE_NEED_AUTH); | 353 DCHECK(server_auth_state_ == net::AUTH_STATE_NEED_AUTH); |
400 server_auth_state_ = net::AUTH_STATE_CANCELED; | 354 server_auth_state_ = net::AUTH_STATE_CANCELED; |
401 } | 355 } |
402 | 356 |
403 // These will be reset in OnStartCompleted. | 357 // These will be reset in OnStartCompleted. |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 | 716 |
763 std::string name = "Strict-Transport-Security"; | 717 std::string name = "Strict-Transport-Security"; |
764 std::string value; | 718 std::string value; |
765 | 719 |
766 void* iter = NULL; | 720 void* iter = NULL; |
767 while (response_info_->headers->EnumerateHeader(&iter, name, &value)) { | 721 while (response_info_->headers->EnumerateHeader(&iter, name, &value)) { |
768 ctx->strict_transport_security_state()->DidReceiveHeader( | 722 ctx->strict_transport_security_state()->DidReceiveHeader( |
769 request_info_.url, value); | 723 request_info_.url, value); |
770 } | 724 } |
771 } | 725 } |
OLD | NEW |