From 32d66d6fd1ac7f90512a573991de4940b5063374 Mon Sep 17 00:00:00 2001 From: Vsevolod Kvachev Date: Tue, 6 Dec 2016 18:22:10 +0300 Subject: Upgrade amalgamate --- include/crow/routing.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/crow/routing.h b/include/crow/routing.h index a8aa233..fe65ac4 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -156,7 +156,7 @@ namespace crow struct Wrapped { template - void set(Func f, typename std::enable_if< + void set_(Func f, typename std::enable_if< !std::is_same>::type, const request&>::value , int>::type = 0) { @@ -190,7 +190,7 @@ namespace crow }; template - void set(Func f, typename std::enable_if< + void set_(Func f, typename std::enable_if< std::is_same>::type, const request&>::value && !std::is_same>::type, response&>::value , int>::type = 0) @@ -205,7 +205,7 @@ namespace crow } template - void set(Func f, typename std::enable_if< + void set_(Func f, typename std::enable_if< std::is_same>::type, const request&>::value && std::is_same>::type, response&>::value , int>::type = 0) @@ -410,7 +410,7 @@ namespace crow throw std::runtime_error("route_dynamic: Handler type is mismatched with URL parameters: " + rule_); } auto ret = detail::routing_handler_call_helper::Wrapped...>(); - ret.template set< + ret.template set_< typename function_t::template arg... >(std::move(f)); return ret; -- cgit v1.2.3-54-g00ecf From bea1ba3797fbf8bbab209edbbf765d0e6d5960d1 Mon Sep 17 00:00:00 2001 From: Marcos Medeiros Date: Wed, 28 Dec 2016 11:06:56 -0200 Subject: Add onaccept handler to websocket rule --- include/crow/routing.h | 12 ++++++++++-- include/crow/websocket.h | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/crow/routing.h b/include/crow/routing.h index a8aa233..95fe354 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -276,12 +276,12 @@ namespace crow void handle_upgrade(const request& req, response&, SocketAdaptor&& adaptor) override { - new crow::websocket::Connection(req, std::move(adaptor), open_handler_, message_handler_, close_handler_, error_handler_); + new crow::websocket::Connection(req, std::move(adaptor), open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); } #ifdef CROW_ENABLE_SSL void handle_upgrade(const request& req, response&, SSLAdaptor&& adaptor) override { - new crow::websocket::Connection(req, std::move(adaptor), open_handler_, message_handler_, close_handler_, error_handler_); + new crow::websocket::Connection(req, std::move(adaptor), open_handler_, message_handler_, close_handler_, error_handler_, accept_handler_); } #endif @@ -313,11 +313,19 @@ namespace crow return *this; } + template + self_t& onaccept(Func f) + { + accept_handler_ = f; + return *this; + } + protected: std::function open_handler_; std::function message_handler_; std::function close_handler_; std::function error_handler_; + std::function accept_handler_; }; template diff --git a/include/crow/websocket.h b/include/crow/websocket.h index a1e8e8f..fa45d1b 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -39,8 +39,10 @@ namespace crow std::function open_handler, std::function message_handler, std::function close_handler, - std::function error_handler) + std::function error_handler, + std::function accept_handler) : adaptor_(std::move(adaptor)), open_handler_(std::move(open_handler)), message_handler_(std::move(message_handler)), close_handler_(std::move(close_handler)), error_handler_(std::move(error_handler)) + , accept_handler_(std::move(accept_handler)) { if (!boost::iequals(req.get_header_value("upgrade"), "websocket")) { @@ -48,6 +50,17 @@ namespace crow delete this; return; } + + if (accept_handler_) + { + if (!accept_handler_(req)) + { + adaptor.close(); + delete this; + return; + } + } + // Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== // Sec-WebSocket-Version: 13 std::string magic = req.get_header_value("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; @@ -484,6 +497,7 @@ namespace crow std::function message_handler_; std::function close_handler_; std::function error_handler_; + std::function accept_handler_; }; } } -- cgit v1.2.3-54-g00ecf From bd2dd4a8e41da76d3b4cd65e79a410c46c1d211d Mon Sep 17 00:00:00 2001 From: ushiyake Date: Mon, 9 Jan 2017 21:24:01 +0900 Subject: Fixed a problem that warning appeared in c ++ 11 --- include/crow/routing.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/crow/routing.h b/include/crow/routing.h index a8aa233..a7995bb 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -456,10 +456,16 @@ namespace crow static_assert(!std::is_same()...))>::value, "Handler function cannot have void return type; valid return types: string, int, crow::resposne, crow::json::wvalue"); - handler_ = [f = std::move(f)](const request&, response& res, Args ... args){ + handler_ = ( +#ifdef CROW_CAN_USE_CPP14 + [f = std::move(f)] +#else + [f] +#endif + (const request&, response& res, Args ... args){ res = response(f(args...)); res.end(); - }; + }); } template @@ -475,10 +481,16 @@ namespace crow static_assert(!std::is_same(), std::declval()...))>::value, "Handler function cannot have void return type; valid return types: string, int, crow::resposne, crow::json::wvalue"); - handler_ = [f = std::move(f)](const crow::request& req, crow::response& res, Args ... args){ + handler_ = ( +#ifdef CROW_CAN_USE_CPP14 + [f = std::move(f)] +#else + [f] +#endif + (const crow::request& req, crow::response& res, Args ... args){ res = response(f(req, args...)); res.end(); - }; + }); } template -- cgit v1.2.3-54-g00ecf From 01b21296bcd855cc851417ab704081c49c1b7c90 Mon Sep 17 00:00:00 2001 From: Rasie1 Date: Thu, 16 Mar 2017 19:25:27 +0300 Subject: Add get_dict method --- include/crow/query_string.h | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'include') diff --git a/include/crow/query_string.h b/include/crow/query_string.h index 3f8bffe..2740441 100644 --- a/include/crow/query_string.h +++ b/include/crow/query_string.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace crow { @@ -197,6 +198,50 @@ inline char * qs_k2v(const char * key, char * const * qs_kv, int qs_kv_size, int return NULL; } +inline boost::optional> qs_dict_name2kv(const char * dict_name, char * const * qs_kv, int qs_kv_size, int nth = 0) +{ + int i; + size_t name_len, skip_to_eq, skip_to_brace_open, skip_to_brace_close; + + name_len = strlen(dict_name); + +#ifdef _qsSORTING +// TODO: binary search for key in the sorted qs_kv +#else // _qsSORTING + for(i=0; i 0 && + skip_to_brace_close > 0 && + nth == 0 ) + { + auto key = std::string(qs_kv[i] + skip_to_brace_open, skip_to_brace_close - skip_to_brace_open); + auto value = std::string(qs_kv[i] + skip_to_eq); + return boost::make_optional(std::make_pair(key, value)); + } + else + { + --nth; + } + } + } +#endif // _qsSORTING + + return boost::none; +} + inline char * qs_scanvalue(const char * key, const char * qs, char * val, size_t val_len) { @@ -336,6 +381,21 @@ namespace crow return ret; } + std::vector> get_dict (const std::string& name) const + { + std::vector> ret; + std::string plus = name; + + int count = 0; + while(1) + { + if (auto element = qs_dict_name2kv(plus.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++)) + ret.push_back(*element); + else + break; + } + return ret; + } private: std::string url_; -- cgit v1.2.3-54-g00ecf From 777852c97d06bd93d0c5a42be0846d98df10609c Mon Sep 17 00:00:00 2001 From: "jaewoo.seo" Date: Fri, 12 May 2017 11:43:27 +0900 Subject: wvalue (json) support vector --- include/crow/json.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'include') diff --git a/include/crow/json.h b/include/crow/json.h index 891fedf..8b58180 100644 --- a/include/crow/json.h +++ b/include/crow/json.h @@ -1264,6 +1264,23 @@ namespace crow return *this; } + wvalue& operator=(std::vector&& v) + { + if (t_ != type::List) + reset(); + t_ = type::List; + if (!l) + l = std::unique_ptr>(new std::vector{}); + l->clear(); + l->resize(v.size()); + size_t idx = 0; + for(auto& x:v) + { + (*l)[idx++] = std::move(x); + } + return *this; + } + template wvalue& operator=(const std::vector& v) { -- cgit v1.2.3-54-g00ecf From 6e4e66ad1233257f6413e643cfb376d2cf298065 Mon Sep 17 00:00:00 2001 From: Jamie Bullock Date: Mon, 4 Sep 2017 16:08:02 +0100 Subject: Add support for HTTP 422 status code See: https://tools.ietf.org/html/rfc4918#section-11.2 --- amalgamate/crow_all.h | 1 + include/crow/http_connection.h | 1 + 2 files changed, 2 insertions(+) (limited to 'include') diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h index 41b06ff..26f0806 100644 --- a/amalgamate/crow_all.h +++ b/amalgamate/crow_all.h @@ -8893,6 +8893,7 @@ namespace crow {401, "HTTP/1.1 401 Unauthorized\r\n"}, {403, "HTTP/1.1 403 Forbidden\r\n"}, {404, "HTTP/1.1 404 Not Found\r\n"}, + {420, "HTTP/1.1 404 Unprocessable Entity\r\n"}, {500, "HTTP/1.1 500 Internal Server Error\r\n"}, {501, "HTTP/1.1 501 Not Implemented\r\n"}, diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index 96f2d14..3ef8089 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -374,6 +374,7 @@ namespace crow {401, "HTTP/1.1 401 Unauthorized\r\n"}, {403, "HTTP/1.1 403 Forbidden\r\n"}, {404, "HTTP/1.1 404 Not Found\r\n"}, + {422, "HTTP/1.1 422 Unprocessable Entity\r\n"}, {500, "HTTP/1.1 500 Internal Server Error\r\n"}, {501, "HTTP/1.1 501 Not Implemented\r\n"}, -- cgit v1.2.3-54-g00ecf From 9a1e4aefddef1010219f7267358b055029488def Mon Sep 17 00:00:00 2001 From: Jamie Bullock Date: Mon, 4 Sep 2017 16:38:51 +0100 Subject: Revert "Add support for HTTP 422 status code" This reverts commit 6e4e66ad1233257f6413e643cfb376d2cf298065. --- amalgamate/crow_all.h | 1 - include/crow/http_connection.h | 1 - 2 files changed, 2 deletions(-) (limited to 'include') diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h index 26f0806..41b06ff 100644 --- a/amalgamate/crow_all.h +++ b/amalgamate/crow_all.h @@ -8893,7 +8893,6 @@ namespace crow {401, "HTTP/1.1 401 Unauthorized\r\n"}, {403, "HTTP/1.1 403 Forbidden\r\n"}, {404, "HTTP/1.1 404 Not Found\r\n"}, - {420, "HTTP/1.1 404 Unprocessable Entity\r\n"}, {500, "HTTP/1.1 500 Internal Server Error\r\n"}, {501, "HTTP/1.1 501 Not Implemented\r\n"}, diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index 3ef8089..96f2d14 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -374,7 +374,6 @@ namespace crow {401, "HTTP/1.1 401 Unauthorized\r\n"}, {403, "HTTP/1.1 403 Forbidden\r\n"}, {404, "HTTP/1.1 404 Not Found\r\n"}, - {422, "HTTP/1.1 422 Unprocessable Entity\r\n"}, {500, "HTTP/1.1 500 Internal Server Error\r\n"}, {501, "HTTP/1.1 501 Not Implemented\r\n"}, -- cgit v1.2.3-54-g00ecf From 679fe4a8aca06844e3a006da9257dd6bf8a54adf Mon Sep 17 00:00:00 2001 From: Jamie Bullock Date: Mon, 4 Sep 2017 16:40:40 +0100 Subject: Add support for HTTP 422 status code See: https://tools.ietf.org/html/rfc4918#section-11.2 --- amalgamate/crow_all.h | 1 + include/crow/http_connection.h | 1 + 2 files changed, 2 insertions(+) (limited to 'include') diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h index 41b06ff..bc23f58 100644 --- a/amalgamate/crow_all.h +++ b/amalgamate/crow_all.h @@ -8893,6 +8893,7 @@ namespace crow {401, "HTTP/1.1 401 Unauthorized\r\n"}, {403, "HTTP/1.1 403 Forbidden\r\n"}, {404, "HTTP/1.1 404 Not Found\r\n"}, + {422, "HTTP/1.1 422 Unprocessable Entity\r\n"}, {500, "HTTP/1.1 500 Internal Server Error\r\n"}, {501, "HTTP/1.1 501 Not Implemented\r\n"}, diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index 96f2d14..3ef8089 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -374,6 +374,7 @@ namespace crow {401, "HTTP/1.1 401 Unauthorized\r\n"}, {403, "HTTP/1.1 403 Forbidden\r\n"}, {404, "HTTP/1.1 404 Not Found\r\n"}, + {422, "HTTP/1.1 422 Unprocessable Entity\r\n"}, {500, "HTTP/1.1 500 Internal Server Error\r\n"}, {501, "HTTP/1.1 501 Not Implemented\r\n"}, -- cgit v1.2.3-54-g00ecf From 69a17f066be71d590f1247ed5c70c2b67eff3b2d Mon Sep 17 00:00:00 2001 From: ipknHama Date: Sun, 17 Sep 2017 12:44:15 +0900 Subject: Fix #245. Trying to keep serving after an exception in the handler. --- include/crow/http_server.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/crow/http_server.h b/include/crow/http_server.h index d5abb11..a247b12 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -121,12 +121,19 @@ namespace crow timer.async_wait(handler); init_count ++; - try + while(1) { - io_service_pool_[i]->run(); - } catch(std::exception& e) - { - CROW_LOG_ERROR << "Worker Crash: An uncaught exception occurred: " << e.what(); + try + { + if (io_service_pool_[i]->run() == 0) + { + // when io_service.run returns 0, there are no more works to do. + break; + } + } catch(std::exception& e) + { + CROW_LOG_ERROR << "Worker Crash: An uncaught exception occurred: " << e.what(); + } } })); -- cgit v1.2.3-54-g00ecf From f35089b2aa80c701087b3c67c63d21893d848fa6 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Sun, 17 Sep 2017 12:52:37 +0900 Subject: Add missed header file. --- include/crow/websocket.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include') diff --git a/include/crow/websocket.h b/include/crow/websocket.h index a1e8e8f..c03547c 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include "crow/socket_adaptors.h" #include "crow/http_request.h" #include "crow/TinySHA1.hpp" -- cgit v1.2.3-54-g00ecf From 92bea9e949ff9ea9e40ef7ba038cc45cb8490180 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Sun, 17 Sep 2017 14:02:46 +0900 Subject: Change vector to unordered_map for get_dict --- include/crow/query_string.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/crow/query_string.h b/include/crow/query_string.h index 2740441..78ccc67 100644 --- a/include/crow/query_string.h +++ b/include/crow/query_string.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -220,8 +221,6 @@ inline boost::optional> qs_dict_name2kv(cons skip_to_brace_open++; skip_to_brace_close = strcspn(qs_kv[i], "]"); - std::cout << skip_to_brace_open << " FUFUU " << skip_to_brace_close << std::endl; - if ( skip_to_brace_open <= skip_to_brace_close && skip_to_brace_open > 0 && skip_to_brace_close > 0 && @@ -381,16 +380,15 @@ namespace crow return ret; } - std::vector> get_dict (const std::string& name) const + std::unordered_map get_dict (const std::string& name) const { - std::vector> ret; - std::string plus = name; + std::unordered_map ret; int count = 0; while(1) { - if (auto element = qs_dict_name2kv(plus.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++)) - ret.push_back(*element); + if (auto element = qs_dict_name2kv(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++)) + ret.insert(*element); else break; } -- cgit v1.2.3-54-g00ecf From c071f643762feb8c1fb06140ab95b93aac22e4e3 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Sun, 17 Sep 2017 14:10:15 +0900 Subject: Delete new connection with error while accepting (kwangsei, PR #205) --- include/crow/http_server.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'include') diff --git a/include/crow/http_server.h b/include/crow/http_server.h index a247b12..7e4da56 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -200,6 +200,10 @@ namespace crow p->start(); }); } + else + { + delete p; + } do_accept(); }); } -- cgit v1.2.3-54-g00ecf