/root/doris/be/src/http/http_client.h
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 | | #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 "http/http_headers.h" |
31 | | #include "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 | | void set_method(HttpMethod method); |
53 | | |
54 | 21 | void set_basic_auth(const std::string& user, const std::string& passwd) { |
55 | 21 | curl_easy_setopt(_curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
56 | 21 | curl_easy_setopt(_curl, CURLOPT_USERNAME, user.c_str()); |
57 | 21 | curl_easy_setopt(_curl, CURLOPT_PASSWORD, passwd.c_str()); |
58 | 21 | } |
59 | | |
60 | | // Auth-Token: xxxx |
61 | 2 | void set_auth_token(const std::string& token) { |
62 | 2 | std::string scratch_str = HttpHeaders::AUTH_TOKEN + ": " + token; |
63 | 2 | _header_list = curl_slist_append(_header_list, scratch_str.c_str()); |
64 | 2 | curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _header_list); |
65 | 2 | } |
66 | | |
67 | | // content_type such as "application/json" |
68 | 0 | void set_content_type(const std::string content_type) { |
69 | 0 | std::string scratch_str = "Content-Type: " + content_type; |
70 | 0 | _header_list = curl_slist_append(_header_list, scratch_str.c_str()); |
71 | 0 | curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _header_list); |
72 | 0 | } |
73 | | |
74 | 12 | void set_payload(const std::string& post_body) { |
75 | 12 | curl_easy_setopt(_curl, CURLOPT_POSTFIELDSIZE, (long)post_body.length()); |
76 | 12 | curl_easy_setopt(_curl, CURLOPT_COPYPOSTFIELDS, post_body.c_str()); |
77 | 12 | } |
78 | | |
79 | | // Currently, only fake SSL configurations are supported |
80 | 0 | void use_untrusted_ssl() { |
81 | 0 | curl_easy_setopt(_curl, CURLOPT_SSL_VERIFYPEER, 0L); |
82 | 0 | curl_easy_setopt(_curl, CURLOPT_SSL_VERIFYHOST, 0L); |
83 | 0 | } |
84 | | |
85 | | void set_speed_limit(); |
86 | | |
87 | | // TODO(zc): support set header |
88 | | // void set_header(const std::string& key, const std::string& value) { |
89 | | // _cntl.http_request().SetHeader(key, value); |
90 | | // } |
91 | | |
92 | 0 | std::string get_response_content_type() { |
93 | 0 | char* ct = nullptr; |
94 | 0 | auto code = curl_easy_getinfo(_curl, CURLINFO_CONTENT_TYPE, &ct); |
95 | 0 | if (code == CURLE_OK && ct != nullptr) { |
96 | 0 | return ct; |
97 | 0 | } |
98 | 0 | return std::string(); |
99 | 0 | } |
100 | | |
101 | | // Set the long gohead parameter to 1L to continue send authentication (user+password) |
102 | | // credentials when following locations, even when hostname changed. |
103 | 0 | void set_unrestricted_auth(int gohead) { |
104 | 0 | curl_easy_setopt(_curl, CURLOPT_UNRESTRICTED_AUTH, gohead); |
105 | 0 | } |
106 | | |
107 | 19 | void set_timeout_ms(int64_t timeout_ms) { |
108 | 19 | curl_easy_setopt(_curl, CURLOPT_TIMEOUT_MS, timeout_ms); |
109 | 19 | } |
110 | | |
111 | | // used to get content length |
112 | | // return -1 as error |
113 | 3 | Status get_content_length(uint64_t* length) const { |
114 | 3 | curl_off_t cl; |
115 | 3 | auto code = curl_easy_getinfo(_curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl); |
116 | 3 | if (!code) { |
117 | 3 | if (cl < 0) { |
118 | 0 | return Status::InternalError( |
119 | 0 | fmt::format("failed to get content length, it should be a positive value, " |
120 | 0 | "actual is : {}", |
121 | 0 | cl)); |
122 | 0 | } |
123 | 3 | *length = (uint64_t)cl; |
124 | 3 | return Status::OK(); |
125 | 3 | } |
126 | 0 | return Status::InternalError("failed to get content length. err code: {}", code); |
127 | 3 | } |
128 | | |
129 | | // Get the value of the header CONTENT-MD5. The output is empty if no such header exists. |
130 | | Status get_content_md5(std::string* md5) const; |
131 | | |
132 | 3 | long get_http_status() const { |
133 | 3 | long code; |
134 | 3 | curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &code); |
135 | 3 | return code; |
136 | 3 | } |
137 | | |
138 | | // execute a head method |
139 | 0 | Status head() { |
140 | 0 | set_method(HEAD); |
141 | 0 | return execute(); |
142 | 0 | } |
143 | | |
144 | | // helper function to download a file, you can call this function to download |
145 | | // a file to local_path |
146 | | Status download(const std::string& local_path); |
147 | | Status download_multi_files(const std::string& local_dir, |
148 | | const std::unordered_set<std::string>& expected_files); |
149 | | |
150 | | Status execute_post_request(const std::string& payload, std::string* response); |
151 | | |
152 | | Status execute_delete_request(const std::string& payload, std::string* response); |
153 | | |
154 | | // execute a simple method, and its response is saved in response argument |
155 | | Status execute(std::string* response); |
156 | | |
157 | | // execute remote call action |
158 | | Status execute(const std::function<bool(const void* data, size_t length)>& callback = {}); |
159 | | |
160 | | // execute remote call action with retry, like execute_with_retry but keep the http client instance |
161 | | Status execute(int retry_times, int sleep_time, |
162 | | const std::function<Status(HttpClient*)>& callback); |
163 | | |
164 | | size_t on_response_data(const void* data, size_t length); |
165 | | |
166 | | // The file name of the variant column with the inverted index contains % |
167 | | // such as: 020000000000003f624c4c322c568271060f9b5b274a4a95_0_10133@properties%2Emessage.idx |
168 | | // {rowset_id}_{seg_num}_{index_id}_{variant_column_name}{%2E}{extracted_column_name}.idx |
169 | | // We need to handle %, otherwise it will cause an HTTP 404 error. |
170 | | // Because the percent ("%") character serves as the indicator for percent-encoded octets, |
171 | | // it must be percent-encoded as "%25" for that octet to be used as data within a URI. |
172 | | // https://datatracker.ietf.org/doc/html/rfc3986 |
173 | | Status _escape_url(const std::string& url, std::string* escaped_url); |
174 | | |
175 | | private: |
176 | | const char* _to_errmsg(CURLcode code) const; |
177 | | const char* _get_url() const; |
178 | | |
179 | | private: |
180 | | CURL* _curl = nullptr; |
181 | | using HttpCallback = std::function<bool(const void* data, size_t length)>; |
182 | | const HttpCallback* _callback = nullptr; |
183 | | char _error_buf[CURL_ERROR_SIZE]; |
184 | | curl_slist* _header_list = nullptr; |
185 | | HttpMethod _method = GET; |
186 | | }; |
187 | | |
188 | | } // namespace doris |