From a5fab23f70e6e33c633ba4b646a41d0851169ad1 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Tue, 7 Oct 2014 21:51:24 +0900 Subject: HTTP GET/POST method distinguish --- amalgamate/crow_all.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'amalgamate/crow_all.h') diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h index d732765..2962e7c 100644 --- a/amalgamate/crow_all.h +++ b/amalgamate/crow_all.h @@ -5014,7 +5014,9 @@ namespace crow constexpr bool is_equ_p(const char* a, const char* b, unsigned n) { return - *a == 0 || *b == 0 + *a == 0 && *b == 0 && n == 0 + ? true : + (*a == 0 || *b == 0) ? false : n == 0 ? true : @@ -5908,8 +5910,13 @@ namespace crow virtual void handle(const request&, response&, const routing_params&) = 0; - protected: + uint32_t methods() + { + return methods_; + } + protected: + uint32_t methods_{1<<(int)HTTPMethod::GET}; }; template @@ -6029,6 +6036,7 @@ namespace crow self_t& methods(HTTPMethod method) { methods_ = 1<<(int)method; + return *this; } template @@ -6036,6 +6044,7 @@ namespace crow { methods(args_method...); methods_ |= 1<<(int)method; + return *this; } void validate() @@ -6139,7 +6148,6 @@ namespace crow std::string rule_; std::string name_; - uint32_t methods_{1<<(int)HTTPMethod::GET}; template struct call_pair @@ -6523,7 +6531,15 @@ public: if (rule_index >= rules_.size()) throw std::runtime_error("Trie internal structure corrupted!"); - CROW_LOG_DEBUG << "Matched rule '" << ((TaggedRule<>*)rules_[rule_index].get())->rule_ << "'"; + if ((rules_[rule_index]->methods() & (1<<(uint32_t)req.method)) == 0) + { + CROW_LOG_DEBUG << "Rule found but method mismatch: " << editedUrl << " with " << method_name(req.method) << "(" << (uint32_t)req.method << ") / " << rules_[rule_index]->methods(); + res = response(404); + res.end(); + return; + } + + CROW_LOG_DEBUG << "Matched rule '" << ((TaggedRule<>*)rules_[rule_index].get())->rule_ << "' " << (uint32_t)req.method << " / " << rules_[rule_index]->methods(); rules_[rule_index]->handle(req, res, found.second); } -- cgit v1.2.3-54-g00ecf