From f0a296d97bcfb32f516c50a925457f02f623d33e Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sun, 4 Jul 2021 01:26:44 -0700 Subject: [PATCH] :sparkles: Accept Filter --- CMakeLists.txt | 1 + include/json_filter.h | 14 ++++++++++++++ src/json_filter.cpp | 17 +++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 include/json_filter.h create mode 100644 src/json_filter.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 02469bd..57fdbaa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable(${PROJECT_NAME} main.cpp ) add_subdirectory(third_party/xxHash/cmake_unofficial third_party/xxHash/build EXCLUDE_FROM_ALL) target_sources(${PROJECT_NAME} PRIVATE + src/json_filter.cpp src/admin_filter.cpp src/bank_api.cpp src/bank.cpp diff --git a/include/json_filter.h b/include/json_filter.h new file mode 100644 index 0000000..b5ea875 --- /dev/null +++ b/include/json_filter.h @@ -0,0 +1,14 @@ +#pragma once +#include + +using namespace drogon; + +class JsonFilter : public HttpFilter +{ +public: + JsonFilter(); + + virtual void doFilter(const HttpRequestPtr &, + FilterCallback &&, + FilterChainCallback &&) override; +}; \ No newline at end of file diff --git a/src/json_filter.cpp b/src/json_filter.cpp new file mode 100644 index 0000000..8b2b5d1 --- /dev/null +++ b/src/json_filter.cpp @@ -0,0 +1,17 @@ +#include "json_filter.h" + +JsonFilter::JsonFilter() {} + +void JsonFilter::doFilter(const HttpRequestPtr &req, + FilterCallback &&fcb, + FilterChainCallback &&fccb) +{ + if (req->getHeader("Content-Type") == "application/json" && req->getHeader("Accept") == "application/json") + { + fccb(); + return; + } + const auto &resp = HttpResponse::newHttpJsonResponse("Client must Accept JSON"); + resp->setStatusCode(k406NotAcceptable); + fcb(resp); +} \ No newline at end of file