-
Notifications
You must be signed in to change notification settings - Fork 5
/
BaseRequestHandler.h
61 lines (51 loc) · 1.87 KB
/
BaseRequestHandler.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
//
// Created by victor on 11/7/17.
//
#ifndef RESTDBXX_BASEREQUESTHANDLER_H
#define RESTDBXX_BASEREQUESTHANDLER_H
//#include <proxygen/lib/http/HTTPMessage.h>
#include <proxygen/lib/http/HTTPMethod.h>
#include <folly/dynamic.h>
#include <proxygen/httpserver/RequestHandler.h>
#include <proxygen/httpserver/ResponseBuilder.h>
#include "DbManager.h"
#include <folly/Try.h>
namespace restdbxx {
const std::string HTTP_MESSAGE_OK = "OK";
using proxygen::RequestHandler;
class not_found_exception : public std::runtime_error {
public:
not_found_exception() : std::runtime_error("not found") {}
virtual ~not_found_exception() = default;
};
class BaseRequestHandler: public proxygen::RequestHandler {
public:
BaseRequestHandler();
virtual ~BaseRequestHandler() = default;
virtual void setResponseHandler(proxygen::ResponseHandler *handler)noexcept override {
RequestHandler::setResponseHandler(handler);
}
virtual void onEgressPaused()noexcept override {
RequestHandler::onEgressPaused();
}
virtual void onEgressResumed()noexcept override {
RequestHandler::onEgressResumed();
}
void onBody(std::unique_ptr<folly::IOBuf> body) noexcept override;
protected:
std::unique_ptr<folly::IOBuf> _body;
std::unique_ptr<proxygen::HTTPMessage> _headers;
proxygen::HTTPMethod _method;
std::string _path;
virtual bool not_found() const;
folly::Try<folly::dynamic> parseBody();
bool is_endpoint_add = false;
virtual void sendEmptyContentResponse(int status, const std::string &message) const;
virtual void sendJsonResponse(const folly::dynamic &json, int status=200, const std::string &message=HTTP_MESSAGE_OK) const;
void sendNotFound() const {
sendEmptyContentResponse(404, "Not Found");
}
virtual void sendStringResponse(const std::string &body, int status=200, const std::string &message=HTTP_MESSAGE_OK) const;
};
}
#endif //RESTDBXX_BASEREQUESTHANDLER_H