blob: 400b43c6a55d7fba97cc002ef82a6d446ee348c4 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Reyes45168102010-11-22 11:13:50 -080016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/http_fetcher.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080018
Alex Deymo60ca1a72015-06-18 18:19:15 -070019#include <base/bind.h>
20
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070021using base::Closure;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070022using brillo::MessageLoop;
Andrew de los Reyes45168102010-11-22 11:13:50 -080023using std::deque;
24using std::string;
25
26namespace chromeos_update_engine {
27
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080028HttpFetcher::~HttpFetcher() {
Alex Deymo60ca1a72015-06-18 18:19:15 -070029 if (no_resolver_idle_id_ != MessageLoop::kTaskIdNull) {
30 MessageLoop::current()->CancelTask(no_resolver_idle_id_);
31 no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080032 }
33}
34
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080035void HttpFetcher::SetPostData(const void* data, size_t size,
36 HttpContentType type) {
Andrew de los Reyes45168102010-11-22 11:13:50 -080037 post_data_set_ = true;
38 post_data_.clear();
Alex Deymo60ca1a72015-06-18 18:19:15 -070039 const char* char_data = reinterpret_cast<const char*>(data);
Andrew de los Reyes45168102010-11-22 11:13:50 -080040 post_data_.insert(post_data_.end(), char_data, char_data + size);
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080041 post_content_type_ = type;
42}
43
44void HttpFetcher::SetPostData(const void* data, size_t size) {
45 SetPostData(data, size, kHttpContentTypeUnspecified);
Andrew de los Reyes45168102010-11-22 11:13:50 -080046}
47
48// Proxy methods to set the proxies, then to pop them off.
Alex Deymo60ca1a72015-06-18 18:19:15 -070049bool HttpFetcher::ResolveProxiesForUrl(const string& url,
50 const Closure& callback) {
51 CHECK_EQ(static_cast<Closure*>(nullptr), callback_.get());
52 callback_.reset(new Closure(callback));
53
Andrew de los Reyes45168102010-11-22 11:13:50 -080054 if (!proxy_resolver_) {
55 LOG(INFO) << "Not resolving proxies (no proxy resolver).";
Alex Deymo60ca1a72015-06-18 18:19:15 -070056 no_resolver_idle_id_ = MessageLoop::current()->PostTask(
57 FROM_HERE,
58 base::Bind(&HttpFetcher::NoProxyResolverCallback,
59 base::Unretained(this)));
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080060 return true;
Andrew de los Reyes45168102010-11-22 11:13:50 -080061 }
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080062 return proxy_resolver_->GetProxiesForUrl(url,
63 &HttpFetcher::StaticProxiesResolved,
64 this);
65}
66
Alex Deymo60ca1a72015-06-18 18:19:15 -070067void HttpFetcher::NoProxyResolverCallback() {
68 ProxiesResolved(deque<string>());
69}
70
Alex Deymof329b932014-10-30 01:37:48 -070071void HttpFetcher::ProxiesResolved(const deque<string>& proxies) {
Alex Deymo60ca1a72015-06-18 18:19:15 -070072 no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080073 if (!proxies.empty())
Andrew de los Reyes45168102010-11-22 11:13:50 -080074 SetProxies(proxies);
Alex Deymo60ca1a72015-06-18 18:19:15 -070075 CHECK_NE(static_cast<Closure*>(nullptr), callback_.get());
76 Closure* callback = callback_.release();
Andrew de los Reyesffcb6d12011-04-04 09:57:38 -070077 // This may indirectly call back into ResolveProxiesForUrl():
78 callback->Run();
Alex Deymoc4acdf42014-05-28 21:07:10 -070079 delete callback;
Andrew de los Reyes45168102010-11-22 11:13:50 -080080}
81
82} // namespace chromeos_update_engine