From a7c74c632761678fc55da55aee61adffa3b49859 Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Tue, 1 Dec 2020 11:50:46 +0530 Subject: [PATCH 01/10] recieve response without request being another argument in handle --- examples/example.cpp | 5 ++--- examples/example_vs.cpp | 5 ++--- include/crow/http_response.h | 5 +++++ include/crow/routing.h | 28 +++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/examples/example.cpp b/examples/example.cpp index a2739a827..7915e469e 100644 --- a/examples/example.cpp +++ b/examples/example.cpp @@ -13,12 +13,11 @@ struct ExampleMiddleware { std::string message; - ExampleMiddleware() + ExampleMiddleware() : message(std::string("foo")) { - message = "foo"; } - void setMessage(std::string newMsg) + void setMessage(const std::string &newMsg) { message = newMsg; } diff --git a/examples/example_vs.cpp b/examples/example_vs.cpp index dbc4d3101..87e344011 100644 --- a/examples/example_vs.cpp +++ b/examples/example_vs.cpp @@ -13,12 +13,11 @@ struct ExampleMiddleware { std::string message; - ExampleMiddleware() + ExampleMiddleware() : message(std::string("foo")) { - message = "foo"; } - void setMessage(std::string newMsg) + void setMessage(const std::string &newMsg) { message = newMsg; } diff --git a/include/crow/http_response.h b/include/crow/http_response.h index 0b46aaecb..ad479ccf6 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -124,6 +124,11 @@ namespace crow } } + void end(const int& _code) { + code = _code; + end(); + } + /// Same as end() except it adds a body part right before ending. void end(const std::string& body_part) { diff --git a/include/crow/routing.h b/include/crow/routing.h index 0ddf72dfa..2403d10a0 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -516,7 +516,33 @@ namespace crow template typename std::enable_if< !black_magic::CallHelper>::value && - !black_magic::CallHelper>::value, + !black_magic::CallHelper>::value && + black_magic::CallHelper>::value, + void>::type + operator()(Func&& f) + { + static_assert(black_magic::CallHelper>::value || + black_magic::CallHelper>::value + , + "Handle type is mismatched with URL parameters"); + static_assert(std::is_same(), std::declval()...))>::value, + "Handler function with response argument should have void return type"); + + handler_ = ( + #ifdef CROW_CAN_USE_CPP14 + [f = std::move(f)] + #else + [f] + #endif + (const crow::request& req, crow::response& res, Args ... args) { + f(res, args...); + }); + } + template + typename std::enable_if< + !black_magic::CallHelper>::value&& + !black_magic::CallHelper>::value && + !black_magic::CallHelper>::value, void>::type operator()(Func&& f) { From acab73ef68718ab98aba83605b9eafb2d980409e Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Tue, 1 Dec 2020 12:35:46 +0530 Subject: [PATCH 02/10] removed extra req arg --- include/crow/routing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/crow/routing.h b/include/crow/routing.h index 2403d10a0..340a4ebe4 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -534,7 +534,7 @@ namespace crow #else [f] #endif - (const crow::request& req, crow::response& res, Args ... args) { + (crow::response& res, Args ... args) { f(res, args...); }); } From 737a3384ae4809d7cf81f99f8f5274a0da87c82c Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Tue, 1 Dec 2020 14:39:11 +0530 Subject: [PATCH 03/10] removed extra changes --- include/crow/http_response.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/include/crow/http_response.h b/include/crow/http_response.h index b402c6d80..e3fc39a38 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -124,11 +124,6 @@ namespace crow } } - void end(const int& _code) { - code = _code; - end(); - } - /// Same as end() except it adds a body part right before ending. void end(const std::string& body_part) { From 770359e79feb8be0daaf398cb946f1c48b2ebb92 Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Wed, 2 Dec 2020 15:36:29 +0530 Subject: [PATCH 04/10] fix: ambigous call and example --- examples/example_with_all.cpp | 4 ++- include/crow/routing.h | 49 ++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/examples/example_with_all.cpp b/examples/example_with_all.cpp index 9661e7546..1968a347e 100644 --- a/examples/example_with_all.cpp +++ b/examples/example_with_all.cpp @@ -41,8 +41,10 @@ int main() return crow::response(os.str()); }); + // example which uses only response as a paramter wihtout + // request being a parameter. CROW_ROUTE(app,"/add//") - ([](const crow::request& /*req*/, crow::response& res, int a, int b){ + ([](crow::response& res, int a, int b){ std::ostringstream os; os << a+b; res.write(os.str()); diff --git a/include/crow/routing.h b/include/crow/routing.h index 7de2a2978..c9cc74c88 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -531,31 +531,31 @@ namespace crow !black_magic::CallHelper>::value && !black_magic::CallHelper>::value && black_magic::CallHelper>::value, - void>::type - operator()(Func&& f) - { - static_assert(black_magic::CallHelper>::value || - black_magic::CallHelper>::value - , - "Handle type is mismatched with URL parameters"); - static_assert(std::is_same(), std::declval()...))>::value, - "Handler function with response argument should have void return type"); - - handler_ = ( - #ifdef CROW_CAN_USE_CPP14 - [f = std::move(f)] - #else - [f] - #endif - (crow::response& res, Args ... args) { - f(res, args...); - }); - } - template - typename std::enable_if< - !black_magic::CallHelper>::value&& + void>::type + operator()(Func&& f) + { + static_assert(black_magic::CallHelper>::value || + black_magic::CallHelper>::value + , + "Handler type is mismatched with URL parameters"); + static_assert(std::is_same(), std::declval()...))>::value, + "Handler function with response argument should have void return type"); + handler_without_request = ( +#ifdef CROW_CAN_USE_CPP14 + [f = std::move(f)] +#else + [f] +#endif + (crow::response& res, Args ... args){ + f(res, args...); + }); + } + + template + typename std::enable_if< + !black_magic::CallHelper>::value && !black_magic::CallHelper>::value && - !black_magic::CallHelper>::value, + !black_magic::CallHelper>::value, void>::type operator()(Func&& f) { @@ -594,6 +594,7 @@ namespace crow private: std::function handler_; + std::function handler_without_request; }; From fcce57d232189748df3bacaf824c77d58c31e7bb Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Wed, 2 Dec 2020 16:37:43 +0530 Subject: [PATCH 05/10] revert: new handler_ changes --- include/crow/routing.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/crow/routing.h b/include/crow/routing.h index c9cc74c88..bb6fbc614 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -540,13 +540,13 @@ namespace crow "Handler type is mismatched with URL parameters"); static_assert(std::is_same(), std::declval()...))>::value, "Handler function with response argument should have void return type"); - handler_without_request = ( + handler_ = ( #ifdef CROW_CAN_USE_CPP14 [f = std::move(f)] #else [f] #endif - (crow::response& res, Args ... args){ + (const crow::request& req, crow::response& res, Args ... args){ f(res, args...); }); } @@ -594,7 +594,6 @@ namespace crow private: std::function handler_; - std::function handler_without_request; }; From ec29121c22d730a885e33ab5f5628d1262873ca3 Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Wed, 2 Dec 2020 22:59:45 +0530 Subject: [PATCH 06/10] fix clang build --- include/crow/routing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/crow/routing.h b/include/crow/routing.h index bb6fbc614..7a8eaded1 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -546,7 +546,7 @@ namespace crow #else [f] #endif - (const crow::request& req, crow::response& res, Args ... args){ + (const crow::request&, crow::response& res, Args ... args){ f(res, args...); }); } From 8371bdb09f0c4ff769260536f6b907916c6a0235 Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Thu, 3 Dec 2020 12:43:50 +0530 Subject: [PATCH 07/10] fix: std::string && req parameter --- examples/example.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/example.cpp b/examples/example.cpp index 7915e469e..82c86daab 100644 --- a/examples/example.cpp +++ b/examples/example.cpp @@ -13,7 +13,7 @@ struct ExampleMiddleware { std::string message; - ExampleMiddleware() : message(std::string("foo")) + ExampleMiddleware() : message("foo") { } @@ -83,7 +83,7 @@ int main() // To see it in action submit {ip}:18080/add/1/2 and you should receive 3 (exciting, isn't it) CROW_ROUTE(app,"/add//") - ([](const crow::request& /*req*/, crow::response& res, int a, int b){ + ([](crow::response& res, int a, int b){ std::ostringstream os; os << a+b; res.write(os.str()); From 87e29c2aba226a3187be99b11d2fe3cbcf4766da Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Thu, 3 Dec 2020 12:46:38 +0530 Subject: [PATCH 08/10] fix: std::string && req parameter --- examples/example_vs.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/example_vs.cpp b/examples/example_vs.cpp index 87e344011..baa9040ec 100644 --- a/examples/example_vs.cpp +++ b/examples/example_vs.cpp @@ -13,7 +13,7 @@ struct ExampleMiddleware { std::string message; - ExampleMiddleware() : message(std::string("foo")) + ExampleMiddleware() : message("foo") { } @@ -77,7 +77,7 @@ int main() }); app.route_dynamic("/add//") - ([](const crow::request& req, crow::response& res, int a, int b){ + ([](crow::response& res, int a, int b){ std::ostringstream os; os << a+b; res.write(os.str()); From 6c9b7e18a406d638576f27694b87077cd15a3eeb Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Thu, 3 Dec 2020 12:48:58 +0530 Subject: [PATCH 09/10] fix: docs routes.md --- docs/guides/routes.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/guides/routes.md b/docs/guides/routes.md index c04b53d61..137f7dd04 100644 --- a/docs/guides/routes.md +++ b/docs/guides/routes.md @@ -30,8 +30,7 @@ You can also access the url parameters in the handler using `#!cpp req.url_param For more information on `crow::request` go [here](../../reference/structcrow_1_1request.html).

