be/src/service/http/http_client.h
Line | Count | Source |
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 | | #pragma once |
19 | | |
20 | | #include <curl/curl.h> |
21 | | #include <curl/system.h> |
22 | | #include <stdint.h> |
23 | | |
24 | | #include <cstdio> |
25 | | #include <functional> |
26 | | #include <string> |
27 | | #include <unordered_set> |
28 | | |
29 | | #include "common/status.h" |
30 | | #include "service/http/http_headers.h" |
31 | | #include "service/http/http_method.h" |
32 | | |
33 | | namespace doris { |
34 | | |
35 | | // Helper class to access HTTP resource |
36 | | class HttpClient { |
37 | | public: |
38 | | HttpClient(); |
39 | | ~HttpClient(); |
40 | | |
41 | | // you can call this function to execute HTTP request with retry, |
42 | | // if callback return OK, this function will end and return OK. |
43 | | // This function will return FAIL if three are more than retry_times |
44 | | // that callback return FAIL. |
45 | | static Status execute_with_retry(int retry_times, int sleep_time, |
46 | | const std::function<Status(HttpClient*)>& callback); |
47 | | |
48 | | // this function must call before other function, |
49 | | // you can call this multiple times to reuse this object |
50 | | Status init(const std::string& url, bool set_fail_on_error = true); |
51 | | |
52 | | // Release request-owned options and callbacks while retaining reusable connections. |
53 | | void reset(); |
54 | | |
55 | | void set_method(HttpMethod method); |
56 | | |
57 | 21 | void set_basic_auth(const std::string& user, const std::string& passwd) { |
58 | 21 | curl_easy_setopt(_curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
59 | 21 | curl_easy_setopt(_curl, CURLOPT_USERNAME, user.c_str()); |
60 | 21 | curl_easy_setopt(_curl, CURLOPT_PASSWORD, passwd.c_str()); |
61 | 21 | } |
62 | | |
63 | | // Auth-Token: xxxx |
64 | 505 | void set_auth_token(const std::string& token) { |
65 | 505 | std::string scratch_str = HttpHeaders::AUTH_TOKEN + ": " + token; |
66 | 505 | _header_list = curl_slist_append(_header_list, scratch_str.c_str()); |
67 | 505 | curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _header_list); |
68 | 505 | } |
69 | | |
70 | | // Authorization: xxxx |
71 | 20 | void set_authorization(const std::string& auth) { |
72 | 20 | std::string scratch_str = std::string(HttpHeaders::AUTHORIZATION) + ": " + auth; |
73 | 20 | _header_list = curl_slist_append(_header_list, scratch_str.c_str()); |
74 | 20 | curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _header_list); |
75 | 20 | } |
76 | | |
77 | | // content_type such as "application/json" |
78 | 322 | void set_content_type(const std::string content_type) { |
79 | 322 | std::string scratch_str = "Content-Type: " + content_type; |
80 | 322 | _header_list = curl_slist_append(_header_list, scratch_str.c_str()); |
81 | 322 | curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _header_list); |
82 | 322 | } |
83 | | |
84 | 315 | void set_payload(const std::string& post_body) { |
85 | 315 | curl_easy_setopt(_curl, CURLOPT_POSTFIELDSIZE, (long)post_body.length()); |
86 | 315 | curl_easy_setopt(_curl, CURLOPT_COPYPOSTFIELDS, post_body.c_str()); |
87 | 315 | } |
88 | | |
89 | | // Currently, only fake SSL configurations are supported |
90 | 0 | void use_untrusted_ssl() { |
91 | 0 | curl_easy_setopt(_curl, CURLOPT_SSL_VERIFYPEER, 0L); |
92 | 0 | curl_easy_setopt(_curl, CURLOPT_SSL_VERIFYHOST, 0L); |
93 | 0 | } |
94 | | |
95 | | void set_speed_limit(); |
96 | | |
97 | | // key: value |
98 | 425 | void set_header(const std::string& key, const std::string& value) { |
99 | 425 | std::string header = key + ": " + value; |
100 | 425 | _header_list = curl_slist_append(_header_list, header.c_str()); |
101 | 425 | curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _header_list); |
102 | 425 | } |
103 | | |
104 | 0 | std::string get_response_content_type() { |
105 | 0 | char* ct = nullptr; |
106 | 0 | auto code = curl_easy_getinfo(_curl, CURLINFO_CONTENT_TYPE, &ct); |
107 | 0 | if (code == CURLE_OK && ct != nullptr) { |
108 | 0 | return ct; |
109 | 0 | } |
110 | 0 | return std::string(); |
111 | 0 | } |
112 | | |
113 | | // Set the long gohead parameter to 1L to continue send authentication (user+password) |
114 | | // credentials when following locations, even when hostname changed. |
115 | 0 | void set_unrestricted_auth(int gohead) { |
116 | 0 | curl_easy_setopt(_curl, CURLOPT_UNRESTRICTED_AUTH, gohead); |
117 | 0 | } |
118 | | |
119 | 354 | void set_timeout_ms(int64_t timeout_ms) { |
120 | 354 | curl_easy_setopt(_curl, CURLOPT_TIMEOUT_MS, timeout_ms); |
121 | 354 | } |
122 | | |
123 | | // Register a callback that libcurl polls while the request is in flight: often while |
124 | | // data is flowing, and about once per second when the connection is idle. Returning |
125 | | // true aborts the transfer right away, so `execute()` fails instead of blocking until |
126 | | // CURLOPT_TIMEOUT_MS expires. |
127 | | // |
128 | | // This is for callers that must be able to give up on a request which may never be |
129 | | // answered, e.g. a background worker that is being stopped while the http service |
130 | | // serving its request is going away. Must be called after init(), which resets all |
131 | | // curl options. Passing an empty callback clears a previously registered one. |
132 | | void set_abort_callback(std::function<bool()> callback); |
133 | | |
134 | | // used to get content length |
135 | | // return -1 as error |
136 | 6 | Status get_content_length(uint64_t* length) const { |
137 | 6 | curl_off_t cl; |
138 | 6 | auto code = curl_easy_getinfo(_curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl); |
139 | 6 | if (!code) { |
140 | 6 | if (cl < 0) { |
141 | 0 | return Status::InternalError( |
142 | 0 | fmt::format("failed to get content length, it should be a positive value, " |
143 | 0 | "actual is : {}", |
144 | 0 | cl)); |
145 | 0 | } |
146 | 6 | *length = (uint64_t)cl; |
147 | 6 | return Status::OK(); |
148 | 6 | } |
149 | 0 | return Status::InternalError("failed to get content length. err code: {}", code); |
150 | 6 | } |
151 | | |
152 | | // Get the value of the header CONTENT-MD5. The output is empty if no such header exists. |
153 | | Status get_content_md5(std::string* md5) const; |
154 | | |
155 | 247 | CURLcode get_last_curl_code() const { return _last_curl_code; } |
156 | | |
157 | | // Read the final response header; clear value when the header is absent. |
158 | | Status get_response_header(const char* name, std::string* value) const; |
159 | | |
160 | 299 | long get_http_status() const { |
161 | 299 | long code; |
162 | 299 | curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &code); |
163 | 299 | return code; |
164 | 299 | } |
165 | | |
166 | | // execute a head method |
167 | 2 | Status head() { |
168 | 2 | set_method(HEAD); |
169 | 2 | return execute(); |
170 | 2 | } |
171 | | |
172 | | // helper function to download a file, you can call this function to download |
173 | | // a file to local_path |
174 | | Status download(const std::string& local_path); |
175 | | Status download_multi_files(const std::string& local_dir, |
176 | | const std::unordered_set<std::string>& expected_files); |
177 | | |
178 | | Status execute_post_request(const std::string& payload, std::string* response); |
179 | | |
180 | | Status execute_put_request(const std::string& payload, std::string* response); |
181 | | |
182 | | Status execute_delete_request(const std::string& payload, std::string* response); |
183 | | |
184 | | // execute a simple method, and its response is saved in response argument |
185 | | Status execute(std::string* response); |
186 | | |
187 | | // execute remote call action |
188 | | Status execute(const std::function<bool(const void* data, size_t length)>& callback = {}); |
189 | | |
190 | | // execute remote call action with retry, like execute_with_retry but keep the http client instance |
191 | | Status execute(int retry_times, int sleep_time, |
192 | | const std::function<Status(HttpClient*)>& callback); |
193 | | |
194 | | size_t on_response_data(const void* data, size_t length); |
195 | | |
196 | | // The file name of the variant column with the inverted index contains % |
197 | | // such as: 020000000000003f624c4c322c568271060f9b5b274a4a95_0_10133@properties%2Emessage.idx |
198 | | // {rowset_id}_{seg_num}_{index_id}_{variant_column_name}{%2E}{extracted_column_name}.idx |
199 | | // We need to handle %, otherwise it will cause an HTTP 404 error. |
200 | | // Because the percent ("%") character serves as the indicator for percent-encoded octets, |
201 | | // it must be percent-encoded as "%25" for that octet to be used as data within a URI. |
202 | | // https://datatracker.ietf.org/doc/html/rfc3986 |
203 | | Status _escape_url(const std::string& url, std::string* escaped_url); |
204 | | |
205 | 2 | void set_range(size_t offset, size_t length) { |
206 | 2 | std::string range_header = "Range: bytes=" + std::to_string(offset) + "-" + |
207 | 2 | std::to_string(offset + length - 1); |
208 | 2 | _header_list = curl_slist_append(_header_list, range_header.c_str()); |
209 | | curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _header_list); |
210 | 2 | } |
211 | | |
212 | | private: |
213 | | static std::string _get_ca_certificate_path(const std::string& configured_paths); |
214 | | Status _set_ca_certificate_from_config(); |
215 | | const char* _to_errmsg(CURLcode code) const; |
216 | | const char* _get_url() const; |
217 | | |
218 | | private: |
219 | | CURL* _curl = nullptr; |
220 | | CURLcode _last_curl_code = CURLE_OK; |
221 | | using HttpCallback = std::function<bool(const void* data, size_t length)>; |
222 | | const HttpCallback* _callback = nullptr; |
223 | | std::function<bool()> _abort_callback; |
224 | | char _error_buf[CURL_ERROR_SIZE]; |
225 | | curl_slist* _header_list = nullptr; |
226 | | HttpMethod _method = GET; |
227 | | }; |
228 | | |
229 | | } // namespace doris |