-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathhttp.cc
More file actions
449 lines (381 loc) · 11.9 KB
/
http.cc
File metadata and controls
449 lines (381 loc) · 11.9 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// MinIO C++ Library for Amazon S3 Compatible Cloud Storage
// Copyright 2022-2024 MinIO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#include "miniocpp/http.h"
#include <curl/curl.h>
#include <curlpp/Easy.hpp>
#include <curlpp/Exception.hpp>
#include <curlpp/Infos.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/cURLpp.hpp>
#include <exception>
#include <functional>
#include <iosfwd>
#include <iostream>
#include <list>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include "miniocpp/error.h"
#include "miniocpp/utils.h"
#ifdef _WIN32
#include <WinSock2.h>
#include <ws2def.h> // NOTE needed for AF_INET6
#include <ws2ipdef.h> // NOTE needed for sockaddr_in6
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#endif
#ifndef CURL_WRITEFUNC_ERROR
#define CURL_WRITEFUNC_ERROR static_cast<size_t>(0xFFFFFFFF)
#endif
namespace minio::http {
// MethodToString converts http Method enum to string.
const char* MethodToString(Method method) noexcept {
switch (method) {
case Method::kGet:
return "GET";
case Method::kHead:
return "HEAD";
case Method::kPost:
return "POST";
case Method::kPut:
return "PUT";
case Method::kDelete:
return "DELETE";
default: {
std::cerr << "ABORT: Unimplemented HTTP method. This should not happen."
<< std::endl;
std::terminate();
}
}
return nullptr;
}
std::string Url::String() const {
if (host.empty()) return {};
std::string url = (https ? "https://" : "http://") + host;
if (port) url += ":" + std::to_string(port);
if (!path.empty()) {
if (path.front() != '/') url += '/';
url += path;
}
if (!query_string.empty()) url += "?" + query_string;
return url;
}
std::string Url::HostHeaderValue() const {
if (!port) {
return host;
}
return host + ":" + std::to_string(port);
}
Url Url::Parse(std::string value) {
std::string scheme;
size_t pos = value.find("://");
if (pos != std::string::npos) {
scheme = value.substr(0, pos);
value.erase(0, pos + 3);
}
scheme = utils::ToLower(scheme);
if (!scheme.empty() && scheme != "http" && scheme != "https") return Url{};
bool https = (scheme.empty() || scheme == "https");
std::string host;
std::string path;
std::string query_string;
pos = value.find("/");
if (pos != std::string::npos) {
host = value.substr(0, pos);
value.erase(0, pos + 1);
pos = value.find("?");
if (pos != std::string::npos) {
path = value.substr(0, pos);
value.erase(0, pos + 1);
query_string = value;
} else {
path = value;
}
} else {
pos = value.find("?");
if (pos != std::string::npos) {
host = value.substr(0, pos);
value.erase(0, pos + 1);
query_string = value;
} else {
host = value;
}
}
if (host.empty()) {
return Url{};
}
unsigned int port = 0;
struct sockaddr_in6 dst;
if (inet_pton(AF_INET6, host.c_str(), &(dst.sin6_addr)) <= 0) {
if (host.front() != '[' || host.back() != ']') {
std::stringstream ss(host);
std::string portstr;
while (std::getline(ss, portstr, ':')) {
}
if (!portstr.empty()) {
try {
port = static_cast<unsigned>(std::stoi(portstr));
host = host.substr(0, host.rfind(":" + portstr));
} catch (const std::invalid_argument&) {
port = 0;
}
}
}
} else {
host = "[" + host + "]";
}
if (!https && port == 80) port = 0;
if (https && port == 443) port = 0;
return Url(https, std::move(host), port, std::move(path),
std::move(query_string));
}
error::Error Response::ReadStatusCode() {
size_t pos = response_.find("\r\n");
if (pos == std::string::npos) {
// Not yet received the first line.
return error::SUCCESS;
}
std::string line = response_.substr(0, pos);
response_.erase(0, pos + 2);
if (continue100_) {
if (!line.empty()) {
// After '100 Continue', next line must be empty new line.
return error::Error("invalid HTTP response");
}
continue100_ = false;
pos = response_.find("\r\n");
if (pos == std::string::npos) {
// Not yet received the first line after '100 Continue'.
return error::SUCCESS;
}
line = response_.substr(0, pos);
response_.erase(0, pos + 2);
}
// Skip HTTP/1.x.
pos = line.find(" ");
if (pos == std::string::npos) {
// First token must be HTTP/1.x
return error::Error("invalid HTTP response");
}
line = line.substr(pos + 1);
// Read status code.
pos = line.find(" ");
if (pos == std::string::npos) {
// The line must contain second token.
return error::Error("invalid HTTP response");
}
std::string code = line.substr(0, pos);
std::string::size_type st;
status_code = std::stoi(code, &st);
if (st == std::string::npos) {
// Code must be a number.
return error::Error("invalid HTTP response code " + code);
}
if (status_code == 100) {
continue100_ = true;
} else {
status_code_read_ = true;
}
return error::SUCCESS;
}
error::Error Response::Error() const {
if (!error.empty()) return error::Error(error);
if (status_code && (status_code < 200 || status_code > 299)) {
return error::Error("failed with HTTP status code " +
std::to_string(status_code));
}
return error::SUCCESS;
}
error::Error Response::ReadHeaders() {
size_t pos = response_.find("\r\n\r\n");
if (pos == std::string::npos) {
// Not yet received the headers.
return error::SUCCESS;
}
headers_read_ = true;
std::string lines = response_.substr(0, pos);
response_.erase(0, pos + 4);
auto add_header = [&headers = headers](std::string line) -> error::Error {
size_t pos = line.find(": ");
if (pos != std::string::npos) {
headers.Add(line.substr(0, pos), line.substr(pos + 2));
return error::SUCCESS;
}
return error::Error("invalid HTTP header: " + line);
};
while ((pos = lines.find("\r\n")) != std::string::npos) {
std::string line = lines.substr(0, pos);
lines.erase(0, pos + 2);
if (error::Error err = add_header(line)) return err;
}
if (!lines.empty()) {
if (error::Error err = add_header(lines)) return err;
}
return error::SUCCESS;
}
size_t Response::ResponseCallback(curlpp::Easy* const request,
const char* const buffer, size_t size,
size_t length) {
size_t realsize = size * length;
// If error occurred previously, just cancel the request.
if (!error.empty()) {
return CURL_WRITEFUNC_ERROR;
}
if (!status_code_read_ || !headers_read_) {
response_.append(buffer, length);
}
if (!status_code_read_) {
if (error::Error err = ReadStatusCode()) {
error = err.String();
return CURL_WRITEFUNC_ERROR;
}
if (!status_code_read_) return realsize;
}
if (!headers_read_) {
if (error::Error err = ReadHeaders()) {
error = err.String();
return CURL_WRITEFUNC_ERROR;
}
if (!headers_read_ || response_.empty()) return realsize;
// If data function is set and the request is successful, send data.
if (datafunc != nullptr && status_code >= 200 && status_code <= 299) {
DataFunctionArgs args(request, this, std::string(this->response_),
userdata);
if (!datafunc(args)) return CURL_WRITEFUNC_ERROR;
} else {
body = response_;
}
return realsize;
}
// If data function is set and the request is successful, send data.
if (datafunc != nullptr && status_code >= 200 && status_code <= 299) {
DataFunctionArgs args(request, this, std::string(buffer, length), userdata);
if (!datafunc(args)) return CURL_WRITEFUNC_ERROR;
} else {
body.append(buffer, length);
}
return realsize;
}
Request::Request(Method method, Url url) {
this->method = method;
this->url = url;
std::string ssl_cert_file;
if (url.https && utils::GetEnv(ssl_cert_file, "SSL_CERT_FILE")) {
this->ssl_cert_file = ssl_cert_file;
}
}
Response Request::execute() {
curlpp::Cleanup cleaner;
curlpp::Easy request;
// Request settings.
request.setOpt(new curlpp::options::CustomRequest{MethodToString(method)});
std::string urlstring = url.String();
request.setOpt(new curlpp::Options::Url(urlstring));
if (debug) request.setOpt(new curlpp::Options::Verbose(true));
if (ignore_cert_check) {
request.setOpt(new curlpp::Options::SslVerifyPeer(false));
request.setOpt(new curlpp::Options::SslVerifyHost(0L));
}
if (url.https) {
if (!ssl_cert_file.empty()) {
request.setOpt(new curlpp::Options::SslVerifyPeer(true));
request.setOpt(new curlpp::Options::CaInfo(ssl_cert_file));
}
if (!key_file.empty()) {
request.setOpt(new curlpp::Options::SslKey(key_file));
}
if (!cert_file.empty()) {
request.setOpt(new curlpp::Options::SslCert(cert_file));
}
}
utils::CharBuffer charbuf((char*)body.data(), body.size());
std::istream body_stream(&charbuf);
switch (method) {
case Method::kDelete:
case Method::kGet:
break;
case Method::kHead:
request.setOpt(new curlpp::options::NoBody(true));
break;
case Method::kPut:
case Method::kPost:
if (!headers.Contains("Content-Length")) {
headers.Add("Content-Length", std::to_string(body.size()));
}
request.setOpt(new curlpp::Options::ReadStream(&body_stream));
request.setOpt(
new curlpp::Options::InfileSize(static_cast<long>(body.size())));
request.setOpt(new curlpp::Options::Upload(true));
break;
}
std::list<std::string> headerlist = headers.ToHttpHeaders();
headerlist.push_back("Expect:"); // Disable 100 continue from server.
request.setOpt(new curlpp::Options::HttpHeader(headerlist));
// Response settings.
request.setOpt(new curlpp::options::Header(true));
Response response;
response.datafunc = datafunc;
response.userdata = userdata;
using namespace std::placeholders;
request.setOpt(new curlpp::options::WriteFunction(
std::bind(&Response::ResponseCallback, &response, &request, _1, _2, _3)));
auto progress =
[&progressfunc = progressfunc, &progress_userdata = progress_userdata](
double dltotal, double dlnow, double ultotal, double ulnow) -> int {
ProgressFunctionArgs args;
args.download_total_bytes = dltotal;
args.downloaded_bytes = dlnow;
args.upload_total_bytes = ultotal;
args.uploaded_bytes = ulnow;
args.userdata = progress_userdata;
if (progressfunc(args)) {
return CURL_PROGRESSFUNC_CONTINUE;
}
return 1;
};
if (progressfunc != nullptr) {
request.setOpt(new curlpp::options::NoProgress(false));
request.setOpt(new curlpp::options::ProgressFunction(progress));
}
// Execute.
request.perform();
if (progressfunc != nullptr) {
ProgressFunctionArgs args;
args.userdata = progress_userdata;
curlpp::infos::SpeedUpload::get(request, args.upload_speed);
curlpp::infos::SpeedDownload::get(request, args.download_speed);
progressfunc(args);
}
return response;
}
Response Request::Execute() {
try {
return execute();
} catch (curlpp::LogicError& e) {
Response response;
response.error = std::string("curlpp::LogicError: ") + e.what();
return response;
} catch (curlpp::RuntimeError& e) {
Response response;
response.error = std::string("curlpp::RuntimeError: ") + e.what();
return response;
}
}
} // namespace minio::http