forked from prestodb/presto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.h
More file actions
384 lines (317 loc) · 10.9 KB
/
Copy pathHttpServer.h
File metadata and controls
384 lines (317 loc) · 10.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <fmt/core.h>
#include <proxygen/httpserver/HTTPServer.h>
#include <proxygen/httpserver/RequestHandlerFactory.h>
#include <proxygen/httpserver/ResponseBuilder.h>
#include <re2/re2.h>
#include <wangle/ssl/SSLContextConfig.h>
#include "presto_cpp/external/json/nlohmann/json.hpp"
#include "presto_cpp/main/http/HttpConstants.h"
namespace facebook::presto::http {
using json = nlohmann::json;
void sendOkResponse(proxygen::ResponseHandler* downstream);
void sendOkResponse(proxygen::ResponseHandler* downstream, const json& body);
void sendOkResponse(
proxygen::ResponseHandler* downstream,
const std::string& body);
void sendOkThriftResponse(
proxygen::ResponseHandler* downstream,
const std::string& body);
void sendErrorResponse(
proxygen::ResponseHandler* downstream,
const std::string& error = "",
uint16_t status = http::kHttpInternalServerError);
void sendResponse(
proxygen::ResponseHandler* downstream,
const json& body,
uint16_t status);
class AbstractRequestHandler : public proxygen::RequestHandler {
public:
void onRequest(
std::unique_ptr<proxygen::HTTPMessage> headers) noexcept override {
headers_ = std::move(headers);
body_.clear();
}
void onBody(std::unique_ptr<folly::IOBuf> body) noexcept override {
body_.emplace_back(std::move(body));
}
void onUpgrade(proxygen::UpgradeProtocol proto) noexcept override {}
void requestComplete() noexcept override {
delete this;
}
void onError(proxygen::ProxygenError err) noexcept override {
delete this;
}
protected:
std::unique_ptr<proxygen::HTTPMessage> headers_;
std::vector<std::unique_ptr<folly::IOBuf>> body_;
};
// Some request handlers delay responses until some asynchronous work
// completes. When they delay responses asynchronously with
// promise/future, they also must handle the case that request may get
// expired in the meantime.
// They should
// 1. Check the state->requestExpired() before responding
// and must do that on the thread that invoked 'onEOM()' to avoid
// race.
// 2. Release any resources (eg. promise) using the
// onFinalizationCallback, which will also run on the same thread
// that invoked 'onEOM()'
// See 'keepPromiseAlive()' in TaskManager.cpp
class CallbackRequestHandlerState {
public:
~CallbackRequestHandlerState() {
finalize();
}
void finalize() {
requestExpired_ = true;
if (onFinalizationCallback_) {
onFinalizationCallback_();
onFinalizationCallback_ = std::function<void(void)>();
}
}
// The function 'fn' will run on the thread that invoked onEOM()
void runOnFinalization(std::function<void(void)> callback) {
onFinalizationCallback_ = callback;
}
bool requestExpired() const {
return requestExpired_;
}
static std::shared_ptr<CallbackRequestHandlerState> create() {
return std::make_shared<CallbackRequestHandlerState>();
}
private:
std::function<void(void)> onFinalizationCallback_;
bool requestExpired_{false};
};
using RequestHandlerCallback = std::function<void(
proxygen::HTTPMessage*,
std::vector<std::unique_ptr<folly::IOBuf>>& body,
proxygen::ResponseHandler* downstream)>;
using AsyncRequestHandlerCallback = std::function<void(
proxygen::HTTPMessage*,
std::vector<std::unique_ptr<folly::IOBuf>>& body,
proxygen::ResponseHandler* downstream,
std::shared_ptr<CallbackRequestHandlerState> state)>;
class CallbackRequestHandler : public AbstractRequestHandler {
public:
explicit CallbackRequestHandler(RequestHandlerCallback callback)
: callback_(wrap(callback)) {}
explicit CallbackRequestHandler(AsyncRequestHandlerCallback callback)
: callback_(callback), state_{CallbackRequestHandlerState::create()} {}
~CallbackRequestHandler() override {
if (state_) {
state_->finalize();
}
}
void onEOM() noexcept override {
callback_(headers_.get(), body_, downstream_, state_);
}
private:
const AsyncRequestHandlerCallback callback_;
std::shared_ptr<CallbackRequestHandlerState> state_;
static AsyncRequestHandlerCallback wrap(RequestHandlerCallback callback) {
return [callback](
proxygen::HTTPMessage* headers,
std::vector<std::unique_ptr<folly::IOBuf>>& body,
proxygen::ResponseHandler* downstream,
std::shared_ptr<CallbackRequestHandlerState> /* state */) {
callback(headers, body, downstream);
};
}
};
class ErrorRequestHandler : public AbstractRequestHandler {
public:
ErrorRequestHandler(uint16_t errorCode, const std::string& errorMessage)
: errorCode_(errorCode), errorMessage_(errorMessage) {}
void onEOM() noexcept override {
proxygen::ResponseBuilder(downstream_)
.status(errorCode_, errorMessage_)
.sendWithEOM();
}
private:
const uint16_t errorCode_;
const std::string errorMessage_;
};
using EndpointRequestHandlerFactory = std::function<proxygen::RequestHandler*(
proxygen::HTTPMessage* message,
const std::vector<std::string>& args)>;
class EndPoint {
public:
EndPoint(
const std::string& pattern,
const EndpointRequestHandlerFactory& factory)
: re_(pattern), factory_(factory) {}
bool check(
const std::string& path,
std::vector<std::string>& matches,
std::vector<RE2::Arg>& args,
std::vector<RE2::Arg*>& argPtrs) const;
proxygen::RequestHandler* checkAndApply(
const std::string& path,
proxygen::HTTPMessage* message,
std::vector<std::string>& matches,
std::vector<RE2::Arg>& args,
std::vector<RE2::Arg*>& argPtrs) const;
const std::string& pattern() const {
return re_.pattern();
}
private:
const RE2 re_;
EndpointRequestHandlerFactory factory_;
};
class DispatchingRequestHandlerFactory
: public proxygen::RequestHandlerFactory {
public:
void onServerStart(folly::EventBase* /*evb*/) noexcept override {}
void onServerStop() noexcept override {}
proxygen::RequestHandler* onRequest(
proxygen::RequestHandler*,
proxygen::HTTPMessage* message) noexcept override;
void registerEndPoint(
proxygen::HTTPMethod method,
const std::string& pattern,
const EndpointRequestHandlerFactory& endpoint);
const std::unordered_map<
proxygen::HTTPMethod,
std::vector<std::unique_ptr<EndPoint>>>&
endpoints() const;
private:
std::unordered_map<
proxygen::HTTPMethod,
std::vector<std::unique_ptr<EndPoint>>>
endpoints_;
};
class HttpConfig {
public:
explicit HttpConfig(
const folly::SocketAddress& address,
bool reusePort = false);
proxygen::HTTPServer::IPConfig ipConfig() const;
private:
const folly::SocketAddress address_;
const bool reusePort_{false};
};
class HttpsConfig {
public:
HttpsConfig(
const folly::SocketAddress& address,
const std::string& certPath,
const std::string& keyPath,
const std::string& supportedCiphers,
bool reusePort = false,
bool http2Enabled = true);
proxygen::HTTPServer::IPConfig ipConfig() const;
private:
const folly::SocketAddress address_;
const std::string certPath_;
const std::string keyPath_;
std::string supportedCiphers_;
const bool reusePort_;
const bool http2Enabled_;
};
class HttpServer {
public:
explicit HttpServer(
const std::shared_ptr<folly::IOThreadPoolExecutor>& httpIOExecutor,
std::unique_ptr<HttpConfig> httpConfig,
std::unique_ptr<HttpsConfig> httpsConfig = nullptr);
void start(
std::vector<std::unique_ptr<proxygen::RequestHandlerFactory>> filters =
{},
std::function<void(proxygen::HTTPServer* /*server*/)> onSuccess = nullptr,
std::function<void(std::exception_ptr)> onError = nullptr);
folly::IOThreadPoolExecutor* getExecutor() {
return httpIOExecutor_.get();
}
void stop() {
server_->stop();
}
void registerGet(
const std::string& pattern,
const EndpointRequestHandlerFactory& endpoint) {
handlerFactory_->registerEndPoint(
proxygen::HTTPMethod::GET, pattern, endpoint);
}
void registerGet(
const std::string& pattern,
const RequestHandlerCallback& callback) {
registerGet(pattern, endPointWrapper(callback));
}
void registerHead(
const std::string& pattern,
const EndpointRequestHandlerFactory& endpoint) {
handlerFactory_->registerEndPoint(
proxygen::HTTPMethod::HEAD, pattern, endpoint);
}
void registerHead(
const std::string& pattern,
const RequestHandlerCallback& callback) {
registerHead(pattern, endPointWrapper(callback));
}
void registerPost(
const std::string& pattern,
const EndpointRequestHandlerFactory& endpoint) {
handlerFactory_->registerEndPoint(
proxygen::HTTPMethod::POST, pattern, endpoint);
}
void registerPost(
const std::string& pattern,
const RequestHandlerCallback& callback) {
registerPost(pattern, endPointWrapper(callback));
}
void registerPut(
const std::string& pattern,
const EndpointRequestHandlerFactory& endpoint) {
handlerFactory_->registerEndPoint(
proxygen::HTTPMethod::PUT, pattern, endpoint);
}
void registerPut(
const std::string& pattern,
const RequestHandlerCallback& callback) {
registerPut(pattern, endPointWrapper(callback));
}
void registerDelete(
const std::string& pattern,
const EndpointRequestHandlerFactory& endpoint) {
handlerFactory_->registerEndPoint(
proxygen::HTTPMethod::DELETE, pattern, endpoint);
}
void registerDelete(
const std::string& pattern,
const RequestHandlerCallback& callback) {
registerDelete(pattern, endPointWrapper(callback));
}
std::unordered_map<
proxygen::HTTPMethod,
std::vector<std::unique_ptr<EndPoint>>>
endpoints() const;
private:
const std::unique_ptr<HttpConfig> httpConfig_;
const std::unique_ptr<HttpsConfig> httpsConfig_;
std::unique_ptr<DispatchingRequestHandlerFactory> handlerFactory_;
std::unique_ptr<proxygen::HTTPServer> server_;
std::shared_ptr<folly::IOThreadPoolExecutor> httpIOExecutor_;
static EndpointRequestHandlerFactory endPointWrapper(
const RequestHandlerCallback& callback) {
return [callback](
proxygen::HTTPMessage* /* headers */,
const std::vector<std::string>& /* args */) {
return new CallbackRequestHandler(callback);
};
}
};
} // namespace facebook::presto::http