Coverage Report

Created: 2026-03-15 22:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/service/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 "service/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 "load/stream_load/stream_load_context.h"
32
#include "service/http/http_handler.h"
33
#include "service/http/http_headers.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
22.9k
static bool is_sensitive_header(const std::string& header_name) {
42
22.9k
    return iequal(header_name, HttpHeaders::AUTHORIZATION) ||
43
22.9k
           iequal(header_name, HttpHeaders::PROXY_AUTHORIZATION) || iequal(header_name, "token") ||
44
22.9k
           iequal(header_name, HttpHeaders::AUTH_TOKEN);
45
22.9k
}
46
47
6.10k
HttpRequest::HttpRequest(evhttp_request* evhttp_request) : _ev_req(evhttp_request) {}
48
49
6.10k
HttpRequest::~HttpRequest() {
50
6.10k
    if (_handler_ctx != nullptr) {
51
314
        DCHECK(_handler != nullptr);
52
314
        _handler->free_handler_ctx(_handler_ctx);
53
314
    }
54
6.10k
}
55
56
6.08k
int HttpRequest::init_from_evhttp() {
57
6.08k
    _method = to_http_method(evhttp_request_get_command(_ev_req));
58
6.08k
    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
6.08k
    _uri = evhttp_request_get_uri(_ev_req);
64
    // conver header
65
6.08k
    auto headers = evhttp_request_get_input_headers(_ev_req);
66
42.2k
    for (auto header = headers->tqh_first; header != nullptr; header = header->next.tqe_next) {
67
36.1k
        _headers.emplace(header->key, header->value);
68
36.1k
    }
69
    // parse
70
6.08k
    auto ev_uri = evhttp_request_get_evhttp_uri(_ev_req);
71
6.08k
    _raw_path = evhttp_uri_get_path(ev_uri);
72
6.08k
    auto query = evhttp_uri_get_query(ev_uri);
73
6.08k
    if (query == nullptr || *query == '\0') {
74
2.41k
        return 0;
75
2.41k
    }
76
3.67k
    struct evkeyvalq params;
77
3.67k
    auto res = evhttp_parse_query_str(query, &params);
78
3.67k
    if (res < 0) {
79
0
        LOG(WARNING) << "parse query str failed, query=" << query;
80
0
        return res;
81
0
    }
82
7.95k
    for (auto param = params.tqh_first; param != nullptr; param = param->next.tqe_next) {
83
4.28k
        _query_params.emplace(param->key, param->value);
84
4.28k
    }
85
3.67k
    _params.insert(_query_params.begin(), _query_params.end());
86
3.67k
    evhttp_clear_headers(&params);
87
3.67k
    return 0;
88
3.67k
}
89
90
36
std::string HttpRequest::debug_string() const {
91
36
    std::stringstream ss;
92
36
    ss << "HttpRequest: \n"
93
36
       << "method:" << _method << "\n"
94
36
       << "uri:" << _uri << "\n"
95
36
       << "raw_path:" << _raw_path << "\n"
96
36
       << "headers: \n";
97
101
    for (auto& iter : _headers) {
98
101
        if (is_sensitive_header(iter.first)) {
99
2
            ss << "key=" << iter.first << ", value=***MASKED***\n";
100
99
        } else {
101
99
            ss << "key=" << iter.first << ", value=" << iter.second << "\n";
102
99
        }
103
101
    }
104
36
    ss << "params: \n";
105
36
    for (auto& iter : _params) {
106
34
        ss << "key=" << iter.first << ", value=" << iter.second << "\n";
107
34
    }
108
109
36
    return ss.str();
110
36
}
111
112
151k
const std::string& HttpRequest::header(const std::string& key) const {
113
151k
    auto iter = _headers.find(key);
114
151k
    if (iter == _headers.end()) {
115
119k
        return s_empty;
116
119k
    }
117
31.9k
    return iter->second;
118
151k
}
119
120
9.24k
const std::string& HttpRequest::param(const std::string& key) const {
121
9.24k
    auto iter = _params.find(key);
122
9.24k
    if (iter == _params.end()) {
123
675
        return s_empty;
124
675
    }
125
8.57k
    return iter->second;
126
9.24k
}
127
128
2.07k
std::string HttpRequest::get_all_headers() const {
129
2.07k
    std::stringstream headers;
130
22.8k
    for (const auto& header : _headers) {
131
        // Mask sensitive headers
132
22.8k
        if (is_sensitive_header(header.first)) {
133
2.18k
            headers << header.first << ":***MASKED***, ";
134
20.6k
        } else {
135
20.6k
            headers << header.first << ":" << header.second + ", ";
136
20.6k
        }
137
22.8k
    }
138
2.07k
    return headers.str();
139
2.07k
}
140
141
5.92k
void HttpRequest::add_output_header(const char* key, const char* value) {
142
5.92k
    evhttp_add_header(evhttp_request_get_output_headers(_ev_req), key, value);
143
5.92k
}
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
4.56k
const char* HttpRequest::remote_host() const {
161
4.56k
    return _ev_req->remote_host;
162
4.56k
}
163
164
6.08k
void HttpRequest::finish_send_reply() {
165
6.08k
    if (_send_reply_type == REPLY_SYNC) {
166
4.01k
        return;
167
4.01k
    }
168
169
2.07k
    std::string infos;
170
2.07k
    if (_handler_ctx != nullptr) {
171
2.06k
        infos = reinterpret_cast<StreamLoadContext*>(_handler_ctx.get())->brief();
172
2.06k
    }
173
2.07k
    _http_reply_promise.set_value(true);
174
2.07k
}
175
176
5.87k
void HttpRequest::wait_finish_send_reply() {
177
5.87k
    if (_send_reply_type == REPLY_SYNC) {
178
3.98k
        return;
179
3.98k
    }
180
181
1.89k
    std::string infos;
182
1.89k
    StreamLoadContext* ctx = nullptr;
183
1.89k
    if (_handler_ctx != nullptr) {
184
1.89k
        ctx = reinterpret_cast<StreamLoadContext*>(_handler_ctx.get());
185
1.89k
        infos = ctx->brief();
186
1.89k
        _handler->free_handler_ctx(_handler_ctx);
187
1.89k
    }
188
189
1.89k
    VLOG_NOTICE << "start to wait send reply, infos=" << infos;
190
1.89k
    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
1.89k
    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
1.89k
    } else {
201
1.89k
        VLOG_NOTICE << "wait send reply finished";
202
1.89k
    }
203
204
    // delete _handler_ctx at the end, in case that finish_send_reply can't get detailed info
205
1.89k
    _handler_ctx = nullptr;
206
1.89k
}
207
208
} // namespace doris