Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2009 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/http_fetcher.h" |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 18 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 19 | #include <base/bind.h> |
| 20 | |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 21 | using base::Closure; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 22 | using brillo::MessageLoop; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 23 | using std::deque; |
| 24 | using std::string; |
| 25 | |
| 26 | namespace chromeos_update_engine { |
| 27 | |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 28 | HttpFetcher::~HttpFetcher() { |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 29 | CancelProxyResolution(); |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 32 | void HttpFetcher::SetPostData(const void* data, |
| 33 | size_t size, |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 34 | HttpContentType type) { |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 35 | post_data_set_ = true; |
| 36 | post_data_.clear(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 37 | const char* char_data = reinterpret_cast<const char*>(data); |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 38 | post_data_.insert(post_data_.end(), char_data, char_data + size); |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 39 | post_content_type_ = type; |
| 40 | } |
| 41 | |
| 42 | void HttpFetcher::SetPostData(const void* data, size_t size) { |
| 43 | SetPostData(data, size, kHttpContentTypeUnspecified); |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Proxy methods to set the proxies, then to pop them off. |
Daniel Erat | e5f6f25 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 47 | void HttpFetcher::ResolveProxiesForUrl(const string& url, |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 48 | const Closure& callback) { |
| 49 | CHECK_EQ(static_cast<Closure*>(nullptr), callback_.get()); |
| 50 | callback_.reset(new Closure(callback)); |
| 51 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 52 | if (!proxy_resolver_) { |
| 53 | LOG(INFO) << "Not resolving proxies (no proxy resolver)."; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 54 | no_resolver_idle_id_ = MessageLoop::current()->PostTask( |
| 55 | FROM_HERE, |
| 56 | base::Bind(&HttpFetcher::NoProxyResolverCallback, |
| 57 | base::Unretained(this))); |
Daniel Erat | e5f6f25 | 2017-04-20 12:09:58 -0600 | [diff] [blame] | 58 | return; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 59 | } |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 60 | proxy_request_ = proxy_resolver_->GetProxiesForUrl( |
Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 61 | url, base::Bind(&HttpFetcher::ProxiesResolved, base::Unretained(this))); |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 64 | void HttpFetcher::NoProxyResolverCallback() { |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 65 | no_resolver_idle_id_ = MessageLoop::kTaskIdNull; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 66 | ProxiesResolved(deque<string>()); |
| 67 | } |
| 68 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 69 | void HttpFetcher::ProxiesResolved(const deque<string>& proxies) { |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 70 | proxy_request_ = kProxyRequestIdNull; |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 71 | if (!proxies.empty()) |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 72 | SetProxies(proxies); |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 73 | CHECK(callback_.get()) << "ProxiesResolved but none pending."; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 74 | Closure* callback = callback_.release(); |
Andrew de los Reyes | ffcb6d1 | 2011-04-04 09:57:38 -0700 | [diff] [blame] | 75 | // This may indirectly call back into ResolveProxiesForUrl(): |
| 76 | callback->Run(); |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 77 | delete callback; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 80 | bool HttpFetcher::CancelProxyResolution() { |
| 81 | bool ret = false; |
| 82 | if (no_resolver_idle_id_ != MessageLoop::kTaskIdNull) { |
| 83 | ret = MessageLoop::current()->CancelTask(no_resolver_idle_id_); |
| 84 | no_resolver_idle_id_ = MessageLoop::kTaskIdNull; |
| 85 | } |
| 86 | if (proxy_request_ && proxy_resolver_) { |
| 87 | ret = proxy_resolver_->CancelProxyRequest(proxy_request_) || ret; |
| 88 | proxy_request_ = kProxyRequestIdNull; |
| 89 | } |
| 90 | return ret; |
| 91 | } |
| 92 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 93 | } // namespace chromeos_update_engine |