/root/doris/be/src/http/http_client.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "http/http_client.h" |
19 | | |
20 | | #include <glog/logging.h> |
21 | | #include <unistd.h> |
22 | | |
23 | | #include <memory> |
24 | | #include <ostream> |
25 | | |
26 | | #include "common/config.h" |
27 | | #include "common/status.h" |
28 | | #include "http/http_headers.h" |
29 | | #include "runtime/exec_env.h" |
30 | | #include "util/security.h" |
31 | | #include "util/stack_util.h" |
32 | | |
33 | | namespace doris { |
34 | | |
35 | | class MultiFileSplitter { |
36 | | public: |
37 | | MultiFileSplitter(std::string local_dir, std::unordered_set<std::string> expected_files) |
38 | 1 | : _local_dir_path(std::move(local_dir)), _expected_files(std::move(expected_files)) {} |
39 | 1 | ~MultiFileSplitter() { |
40 | 1 | if (_fd >= 0) { |
41 | 0 | close(_fd); |
42 | 0 | } |
43 | | |
44 | 1 | if (!_status.ok() && !downloaded_files.empty()) { |
45 | 0 | LOG(WARNING) << "download files to " << _local_dir_path << " failed, try remove the " |
46 | 0 | << downloaded_files.size() << " downloaded files"; |
47 | 0 | for (const auto& file : downloaded_files) { |
48 | 0 | remove(file.c_str()); |
49 | 0 | } |
50 | 0 | } |
51 | 1 | } |
52 | | |
53 | 661 | bool append(const char* data, size_t length) { |
54 | | // Already failed. |
55 | 661 | if (!_status.ok()) { |
56 | 0 | return false; |
57 | 0 | } |
58 | | |
59 | 661 | std::string buf; |
60 | 661 | if (!_buffer.empty()) { |
61 | 0 | buf.swap(_buffer); |
62 | 0 | buf.append(data, length); |
63 | 0 | data = buf.data(); |
64 | 0 | length = buf.size(); |
65 | 0 | } |
66 | 661 | return append_inner(data, length); |
67 | 661 | } |
68 | | |
69 | 1 | Status finish() { |
70 | 1 | if (_status.ok()) { |
71 | 1 | _status = finish_inner(); |
72 | 1 | } |
73 | | |
74 | 1 | return _status; |
75 | 1 | } |
76 | | |
77 | | private: |
78 | 661 | bool append_inner(const char* data, size_t length) { |
79 | 1.39k | while (length > 0) { |
80 | 729 | int consumed = 0; |
81 | 729 | if (_is_reading_header) { |
82 | 35 | consumed = parse_header(data, length); |
83 | 694 | } else { |
84 | 694 | consumed = append_file(data, length); |
85 | 694 | } |
86 | | |
87 | 729 | if (consumed < 0) { |
88 | 0 | return false; |
89 | 0 | } |
90 | | |
91 | 729 | DCHECK(consumed <= length); |
92 | 729 | data += consumed; |
93 | 729 | length -= consumed; |
94 | 729 | } |
95 | 661 | return true; |
96 | 661 | } |
97 | | |
98 | 35 | int parse_header(const char* data, size_t length) { |
99 | 35 | DCHECK(_fd < 0); |
100 | | |
101 | 35 | std::string_view buf(data, length); |
102 | 35 | size_t pos = buf.find("\r\n\r\n"); |
103 | 35 | if (pos == std::string::npos) { |
104 | 0 | _buffer.append(data, length); |
105 | 0 | return static_cast<int>(length); |
106 | 0 | } |
107 | | |
108 | | // header already read. |
109 | 35 | _is_reading_header = false; |
110 | | |
111 | 35 | bool has_file_name = false; |
112 | 35 | bool has_file_size = false; |
113 | 35 | std::string_view header = buf.substr(0, pos); |
114 | 35 | std::vector<std::string> headers = |
115 | 35 | strings::Split(header, "\r\n", strings::SkipWhitespace()); |
116 | 70 | for (auto& s : headers) { |
117 | 70 | size_t header_pos = s.find(':'); |
118 | 70 | if (header_pos == std::string::npos) { |
119 | 0 | continue; |
120 | 0 | } |
121 | 70 | std::string_view header_view(s); |
122 | 70 | std::string_view key = header_view.substr(0, header_pos); |
123 | 70 | std::string_view value = header_view.substr(header_pos + 1); |
124 | 70 | if (value.starts_with(' ')) { |
125 | 70 | value.remove_prefix(std::min(value.find_first_not_of(' '), value.size())); |
126 | 70 | } |
127 | 70 | if (key == "File-Name") { |
128 | 35 | _file_name = value; |
129 | 35 | has_file_name = true; |
130 | 35 | } else if (key == "Content-Length") { |
131 | 35 | auto res = std::from_chars(value.data(), value.data() + value.size(), _file_size); |
132 | 35 | if (res.ec != std::errc()) { |
133 | 0 | std::string error_msg = fmt::format("invalid content length: {}", value); |
134 | 0 | LOG(WARNING) << "download files to " << _local_dir_path |
135 | 0 | << "failed, err=" << error_msg; |
136 | 0 | _status = Status::HttpError(std::move(error_msg)); |
137 | 0 | return -1; |
138 | 0 | } |
139 | 35 | has_file_size = true; |
140 | 35 | } |
141 | 70 | } |
142 | | |
143 | 35 | if (!has_file_name || !has_file_size) { |
144 | 0 | std::string error_msg = |
145 | 0 | fmt::format("invalid multi part header, has file name: {}, has file size: {}", |
146 | 0 | has_file_name, has_file_size); |
147 | 0 | LOG(WARNING) << "download files to " << _local_dir_path << "failed, err=" << error_msg; |
148 | 0 | _status = Status::HttpError(std::move(error_msg)); |
149 | 0 | return -1; |
150 | 0 | } |
151 | | |
152 | 35 | if (!_expected_files.contains(_file_name)) { |
153 | 0 | std::string error_msg = fmt::format("unexpected file: {}", _file_name); |
154 | 0 | LOG(WARNING) << "download files to " << _local_dir_path << "failed, err=" << error_msg; |
155 | 0 | _status = Status::HttpError(std::move(error_msg)); |
156 | 0 | return -1; |
157 | 0 | } |
158 | | |
159 | 35 | VLOG_DEBUG << "receive file " << _file_name << ", size " << _file_size; |
160 | | |
161 | 35 | _written_size = 0; |
162 | 35 | _local_file_path = fmt::format("{}/{}", _local_dir_path, _file_name); |
163 | 35 | _fd = open(_local_file_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644); |
164 | 35 | if (_fd < 0) { |
165 | 0 | std::string error_msg = "fail to open file to write: " + _local_file_path; |
166 | 0 | LOG(WARNING) << "download files to " << _local_dir_path << "failed, err=" << error_msg; |
167 | 0 | _status = Status::IOError(std::move(error_msg)); |
168 | 0 | return -1; |
169 | 0 | } |
170 | 35 | downloaded_files.push_back(_local_file_path); |
171 | | |
172 | 35 | return static_cast<int>(pos + 4); |
173 | 35 | } |
174 | | |
175 | 694 | int append_file(const char* data, size_t length) { |
176 | 694 | DCHECK(_fd >= 0); |
177 | 694 | DCHECK(_file_size >= _written_size); |
178 | | |
179 | 694 | size_t write_size = std::min(length, _file_size - _written_size); |
180 | 694 | if (write_size > 0 && write(_fd, data, write_size) < 0) { |
181 | 0 | auto msg = fmt::format("write file failed, file={}, error={}", _local_file_path, |
182 | 0 | strerror(errno)); |
183 | 0 | LOG(WARNING) << "download files to " << _local_dir_path << "failed, err=" << msg; |
184 | 0 | _status = Status::HttpError(std::move(msg)); |
185 | 0 | return -1; |
186 | 0 | } |
187 | | |
188 | 694 | _written_size += write_size; |
189 | 694 | if (_written_size == _file_size) { |
190 | | // This file has been downloaded, switch to the next one. |
191 | 34 | switch_to_next_file(); |
192 | 34 | } |
193 | | |
194 | 694 | return write_size; |
195 | 694 | } |
196 | | |
197 | 1 | Status finish_inner() { |
198 | 1 | if (!_is_reading_header && _written_size == _file_size) { |
199 | 1 | switch_to_next_file(); |
200 | 1 | } |
201 | | |
202 | 1 | if (_fd >= 0) { |
203 | | // This file is not completely downloaded. |
204 | 0 | close(_fd); |
205 | 0 | _fd = -1; |
206 | 0 | auto error_msg = fmt::format("file {} is not completely downloaded", _local_file_path); |
207 | 0 | LOG(WARNING) << "download files to " << _local_dir_path << "failed, err=" << error_msg; |
208 | 0 | return Status::HttpError(std::move(error_msg)); |
209 | 0 | } |
210 | | |
211 | 1 | if (!_expected_files.empty()) { |
212 | 0 | auto error_msg = fmt::format("not all files are downloaded, {} missing files", |
213 | 0 | _expected_files.size()); |
214 | 0 | LOG(WARNING) << "download files to " << _local_dir_path << "failed, err=" << error_msg; |
215 | 0 | return Status::HttpError(std::move(error_msg)); |
216 | 0 | } |
217 | | |
218 | 1 | downloaded_files.clear(); |
219 | 1 | return Status::OK(); |
220 | 1 | } |
221 | | |
222 | 35 | void switch_to_next_file() { |
223 | 35 | DCHECK(_fd >= 0); |
224 | 35 | DCHECK(_written_size == _file_size); |
225 | | |
226 | 35 | close(_fd); |
227 | 35 | _fd = -1; |
228 | 35 | _expected_files.erase(_file_name); |
229 | 35 | _is_reading_header = true; |
230 | 35 | } |
231 | | |
232 | | const std::string _local_dir_path; |
233 | | std::string _buffer; |
234 | | std::unordered_set<std::string> _expected_files; |
235 | | Status _status; |
236 | | |
237 | | bool _is_reading_header = true; |
238 | | int _fd = -1; |
239 | | std::string _local_file_path; |
240 | | std::string _file_name; |
241 | | size_t _file_size = 0; |
242 | | size_t _written_size = 0; |
243 | | std::vector<std::string> downloaded_files; |
244 | | }; |
245 | | |
246 | 0 | static const char* header_error_msg(CURLHcode code) { |
247 | 0 | switch (code) { |
248 | 0 | case CURLHE_OK: |
249 | 0 | return "OK"; |
250 | 0 | case CURLHE_BADINDEX: |
251 | 0 | return "header exists but not with this index "; |
252 | 0 | case CURLHE_MISSING: |
253 | 0 | return "no such header exists"; |
254 | 0 | case CURLHE_NOHEADERS: |
255 | 0 | return "no headers at all exist (yet)"; |
256 | 0 | case CURLHE_NOREQUEST: |
257 | 0 | return "no request with this number was used"; |
258 | 0 | case CURLHE_OUT_OF_MEMORY: |
259 | 0 | return "out of memory while processing"; |
260 | 0 | case CURLHE_BAD_ARGUMENT: |
261 | 0 | return "a function argument was not okay"; |
262 | 0 | case CURLHE_NOT_BUILT_IN: |
263 | 0 | return "curl_easy_header() was disabled in the build"; |
264 | 0 | default: |
265 | 0 | return "unknown"; |
266 | 0 | } |
267 | 0 | } |
268 | | |
269 | 51 | HttpClient::HttpClient() = default; |
270 | | |
271 | 51 | HttpClient::~HttpClient() { |
272 | 51 | if (_curl != nullptr) { |
273 | 50 | curl_easy_cleanup(_curl); |
274 | 50 | _curl = nullptr; |
275 | 50 | } |
276 | 51 | if (_header_list != nullptr) { |
277 | 2 | curl_slist_free_all(_header_list); |
278 | 2 | _header_list = nullptr; |
279 | 2 | } |
280 | 51 | } |
281 | | |
282 | 50 | Status HttpClient::init(const std::string& url, bool set_fail_on_error) { |
283 | 50 | if (_curl == nullptr) { |
284 | 49 | _curl = curl_easy_init(); |
285 | 49 | if (_curl == nullptr) { |
286 | 0 | return Status::InternalError("fail to initialize curl"); |
287 | 0 | } |
288 | 49 | } else { |
289 | 1 | curl_easy_reset(_curl); |
290 | 1 | } |
291 | | |
292 | 50 | if (_header_list != nullptr) { |
293 | 0 | curl_slist_free_all(_header_list); |
294 | 0 | _header_list = nullptr; |
295 | 0 | } |
296 | | // set error_buf |
297 | 50 | _error_buf[0] = 0; |
298 | 50 | auto code = curl_easy_setopt(_curl, CURLOPT_ERRORBUFFER, _error_buf); |
299 | 50 | if (code != CURLE_OK) { |
300 | 0 | LOG(WARNING) << "fail to set CURLOPT_ERRORBUFFER, msg=" << _to_errmsg(code); |
301 | 0 | return Status::InternalError("fail to set error buffer"); |
302 | 0 | } |
303 | | // forbid signals |
304 | 50 | code = curl_easy_setopt(_curl, CURLOPT_NOSIGNAL, 1L); |
305 | 50 | if (code != CURLE_OK) { |
306 | 0 | LOG(WARNING) << "fail to set CURLOPT_NOSIGNAL, msg=" << _to_errmsg(code); |
307 | 0 | return Status::InternalError("fail to set CURLOPT_NOSIGNAL"); |
308 | 0 | } |
309 | | // set fail on error |
310 | | // When this option is set to `1L` (enabled), libcurl will return an error directly |
311 | | // when encountering HTTP error codes (>= 400), without reading the body of the error response. |
312 | 50 | if (set_fail_on_error) { |
313 | 48 | code = curl_easy_setopt(_curl, CURLOPT_FAILONERROR, 1L); |
314 | 48 | if (code != CURLE_OK) { |
315 | 0 | LOG(WARNING) << "fail to set CURLOPT_FAILONERROR, msg=" << _to_errmsg(code); |
316 | 0 | return Status::InternalError("fail to set CURLOPT_FAILONERROR"); |
317 | 0 | } |
318 | 48 | } |
319 | | // set redirect |
320 | 50 | code = curl_easy_setopt(_curl, CURLOPT_FOLLOWLOCATION, 1L); |
321 | 50 | if (code != CURLE_OK) { |
322 | 0 | LOG(WARNING) << "fail to set CURLOPT_FOLLOWLOCATION, msg=" << _to_errmsg(code); |
323 | 0 | return Status::InternalError("fail to set CURLOPT_FOLLOWLOCATION"); |
324 | 0 | } |
325 | 50 | code = curl_easy_setopt(_curl, CURLOPT_MAXREDIRS, 20); |
326 | 50 | if (code != CURLE_OK) { |
327 | 0 | LOG(WARNING) << "fail to set CURLOPT_MAXREDIRS, msg=" << _to_errmsg(code); |
328 | 0 | return Status::InternalError("fail to set CURLOPT_MAXREDIRS"); |
329 | 0 | } |
330 | | |
331 | 678 | curl_write_callback callback = [](char* buffer, size_t size, size_t nmemb, void* param) { |
332 | 678 | auto* client = (HttpClient*)param; |
333 | 678 | return client->on_response_data(buffer, size * nmemb); |
334 | 678 | }; |
335 | | |
336 | | // set callback function |
337 | 50 | code = curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, callback); |
338 | 50 | if (code != CURLE_OK) { |
339 | 0 | LOG(WARNING) << "fail to set CURLOPT_WRITEFUNCTION, msg=" << _to_errmsg(code); |
340 | 0 | return Status::InternalError("fail to set CURLOPT_WRITEFUNCTION"); |
341 | 0 | } |
342 | 50 | code = curl_easy_setopt(_curl, CURLOPT_WRITEDATA, (void*)this); |
343 | 50 | if (code != CURLE_OK) { |
344 | 0 | LOG(WARNING) << "fail to set CURLOPT_WRITEDATA, msg=" << _to_errmsg(code); |
345 | 0 | return Status::InternalError("fail to set CURLOPT_WRITEDATA"); |
346 | 0 | } |
347 | | |
348 | 50 | std::string escaped_url; |
349 | 50 | RETURN_IF_ERROR(_escape_url(url, &escaped_url)); |
350 | | // set url |
351 | 50 | code = curl_easy_setopt(_curl, CURLOPT_URL, escaped_url.c_str()); |
352 | 50 | if (code != CURLE_OK) { |
353 | 0 | LOG(WARNING) << "failed to set CURLOPT_URL, errmsg=" << _to_errmsg(code); |
354 | 0 | return Status::InternalError("fail to set CURLOPT_URL"); |
355 | 0 | } |
356 | | |
357 | | #ifndef BE_TEST |
358 | | set_auth_token(ExecEnv::GetInstance()->cluster_info()->curr_auth_token); |
359 | | #endif |
360 | 50 | return Status::OK(); |
361 | 50 | } |
362 | | |
363 | 50 | void HttpClient::set_method(HttpMethod method) { |
364 | 50 | _method = method; |
365 | 50 | switch (method) { |
366 | 30 | case GET: |
367 | 30 | curl_easy_setopt(_curl, CURLOPT_HTTPGET, 1L); |
368 | 30 | return; |
369 | 0 | case PUT: |
370 | 0 | curl_easy_setopt(_curl, CURLOPT_UPLOAD, 1L); |
371 | 0 | return; |
372 | 15 | case POST: |
373 | 15 | curl_easy_setopt(_curl, CURLOPT_POST, 1L); |
374 | 15 | return; |
375 | 0 | case DELETE: |
376 | 0 | curl_easy_setopt(_curl, CURLOPT_CUSTOMREQUEST, "DELETE"); |
377 | 0 | return; |
378 | 5 | case HEAD: |
379 | 5 | curl_easy_setopt(_curl, CURLOPT_NOBODY, 1L); |
380 | 5 | return; |
381 | 0 | case OPTIONS: |
382 | 0 | curl_easy_setopt(_curl, CURLOPT_CUSTOMREQUEST, "OPTIONS"); |
383 | 0 | return; |
384 | 0 | default: |
385 | 0 | return; |
386 | 50 | } |
387 | 50 | } |
388 | | |
389 | 2 | void HttpClient::set_speed_limit() { |
390 | 2 | curl_easy_setopt(_curl, CURLOPT_LOW_SPEED_LIMIT, config::download_low_speed_limit_kbps * 1024); |
391 | 2 | curl_easy_setopt(_curl, CURLOPT_LOW_SPEED_TIME, config::download_low_speed_time); |
392 | 2 | curl_easy_setopt(_curl, CURLOPT_MAX_RECV_SPEED_LARGE, config::max_download_speed_kbps * 1024); |
393 | 2 | } |
394 | | |
395 | 678 | size_t HttpClient::on_response_data(const void* data, size_t length) { |
396 | 678 | if (*_callback != nullptr) { |
397 | 678 | bool is_continue = (*_callback)(data, length); |
398 | 678 | if (!is_continue) { |
399 | 0 | return -1; |
400 | 0 | } |
401 | 678 | } |
402 | 678 | return length; |
403 | 678 | } |
404 | | |
405 | 11 | Status HttpClient::execute_post_request(const std::string& payload, std::string* response) { |
406 | 11 | set_method(POST); |
407 | 11 | set_payload(payload); |
408 | 11 | return execute(response); |
409 | 11 | } |
410 | | |
411 | 0 | Status HttpClient::execute_delete_request(const std::string& payload, std::string* response) { |
412 | 0 | set_method(DELETE); |
413 | 0 | set_payload(payload); |
414 | 0 | return execute(response); |
415 | 0 | } |
416 | | |
417 | 50 | Status HttpClient::execute(const std::function<bool(const void* data, size_t length)>& callback) { |
418 | 50 | if (VLOG_DEBUG_IS_ON) { |
419 | 0 | VLOG_DEBUG << "execute http " << to_method_desc(_method) << " request, url " << _get_url(); |
420 | 0 | } |
421 | 50 | _callback = &callback; |
422 | 50 | auto code = curl_easy_perform(_curl); |
423 | 50 | if (code != CURLE_OK) { |
424 | 27 | std::string url = mask_token(_get_url()); |
425 | 27 | LOG(WARNING) << "fail to execute HTTP client, errmsg=" << _to_errmsg(code) |
426 | 27 | << ", trace=" << get_stack_trace() << ", url=" << url; |
427 | 27 | std::string errmsg = fmt::format("{}, url={}", _to_errmsg(code), url); |
428 | 27 | return Status::HttpError(std::move(errmsg)); |
429 | 27 | } |
430 | 23 | if (VLOG_DEBUG_IS_ON) { |
431 | 0 | VLOG_DEBUG << "execute http " << to_method_desc(_method) << " request, url " << _get_url() |
432 | 0 | << " done"; |
433 | 0 | } |
434 | 23 | return Status::OK(); |
435 | 50 | } |
436 | | |
437 | 3 | Status HttpClient::get_content_md5(std::string* md5) const { |
438 | 3 | struct curl_header* header_ptr; |
439 | 3 | auto code = curl_easy_header(_curl, HttpHeaders::CONTENT_MD5, 0, CURLH_HEADER, 0, &header_ptr); |
440 | 3 | if (code == CURLHE_MISSING || code == CURLHE_NOHEADERS) { |
441 | | // no such headers exists |
442 | 1 | md5->clear(); |
443 | 1 | return Status::OK(); |
444 | 2 | } else if (code != CURLHE_OK) { |
445 | 0 | auto msg = fmt::format("failed to get http header {}: {} ({})", HttpHeaders::CONTENT_MD5, |
446 | 0 | header_error_msg(code), code); |
447 | 0 | LOG(WARNING) << msg << ", trace=" << get_stack_trace(); |
448 | 0 | return Status::HttpError(std::move(msg)); |
449 | 0 | } |
450 | | |
451 | 2 | *md5 = header_ptr->value; |
452 | 2 | return Status::OK(); |
453 | 3 | } |
454 | | |
455 | 1 | Status HttpClient::download(const std::string& local_path) { |
456 | 1 | set_method(GET); |
457 | 1 | set_speed_limit(); |
458 | | |
459 | 1 | auto fp_closer = [](FILE* fp) { fclose(fp); }; |
460 | 1 | std::unique_ptr<FILE, decltype(fp_closer)> fp(fopen(local_path.c_str(), "w"), fp_closer); |
461 | 1 | if (fp == nullptr) { |
462 | 0 | LOG(WARNING) << "open file failed, file=" << local_path; |
463 | 0 | return Status::InternalError("open file failed"); |
464 | 0 | } |
465 | 1 | Status status; |
466 | 1 | auto callback = [&status, &fp, &local_path](const void* data, size_t length) { |
467 | 1 | auto res = fwrite(data, length, 1, fp.get()); |
468 | 1 | if (res != 1) { |
469 | 0 | LOG(WARNING) << "fail to write data to file, file=" << local_path |
470 | 0 | << ", error=" << ferror(fp.get()); |
471 | 0 | status = Status::InternalError("fail to write data when download"); |
472 | 0 | return false; |
473 | 0 | } |
474 | 1 | return true; |
475 | 1 | }; |
476 | | |
477 | 1 | if (auto s = execute(callback); !s.ok()) { |
478 | 0 | status = s; |
479 | 0 | } |
480 | 1 | if (!status.ok()) { |
481 | 0 | remove(local_path.c_str()); |
482 | 0 | } |
483 | 1 | return status; |
484 | 1 | } |
485 | | |
486 | | Status HttpClient::download_multi_files(const std::string& local_dir, |
487 | 1 | const std::unordered_set<std::string>& expected_files) { |
488 | 1 | set_speed_limit(); |
489 | | |
490 | 1 | MultiFileSplitter splitter(local_dir, expected_files); |
491 | 661 | auto callback = [&](const void* data, size_t length) { |
492 | 661 | return splitter.append(reinterpret_cast<const char*>(data), length); |
493 | 661 | }; |
494 | 1 | if (auto s = execute(callback); !s.ok()) { |
495 | 0 | return s; |
496 | 0 | } |
497 | 1 | return splitter.finish(); |
498 | 1 | } |
499 | | |
500 | 44 | Status HttpClient::execute(std::string* response) { |
501 | 44 | auto callback = [response](const void* data, size_t length) { |
502 | 16 | response->append((char*)data, length); |
503 | 16 | return true; |
504 | 16 | }; |
505 | 44 | return execute(callback); |
506 | 44 | } |
507 | | |
508 | 54 | const char* HttpClient::_to_errmsg(CURLcode code) const { |
509 | 54 | if (_error_buf[0] == 0) { |
510 | 0 | return curl_easy_strerror(code); |
511 | 0 | } |
512 | 54 | return _error_buf; |
513 | 54 | } |
514 | | |
515 | 27 | const char* HttpClient::_get_url() const { |
516 | 27 | const char* url = nullptr; |
517 | 27 | curl_easy_getinfo(_curl, CURLINFO_EFFECTIVE_URL, &url); |
518 | 27 | if (!url) { |
519 | 0 | url = "<unknown>"; |
520 | 0 | } |
521 | 27 | return url; |
522 | 27 | } |
523 | | |
524 | | // execute remote call action with retry |
525 | | Status HttpClient::execute(int retry_times, int sleep_time, |
526 | 0 | const std::function<Status(HttpClient*)>& callback) { |
527 | 0 | Status status; |
528 | 0 | for (int i = 0; i < retry_times; ++i) { |
529 | 0 | status = callback(this); |
530 | 0 | if (status.ok()) { |
531 | 0 | auto http_status = get_http_status(); |
532 | 0 | if (http_status == 200) { |
533 | 0 | return status; |
534 | 0 | } else { |
535 | 0 | std::string url = mask_token(_get_url()); |
536 | 0 | auto error_msg = fmt::format("http status code is not 200, code={}, url={}", |
537 | 0 | http_status, url); |
538 | 0 | LOG(WARNING) << error_msg; |
539 | 0 | return Status::HttpError(error_msg); |
540 | 0 | } |
541 | 0 | } |
542 | 0 | sleep(sleep_time); |
543 | 0 | } |
544 | 0 | return status; |
545 | 0 | } |
546 | | |
547 | | Status HttpClient::execute_with_retry(int retry_times, int sleep_time, |
548 | 4 | const std::function<Status(HttpClient*)>& callback) { |
549 | 4 | Status status; |
550 | 7 | for (int i = 0; i < retry_times; ++i) { |
551 | 6 | HttpClient client; |
552 | 6 | status = callback(&client); |
553 | 6 | if (status.ok()) { |
554 | 3 | auto http_status = client.get_http_status(); |
555 | 3 | if (http_status == 200) { |
556 | 3 | return status; |
557 | 3 | } else { |
558 | 0 | std::string url = mask_token(client._get_url()); |
559 | 0 | auto error_msg = fmt::format("http status code is not 200, code={}, url={}", |
560 | 0 | http_status, url); |
561 | 0 | LOG(WARNING) << error_msg; |
562 | 0 | return Status::HttpError(error_msg); |
563 | 0 | } |
564 | 3 | } |
565 | 3 | sleep(sleep_time); |
566 | 3 | } |
567 | 1 | return status; |
568 | 4 | } |
569 | | |
570 | | // http://example.com/page?param1=value1¶m2=value+with+spaces#section |
571 | 57 | Status HttpClient::_escape_url(const std::string& url, std::string* escaped_url) { |
572 | 57 | size_t query_pos = url.find('?'); |
573 | 57 | if (query_pos == std::string::npos) { |
574 | 43 | *escaped_url = url; |
575 | 43 | return Status::OK(); |
576 | 43 | } |
577 | 14 | size_t fragment_pos = url.find('#'); |
578 | 14 | std::string query; |
579 | 14 | std::string fragment; |
580 | | |
581 | 14 | if (fragment_pos == std::string::npos) { |
582 | 13 | query = url.substr(query_pos + 1, url.length() - query_pos - 1); |
583 | 13 | } else { |
584 | 1 | query = url.substr(query_pos + 1, fragment_pos - query_pos - 1); |
585 | 1 | fragment = url.substr(fragment_pos, url.length() - fragment_pos); |
586 | 1 | } |
587 | | |
588 | 14 | std::string encoded_query; |
589 | 14 | size_t ampersand_pos = query.find('&'); |
590 | 14 | size_t equal_pos; |
591 | | |
592 | 14 | if (ampersand_pos == std::string::npos) { |
593 | 7 | ampersand_pos = query.length(); |
594 | 7 | } |
595 | | |
596 | 25 | while (true) { |
597 | 25 | equal_pos = query.find('='); |
598 | 25 | if (equal_pos != std::string::npos) { |
599 | 22 | std::string key = query.substr(0, equal_pos); |
600 | 22 | std::string value = query.substr(equal_pos + 1, ampersand_pos - equal_pos - 1); |
601 | | |
602 | 22 | auto encoded_value = std::unique_ptr<char, decltype(&curl_free)>( |
603 | 22 | curl_easy_escape(_curl, value.c_str(), value.length()), &curl_free); |
604 | 22 | if (encoded_value) { |
605 | 22 | encoded_query += key + "=" + std::string(encoded_value.get()); |
606 | 22 | } else { |
607 | 0 | return Status::InternalError("escape url failed, url={}", url); |
608 | 0 | } |
609 | 22 | } else { |
610 | 3 | encoded_query += query.substr(0, ampersand_pos); |
611 | 3 | } |
612 | | |
613 | 25 | if (ampersand_pos == query.length() || ampersand_pos == std::string::npos) { |
614 | 14 | break; |
615 | 14 | } |
616 | | |
617 | 11 | encoded_query += "&"; |
618 | 11 | query = query.substr(ampersand_pos + 1); |
619 | 11 | ampersand_pos = query.find('&'); |
620 | 11 | } |
621 | 14 | *escaped_url = url.substr(0, query_pos + 1) + encoded_query + fragment; |
622 | 14 | return Status::OK(); |
623 | 14 | } |
624 | | |
625 | | } // namespace doris |