blob: 73c0d484156946c7a81c0fe93bd7b17a2da20e09 [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 Deymo71f67622017-02-03 21:30:24 -080029 CancelProxyResolution();
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080030}
31
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080032void HttpFetcher::SetPostData(const void* data, size_t size,
33 HttpContentType type) {
Andrew de los Reyes45168102010-11-22 11:13:50 -080034 post_data_set_ = true;
35 post_data_.clear();
Alex Deymo60ca1a72015-06-18 18:19:15 -070036 const char* char_data = reinterpret_cast<const char*>(data);
Andrew de los Reyes45168102010-11-22 11:13:50 -080037 post_data_.insert(post_data_.end(), char_data, char_data + size);
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080038 post_content_type_ = type;
39}
40
41void HttpFetcher::SetPostData(const void* data, size_t size) {
42 SetPostData(data, size, kHttpContentTypeUnspecified);
Andrew de los Reyes45168102010-11-22 11:13:50 -080043}
44
45// Proxy methods to set the proxies, then to pop them off.
Daniel Erate5f6f252017-04-20 12:09:58 -060046void HttpFetcher::ResolveProxiesForUrl(const string& url,
Alex Deymo60ca1a72015-06-18 18:19:15 -070047 const Closure& callback) {
48 CHECK_EQ(static_cast<Closure*>(nullptr), callback_.get());
49 callback_.reset(new Closure(callback));
50
Andrew de los Reyes45168102010-11-22 11:13:50 -080051 if (!proxy_resolver_) {
52 LOG(INFO) << "Not resolving proxies (no proxy resolver).";
Alex Deymo60ca1a72015-06-18 18:19:15 -070053 no_resolver_idle_id_ = MessageLoop::current()->PostTask(
54 FROM_HERE,
55 base::Bind(&HttpFetcher::NoProxyResolverCallback,
56 base::Unretained(this)));
Daniel Erate5f6f252017-04-20 12:09:58 -060057 return;
Andrew de los Reyes45168102010-11-22 11:13:50 -080058 }
Alex Deymo71f67622017-02-03 21:30:24 -080059 proxy_request_ = proxy_resolver_->GetProxiesForUrl(
Alex Deymo35821942017-02-05 04:36:02 +000060 url, base::Bind(&HttpFetcher::ProxiesResolved, base::Unretained(this)));
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080061}
62
Alex Deymo60ca1a72015-06-18 18:19:15 -070063void HttpFetcher::NoProxyResolverCallback() {
Alex Deymo71f67622017-02-03 21:30:24 -080064 no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
Alex Deymo60ca1a72015-06-18 18:19:15 -070065 ProxiesResolved(deque<string>());
66}
67
Alex Deymof329b932014-10-30 01:37:48 -070068void HttpFetcher::ProxiesResolved(const deque<string>& proxies) {
Alex Deymo71f67622017-02-03 21:30:24 -080069 proxy_request_ = kProxyRequestIdNull;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080070 if (!proxies.empty())
Andrew de los Reyes45168102010-11-22 11:13:50 -080071 SetProxies(proxies);
Alex Deymo71f67622017-02-03 21:30:24 -080072 CHECK(callback_.get()) << "ProxiesResolved but none pending.";
Alex Deymo60ca1a72015-06-18 18:19:15 -070073 Closure* callback = callback_.release();
Andrew de los Reyesffcb6d12011-04-04 09:57:38 -070074 // This may indirectly call back into ResolveProxiesForUrl():
75 callback->Run();
Alex Deymoc4acdf42014-05-28 21:07:10 -070076 delete callback;
Andrew de los Reyes45168102010-11-22 11:13:50 -080077}
78
Alex Deymo71f67622017-02-03 21:30:24 -080079bool HttpFetcher::CancelProxyResolution() {
80 bool ret = false;
81 if (no_resolver_idle_id_ != MessageLoop::kTaskIdNull) {
82 ret = MessageLoop::current()->CancelTask(no_resolver_idle_id_);
83 no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
84 }
85 if (proxy_request_ && proxy_resolver_) {
86 ret = proxy_resolver_->CancelProxyRequest(proxy_request_) || ret;
87 proxy_request_ = kProxyRequestIdNull;
88 }
89 return ret;
90}
91
Andrew de los Reyes45168102010-11-22 11:13:50 -080092} // namespace chromeos_update_engine