-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_request.cc
249 lines (216 loc) · 7.95 KB
/
http_request.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "shill/http_request.h"
#include <curl/curl.h>
#include <string>
#include <utility>
#include <base/check.h>
#include <base/functional/bind.h>
#include <base/logging.h>
#include <base/strings/string_number_conversions.h>
#include <base/time/time.h>
#include <brillo/http/http_utils.h>
#include "shill/dns_client.h"
#include "shill/error.h"
#include "shill/event_dispatcher.h"
#include "shill/http_url.h"
#include "shill/logging.h"
namespace {
// The curl error domain for http requests
const char kCurlEasyError[] = "curl_easy_error";
} // namespace
namespace shill {
namespace Logging {
static auto kModuleLogScope = ScopeLogger::kHTTP;
static std::string ObjectID(const HttpRequest* r) {
return r->logging_tag();
}
} // namespace Logging
HttpRequest::HttpRequest(EventDispatcher* dispatcher,
const std::string& interface_name,
net_base::IPFamily ip_family,
const std::vector<net_base::IPAddress>& dns_list,
bool allow_non_google_https)
: ip_family_(ip_family),
dns_list_(dns_list),
weak_ptr_factory_(this),
dns_client_callback_(base::BindRepeating(&HttpRequest::GetDNSResult,
weak_ptr_factory_.GetWeakPtr())),
dns_client_(new DnsClient(ip_family_,
interface_name,
DnsClient::kDnsTimeout,
dispatcher,
dns_client_callback_)),
transport_(brillo::http::Transport::CreateDefault()),
request_id_(-1),
is_running_(false) {
// b/180521518, Force the transport to bind to |interface_name|. Otherwise,
// the request would be routed by default through the current physical default
// network. b/288351302: binding to an IP address of the interface name is not
// enough to disambiguate all IPv4 multi-network scenarios.
transport_->SetInterface(interface_name);
if (allow_non_google_https) {
transport_->UseCustomCertificate(
brillo::http::Transport::Certificate::kNss);
}
}
HttpRequest::~HttpRequest() {
Stop();
}
HttpRequest::Result HttpRequest::Start(
const std::string& logging_tag,
const HttpUrl& url,
const brillo::http::HeaderList& headers,
base::OnceCallback<void(std::shared_ptr<brillo::http::Response>)>
request_success_callback,
base::OnceCallback<void(Result)> request_error_callback) {
SLOG(this, 3) << "In " << __func__;
DCHECK(!is_running_);
logging_tag_ = logging_tag;
url_ = url;
headers_ = headers;
is_running_ = true;
transport_->SetDefaultTimeout(kRequestTimeout);
request_success_callback_ = std::move(request_success_callback);
request_error_callback_ = std::move(request_error_callback);
const auto server_addr = net_base::IPAddress::CreateFromString(url_.host());
if (server_addr && server_addr->GetFamily() != ip_family_) {
LOG(ERROR) << logging_tag_ << ": Server hostname " << url_.host()
<< " doesn't match the IP family " << ip_family_;
Stop();
return kResultDNSFailure;
}
if (server_addr) {
StartRequest();
} else {
SLOG(this, 2) << "Looking up host: " << url_.host();
Error error;
std::vector<std::string> dns_addresses;
// TODO(b/307880493): Migrate to net_base::DNSClient and avoid
// converting the net_base::IPAddress values to std::string.
for (const auto& dns : dns_list_) {
if (dns.GetFamily() == ip_family_) {
dns_addresses.push_back(dns.ToString());
}
}
if (!dns_client_->Start(dns_addresses, url_.host(), &error)) {
LOG(ERROR) << logging_tag_
<< ": Failed to start DNS client: " << error.message();
Stop();
return kResultDNSFailure;
}
}
return kResultInProgress;
}
void HttpRequest::StartRequest() {
std::string url_string = url_.ToString();
SLOG(this, 2) << logging_tag_ << ": Starting request to " << url_string;
request_id_ =
brillo::http::Get(url_string, headers_, transport_,
base::BindOnce(&HttpRequest::SuccessCallback,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&HttpRequest::ErrorCallback,
weak_ptr_factory_.GetWeakPtr()));
}
void HttpRequest::SuccessCallback(
brillo::http::RequestID request_id,
std::unique_ptr<brillo::http::Response> response) {
if (request_id != request_id_) {
LOG(ERROR) << logging_tag_ << ": Expected request ID " << request_id_
<< " but got " << request_id;
SendStatus(kResultUnknown);
return;
}
base::OnceCallback<void(std::shared_ptr<brillo::http::Response>)>
request_success_callback = std::move(request_success_callback_);
Stop();
if (!request_success_callback.is_null()) {
std::move(request_success_callback).Run(std::move(response));
}
}
void HttpRequest::ErrorCallback(brillo::http::RequestID request_id,
const brillo::Error* error) {
int error_code;
if (error->GetDomain() != kCurlEasyError) {
LOG(ERROR) << logging_tag_ << ": Expected error domain " << kCurlEasyError
<< " but got " << error->GetDomain();
SendStatus(kResultUnknown);
return;
}
if (request_id != request_id_) {
LOG(ERROR) << logging_tag_ << ": Expected request ID " << request_id_
<< " but got " << request_id;
SendStatus(kResultUnknown);
return;
}
if (!base::StringToInt(error->GetCode(), &error_code)) {
LOG(ERROR) << logging_tag_ << ": Unable to convert error code "
<< error->GetCode() << " to Int";
SendStatus(kResultUnknown);
return;
}
// TODO(matthewmwang): This breaks abstraction. Modify brillo::http::Transport
// to provide an implementation agnostic error code.
switch (error_code) {
case CURLE_COULDNT_CONNECT:
SendStatus(kResultConnectionFailure);
break;
case CURLE_WRITE_ERROR:
case CURLE_READ_ERROR:
SendStatus(kResultHTTPFailure);
break;
case CURLE_OPERATION_TIMEDOUT:
SendStatus(kResultHTTPTimeout);
break;
default:
SendStatus(kResultUnknown);
}
}
void HttpRequest::Stop() {
SLOG(this, 3) << "In " << __func__ << ": running is " << is_running_;
if (!is_running_) {
return;
}
// Clear IO handlers first so that closing the socket doesn't cause
// events to fire.
dns_client_->Stop();
is_running_ = false;
request_id_ = -1;
request_error_callback_.Reset();
request_success_callback_.Reset();
}
// DnsClient callback that fires when the DNS request completes.
void HttpRequest::GetDNSResult(
const base::expected<net_base::IPAddress, Error>& address) {
SLOG(this, 3) << "In " << __func__;
if (!address.has_value()) {
LOG(ERROR) << logging_tag_ << ": Could not resolve " << url_.host() << ": "
<< address.error().message();
if (address.error().message() == DnsClient::kErrorTimedOut) {
SendStatus(kResultDNSTimeout);
} else {
SendStatus(kResultDNSFailure);
}
return;
}
// Add the host/port to IP mapping to the DNS cache to force curl to resolve
// the URL to the given IP. Otherwise, will do its own DNS resolution and not
// use the IP we provide to it.
transport_->ResolveHostToIp(url_.host(), url_.port(), address->ToString());
LOG(INFO) << logging_tag_ << ": Resolved " << url_.host() << " to "
<< *address;
StartRequest();
}
void HttpRequest::SendStatus(Result result) {
// Save copies on the stack, since Stop() will remove them.
base::OnceCallback<void(Result)> request_error_callback =
std::move(request_error_callback_);
Stop();
// Call the callback last, since it may delete us and |this| may no longer
// be valid.
if (!request_error_callback.is_null()) {
std::move(request_error_callback).Run(result);
}
}
} // namespace shill