Coverage Report

Created: 2026-06-03 03:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/service/http/http_request.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 <glog/logging.h>
21
22
#include <functional>
23
#include <future>
24
#include <map>
25
#include <memory>
26
#include <mutex>
27
#include <string>
28
29
#include "common/config.h"
30
#include "service/http/http_method.h"
31
#include "util/string_util.h"
32
33
struct evhttp_request;
34
35
namespace doris {
36
37
class HttpHandler;
38
39
enum SendReplyType { REPLY_SYNC = 0, REPLY_ASYNC = 1 };
40
41
class HttpRequest {
42
public:
43
    HttpRequest(evhttp_request* ev_req);
44
45
    ~HttpRequest();
46
47
    int init_from_evhttp();
48
49
8.25k
    HttpMethod method() const { return _method; }
50
51
    // path + '?' + query
52
0
    const std::string& uri() const { return _uri; }
53
54
    // return raw path without query string after '?'
55
8.24k
    const std::string& raw_path() const { return _raw_path; }
56
57
    const std::string& header(const std::string& key) const;
58
59
    const std::string& param(const std::string& key) const;
60
61
    // return params
62
0
    const StringCaseUnorderedMap<std::string>& headers() { return _headers; }
63
64
    std::string get_all_headers() const;
65
66
    // return params
67
8.41k
    std::map<std::string, std::string>* params() { return &_params; }
68
69
0
    const std::map<std::string, std::string>& query_params() const { return _query_params; }
70
71
    std::string get_request_body();
72
73
    void add_output_header(const char* key, const char* value);
74
75
    std::string debug_string() const;
76
77
8.23k
    void set_handler(HttpHandler* handler) { _handler = handler; }
78
334k
    HttpHandler* handler() const { return _handler; }
79
80
313k
    struct evhttp_request* get_evhttp_request() const { return _ev_req; }
81
82
329k
    std::shared_ptr<void> handler_ctx() const { return _handler_ctx; }
83
4.05k
    void set_handler_ctx(std::shared_ptr<void> ctx) {
84
4.05k
        DCHECK(_handler != nullptr);
85
4.05k
        _handler_ctx = ctx;
86
4.05k
    }
87
88
    const char* remote_host() const;
89
90
3.93k
    void mark_send_reply(SendReplyType type = REPLY_ASYNC) { _send_reply_type = type; }
91
    void set_cancel_callback(std::function<void()> callback);
92
93
    void finish_send_reply();
94
    void wait_finish_send_reply();
95
96
private:
97
    void cancel_async_reply();
98
99
    SendReplyType _send_reply_type = REPLY_SYNC;
100
    HttpMethod _method;
101
    std::string _uri;
102
    std::string _raw_path;
103
104
    StringCaseUnorderedMap<std::string> _headers;
105
    std::map<std::string, std::string> _params;
106
    std::map<std::string, std::string> _query_params;
107
108
    struct evhttp_request* _ev_req = nullptr;
109
    HttpHandler* _handler = nullptr;
110
111
    std::shared_ptr<void> _handler_ctx;
112
    std::string _request_body;
113
114
    // ensure send_reply finished
115
    std::promise<bool> _http_reply_promise;
116
    std::future<bool> _http_reply_future = _http_reply_promise.get_future();
117
    std::mutex _cancel_callback_mutex;
118
    std::function<void()> _cancel_callback;
119
};
120
121
} // namespace doris