/
http_server.h
181 lines (113 loc) · 5.54 KB
/
http_server.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#pragma once
#define H2O_USE_LIBUV 0
extern "C" {
#include "h2o.h"
#include "h2o/http1.h"
#include "h2o/http2.h"
#include "h2o/multithread.h"
}
#include <map>
#include <string>
#include <cstdio>
#include "http_data.h"
#include "option.h"
class ReplicationState;
class HttpServer;
struct h2o_custom_req_handler_t {
h2o_handler_t super;
HttpServer* http_server;
};
struct h2o_custom_generator_t {
h2o_generator_t super;
h2o_custom_req_handler_t* h2o_handler;
route_path* rpath;
http_req* request;
http_res* response;
};
struct deferred_req_res_t {
http_req* req;
http_res* res;
HttpServer* server;
};
class HttpServer {
private:
h2o_globalconf_t config;
h2o_compress_args_t compress_args;
h2o_context_t ctx;
h2o_accept_ctx_t* accept_ctx;
h2o_hostconf_t *hostconf;
h2o_socket_t* listener_socket;
static const size_t ACTIVE_STREAM_WINDOW_SIZE = 196605;
static const size_t REQ_TIMEOUT_MS = 60000;
static const uint64_t SSL_REFRESH_INTERVAL_MS = 8 * 60 * 60 * 1000;
h2o_custom_timer_t ssl_refresh_timer;
h2o_custom_timer_t metrics_refresh_timer;
http_message_dispatcher* message_dispatcher;
ReplicationState* replication_state;
std::atomic<bool> exit_loop;
std::string version;
// must be a vector since order of routes matter
std::vector<std::pair<uint64_t, route_path>> routes;
const std::string listen_address;
const uint32_t listen_port;
std::string ssl_cert_path;
std::string ssl_cert_key_path;
bool cors_enabled;
bool (*auth_handler)(std::map<std::string, std::string>& params, const std::string& body, const route_path& rpath,
const std::string& auth_key);
static void on_accept(h2o_socket_t *listener, const char *err);
int setup_ssl(const char *cert_file, const char *key_file);
static void on_ssl_refresh_timeout(h2o_timer_t *entry);
static void on_metrics_refresh_timeout(h2o_timer_t *entry);
int create_listener();
h2o_pathconf_t *register_handler(h2o_hostconf_t *hostconf, const char *path,
int (*on_req)(h2o_handler_t *, h2o_req_t *));
static std::map<std::string, std::string> parse_query(const std::string& query);
static int catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req);
static void response_proceed(h2o_generator_t *generator, h2o_req_t *req);
static void response_abort(h2o_generator_t *generator, h2o_req_t *req);
static void on_res_generator_dispose(void *self);
static int send_response(h2o_req_t *req, int status_code, const std::string & message);
static int async_req_cb(void *ctx, h2o_iovec_t chunk, int is_end_stream);
public:
HttpServer(const std::string & version,
const std::string & listen_address, uint32_t listen_port,
const std::string & ssl_cert_path, const std::string & ssl_cert_key_path, bool cors_enabled);
~HttpServer();
http_message_dispatcher* get_message_dispatcher() const;
ReplicationState* get_replication_state() const;
bool is_alive() const;
uint64_t node_state() const;
void set_auth_handler(bool (*handler)(std::map<std::string, std::string>& params, const std::string& body,
const route_path & rpath, const std::string & auth_key));
void get(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);
void post(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);
void put(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);
void patch(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);
void del(const std::string & path, bool (*handler)(http_req & req, http_res & res), bool async_req=false, bool async_res=false);
void on(const std::string & message, bool (*handler)(void*));
void send_message(const std::string & type, void* data);
void send_response(http_req* request, http_res* response);
static void destroy_request_response(http_req* request, http_res* response);
static void stream_response(http_req& request, http_res& response);
uint64_t find_route(const std::vector<std::string> & path_parts, const std::string & http_method,
route_path** found_rpath);
bool get_route(uint64_t hash, route_path** found_rpath);
int run(ReplicationState* replication_state);
void stop();
bool has_exited() const;
void clear_timeouts(const std::vector<h2o_timer_t*> & timers, bool trigger_callback = true);
static bool on_stop_server(void *data);
static bool on_stream_response_message(void *data);
static bool on_request_proceed_message(void *data);
std::string get_version();
static constexpr const char* STOP_SERVER_MESSAGE = "STOP_SERVER";
static constexpr const char* STREAM_RESPONSE_MESSAGE = "STREAM_RESPONSE";
static constexpr const char* REQUEST_PROCEED_MESSAGE = "REQUEST_PROCEED";
static int process_request(http_req* request, http_res* response, route_path *rpath,
const h2o_custom_req_handler_t *req_handler);
static void on_deferred_process_request(h2o_timer_t *entry);
void defer_processing(http_req& req, http_res& res, size_t timeout_ms);
void do_snapshot(const std::string& snapshot_path, http_req& req, http_res& res);
bool trigger_vote();
};