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
|
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// 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.
#include "src/other_tools/utils/curl_easy_handle.hpp"
#include <cstdio>
#include <exception>
#include <fstream>
#include "fmt/core.h"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
extern "C" {
#include "curl/curl.h"
}
void curl_easy_closer(gsl::owner<CURL*> curl) {
curl_easy_cleanup(curl);
}
namespace {
auto read_stream_data(gsl::not_null<std::FILE*> const& stream) noexcept
-> std::string {
// obtain stream size
std::fseek(stream, 0, SEEK_END);
auto size = std::ftell(stream);
auto pos = std::fseek(stream, 0, SEEK_SET);
if (pos != 0) {
Logger::Log(LogLevel::Warning,
"Rewinding temporary file for curl log failed.");
}
// create string buffer to hold stream content
std::string content(static_cast<std::size_t>(size), '\0');
// read stream content into string buffer
auto n = std::fread(content.data(), 1, content.size(), stream);
if (n != static_cast<std::size_t>(size)) {
Logger::Log(LogLevel::Warning,
"Reading curl log from temporary file failed: read only {} "
"bytes while {} were expected",
n,
size);
}
return content;
}
} // namespace
auto CurlEasyHandle::Create(LogLevel log_level) noexcept
-> std::shared_ptr<CurlEasyHandle> {
return Create(false, std::nullopt, log_level);
}
auto CurlEasyHandle::Create(
bool no_ssl_verify,
std::optional<std::filesystem::path> const& ca_bundle,
LogLevel log_level) noexcept -> std::shared_ptr<CurlEasyHandle> {
try {
auto curl = std::make_shared<CurlEasyHandle>();
auto* handle = curl_easy_init();
if (handle == nullptr) {
return nullptr;
}
curl->handle_.reset(handle);
// store CA info
curl->no_ssl_verify_ = no_ssl_verify;
curl->ca_bundle_ = ca_bundle;
// store log level
curl->log_level_ = log_level;
return curl;
} catch (std::exception const& ex) {
Logger::Log(LogLevel::Error,
"create curl easy handle failed with:\n{}",
ex.what());
return nullptr;
}
}
auto CurlEasyHandle::EasyWriteToFile(gsl::owner<char*> data,
std::size_t size,
std::size_t nmemb,
gsl::owner<void*> userptr)
-> std::streamsize {
auto actual_size = static_cast<std::streamsize>(size * nmemb);
auto* file = static_cast<std::ofstream*>(userptr);
file->write(data, actual_size); // append chunk
return actual_size;
}
auto CurlEasyHandle::EasyWriteToString(gsl::owner<char*> data,
std::size_t size,
std::size_t nmemb,
gsl::owner<void*> userptr)
-> std::streamsize {
size_t actual_size = size * nmemb;
(static_cast<std::string*>(userptr))->append(data, actual_size);
return static_cast<std::streamsize>(actual_size);
}
auto CurlEasyHandle::DownloadToFile(
std::string const& url,
std::filesystem::path const& file_path) noexcept -> int {
// create temporary file to capture curl debug output
gsl::owner<std::FILE*> tmp_file = std::tmpfile();
try {
// set URL
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_URL, url.c_str());
// ensure redirects are allowed, otherwise it might simply read empty
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_FOLLOWLOCATION, 1);
// ensure failure on error codes that otherwise might return OK
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_FAILONERROR, 1);
// set callback for writing to file
std::ofstream file(file_path.c_str(), std::ios::binary);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_WRITEFUNCTION, EasyWriteToFile);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(
handle_.get(), CURLOPT_WRITEDATA, static_cast<void*>(&file));
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_VERBOSE, 1);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_STDERR, tmp_file);
// set SSL options
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(),
CURLOPT_SSL_VERIFYPEER,
static_cast<int>(not no_ssl_verify_));
if (ca_bundle_) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(
handle_.get(), CURLOPT_CAINFO, ca_bundle_->c_str());
}
// perform download
auto res = curl_easy_perform(handle_.get());
// close file
file.close();
// check result
if (res != CURLE_OK) {
// cleanup failed downloaded file, if created
[[maybe_unused]] auto tmp_res =
FileSystemManager::RemoveFile(file_path);
Logger::Log(log_level_, [&tmp_file]() {
return fmt::format("curl download to file failed:\n{}",
read_stream_data(tmp_file));
});
std::fclose(tmp_file);
return 1;
}
// print curl debug output if log level is tracing
Logger::Log(LogLevel::Trace, [&tmp_file]() {
return fmt::format("stderr of curl downloading to file:\n{}",
read_stream_data(tmp_file));
});
std::fclose(tmp_file);
return res;
} catch (std::exception const& ex) {
Logger::Log(log_level_, [&ex, &tmp_file]() {
return fmt::format(
"curl download to file failed with:\n{}\n"
"while performing:\n{}",
ex.what(),
read_stream_data(tmp_file));
});
std::fclose(tmp_file);
return 1;
}
}
auto CurlEasyHandle::DownloadToString(std::string const& url) noexcept
-> std::optional<std::string> {
// create temporary file to capture curl debug output
gsl::owner<std::FILE*> tmp_file = std::tmpfile();
try {
// set URL
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_URL, url.c_str());
// ensure redirects are allowed, otherwise it might simply read empty
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_FOLLOWLOCATION, 1);
// ensure failure on error codes that otherwise might return OK
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_FAILONERROR, 1);
// set callback for writing to string
std::string content{};
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(
handle_.get(), CURLOPT_WRITEFUNCTION, EasyWriteToString);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(
handle_.get(), CURLOPT_WRITEDATA, static_cast<void*>(&content));
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_VERBOSE, 1);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(), CURLOPT_STDERR, tmp_file);
// set SSL options
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(handle_.get(),
CURLOPT_SSL_VERIFYPEER,
static_cast<int>(not no_ssl_verify_));
if (ca_bundle_) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, hicpp-vararg)
curl_easy_setopt(
handle_.get(), CURLOPT_CAINFO, ca_bundle_->c_str());
}
// perform download
auto res = curl_easy_perform(handle_.get());
// check result
if (res != CURLE_OK) {
Logger::Log(log_level_, [&tmp_file]() {
return fmt::format("curl download to string failed:\n{}",
read_stream_data(tmp_file));
});
std::fclose(tmp_file);
return std::nullopt;
}
// print curl debug output if log level is tracing
Logger::Log(LogLevel::Trace, [&tmp_file]() {
return fmt::format("stderr of curl downloading to string:\n{}",
read_stream_data(tmp_file));
});
std::fclose(tmp_file);
return content;
} catch (std::exception const& ex) {
Logger::Log(log_level_, [&ex, &tmp_file]() {
return fmt::format(
"curl download to string failed with:\n{}\n"
"while performing:\n{}",
ex.what(),
read_stream_data(tmp_file));
});
std::fclose(tmp_file);
return std::nullopt;
}
}
|