mirror of
https://github.com/CrowCpp/Crow.git
synced 2024-06-07 21:10:44 +00:00
Fixed merge oddities
This commit is contained in:
commit
6a2def410c
@ -67,7 +67,9 @@ namespace crow
|
|||||||
constexpr bool is_equ_p(const char* a, const char* b, unsigned n)
|
constexpr bool is_equ_p(const char* a, const char* b, unsigned n)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
*a == 0 || *b == 0
|
*a == 0 && *b == 0 && n == 0
|
||||||
|
? true :
|
||||||
|
(*a == 0 || *b == 0)
|
||||||
? false :
|
? false :
|
||||||
n == 0
|
n == 0
|
||||||
? true :
|
? true :
|
||||||
@ -6113,8 +6115,13 @@ namespace crow
|
|||||||
|
|
||||||
virtual void handle(const request&, response&, const routing_params&) = 0;
|
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 <typename ... Args>
|
template <typename ... Args>
|
||||||
@ -6234,6 +6241,7 @@ namespace crow
|
|||||||
self_t& methods(HTTPMethod method)
|
self_t& methods(HTTPMethod method)
|
||||||
{
|
{
|
||||||
methods_ = 1<<(int)method;
|
methods_ = 1<<(int)method;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ... MethodArgs>
|
template <typename ... MethodArgs>
|
||||||
@ -6241,6 +6249,7 @@ namespace crow
|
|||||||
{
|
{
|
||||||
methods(args_method...);
|
methods(args_method...);
|
||||||
methods_ |= 1<<(int)method;
|
methods_ |= 1<<(int)method;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void validate()
|
void validate()
|
||||||
@ -6344,7 +6353,6 @@ namespace crow
|
|||||||
|
|
||||||
std::string rule_;
|
std::string rule_;
|
||||||
std::string name_;
|
std::string name_;
|
||||||
uint32_t methods_{1<<(int)HTTPMethod::GET};
|
|
||||||
|
|
||||||
template <typename T, int Pos>
|
template <typename T, int Pos>
|
||||||
struct call_pair
|
struct call_pair
|
||||||
@ -6725,7 +6733,15 @@ public:
|
|||||||
if (rule_index >= rules_.size())
|
if (rule_index >= rules_.size())
|
||||||
throw std::runtime_error("Trie internal structure corrupted!");
|
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: " << req.url << " 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);
|
rules_[rule_index]->handle(req, res, found.second);
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,7 @@ int main()
|
|||||||
});
|
});
|
||||||
|
|
||||||
CROW_ROUTE(app, "/send")
|
CROW_ROUTE(app, "/send")
|
||||||
|
.methods("GET"_method, "POST"_method)
|
||||||
([](const crow::request& req)
|
([](const crow::request& req)
|
||||||
{
|
{
|
||||||
CROW_LOG_INFO << "msg from client: " << req.body;
|
CROW_LOG_INFO << "msg from client: " << req.body;
|
||||||
|
@ -26,8 +26,13 @@ namespace crow
|
|||||||
|
|
||||||
virtual void handle(const request&, response&, const routing_params&) = 0;
|
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 <typename ... Args>
|
template <typename ... Args>
|
||||||
@ -147,6 +152,7 @@ namespace crow
|
|||||||
self_t& methods(HTTPMethod method)
|
self_t& methods(HTTPMethod method)
|
||||||
{
|
{
|
||||||
methods_ = 1<<(int)method;
|
methods_ = 1<<(int)method;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ... MethodArgs>
|
template <typename ... MethodArgs>
|
||||||
@ -154,6 +160,7 @@ namespace crow
|
|||||||
{
|
{
|
||||||
methods(args_method...);
|
methods(args_method...);
|
||||||
methods_ |= 1<<(int)method;
|
methods_ |= 1<<(int)method;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void validate()
|
void validate()
|
||||||
@ -257,7 +264,6 @@ namespace crow
|
|||||||
|
|
||||||
std::string rule_;
|
std::string rule_;
|
||||||
std::string name_;
|
std::string name_;
|
||||||
uint32_t methods_{1<<(int)HTTPMethod::GET};
|
|
||||||
|
|
||||||
template <typename T, int Pos>
|
template <typename T, int Pos>
|
||||||
struct call_pair
|
struct call_pair
|
||||||
@ -638,7 +644,15 @@ public:
|
|||||||
if (rule_index >= rules_.size())
|
if (rule_index >= rules_.size())
|
||||||
throw std::runtime_error("Trie internal structure corrupted!");
|
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: " << req.url << " 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);
|
rules_[rule_index]->handle(req, res, found.second);
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,9 @@ namespace crow
|
|||||||
constexpr bool is_equ_p(const char* a, const char* b, unsigned n)
|
constexpr bool is_equ_p(const char* a, const char* b, unsigned n)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
*a == 0 || *b == 0
|
*a == 0 && *b == 0 && n == 0
|
||||||
|
? true :
|
||||||
|
(*a == 0 || *b == 0)
|
||||||
? false :
|
? false :
|
||||||
n == 0
|
n == 0
|
||||||
? true :
|
? true :
|
||||||
|
@ -250,6 +250,79 @@ TEST(handler_with_response)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(http_method)
|
||||||
|
{
|
||||||
|
SimpleApp app;
|
||||||
|
|
||||||
|
CROW_ROUTE(app, "/")
|
||||||
|
.methods("POST"_method, "GET"_method)
|
||||||
|
([](const request& req){
|
||||||
|
if (req.method == "GET"_method)
|
||||||
|
return "2";
|
||||||
|
else
|
||||||
|
return "1";
|
||||||
|
});
|
||||||
|
|
||||||
|
CROW_ROUTE(app, "/get_only")
|
||||||
|
.methods("GET"_method)
|
||||||
|
([](const request& req){
|
||||||
|
return "get";
|
||||||
|
});
|
||||||
|
CROW_ROUTE(app, "/post_only")
|
||||||
|
.methods("POST"_method)
|
||||||
|
([](const request& req){
|
||||||
|
return "post";
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// cannot have multiple handlers for the same url
|
||||||
|
//CROW_ROUTE(app, "/")
|
||||||
|
//.methods("GET"_method)
|
||||||
|
//([]{ return "2"; });
|
||||||
|
|
||||||
|
{
|
||||||
|
request req;
|
||||||
|
response res;
|
||||||
|
|
||||||
|
req.url = "/";
|
||||||
|
app.handle(req, res);
|
||||||
|
|
||||||
|
ASSERT_EQUAL("2", res.body);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
request req;
|
||||||
|
response res;
|
||||||
|
|
||||||
|
req.url = "/";
|
||||||
|
req.method = "POST"_method;
|
||||||
|
app.handle(req, res);
|
||||||
|
|
||||||
|
ASSERT_EQUAL("1", res.body);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
request req;
|
||||||
|
response res;
|
||||||
|
|
||||||
|
req.url = "/get_only";
|
||||||
|
app.handle(req, res);
|
||||||
|
|
||||||
|
ASSERT_EQUAL("get", res.body);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
request req;
|
||||||
|
response res;
|
||||||
|
|
||||||
|
req.url = "/get_only";
|
||||||
|
req.method = "POST"_method;
|
||||||
|
app.handle(req, res);
|
||||||
|
|
||||||
|
ASSERT_NOTEQUAL("get", res.body);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
TEST(server_handling_error_request)
|
TEST(server_handling_error_request)
|
||||||
{
|
{
|
||||||
static char buf[2048];
|
static char buf[2048];
|
||||||
@ -283,8 +356,8 @@ TEST(multi_server)
|
|||||||
{
|
{
|
||||||
static char buf[2048];
|
static char buf[2048];
|
||||||
SimpleApp app1, app2;
|
SimpleApp app1, app2;
|
||||||
CROW_ROUTE(app1, "/")([]{return "A";});
|
CROW_ROUTE(app1, "/").methods("GET"_method, "POST"_method)([]{return "A";});
|
||||||
CROW_ROUTE(app2, "/")([]{return "B";});
|
CROW_ROUTE(app2, "/").methods("GET"_method, "POST"_method)([]{return "B";});
|
||||||
|
|
||||||
Server<SimpleApp> server1(&app1, 45451);
|
Server<SimpleApp> server1(&app1, 45451);
|
||||||
Server<SimpleApp> server2(&app2, 45452);
|
Server<SimpleApp> server2(&app2, 45452);
|
||||||
@ -298,7 +371,6 @@ TEST(multi_server)
|
|||||||
asio::ip::tcp::socket c(is);
|
asio::ip::tcp::socket c(is);
|
||||||
c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45451));
|
c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45451));
|
||||||
|
|
||||||
|
|
||||||
c.send(asio::buffer(sendmsg));
|
c.send(asio::buffer(sendmsg));
|
||||||
|
|
||||||
size_t recved = c.receive(asio::buffer(buf, 2048));
|
size_t recved = c.receive(asio::buffer(buf, 2048));
|
||||||
|
Loading…
Reference in New Issue
Block a user