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