blob: 4fd10826fe1df515a650d8fc39f788777fb7b18e [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.
Alex Deymo60ca1a72015-06-18 18:19:15 -070046bool HttpFetcher::ResolveProxiesForUrl(const string& url,
47 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)));
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080057 return true;
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)));
Alex Deymo71f67622017-02-03 21:30:24 -080061 if (proxy_request_ == kProxyRequestIdNull) {
62 callback_.reset();
63 return false;
64 }
65 return true;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080066}
67
Alex Deymo60ca1a72015-06-18 18:19:15 -070068void HttpFetcher::NoProxyResolverCallback() {
Alex Deymo71f67622017-02-03 21:30:24 -080069 no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
Alex Deymo60ca1a72015-06-18 18:19:15 -070070 ProxiesResolved(deque<string>());
71}
72
Alex Deymof329b932014-10-30 01:37:48 -070073void HttpFetcher::ProxiesResolved(const deque<string>& proxies) {
Alex Deymo71f67622017-02-03 21:30:24 -080074 proxy_request_ = kProxyRequestIdNull;
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -080075 if (!proxies.empty())
Andrew de los Reyes45168102010-11-22 11:13:50 -080076 SetProxies(proxies);
Alex Deymo71f67622017-02-03 21:30:24 -080077 CHECK(callback_.get()) << "ProxiesResolved but none pending.";
Alex Deymo60ca1a72015-06-18 18:19:15 -070078 Closure* callback = callback_.release();
Andrew de los Reyesffcb6d12011-04-04 09:57:38 -070079 // This may indirectly call back into ResolveProxiesForUrl():
80 callback->Run();
Alex Deymoc4acdf42014-05-28 21:07:10 -070081 delete callback;
Andrew de los Reyes45168102010-11-22 11:13:50 -080082}
83
Alex Deymo71f67622017-02-03 21:30:24 -080084bool HttpFetcher::CancelProxyResolution() {
85 bool ret = false;
86 if (no_resolver_idle_id_ != MessageLoop::kTaskIdNull) {
87 ret = MessageLoop::current()->CancelTask(no_resolver_idle_id_);
88 no_resolver_idle_id_ = MessageLoop::kTaskIdNull;
89 }
90 if (proxy_request_ && proxy_resolver_) {
91 ret = proxy_resolver_->CancelProxyRequest(proxy_request_) || ret;
92 proxy_request_ = kProxyRequestIdNull;
93 }
94 return ret;
95}
96
Andrew de los Reyes45168102010-11-22 11:13:50 -080097} // namespace chromeos_update_engine