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