Skip to content

Commit

Permalink
feat: add body check callback
Browse files Browse the repository at this point in the history
user can set callback,check request and early response.
  • Loading branch information
helintongh committed Aug 6, 2023
1 parent c42486c commit de9feea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
25 changes: 17 additions & 8 deletions include/cinatra/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ class connection : public base_connection,
asio::io_service &io_service, ssl_configure ssl_conf,
std::size_t max_req_size, long keep_alive_timeout, http_handler &handler,
std::string &static_dir,
std::function<bool(request &req, response &res)> *upload_check)
std::function<bool(request &req, response &res)> *upload_check,
std::string &checker_resp,
std::function<bool(request &req, response &res)> *req_body_check)
: socket_(io_service),
MAX_REQ_SIZE_(max_req_size),
KEEP_ALIVE_TIMEOUT_(keep_alive_timeout),
timer_(io_service),
http_handler_(handler),
req_(res_),
static_dir_(static_dir),
upload_check_(upload_check) {
upload_check_(upload_check),
checker_resp_(checker_resp),
req_body_check_(req_body_check) {
if constexpr (is_ssl_) {
init_ssl_context(std::move(ssl_conf));
}
Expand Down Expand Up @@ -408,12 +412,6 @@ class connection : public base_connection,
return;
}

size_t body_len = req_.body_len();
if (body_len > max_body_len_) {
response_back();
return;
}

int ret = req_.parse_header(len_);

if (ret == parse_status::has_error) {
Expand Down Expand Up @@ -448,7 +446,16 @@ class connection : public base_connection,
void handle_request(std::size_t bytes_transferred) {
auto type = get_content_type();
req_.set_http_type(type);

if (req_.has_body()) {
if (req_body_check_) {
bool r = (*req_body_check_)(req_, res_);
if (!r) {
response_back(status_type::entity_too_large,
std::move(checker_resp_));
return;
}
}
switch (type) {
case cinatra::content_type::string:
case cinatra::content_type::websocket:
Expand Down Expand Up @@ -1476,6 +1483,8 @@ class connection : public base_connection,
// callback handler to application layer
const http_handler &http_handler_;
std::function<bool(request &req, response &res)> *upload_check_ = nullptr;
std::string checker_resp_;
std::function<bool(request &req, response &res)> *req_body_check_ = nullptr;
std::any tag_;
std::function<void(request &, std::string &)> multipart_begin_ = nullptr;

Expand Down
12 changes: 11 additions & 1 deletion include/cinatra/http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ class http_server_ : private noncopyable {
upload_check_ = std::move(checker);
}

// should be called before listen
void set_body_check(std::function<bool(request &req, response &res)> checker,
std::string resp_str = "") {
checker_resp_ = resp_str;
req_body_check_ = std::move(checker);
}

void mapping_to_root_path(std::string relate_path) {
relate_paths_.emplace_back("." + std::move(relate_path));
}
Expand Down Expand Up @@ -280,7 +287,8 @@ class http_server_ : private noncopyable {
auto new_conn = std::make_shared<connection<ScoketType>>(
io_service_pool_.get_io_service(), ssl_conf_, max_req_buf_size_,
keep_alive_timeout_, http_handler_, upload_dir_,
upload_check_ ? &upload_check_ : nullptr);
upload_check_ ? &upload_check_ : nullptr, checker_resp_,
req_body_check_ ? &req_body_check_ : nullptr);

acceptor_->async_accept(
new_conn->tcp_socket(), [this, new_conn](const std::error_code &e) {
Expand Down Expand Up @@ -603,6 +611,8 @@ class http_server_ : private noncopyable {
std::function<bool(request &req, response &res)> download_check_;
std::vector<std::string> relate_paths_;
std::function<bool(request &req, response &res)> upload_check_ = nullptr;
std::string checker_resp_;
std::function<bool(request &req, response &res)> req_body_check_ = nullptr;

std::function<void(request &req, response &res)> not_found_ = nullptr;
std::function<void(request &, std::string &)> multipart_begin_ = nullptr;
Expand Down

0 comments on commit de9feea

Please sign in to comment.