Coverage Report

Created: 2026-01-23 21:57

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