1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-11-23 00:33:44 +00:00

Don't respond if the request method is unexpected.

This includes the jolly responder, which now only works
for GET requests.
This commit is contained in:
King_DuckZ 2017-05-24 19:09:42 +01:00
parent 9fff9d793c
commit 4e32006e08
3 changed files with 13 additions and 10 deletions

View file

@ -163,12 +163,13 @@ int main (int parArgc, char* parArgv[], char* parEnvp[]) {
resp_factory.register_maker("", RequestMethodType::POST, &make_response<QuickSubmitPasteResponse>); resp_factory.register_maker("", RequestMethodType::POST, &make_response<QuickSubmitPasteResponse>);
resp_factory.register_maker("paste.cgi", RequestMethodType::POST, &make_response<SubmitPasteResponse>); resp_factory.register_maker("paste.cgi", RequestMethodType::POST, &make_response<SubmitPasteResponse>);
resp_factory.register_maker("error.cgi", RequestMethodType::GET, &make_response<ErrorResponse>); resp_factory.register_maker("error.cgi", RequestMethodType::GET, &make_response<ErrorResponse>);
resp_factory.register_jolly_maker(&make_response<PastieResponse>); resp_factory.register_jolly_maker(&make_response<PastieResponse>, RequestMethodType::GET);
std::unique_ptr<Response> response = resp_factory.make_response( std::unique_ptr<Response> response = resp_factory.make_response(
cgi_env->path_info(), cgi_env->path_info(),
cgi_env->request_method() cgi_env->request_method()
); );
if (response)
response->send(); response->send();
} }
catch (const std::exception& e) { catch (const std::exception& e) {

View file

@ -21,6 +21,8 @@
#include <functional> #include <functional>
#include <boost/container/flat_map.hpp> #include <boost/container/flat_map.hpp>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <algorithm>
#include <array>
namespace tawashi { namespace tawashi {
namespace { namespace {
@ -30,7 +32,7 @@ namespace tawashi {
Kakoune::SafePtr<SettingsBag> settings; Kakoune::SafePtr<SettingsBag> settings;
boost::container::flat_map<std::string, ResponseMakerFunc> makers_get; boost::container::flat_map<std::string, ResponseMakerFunc> makers_get;
boost::container::flat_map<std::string, ResponseMakerFunc> makers_post; boost::container::flat_map<std::string, ResponseMakerFunc> makers_post;
ResponseMakerFunc jolly_maker; std::array<ResponseMakerFunc, RequestMethodType::_size()> jolly_makers;
Kakoune::SafePtr<cgi::Env> cgi_env; Kakoune::SafePtr<cgi::Env> cgi_env;
}; };
@ -39,6 +41,7 @@ namespace tawashi {
{ {
m_local_data->settings = parSettings; m_local_data->settings = parSettings;
m_local_data->cgi_env = parCgiEnv; m_local_data->cgi_env = parCgiEnv;
std::fill(m_local_data->jolly_makers.begin(), m_local_data->jolly_makers.end(), nullptr);
} }
ResponseFactory::~ResponseFactory() noexcept = default; ResponseFactory::~ResponseFactory() noexcept = default;
@ -56,13 +59,12 @@ namespace tawashi {
if (makers.end() != maker_it) { if (makers.end() != maker_it) {
return maker_it->second(m_local_data->settings, m_local_data->cgi_env); return maker_it->second(m_local_data->settings, m_local_data->cgi_env);
} }
else if (m_local_data->jolly_maker) { else if (m_local_data->jolly_makers[parReqType]) {
spdlog::get("statuslog")->info("no exact match found for \"{}\", assuming it's a pastie's token", name); spdlog::get("statuslog")->info("no exact match found for \"{}\", assuming it's a pastie's token", name);
return m_local_data->jolly_maker(m_local_data->settings, m_local_data->cgi_env); return m_local_data->jolly_makers[parReqType](m_local_data->settings, m_local_data->cgi_env);
} }
else { else {
assert(false); spdlog::get("statuslog")->critical("no exact match found for \"{}\" with method {} and no jolly maker given, this should not happen", name, parReqType._to_string());
spdlog::get("statuslog")->critical("no exact match found for \"{}\" and no jolly maker given, this should not happen", name);
return std::unique_ptr<Response>(); return std::unique_ptr<Response>();
} }
} }
@ -83,7 +85,7 @@ namespace tawashi {
}; };
} }
void ResponseFactory::register_jolly_maker (ResponseMakerFunc parMaker) { void ResponseFactory::register_jolly_maker (ResponseMakerFunc parMaker, RequestMethodType parReqType) {
m_local_data->jolly_maker = parMaker; m_local_data->jolly_makers[parReqType] = parMaker;
} }
} //namespace tawashi } //namespace tawashi

View file

@ -39,7 +39,7 @@ namespace tawashi {
std::unique_ptr<Response> make_response(const boost::string_ref& parName, RequestMethodType parReqType); std::unique_ptr<Response> make_response(const boost::string_ref& parName, RequestMethodType parReqType);
void register_maker (std::string&& parName, ResponseMakerFunc parMaker); void register_maker (std::string&& parName, ResponseMakerFunc parMaker);
void register_maker (std::string&& parName, RequestMethodType parReqType, ResponseMakerFunc parMaker); void register_maker (std::string&& parName, RequestMethodType parReqType, ResponseMakerFunc parMaker);
void register_jolly_maker (ResponseMakerFunc parMaker); void register_jolly_maker (ResponseMakerFunc parMaker, RequestMethodType parReqType);
private: private:
struct LocalData; struct LocalData;