blob: 5a98dfcfd843993ec9e7a54f8bd4e6be616a65c7 [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
Amin Hassanib2689592019-01-13 17:04:28 -080032void HttpFetcher::SetPostData(const void* data,
33 size_t size,
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080034 HttpContentType type) {
Andrew de los Reyes45168102010-11-22 11:13:50 -080035 post_data_set_ = true;
36 post_data_.clear();
Alex Deymo60ca1a72015-06-18 18:19:15 -070037 const char* char_data = reinterpret_cast<const char*>(data);
Andrew de los Reyes45168102010-11-22 11:13:50 -080038 post_data_.insert(post_data_.end(), char_data, char_data + size);
Gilad Arnold9dd1e7c2012-02-16 12:13:36 -080039 post_content_type_ = type;
40}
41
42void HttpFetcher::SetPostData(const void* data, size_t size) {
43 SetPostData(data, size, kHttpContentTypeUnspecified);
Andrew de los Reyes45168102010-11-22 11:13:50 -080044}
45
46// Proxy methods to set the proxies, then to pop them off.
Daniel Erate5f6f252017-04-20 12:09:58 -060047void HttpFetcher::ResolveProxiesForUrl(const string& url,
Alex Deymo60ca1a72015-06-18 18:19:15 -070048 const Closure& callback) {
49 CHECK_EQ(static_cast<Closure*>(nullptr), callback_.get());
50 callback_.reset(new Closure(callback));
51
Andrew de los Reyes45168102010-11-22 11:13:50 -080052 if (!proxy_resolver_) {
53 LOG(INFO) << "Not resolving proxies (no proxy resolver).";
Alex Deymo60ca1a72015-06-18 18:19:15 -070054 no_resolver_idle_id_ = MessageLoop::current()->PostTask(
55 FROM_HERE,
56 base::Bind(&HttpFetcher::NoProxyResolverCallback,
57 base::Unretained(this)));
Daniel Erate5f6f252017-04-20 12:09:58 -060058 return;
Andrew de los Reyes45168102010-11-22 11:13:50 -080059 }
Alex Deymo71f67622017-02-03 21:30:24 -080060 proxy_request_ = proxy_resolver_->GetProxiesForUrl(
Alex Deymo35821942017-02-05 04:36:02 +000061 url, base::Bind(&HttpFetcher::ProxiesResolved, base::Unretained(this)));
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080062}
63
Alex Deymo60ca1a72015-06-18 18:19:15 -070064void HttpFetcher::NoProxyResolverCallback() {
Alex Deymo71f67622017-02-03 21:30:24 -080065 no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
Alex Deymo60ca1a72015-06-18 18:19:15 -070066 ProxiesResolved(deque<string>());
67}
68
Alex Deymof329b932014-10-30 01:37:48 -070069void HttpFetcher::ProxiesResolved(const deque<string>& proxies) {
Alex Deymo71f67622017-02-03 21:30:24 -080070 proxy_request_ = kProxyRequestIdNull;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080071 if (!proxies.empty())
Andrew de los Reyes45168102010-11-22 11:13:50 -080072 SetProxies(proxies);
Alex Deymo71f67622017-02-03 21:30:24 -080073 CHECK(callback_.get()) << "ProxiesResolved but none pending.";
Alex Deymo60ca1a72015-06-18 18:19:15 -070074 Closure* callback = callback_.release();
Andrew de los Reyesffcb6d12011-04-04 09:57:38 -070075 // This may indirectly call back into ResolveProxiesForUrl():
76 callback->Run();
Alex Deymoc4acdf42014-05-28 21:07:10 -070077 delete callback;
Andrew de los Reyes45168102010-11-22 11:13:50 -080078}
79
Alex Deymo71f67622017-02-03 21:30:24 -080080bool 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 Reyes45168102010-11-22 11:13:50 -080093} // namespace chromeos_update_engine