###Response -Crow also provides the ability to define a response in the parameters by using `#!cpp ([](const crow::request& req, crow::response& res){...})`.
-If you don't want to use the request you can write `#!cpp ([](const crow::request& , crow::response& res){...})`.

+Crow also provides the ability to define a response in the parameters by using `#!cpp ([](crow::response& res){...})`.
Please note that in order to return a response defined as a parameter you'll need to use `res.end();`.

From 0bca73dbf675abda74c84c7ef736803d7546f57b Mon Sep 17 00:00:00 2001 From: ayaankhan98 Date: Thu, 3 Dec 2020 18:16:19 +0530 Subject: [PATCH 10/10] added missing
--- docs/guides/routes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guides/routes.md b/docs/guides/routes.md index 137f7dd04..ebb906940 100644 --- a/docs/guides/routes.md +++ b/docs/guides/routes.md @@ -30,7 +30,7 @@ You can also access the url parameters in the handler using `#!cpp req.url_param For more information on `crow::request` go [here](../../reference/structcrow_1_1request.html).

###Response -Crow also provides the ability to define a response in the parameters by using `#!cpp ([](crow::response& res){...})`.
+Crow also provides the ability to define a response in the parameters by using `#!cpp ([](crow::response& res){...})`.

Please note that in order to return a response defined as a parameter you'll need to use `res.end();`.