blob: b59f988eb75f1351dfc31b0a739dd5505059a67c [file] [log] [blame]
Andrew de los Reyes45168102010-11-22 11:13:50 -08001// 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 Reyesf3ed8e72011-02-16 10:35:46 -08007using google::protobuf::Closure;
Andrew de los Reyes45168102010-11-22 11:13:50 -08008using std::deque;
9using std::string;
10
11namespace chromeos_update_engine {
12
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080013HttpFetcher::~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 Arnold9dd1e7c2012-02-16 12:13:36 -080020void HttpFetcher::SetPostData(const void* data, size_t size,
21 HttpContentType type) {
Andrew de los Reyes45168102010-11-22 11:13:50 -080022 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 Arnold9dd1e7c2012-02-16 12:13:36 -080026 post_content_type_ = type;
27}
28
29void HttpFetcher::SetPostData(const void* data, size_t size) {
30 SetPostData(data, size, kHttpContentTypeUnspecified);
Andrew de los Reyes45168102010-11-22 11:13:50 -080031}
32
33// Proxy methods to set the proxies, then to pop them off.
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080034bool HttpFetcher::ResolveProxiesForUrl(const string& url, Closure* callback) {
Andrew de los Reyes45168102010-11-22 11:13:50 -080035 if (!proxy_resolver_) {
36 LOG(INFO) << "Not resolving proxies (no proxy resolver).";
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080037 no_resolver_idle_id_ = g_idle_add(utils::GlibRunClosure, callback);
38 return true;
Andrew de los Reyes45168102010-11-22 11:13:50 -080039 }
Andrew de los Reyesffcb6d12011-04-04 09:57:38 -070040 CHECK_EQ(reinterpret_cast<Closure*>(NULL), callback_);
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080041 callback_ = callback;
42 return proxy_resolver_->GetProxiesForUrl(url,
43 &HttpFetcher::StaticProxiesResolved,
44 this);
45}
46
47void HttpFetcher::ProxiesResolved(const std::deque<std::string>& proxies) {
48 no_resolver_idle_id_ = 0;
49 if (!proxies.empty())
Andrew de los Reyes45168102010-11-22 11:13:50 -080050 SetProxies(proxies);
Andrew de los Reyesffcb6d12011-04-04 09:57:38 -070051 CHECK_NE(reinterpret_cast<Closure*>(NULL), callback_);
52 Closure* callback = callback_;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080053 callback_ = NULL;
Andrew de los Reyesffcb6d12011-04-04 09:57:38 -070054 // This may indirectly call back into ResolveProxiesForUrl():
55 callback->Run();
Andrew de los Reyes45168102010-11-22 11:13:50 -080056}
57
58} // namespace chromeos_update_engine