Coverage Report

Created: 2025-09-15 22:11

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