be/src/service/http/ev_http_server.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 <memory> |
21 | | #include <mutex> |
22 | | #include <string> |
23 | | #include <vector> |
24 | | |
25 | | #include "common/status.h" |
26 | | #include "service/http/http_method.h" |
27 | | #include "util/path_trie.hpp" |
28 | | |
29 | | struct event_base; |
30 | | struct evhttp; |
31 | | |
32 | | namespace doris { |
33 | | |
34 | | class HttpHandler; |
35 | | class HttpRequest; |
36 | | class ThreadPool; |
37 | | |
38 | | class EvHttpServer { |
39 | | public: |
40 | | EvHttpServer(int port, int num_workers = 1); |
41 | | EvHttpServer(const std::string& host, int port, int num_workers = 1); |
42 | | ~EvHttpServer(); |
43 | | |
44 | | // register handler for an a path-method pair |
45 | | bool register_handler(const HttpMethod& method, const std::string& path, HttpHandler* handler); |
46 | | |
47 | | void register_static_file_handler(HttpHandler* handler); |
48 | | |
49 | | void start(); |
50 | | void stop(); |
51 | | void join(); |
52 | | |
53 | | // callback |
54 | | int on_header(struct evhttp_request* ev_req); |
55 | | |
56 | | // get real port |
57 | 2 | int get_real_port() const { return _real_port; } |
58 | | |
59 | 1 | std::vector<std::shared_ptr<event_base>> get_event_bases() { |
60 | 1 | std::lock_guard lock(_event_bases_lock); |
61 | 1 | return _event_bases; |
62 | 1 | } |
63 | | |
64 | | private: |
65 | | Status _bind(); |
66 | | HttpHandler* _find_handler(HttpRequest* req); |
67 | | |
68 | | private: |
69 | | // input param |
70 | | std::string _host; |
71 | | int _port; |
72 | | int _num_workers; |
73 | | // used for unittest, set port to 0, os will choose a free port; |
74 | | int _real_port; |
75 | | |
76 | | int _server_fd = -1; |
77 | | std::unique_ptr<ThreadPool> _workers; |
78 | | std::mutex _event_bases_lock; // protect _event_bases and _evhttp_servers |
79 | | std::vector<std::shared_ptr<event_base>> _event_bases; |
80 | | std::vector<std::shared_ptr<evhttp>> _evhttp_servers; |
81 | | |
82 | | std::mutex _handler_lock; |
83 | | PathTrie<HttpHandler*> _get_handlers; |
84 | | HttpHandler* _static_file_handler = nullptr; |
85 | | PathTrie<HttpHandler*> _put_handlers; |
86 | | PathTrie<HttpHandler*> _post_handlers; |
87 | | PathTrie<HttpHandler*> _delete_handlers; |
88 | | PathTrie<HttpHandler*> _head_handlers; |
89 | | PathTrie<HttpHandler*> _options_handlers; |
90 | | bool _started = false; |
91 | | }; |
92 | | |
93 | | } // namespace doris |