Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS 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 | #include "update_engine/http_fetcher.h" |
| 6 | |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 7 | using google::protobuf::Closure; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 8 | using std::deque; |
| 9 | using std::string; |
| 10 | |
| 11 | namespace chromeos_update_engine { |
| 12 | |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 13 | HttpFetcher::~HttpFetcher() { |
| 14 | if (no_resolver_idle_id_) { |
| 15 | g_source_remove(no_resolver_idle_id_); |
| 16 | no_resolver_idle_id_ = 0; |
| 17 | } |
| 18 | } |
| 19 | |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 20 | void HttpFetcher::SetPostData(const void* data, size_t size, |
| 21 | HttpContentType type) { |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 22 | post_data_set_ = true; |
| 23 | post_data_.clear(); |
| 24 | const char *char_data = reinterpret_cast<const char*>(data); |
| 25 | post_data_.insert(post_data_.end(), char_data, char_data + size); |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 26 | post_content_type_ = type; |
| 27 | } |
| 28 | |
| 29 | void HttpFetcher::SetPostData(const void* data, size_t size) { |
| 30 | SetPostData(data, size, kHttpContentTypeUnspecified); |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // Proxy methods to set the proxies, then to pop them off. |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 34 | bool HttpFetcher::ResolveProxiesForUrl(const string& url, Closure* callback) { |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 35 | if (!proxy_resolver_) { |
| 36 | LOG(INFO) << "Not resolving proxies (no proxy resolver)."; |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 37 | no_resolver_idle_id_ = g_idle_add_full( |
| 38 | G_PRIORITY_DEFAULT, |
| 39 | utils::GlibRunClosure, |
| 40 | callback, |
| 41 | utils::GlibDestroyClosure); |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 42 | return true; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 43 | } |
Andrew de los Reyes | ffcb6d1 | 2011-04-04 09:57:38 -0700 | [diff] [blame] | 44 | CHECK_EQ(reinterpret_cast<Closure*>(NULL), callback_); |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 45 | callback_ = callback; |
| 46 | return proxy_resolver_->GetProxiesForUrl(url, |
| 47 | &HttpFetcher::StaticProxiesResolved, |
| 48 | this); |
| 49 | } |
| 50 | |
| 51 | void HttpFetcher::ProxiesResolved(const std::deque<std::string>& proxies) { |
| 52 | no_resolver_idle_id_ = 0; |
| 53 | if (!proxies.empty()) |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 54 | SetProxies(proxies); |
Andrew de los Reyes | ffcb6d1 | 2011-04-04 09:57:38 -0700 | [diff] [blame] | 55 | CHECK_NE(reinterpret_cast<Closure*>(NULL), callback_); |
| 56 | Closure* callback = callback_; |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 57 | callback_ = NULL; |
Andrew de los Reyes | ffcb6d1 | 2011-04-04 09:57:38 -0700 | [diff] [blame] | 58 | // This may indirectly call back into ResolveProxiesForUrl(): |
| 59 | callback->Run(); |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 60 | delete callback; |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | } // namespace chromeos_update_engine |