Coverage Report

Created: 2026-02-12 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/http/http_request.cpp
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
#include "http/http_request.h"
19
20
#include <event2/buffer.h>
21
#include <event2/http.h>
22
#include <event2/http_struct.h>
23
#include <event2/keyvalq_struct.h>
24
25
#include <memory>
26
#include <sstream>
27
#include <string>
28
#include <unordered_map>
29
#include <utility>
30
31
#include "http/http_handler.h"
32
#include "http/http_headers.h"
33
#include "runtime/stream_load/stream_load_context.h"
34
#include "util/stack_util.h"
35
36
namespace doris {
37
38
static std::string s_empty = "";
39
40
// Helper function to check if a header should be masked in logs
41
64
static bool is_sensitive_header(const std::string& header_name) {
42
64
    return iequal(header_name, HttpHeaders::AUTHORIZATION) ||
43
64
           iequal(header_name, HttpHeaders::PROXY_AUTHORIZATION) || iequal(header_name, "token") ||
44
64
           iequal(header_name, HttpHeaders::AUTH_TOKEN);
45
64
}
46
47
70
HttpRequest::HttpRequest(evhttp_request* evhttp_request) : _ev_req(evhttp_request) {}
48
49
70
HttpRequest::~HttpRequest() {
50
70
    if (_handler_ctx != nullptr) {
51
0
        DCHECK(_handler != nullptr);
52
0
        _handler->free_handler_ctx(_handler_ctx);
53
0
    }
54
70
}
55
56
57
int HttpRequest::init_from_evhttp() {
57
57
    _method = to_http_method(evhttp_request_get_command(_ev_req));
58
57
    if (_method == HttpMethod::UNKNOWN) {
59
0
        LOG(WARNING) << "unknown method of HTTP request, method="
60
0
                     << evhttp_request_get_command(_ev_req);
61
0
        return -1;
62
0
    }
63
57
    _uri = evhttp_request_get_uri(_ev_req);
64
    // conver header
65
57
    auto headers = evhttp_request_get_input_headers(_ev_req);
66
217
    for (auto header = headers->tqh_first; header != nullptr; header = header->next.tqe_next) {
67
160
        _headers.emplace(header->key, header->value);
68
160
    }
69
    // parse
70
57
    auto ev_uri = evhttp_request_get_evhttp_uri(_ev_req);
71
57
    _raw_path = evhttp_uri_get_path(ev_uri);
72
57
    auto query = evhttp_uri_get_query(ev_uri);
73
57
    if (query == nullptr || *query == '\0') {
74
45
        return 0;
75
45
    }
76
12
    struct evkeyvalq params;
77
12
    auto res = evhttp_parse_query_str(query, &params);
78
12
    if (res < 0) {
79
0
        LOG(WARNING) << "parse query str failed, query=" << query;
80
0
        return res;
81
0
    }
82
39
    for (auto param = params.tqh_first; param != nullptr; param = param->next.tqe_next) {
83
27
        _query_params.emplace(param->key, param->value);
84
27
    }
85
12
    _params.insert(_query_params.begin(), _query_params.end());
86
12
    evhttp_clear_headers(&params);
87
12
    return 0;
88
12
}
89
90
25
std::string HttpRequest::debug_string() const {
91
25
    std::stringstream ss;
92
25
    ss << "HttpRequest: \n"
93
25
       << "method:" << _method << "\n"
94
25
       << "uri:" << _uri << "\n"
95
25
       << "raw_path:" << _raw_path << "\n"
96
25
       << "headers: \n";
97
64
    for (auto& iter : _headers) {
98
64
        if (is_sensitive_header(iter.first)) {
99
2
            ss << "key=" << iter.first << ", value=***MASKED***\n";
100
62
        } else {
101
62
            ss << "key=" << iter.first << ", value=" << iter.second << "\n";
102
62
        }
103
64
    }
104
25
    ss << "params: \n";
105
30
    for (auto& iter : _params) {
106
30
        ss << "key=" << iter.first << ", value=" << iter.second << "\n";
107
30
    }
108
109
25
    return ss.str();
110
25
}
111
112
155
const std::string& HttpRequest::header(const std::string& key) const {
113
155
    auto iter = _headers.find(key);
114
155
    if (iter == _headers.end()) {
115
126
        return s_empty;
116
126
    }
117
29
    return iter->second;
118
155
}
119
120
60
const std::string& HttpRequest::param(const std::string& key) const {
121
60
    auto iter = _params.find(key);
122
60
    if (iter == _params.end()) {
123
28
        return s_empty;
124
28
    }
125
32
    return iter->second;
126
60
}
127
128
0
std::string HttpRequest::get_all_headers() const {
129
0
    std::stringstream headers;
130
0
    for (const auto& header : _headers) {
131
        // Mask sensitive headers
132
0
        if (is_sensitive_header(header.first)) {
133
0
            headers << header.first << ":***MASKED***, ";
134
0
        } else {
135
0
            headers << header.first << ":" << header.second + ", ";
136
0
        }
137
0
    }
138
0
    return headers.str();
139
0
}
140
141
27
void HttpRequest::add_output_header(const char* key, const char* value) {
142
27
    evhttp_add_header(evhttp_request_get_output_headers(_ev_req), key, value);
143
27
}
144
145
2
std::string HttpRequest::get_request_body() {
146
2
    if (!_request_body.empty()) {
147
0
        return _request_body;
148
0
    }
149
    // read buf
150
2
    auto evbuf = evhttp_request_get_input_buffer(_ev_req);
151
2
    if (evbuf == nullptr) {
152
0
        return _request_body;
153
0
    }
154
2
    auto length = evbuffer_get_length(evbuf);
155
2
    _request_body.resize(length);
156
2
    evbuffer_remove(evbuf, (char*)_request_body.data(), length);
157
2
    return _request_body;
158
2
}
159
160
32
const char* HttpRequest::remote_host() const {
161
32
    return _ev_req->remote_host;
162
32
}
163
164
53
void HttpRequest::finish_send_reply() {
165
53
    if (_send_reply_type == REPLY_SYNC) {
166
53
        return;
167
53
    }
168
169
0
    std::string infos;
170
0
    if (_handler_ctx != nullptr) {
171
0
        infos = reinterpret_cast<StreamLoadContext*>(_handler_ctx.get())->brief();
172
0
    }
173
0
    _http_reply_promise.set_value(true);
174
0
}
175
176
31
void HttpRequest::wait_finish_send_reply() {
177
31
    if (_send_reply_type == REPLY_SYNC) {
178
31
        return;
179
31
    }
180
181
0
    std::string infos;
182
0
    StreamLoadContext* ctx = nullptr;
183
0
    if (_handler_ctx != nullptr) {
184
0
        ctx = reinterpret_cast<StreamLoadContext*>(_handler_ctx.get());
185
0
        infos = ctx->brief();
186
0
        _handler->free_handler_ctx(_handler_ctx);
187
0
    }
188
189
0
    VLOG_NOTICE << "start to wait send reply, infos=" << infos;
190
0
    auto status = _http_reply_future.wait_for(std::chrono::seconds(config::async_reply_timeout_s));
191
    // if request is timeout and can't cancel fragment in time, it will cause some new request block
192
    // so we will free cancelled request in time.
193
0
    if (status != std::future_status::ready) {
194
0
        LOG(WARNING) << "wait for send reply timeout, " << this->debug_string();
195
0
        std::unique_lock<std::mutex> lock1(ctx->_send_reply_lock);
196
        // do not send_reply after free current request
197
0
        ctx->_can_send_reply = false;
198
0
        ctx->_finish_send_reply = true;
199
0
        ctx->_can_send_reply_cv.notify_all();
200
0
    } else {
201
0
        VLOG_NOTICE << "wait send reply finished";
202
0
    }
203
204
    // delete _handler_ctx at the end, in case that finish_send_reply can't get detailed info
205
0
    _handler_ctx = nullptr;
206
0
}
207
208
} // namespace doris