diff options
Diffstat (limited to 'amalgamate')
-rw-r--r-- | amalgamate/crow_all.h | 1876 |
1 files changed, 1096 insertions, 780 deletions
diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h index 2962e7c..743eb52 100644 --- a/amalgamate/crow_all.h +++ b/amalgamate/crow_all.h @@ -1,5 +1,824 @@ #pragma once +#include <cstdint> +#include <stdexcept> +#include <tuple> +#include <type_traits> + +namespace crow +{ + namespace black_magic + { + struct OutOfRange + { + OutOfRange(unsigned pos, unsigned length) {} + }; + constexpr unsigned requires_in_range( unsigned i, unsigned len ) + { + return i >= len ? throw OutOfRange(i, len) : i; + } + + class const_str + { + const char * const begin_; + unsigned size_; + + public: + template< unsigned N > + constexpr const_str( const char(&arr)[N] ) : begin_(arr), size_(N - 1) { + static_assert( N >= 1, "not a string literal"); + } + constexpr char operator[]( unsigned i ) const { + return requires_in_range(i, size_), begin_[i]; + } + + constexpr operator const char *() const { + return begin_; + } + + constexpr const char* begin() const { return begin_; } + constexpr const char* end() const { return begin_ + size_; } + + constexpr unsigned size() const { + return size_; + } + }; + + + constexpr unsigned find_closing_tag(const_str s, unsigned p) + { + return s[p] == '>' ? p : find_closing_tag(s, p+1); + } + + constexpr bool is_valid(const_str s, unsigned i = 0, int f = 0) + { + return + i == s.size() + ? f == 0 : + f < 0 || f >= 2 + ? false : + s[i] == '<' + ? is_valid(s, i+1, f+1) : + s[i] == '>' + ? is_valid(s, i+1, f-1) : + is_valid(s, i+1, f); + } + + constexpr bool is_equ_p(const char* a, const char* b, unsigned n) + { + return + *a == 0 && *b == 0 && n == 0 + ? true : + (*a == 0 || *b == 0) + ? false : + n == 0 + ? true : + *a != *b + ? false : + is_equ_p(a+1, b+1, n-1); + } + + constexpr bool is_equ_n(const_str a, unsigned ai, const_str b, unsigned bi, unsigned n) + { + return + ai + n > a.size() || bi + n > b.size() + ? false : + n == 0 + ? true : + a[ai] != b[bi] + ? false : + is_equ_n(a,ai+1,b,bi+1,n-1); + } + + constexpr bool is_int(const_str s, unsigned i) + { + return is_equ_n(s, i, "<int>", 0, 5); + } + + constexpr bool is_uint(const_str s, unsigned i) + { + return is_equ_n(s, i, "<uint>", 0, 6); + } + + constexpr bool is_float(const_str s, unsigned i) + { + return is_equ_n(s, i, "<float>", 0, 7) || + is_equ_n(s, i, "<double>", 0, 8); + } + + constexpr bool is_str(const_str s, unsigned i) + { + return is_equ_n(s, i, "<str>", 0, 5) || + is_equ_n(s, i, "<string>", 0, 8); + } + + constexpr bool is_path(const_str s, unsigned i) + { + return is_equ_n(s, i, "<path>", 0, 6); + } + + constexpr uint64_t get_parameter_tag(const_str s, unsigned p = 0) + { + return + p == s.size() + ? 0 : + s[p] == '<' ? ( + is_int(s, p) + ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 1 : + is_uint(s, p) + ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 2 : + is_float(s, p) + ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 3 : + is_str(s, p) + ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 4 : + is_path(s, p) + ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 5 : + throw std::runtime_error("invalid parameter type") + ) : + get_parameter_tag(s, p+1); + } + + template <typename ... T> + struct S + { + template <typename U> + using push = S<U, T...>; + template <typename U> + using push_back = S<T..., U>; + template <template<typename ... Args> class U> + using rebind = U<T...>; + }; +template <typename F, typename Set> + struct CallHelper; + template <typename F, typename ...Args> + struct CallHelper<F, S<Args...>> + { + template <typename F1, typename ...Args1, typename = + decltype(std::declval<F1>()(std::declval<Args1>()...)) + > + static char __test(int); + + template <typename ...> + static int __test(...); + + static constexpr bool value = sizeof(__test<F, Args...>(0)) == sizeof(char); + }; + + + template <int N> + struct single_tag_to_type + { + }; + + template <> + struct single_tag_to_type<1> + { + using type = int64_t; + }; + + template <> + struct single_tag_to_type<2> + { + using type = uint64_t; + }; + + template <> + struct single_tag_to_type<3> + { + using type = double; + }; + + template <> + struct single_tag_to_type<4> + { + using type = std::string; + }; + + template <> + struct single_tag_to_type<5> + { + using type = std::string; + }; + + + template <uint64_t Tag> + struct arguments + { + using subarguments = typename arguments<Tag/6>::type; + using type = + typename subarguments::template push<typename single_tag_to_type<Tag%6>::type>; + }; + + template <> + struct arguments<0> + { + using type = S<>; + }; + + template <typename ... T> + struct last_element_type + { + using type = typename std::tuple_element<sizeof...(T)-1, std::tuple<T...>>::type; + }; + + + template <> + struct last_element_type<> + { + }; + + + // from http://stackoverflow.com/questions/13072359/c11-compile-time-array-with-logarithmic-evaluation-depth + template<class T> using Invoke = typename T::type; + + template<unsigned...> struct seq{ using type = seq; }; + + template<class S1, class S2> struct concat; + + template<unsigned... I1, unsigned... I2> + struct concat<seq<I1...>, seq<I2...>> + : seq<I1..., (sizeof...(I1)+I2)...>{}; + + template<class S1, class S2> + using Concat = Invoke<concat<S1, S2>>; + + template<unsigned N> struct gen_seq; + template<unsigned N> using GenSeq = Invoke<gen_seq<N>>; + + template<unsigned N> + struct gen_seq : Concat<GenSeq<N/2>, GenSeq<N - N/2>>{}; + + template<> struct gen_seq<0> : seq<>{}; + template<> struct gen_seq<1> : seq<0>{}; + + template <typename Seq, typename Tuple> + struct pop_back_helper; + + template <unsigned ... N, typename Tuple> + struct pop_back_helper<seq<N...>, Tuple> + { + template <template <typename ... Args> class U> + using rebind = U<typename std::tuple_element<N, Tuple>::type...>; + }; + + template <typename ... T> + struct pop_back //: public pop_back_helper<typename gen_seq<sizeof...(T)-1>::type, std::tuple<T...>> + { + template <template <typename ... Args> class U> + using rebind = typename pop_back_helper<typename gen_seq<sizeof...(T)-1>::type, std::tuple<T...>>::template rebind<U>; + }; + + template <> + struct pop_back<> + { + template <template <typename ... Args> class U> + using rebind = U<>; + }; + + // from http://stackoverflow.com/questions/2118541/check-if-c0x-parameter-pack-contains-a-type + template < typename Tp, typename... List > + struct contains : std::true_type {}; + + template < typename Tp, typename Head, typename... Rest > + struct contains<Tp, Head, Rest...> + : std::conditional< std::is_same<Tp, Head>::value, + std::true_type, + contains<Tp, Rest...> + >::type {}; + + template < typename Tp > + struct contains<Tp> : std::false_type {}; + + template <typename T> + struct empty_context + { + }; + } +} + + + +#pragma once +// settings for crow +// TODO - replace with runtime config. libucl? + +/* #ifdef - enables debug mode */ +#define CROW_ENABLE_DEBUG + +/* #ifdef - enables logging */ +#define CROW_ENABLE_LOGGING + +/* #define - specifies log level */ +/* + DEBUG = 0 + INFO = 1 + WARNING = 2 + ERROR = 3 + CRITICAL = 4 + + default to INFO +*/ +#define CROW_LOG_LEVEL 1 + + + +#pragma once + +#include <stdio.h> +#include <string> + +// ---------------------------------------------------------------------------- +// qs_parse (modified) +// https://github.com/bartgrantham/qs_parse +// ---------------------------------------------------------------------------- +/* Similar to strncmp, but handles URL-encoding for either string */ +int qs_strncmp(const char * s, const char * qs, register size_t n); + + +/* Finds the beginning of each key/value pair and stores a pointer in qs_kv. + * Also decodes the value portion of the k/v pair *in-place*. In a future + * enhancement it will also have a compile-time option of sorting qs_kv + * alphabetically by key. */ +int qs_parse(char * qs, char * qs_kv[], int qs_kv_size); + + +/* Used by qs_parse to decode the value portion of a k/v pair */ +int qs_decode(char * qs); + + +/* Looks up the value according to the key on a pre-processed query string + * A future enhancement will be a compile-time option to look up the key + * in a pre-sorted qs_kv array via a binary search. */ +//char * qs_k2v(const char * key, char * qs_kv[], int qs_kv_size); + char * qs_k2v(const char * key, char * const * qs_kv, int qs_kv_size, int nth); + + +/* Non-destructive lookup of value, based on key. User provides the + * destinaton string and length. */ +char * qs_scanvalue(const char * key, const char * qs, char * val, size_t val_len); + +// TODO: implement sorting of the qs_kv array; for now ensure it's not compiled +#undef _qsSORTING + +// isxdigit _is_ available in <ctype.h>, but let's avoid another header instead +#define ISHEX(x) ((((x)>='0'&&(x)<='9') || ((x)>='A'&&(x)<='F') || ((x)>='a'&&(x)<='f')) ? 1 : 0) +#define HEX2DEC(x) (((x)>='0'&&(x)<='9') ? (x)-48 : ((x)>='A'&&(x)<='F') ? (x)-55 : ((x)>='a'&&(x)<='f') ? (x)-87 : 0) +#define ISQSCHR(x) ((((x)=='=')||((x)=='#')||((x)=='&')||((x)=='\0')) ? 0 : 1) + +inline int qs_strncmp(const char * s, const char * qs, register size_t n) +{ + int i=0; + register unsigned char u1, u2, unyb, lnyb; + + while(n-- > 0) + { + u1 = (unsigned char) *s++; + u2 = (unsigned char) *qs++; + + if ( ! ISQSCHR(u1) ) { u1 = '\0'; } + if ( ! ISQSCHR(u2) ) { u2 = '\0'; } + + if ( u1 == '+' ) { u1 = ' '; } + if ( u1 == '%' ) // easier/safer than scanf + { + unyb = (unsigned char) *s++; + lnyb = (unsigned char) *s++; + if ( ISHEX(unyb) && ISHEX(lnyb) ) + u1 = (HEX2DEC(unyb) * 16) + HEX2DEC(lnyb); + else + u1 = '\0'; + } + + if ( u2 == '+' ) { u2 = ' '; } + if ( u2 == '%' ) // easier/safer than scanf + { + unyb = (unsigned char) *qs++; + lnyb = (unsigned char) *qs++; + if ( ISHEX(unyb) && ISHEX(lnyb) ) + u2 = (HEX2DEC(unyb) * 16) + HEX2DEC(lnyb); + else + u2 = '\0'; + } + + if ( u1 != u2 ) + return u1 - u2; + if ( u1 == '\0' ) + return 0; + i++; + } + if ( ISQSCHR(*qs) ) + return -1; + else + return 0; +} + + +inline int qs_parse(char * qs, char * qs_kv[], int qs_kv_size) +{ + int i, j; + char * substr_ptr; + + for(i=0; i<qs_kv_size; i++) qs_kv[i] = NULL; + + // find the beginning of the k/v substrings + if ( (substr_ptr = strchr(qs, '?')) != NULL ) + substr_ptr++; + else + substr_ptr = qs; + + i=0; + while(i<qs_kv_size) + { + qs_kv[i] = substr_ptr; + j = strcspn(substr_ptr, "&"); + if ( substr_ptr[j] == '\0' ) { break; } + substr_ptr += j + 1; + i++; + } + i++; // x &'s -> means x iterations of this loop -> means *x+1* k/v pairs + + // we only decode the values in place, the keys could have '='s in them + // which will hose our ability to distinguish keys from values later + for(j=0; j<i; j++) + { + substr_ptr = qs_kv[j] + strcspn(qs_kv[j], "=&#"); + if ( substr_ptr[0] == '&' ) // blank value: skip decoding + substr_ptr[0] = '\0'; + else + qs_decode(++substr_ptr); + } + +#ifdef _qsSORTING +// TODO: qsort qs_kv, using qs_strncmp() for the comparison +#endif + + return i; +} + + +inline int qs_decode(char * qs) +{ + int i=0, j=0; + + while( ISQSCHR(qs[j]) ) + { + if ( qs[j] == '+' ) { qs[i] = ' '; } + else if ( qs[j] == '%' ) // easier/safer than scanf + { + if ( ! ISHEX(qs[j+1]) || ! ISHEX(qs[j+2]) ) + { + qs[i] = '\0'; + return i; + } + qs[i] = (HEX2DEC(qs[j+1]) * 16) + HEX2DEC(qs[j+2]); + j+=2; + } + else + { + qs[i] = qs[j]; + } + i++; j++; + } + qs[i] = '\0'; + + return i; +} + + +inline char * qs_k2v(const char * key, char * const * qs_kv, int qs_kv_size, int nth = 0) +{ + int i; + size_t key_len, skip; + + key_len = strlen(key); + +#ifdef _qsSORTING +// TODO: binary search for key in the sorted qs_kv +#else // _qsSORTING + for(i=0; i<qs_kv_size; i++) + { + // we rely on the unambiguous '=' to find the value in our k/v pair + if ( qs_strncmp(key, qs_kv[i], key_len) == 0 ) + { + skip = strcspn(qs_kv[i], "="); + if ( qs_kv[i][skip] == '=' ) + skip++; + // return (zero-char value) ? ptr to trailing '\0' : ptr to value + if(nth == 0) + return qs_kv[i] + skip; + else + --nth; + } + } +#endif // _qsSORTING + + return NULL; +} + + +inline char * qs_scanvalue(const char * key, const char * qs, char * val, size_t val_len) +{ + size_t i, key_len; + const char * tmp; + + // find the beginning of the k/v substrings + if ( (tmp = strchr(qs, '?')) != NULL ) + qs = tmp + 1; + + key_len = strlen(key); + while(qs[0] != '#' && qs[0] != '\0') + { + if ( qs_strncmp(key, qs, key_len) == 0 ) + break; + qs += strcspn(qs, "&") + 1; + } + + if ( qs[0] == '\0' ) return NULL; + + qs += strcspn(qs, "=&#"); + if ( qs[0] == '=' ) + { + qs++; + i = strcspn(qs, "&=#"); + strncpy(val, qs, (val_len-1)<(i+1) ? (val_len-1) : (i+1)); + qs_decode(val); + } + else + { + if ( val_len > 0 ) + val[0] = '\0'; + } + + return val; +} +// ---------------------------------------------------------------------------- + +// TODO to save allocs, capping url size to 2048 seems sane and reasonable but +// crow should *technically* return a 413 if a URL is longer than this. +#define MAX_URL_SIZE (2048) + +#define NUM_KV_PAIRS (256) +#define VAL_SIZE (256) + +namespace crow +{ + class query_string + { + public: + query_string() + { + + } + query_string(std::string url) + { + if(url.length() <= MAX_URL_SIZE) { + memset(_url, 0, MAX_URL_SIZE); // overkill? + memcpy(_url, url.c_str(), url.length()); + } + _kv_size = qs_parse(_url, _kv_pairs, NUM_KV_PAIRS); + } + void clear() { + _url[0] = 0; + } + + friend std::ostream& operator<<(std::ostream& os, const query_string& qs) + { + os << "[ "; + for(int i = 0; i < qs._kv_size; ++i) { + os << qs._kv_pairs[i]; + if((i + 1) < qs._kv_size) { + os << ", "; + } + } + os << " ]"; + return os; + + } + + char* get (const std::string name) const + { + char* ret = qs_k2v(name.c_str(), _kv_pairs, _kv_size); + return ret != 0 ? ret : nullptr; + } + + std::vector<char*> get_list (const std::string name) const + { + std::vector<char*> ret; + std::string plus = name + "[]"; + char* tmp = nullptr; + int count = 0; + do + { + tmp = qs_k2v(plus.c_str(), _kv_pairs, _kv_size, count++); + if(tmp != nullptr) { + ret.push_back(tmp); + } + } while(tmp != nullptr); + return move(ret); + } + + + private: + char _url[MAX_URL_SIZE]; + char* _kv_pairs[NUM_KV_PAIRS]; + int _kv_size; + }; + +} // end namespace + + +#pragma once + + + + +namespace crow +{ + namespace detail + { + template <typename ... Middlewares> + struct partial_context + : public black_magic::pop_back<Middlewares...>::template rebind<partial_context> + , public black_magic::last_element_type<Middlewares...>::type::context + { + using parent_context = typename black_magic::pop_back<Middlewares...>::template rebind<::crow::detail::partial_context>; + template <int N> + using partial = typename std::conditional<N == sizeof...(Middlewares)-1, partial_context, typename parent_context::template partial<N>>::type; + + template <typename T> + typename T::context& get() + { + return static_cast<typename T::context&>(*this); + } + }; + + template <> + struct partial_context<> + { + template <int> + using partial = partial_context; + }; + + template <int N, typename Context, typename Container, typename CurrentMW, typename ... Middlewares> + bool middleware_call_helper(Container& middlewares, request& req, response& res, Context& ctx); + + template <typename ... Middlewares> + struct context : private partial_context<Middlewares...> + //struct context : private Middlewares::context... // simple but less type-safe + { + template <int N, typename Context, typename Container> + friend typename std::enable_if<(N==0)>::type after_handlers_call_helper(Container& middlewares, Context& ctx, request& req, response& res); + template <int N, typename Context, typename Container> + friend typename std::enable_if<(N>0)>::type after_handlers_call_helper(Container& middlewares, Context& ctx, request& req, response& res); + + template <int N, typename Context, typename Container, typename CurrentMW, typename ... Middlewares2> + friend bool middleware_call_helper(Container& middlewares, request& req, response& res, Context& ctx); + + template <typename T> + typename T::context& get() + { + return static_cast<typename T::context&>(*this); + } + + template <int N> + using partial = typename partial_context<Middlewares...>::template partial<N>; + }; + } +} + + + +#pragma once + +#include <string> +#include <cstdio> +#include <cstdlib> +#include <ctime> +#include <iostream> +#include <sstream> + + + + +namespace crow +{ + enum class LogLevel + { + DEBUG, + INFO, + WARNING, + ERROR, + CRITICAL, + }; + + class ILogHandler { + public: + virtual void log(std::string message, LogLevel level) = 0; + }; + + class CerrLogHandler : public ILogHandler { + public: + void log(std::string message, LogLevel level) override { + std::cerr << message; + } + }; + + class logger { + + private: + // + static std::string timestamp() + { + char date[32]; + time_t t = time(0); + strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", gmtime(&t)); + return std::string(date); + } + + public: + + + logger(std::string prefix, LogLevel level) : level_(level) { + #ifdef CROW_ENABLE_LOGGING + stringstream_ << "(" << timestamp() << ") [" << prefix << "] "; + #endif + + } + ~logger() { + #ifdef CROW_ENABLE_LOGGING + if(level_ >= get_current_log_level()) { + stringstream_ << std::endl; + get_handler_ref()->log(stringstream_.str(), level_); + } + #endif + } + + // + template <typename T> + logger& operator<<(T const &value) { + + #ifdef CROW_ENABLE_LOGGING + if(level_ >= get_current_log_level()) { + stringstream_ << value; + } + #endif + return *this; + } + + // + static void setLogLevel(LogLevel level) { + get_log_level_ref() = level; + } + + static void setHandler(ILogHandler* handler) { + get_handler_ref() = handler; + } + + static LogLevel get_current_log_level() { + return get_log_level_ref(); + } + + private: + // + static LogLevel& get_log_level_ref() + { + static LogLevel current_level = (LogLevel)CROW_LOG_LEVEL; + return current_level; + } + static ILogHandler*& get_handler_ref() + { + static CerrLogHandler default_handler; + static ILogHandler* current_handler = &default_handler; + return current_handler; + } + + // + std::ostringstream stringstream_; + LogLevel level_; + }; +} + +#define CROW_LOG_CRITICAL \ + if (crow::logger::get_current_log_level() <= crow::LogLevel::CRITICAL) \ + crow::logger("CRITICAL", crow::LogLevel::CRITICAL) +#define CROW_LOG_ERROR \ + if (crow::logger::get_current_log_level() <= crow::LogLevel::ERROR) \ + crow::logger("ERROR ", crow::LogLevel::ERROR) +#define CROW_LOG_WARNING \ + if (crow::logger::get_current_log_level() <= crow::LogLevel::WARNING) \ + crow::logger("WARNING ", crow::LogLevel::WARNING) +#define CROW_LOG_INFO \ + if (crow::logger::get_current_log_level() <= crow::LogLevel::INFO) \ + crow::logger("INFO ", crow::LogLevel::INFO) +#define CROW_LOG_DEBUG \ + if (crow::logger::get_current_log_level() <= crow::LogLevel::DEBUG) \ + crow::logger("DEBUG ", crow::LogLevel::DEBUG) + + + + +#pragma once + //#define CROW_JSON_NO_ERROR_CHECK #include <string> @@ -4586,276 +5405,6 @@ http_parser_version(void) { #pragma once -#include <boost/functional/hash.hpp> - -namespace crow -{ - struct ci_hash - { - size_t operator()(const std::string& key) const - { - std::size_t seed = 0; - std::locale locale; - - for(auto c : key) - { - boost::hash_combine(seed, std::toupper(c, locale)); - } - - return seed; - } - }; - - struct ci_key_eq - { - bool operator()(const std::string& l, const std::string& r) const - { - return boost::iequals(l, r); - } - }; - - using ci_map = std::unordered_multimap<std::string, std::string, ci_hash, ci_key_eq>; -} - - - -#pragma once - -#include <string> -#include <boost/date_time/local_time/local_time.hpp> -#include <boost/filesystem.hpp> - -namespace crow -{ - // code from http://stackoverflow.com/questions/2838524/use-boost-date-time-to-parse-and-create-http-dates - class DateTime - { - public: - DateTime() - : m_dt(boost::local_time::local_sec_clock::local_time(boost::local_time::time_zone_ptr())) - { - } - DateTime(const std::string& path) - : DateTime() - { - from_file(path); - } - - // return datetime string - std::string str() - { - static const std::locale locale_(std::locale::classic(), new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT") ); - std::string result; - try - { - std::stringstream ss; - ss.imbue(locale_); - ss << m_dt; - result = ss.str(); - } - catch (std::exception& e) - { - std::cerr << "Exception: " << e.what() << std::endl; - } - return result; - } - - // update datetime from file mod date - std::string from_file(const std::string& path) - { - try - { - boost::filesystem::path p(path); - boost::posix_time::ptime pt = boost::posix_time::from_time_t( - boost::filesystem::last_write_time(p)); - m_dt = boost::local_time::local_date_time(pt, boost::local_time::time_zone_ptr()); - } - catch (std::exception& e) - { - std::cout << "Exception: " << e.what() << std::endl; - } - return str(); - } - - // parse datetime string - void parse(const std::string& dt) - { - static const std::locale locale_(std::locale::classic(), new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT") ); - std::stringstream ss(dt); - ss.imbue(locale_); - ss >> m_dt; - } - - // boolean equal operator - friend bool operator==(const DateTime& left, const DateTime& right) - { - return (left.m_dt == right.m_dt); - } - - private: - boost::local_time::local_date_time m_dt; - }; -} - - - -#pragma once -// settings for crow -// TODO - replace with runtime config. libucl? - -/* #ifdef - enables debug mode */ -#define CROW_ENABLE_DEBUG - -/* #ifdef - enables logging */ -#define CROW_ENABLE_LOGGING - -/* #define - specifies log level */ -/* - DEBUG = 0 - INFO = 1 - WARNING = 2 - ERROR = 3 - CRITICAL = 4 - - default to INFO -*/ -#define CROW_LOG_LEVEL 1 - - - -#pragma once - -#include <string> -#include <cstdio> -#include <cstdlib> -#include <ctime> -#include <iostream> -#include <sstream> - - - - -using namespace std; - -namespace crow -{ - enum class LogLevel - { - DEBUG, - INFO, - WARNING, - ERROR, - CRITICAL, - }; - - class ILogHandler { - public: - virtual void log(string message, LogLevel level) = 0; - }; - - class CerrLogHandler : public ILogHandler { - public: - void log(string message, LogLevel level) override { - cerr << message; - } - }; - - class logger { - - private: - // - static string timestamp() - { - char date[32]; - time_t t = time(0); - strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", gmtime(&t)); - return string(date); - } - - public: - - - logger(string prefix, LogLevel level) : level_(level) { - #ifdef CROW_ENABLE_LOGGING - stringstream_ << "(" << timestamp() << ") [" << prefix << "] "; - #endif - - } - ~logger() { - #ifdef CROW_ENABLE_LOGGING - if(level_ >= get_current_log_level()) { - stringstream_ << endl; - get_handler_ref()->log(stringstream_.str(), level_); - } - #endif - } - - // - template <typename T> - logger& operator<<(T const &value) { - - #ifdef CROW_ENABLE_LOGGING - if(level_ >= get_current_log_level()) { - stringstream_ << value; - } - #endif - return *this; - } - - // - static void setLogLevel(LogLevel level) { - get_log_level_ref() = level; - } - - static void setHandler(ILogHandler* handler) { - get_handler_ref() = handler; - } - - static LogLevel get_current_log_level() { - return get_log_level_ref(); - } - - private: - // - static LogLevel& get_log_level_ref() - { - static LogLevel current_level = (LogLevel)CROW_LOG_LEVEL; - return current_level; - } - static ILogHandler*& get_handler_ref() - { - static CerrLogHandler default_handler; - static ILogHandler* current_handler = &default_handler; - return current_handler; - } - - // - ostringstream stringstream_; - LogLevel level_; - }; -} - -#define CROW_LOG_CRITICAL \ - if (crow::logger::get_current_log_level() <= crow::LogLevel::CRITICAL) \ - crow::logger("CRITICAL", crow::LogLevel::CRITICAL) -#define CROW_LOG_ERROR \ - if (crow::logger::get_current_log_level() <= crow::LogLevel::ERROR) \ - crow::logger("ERROR ", crow::LogLevel::ERROR) -#define CROW_LOG_WARNING \ - if (crow::logger::get_current_log_level() <= crow::LogLevel::WARNING) \ - crow::logger("WARNING ", crow::LogLevel::WARNING) -#define CROW_LOG_INFO \ - if (crow::logger::get_current_log_level() <= crow::LogLevel::INFO) \ - crow::logger("INFO ", crow::LogLevel::INFO) -#define CROW_LOG_DEBUG \ - if (crow::logger::get_current_log_level() <= crow::LogLevel::DEBUG) \ - crow::logger("DEBUG ", crow::LogLevel::DEBUG) - - - - -#pragma once - #include <boost/asio.hpp> #include <deque> #include <functional> @@ -4947,300 +5496,80 @@ namespace crow #pragma once -#include <cstdint> -#include <stdexcept> -#include <tuple> -#include <type_traits> +#include <string> +#include <boost/date_time/local_time/local_time.hpp> +#include <boost/filesystem.hpp> namespace crow { - namespace black_magic - { - struct OutOfRange - { - OutOfRange(unsigned pos, unsigned length) {} - }; - constexpr unsigned requires_in_range( unsigned i, unsigned len ) - { - return i >= len ? throw OutOfRange(i, len) : i; - } - - class const_str - { - const char * const begin_; - unsigned size_; - - public: - template< unsigned N > - constexpr const_str( const char(&arr)[N] ) : begin_(arr), size_(N - 1) { - static_assert( N >= 1, "not a string literal"); - } - constexpr char operator[]( unsigned i ) const { - return requires_in_range(i, size_), begin_[i]; - } - - constexpr operator const char *() const { - return begin_; - } - - constexpr const char* begin() const { return begin_; } - constexpr const char* end() const { return begin_ + size_; } - - constexpr unsigned size() const { - return size_; - } - }; - - - constexpr unsigned find_closing_tag(const_str s, unsigned p) - { - return s[p] == '>' ? p : find_closing_tag(s, p+1); - } - - constexpr bool is_valid(const_str s, unsigned i = 0, int f = 0) - { - return - i == s.size() - ? f == 0 : - f < 0 || f >= 2 - ? false : - s[i] == '<' - ? is_valid(s, i+1, f+1) : - s[i] == '>' - ? is_valid(s, i+1, f-1) : - is_valid(s, i+1, f); - } - - constexpr bool is_equ_p(const char* a, const char* b, unsigned n) - { - return - *a == 0 && *b == 0 && n == 0 - ? true : - (*a == 0 || *b == 0) - ? false : - n == 0 - ? true : - *a != *b - ? false : - is_equ_p(a+1, b+1, n-1); - } - - constexpr bool is_equ_n(const_str a, unsigned ai, const_str b, unsigned bi, unsigned n) - { - return - ai + n > a.size() || bi + n > b.size() - ? false : - n == 0 - ? true : - a[ai] != b[bi] - ? false : - is_equ_n(a,ai+1,b,bi+1,n-1); - } - - constexpr bool is_int(const_str s, unsigned i) - { - return is_equ_n(s, i, "<int>", 0, 5); - } - - constexpr bool is_uint(const_str s, unsigned i) - { - return is_equ_n(s, i, "<uint>", 0, 6); - } - - constexpr bool is_float(const_str s, unsigned i) - { - return is_equ_n(s, i, "<float>", 0, 7) || - is_equ_n(s, i, "<double>", 0, 8); - } - - constexpr bool is_str(const_str s, unsigned i) - { - return is_equ_n(s, i, "<str>", 0, 5) || - is_equ_n(s, i, "<string>", 0, 8); - } - - constexpr bool is_path(const_str s, unsigned i) - { - return is_equ_n(s, i, "<path>", 0, 6); - } - - constexpr uint64_t get_parameter_tag(const_str s, unsigned p = 0) - { - return - p == s.size() - ? 0 : - s[p] == '<' ? ( - is_int(s, p) - ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 1 : - is_uint(s, p) - ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 2 : - is_float(s, p) - ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 3 : - is_str(s, p) - ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 4 : - is_path(s, p) - ? get_parameter_tag(s, find_closing_tag(s, p)) * 6 + 5 : - throw std::runtime_error("invalid parameter type") - ) : - get_parameter_tag(s, p+1); - } - - template <typename ... T> - struct S - { - template <typename U> - using push = S<U, T...>; - template <typename U> - using push_back = S<T..., U>; - template <template<typename ... Args> class U> - using rebind = U<T...>; - }; -template <typename F, typename Set> - struct CallHelper; - template <typename F, typename ...Args> - struct CallHelper<F, S<Args...>> - { - template <typename F1, typename ...Args1, typename = - decltype(std::declval<F1>()(std::declval<Args1>()...)) - > - static char __test(int); - - template <typename ...> - static int __test(...); - - static constexpr bool value = sizeof(__test<F, Args...>(0)) == sizeof(char); - }; - - - template <int N> - struct single_tag_to_type - { - }; - - template <> - struct single_tag_to_type<1> - { - using type = int64_t; - }; - - template <> - struct single_tag_to_type<2> - { - using type = uint64_t; - }; - - template <> - struct single_tag_to_type<3> - { - using type = double; - }; - - template <> - struct single_tag_to_type<4> - { - using type = std::string; - }; - - template <> - struct single_tag_to_type<5> - { - using type = std::string; - }; - - - template <uint64_t Tag> - struct arguments - { - using subarguments = typename arguments<Tag/6>::type; - using type = - typename subarguments::template push<typename single_tag_to_type<Tag%6>::type>; - }; - - template <> - struct arguments<0> - { - using type = S<>; - }; - - template <typename ... T> - struct last_element_type - { - using type = typename std::tuple_element<sizeof...(T)-1, std::tuple<T...>>::type; - }; - - - template <> - struct last_element_type<> - { - }; - - - // from http://stackoverflow.com/questions/13072359/c11-compile-time-array-with-logarithmic-evaluation-depth - template<class T> using Invoke = typename T::type; - - template<unsigned...> struct seq{ using type = seq; }; - - template<class S1, class S2> struct concat; - - template<unsigned... I1, unsigned... I2> - struct concat<seq<I1...>, seq<I2...>> - : seq<I1..., (sizeof...(I1)+I2)...>{}; - - template<class S1, class S2> - using Concat = Invoke<concat<S1, S2>>; - - template<unsigned N> struct gen_seq; - template<unsigned N> using GenSeq = Invoke<gen_seq<N>>; - - template<unsigned N> - struct gen_seq : Concat<GenSeq<N/2>, GenSeq<N - N/2>>{}; - - template<> struct gen_seq<0> : seq<>{}; - template<> struct gen_seq<1> : seq<0>{}; - - template <typename Seq, typename Tuple> - struct pop_back_helper; - - template <unsigned ... N, typename Tuple> - struct pop_back_helper<seq<N...>, Tuple> - { - template <template <typename ... Args> class U> - using rebind = U<typename std::tuple_element<N, Tuple>::type...>; - }; - - template <typename ... T> - struct pop_back //: public pop_back_helper<typename gen_seq<sizeof...(T)-1>::type, std::tuple<T...>> - { - template <template <typename ... Args> class U> - using rebind = typename pop_back_helper<typename gen_seq<sizeof...(T)-1>::type, std::tuple<T...>>::template rebind<U>; - }; + // code from http://stackoverflow.com/questions/2838524/use-boost-date-time-to-parse-and-create-http-dates + class DateTime + { + public: + DateTime() + : m_dt(boost::local_time::local_sec_clock::local_time(boost::local_time::time_zone_ptr())) + { + } + DateTime(const std::string& path) + : DateTime() + { + from_file(path); + } - template <> - struct pop_back<> - { - template <template <typename ... Args> class U> - using rebind = U<>; - }; + // return datetime string + std::string str() + { + static const std::locale locale_(std::locale::classic(), new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT") ); + std::string result; + try + { + std::stringstream ss; + ss.imbue(locale_); + ss << m_dt; + result = ss.str(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << std::endl; + } + return result; + } - // from http://stackoverflow.com/questions/2118541/check-if-c0x-parameter-pack-contains-a-type - template < typename Tp, typename... List > - struct contains : std::true_type {}; + // update datetime from file mod date + std::string from_file(const std::string& path) + { + try + { + boost::filesystem::path p(path); + boost::posix_time::ptime pt = boost::posix_time::from_time_t( + boost::filesystem::last_write_time(p)); + m_dt = boost::local_time::local_date_time(pt, boost::local_time::time_zone_ptr()); + } + catch (std::exception& e) + { + std::cout << "Exception: " << e.what() << std::endl; + } + return str(); + } - template < typename Tp, typename Head, typename... Rest > - struct contains<Tp, Head, Rest...> - : std::conditional< std::is_same<Tp, Head>::value, - std::true_type, - contains<Tp, Rest...> - >::type {}; + // parse datetime string + void parse(const std::string& dt) + { + static const std::locale locale_(std::locale::classic(), new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT") ); + std::stringstream ss(dt); + ss.imbue(locale_); + ss >> m_dt; + } - template < typename Tp > - struct contains<Tp> : std::false_type {}; + // boolean equal operator + friend bool operator==(const DateTime& left, const DateTime& right) + { + return (left.m_dt == right.m_dt); + } - template <typename T> - struct empty_context - { - }; - } + private: + boost::local_time::local_date_time m_dt; + }; } @@ -5374,6 +5703,43 @@ constexpr crow::HTTPMethod operator "" _method(const char* str, size_t len) #pragma once +#include <boost/functional/hash.hpp> + +namespace crow +{ + struct ci_hash + { + size_t operator()(const std::string& key) const + { + std::size_t seed = 0; + std::locale locale; + + for(auto c : key) + { + boost::hash_combine(seed, std::toupper(c, locale)); + } + + return seed; + } + }; + + struct ci_key_eq + { + bool operator()(const std::string& l, const std::string& r) const + { + return boost::iequals(l, r); + } + }; + + using ci_map = std::unordered_multimap<std::string, std::string, ci_hash, ci_key_eq>; +} + + + +#pragma once + + + @@ -5395,7 +5761,9 @@ namespace crow struct request { HTTPMethod method; + std::string raw_url; std::string url; + query_string url_params; ci_map headers; std::string body; @@ -5406,8 +5774,8 @@ namespace crow { } - request(HTTPMethod method, std::string url, ci_map headers, std::string body) - : method(method), url(std::move(url)), headers(std::move(headers)), body(std::move(body)) + request(HTTPMethod method, std::string raw_url, std::string url, query_string url_params, ci_map headers, std::string body) + : method(method), raw_url(std::move(raw_url)), url(std::move(url)), url_params(std::move(url_params)), headers(std::move(headers)), body(std::move(body)) { } @@ -5431,6 +5799,8 @@ namespace crow #include <string> #include <unordered_map> #include <boost/algorithm/string.hpp> +#include <boost/tokenizer.hpp> +#include <algorithm> @@ -5449,7 +5819,7 @@ namespace crow static int on_url(http_parser* self_, const char* at, size_t length) { HTTPParser* self = static_cast<HTTPParser*>(self_); - self->url.insert(self->url.end(), at, at+length); + self->raw_url.insert(self->raw_url.end(), at, at+length); return 0; } static int on_header_field(http_parser* self_, const char* at, size_t length) @@ -5505,6 +5875,11 @@ namespace crow static int on_message_complete(http_parser* self_) { HTTPParser* self = static_cast<HTTPParser*>(self_); + + // url params + self->url = self->raw_url.substr(0, self->raw_url.find("?")); + self->url_params = query_string(self->raw_url); + self->process_message(); return 0; } @@ -5540,10 +5915,12 @@ namespace crow void clear() { url.clear(); + raw_url.clear(); header_building_state = 0; header_field.clear(); header_value.clear(); headers.clear(); + url_params.clear(); body.clear(); } @@ -5559,7 +5936,7 @@ namespace crow request to_request() const { - return request{(HTTPMethod)method, std::move(url), std::move(headers), std::move(body)}; + return request{(HTTPMethod)method, std::move(raw_url), std::move(url), std::move(url_params), std::move(headers), std::move(body)}; } bool check_version(int major, int minor) const @@ -5567,11 +5944,14 @@ namespace crow return http_major == major && http_minor == minor; } + std::string raw_url; std::string url; + int header_building_state = 0; std::string header_field; std::string header_value; ci_map headers; + query_string url_params; std::string body; Handler* handler_; @@ -5699,185 +6079,6 @@ namespace crow #pragma once -#include <boost/algorithm/string/trim.hpp> - - - - - -namespace crow -{ - // Any middleware requires following 3 members: - - // struct context; - // storing data for the middleware; can be read from another middleware or handlers - - // before_handle - // called before handling the request. - // if res.end() is called, the operation is halted. - // (still call after_handle of this middleware) - // 2 signatures: - // void before_handle(request& req, response& res, context& ctx) - // if you only need to access this middlewares context. - // template <typename AllContext> - // void before_handle(request& req, response& res, context& ctx, AllContext& all_ctx) - // you can access another middlewares' context by calling `all_ctx.template get<MW>()' - // ctx == all_ctx.template get<CurrentMiddleware>() - - // after_handle - // called after handling the request. - // void after_handle(request& req, response& res, context& ctx) - // template <typename AllContext> - // void after_handle(request& req, response& res, context& ctx, AllContext& all_ctx) - - struct CookieParser - { - struct context - { - std::unordered_map<std::string, std::string> jar; - std::unordered_map<std::string, std::string> cookies_to_add; - - std::string get_cookie(const std::string& key) - { - if (jar.count(key)) - return jar[key]; - return {}; - } - - void set_cookie(const std::string& key, const std::string& value) - { - cookies_to_add.emplace(key, value); - } - }; - - void before_handle(request& req, response& res, context& ctx) - { - int count = req.headers.count("Cookie"); - if (!count) - return; - if (count > 1) - { - res.code = 400; - res.end(); - return; - } - std::string cookies = req.get_header_value("Cookie"); - size_t pos = 0; - while(pos < cookies.size()) - { - size_t pos_equal = cookies.find('=', pos); - if (pos_equal == cookies.npos) - break; - std::string name = cookies.substr(pos, pos_equal-pos); - boost::trim(name); - pos = pos_equal+1; - while(pos < cookies.size() && cookies[pos] == ' ') pos++; - if (pos == cookies.size()) - break; - - std::string value; - - if (cookies[pos] == '"') - { - int dquote_meet_count = 0; - pos ++; - size_t pos_dquote = pos-1; - do - { - pos_dquote = cookies.find('"', pos_dquote+1); - dquote_meet_count ++; - } while(pos_dquote < cookies.size() && cookies[pos_dquote-1] == '\\'); - if (pos_dquote == cookies.npos) - break; - - if (dquote_meet_count == 1) - value = cookies.substr(pos, pos_dquote - pos); - else - { - value.clear(); - value.reserve(pos_dquote-pos); - for(size_t p = pos; p < pos_dquote; p++) - { - // FIXME minimal escaping - if (cookies[p] == '\\' && p + 1 < pos_dquote) - { - p++; - if (cookies[p] == '\\' || cookies[p] == '"') - value += cookies[p]; - else - { - value += '\\'; - value += cookies[p]; - } - } - else - value += cookies[p]; - } - } - - ctx.jar.emplace(std::move(name), std::move(value)); - pos = cookies.find(";", pos_dquote+1); - if (pos == cookies.npos) - break; - pos++; - while(pos < cookies.size() && cookies[pos] == ' ') pos++; - if (pos == cookies.size()) - break; - } - else - { - size_t pos_semicolon = cookies.find(';', pos); - value = cookies.substr(pos, pos_semicolon - pos); - boost::trim(value); - ctx.jar.emplace(std::move(name), std::move(value)); - pos = pos_semicolon; - if (pos == cookies.npos) - break; - pos ++; - while(pos < cookies.size() && cookies[pos] == ' ') pos++; - if (pos == cookies.size()) - break; - } - } - } - - void after_handle(request& req, response& res, context& ctx) - { - for(auto& cookie:ctx.cookies_to_add) - { - res.add_header("Set-Cookie", cookie.first + "=" + cookie.second); - } - } - }; - - /* - App<CookieParser, AnotherJarMW> app; - A B C - A::context - int aa; - - ctx1 : public A::context - ctx2 : public ctx1, public B::context - ctx3 : public ctx2, public C::context - - C depends on A - - C::handle - context.aaa - - App::context : private CookieParser::contetx, ... - { - jar - - } - - SimpleApp - */ -} - - - -#pragma once #include <cstdint> #include <utility> @@ -6513,16 +6714,13 @@ public: void handle(const request& req, response& res) { - // remove url params - auto editedUrl = req.url.substr(0, req.url.find("?")); - - auto found = trie_.find(editedUrl); + auto found = trie_.find(req.url); unsigned rule_index = found.first; if (!rule_index) { - CROW_LOG_DEBUG << "Cannot match rules " << editedUrl; + CROW_LOG_DEBUG << "Cannot match rules " << req.url; res = response(404); res.end(); return; @@ -6533,7 +6731,7 @@ public: 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(); + 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; @@ -6558,62 +6756,180 @@ public: #pragma once +#include <boost/algorithm/string/trim.hpp> + namespace crow { - namespace detail + // Any middleware requires following 3 members: + + // struct context; + // storing data for the middleware; can be read from another middleware or handlers + + // before_handle + // called before handling the request. + // if res.end() is called, the operation is halted. + // (still call after_handle of this middleware) + // 2 signatures: + // void before_handle(request& req, response& res, context& ctx) + // if you only need to access this middlewares context. + // template <typename AllContext> + // void before_handle(request& req, response& res, context& ctx, AllContext& all_ctx) + // you can access another middlewares' context by calling `all_ctx.template get<MW>()' + // ctx == all_ctx.template get<CurrentMiddleware>() + + // after_handle + // called after handling the request. + // void after_handle(request& req, response& res, context& ctx) + // template <typename AllContext> + // void after_handle(request& req, response& res, context& ctx, AllContext& all_ctx) + + struct CookieParser { - template <typename ... Middlewares> - struct partial_context - : public black_magic::pop_back<Middlewares...>::template rebind<partial_context> - , public black_magic::last_element_type<Middlewares...>::type::context + struct context { - using parent_context = typename black_magic::pop_back<Middlewares...>::template rebind<::crow::detail::partial_context>; - template <int N> - using partial = typename std::conditional<N == sizeof...(Middlewares)-1, partial_context, typename parent_context::template partial<N>>::type; + std::unordered_map<std::string, std::string> jar; + std::unordered_map<std::string, std::string> cookies_to_add; - template <typename T> - typename T::context& get() + std::string get_cookie(const std::string& key) { - return static_cast<typename T::context&>(*this); + if (jar.count(key)) + return jar[key]; + return {}; + } + + void set_cookie(const std::string& key, const std::string& value) + { + cookies_to_add.emplace(key, value); } }; - template <> - struct partial_context<> + void before_handle(request& req, response& res, context& ctx) { - template <int> - using partial = partial_context; - }; + int count = req.headers.count("Cookie"); + if (!count) + return; + if (count > 1) + { + res.code = 400; + res.end(); + return; + } + std::string cookies = req.get_header_value("Cookie"); + size_t pos = 0; + while(pos < cookies.size()) + { + size_t pos_equal = cookies.find('=', pos); + if (pos_equal == cookies.npos) + break; + std::string name = cookies.substr(pos, pos_equal-pos); + boost::trim(name); + pos = pos_equal+1; + while(pos < cookies.size() && cookies[pos] == ' ') pos++; + if (pos == cookies.size()) + break; - template <int N, typename Context, typename Container, typename CurrentMW, typename ... Middlewares> - bool middleware_call_helper(Container& middlewares, request& req, response& res, Context& ctx); + std::string value; - template <typename ... Middlewares> - struct context : private partial_context<Middlewares...> - //struct context : private Middlewares::context... // simple but less type-safe - { - template <int N, typename Context, typename Container> - friend typename std::enable_if<(N==0)>::type after_handlers_call_helper(Container& middlewares, Context& ctx, request& req, response& res); - template <int N, typename Context, typename Container> - friend typename std::enable_if<(N>0)>::type after_handlers_call_helper(Container& middlewares, Context& ctx, request& req, response& res); + if (cookies[pos] == '"') + { + int dquote_meet_count = 0; + pos ++; + size_t pos_dquote = pos-1; + do + { + pos_dquote = cookies.find('"', pos_dquote+1); + dquote_meet_count ++; + } while(pos_dquote < cookies.size() && cookies[pos_dquote-1] == '\\'); + if (pos_dquote == cookies.npos) + break; - template <int N, typename Context, typename Container, typename CurrentMW, typename ... Middlewares2> - friend bool middleware_call_helper(Container& middlewares, request& req, response& res, Context& ctx); + if (dquote_meet_count == 1) + value = cookies.substr(pos, pos_dquote - pos); + else + { + value.clear(); + value.reserve(pos_dquote-pos); + for(size_t p = pos; p < pos_dquote; p++) + { + // FIXME minimal escaping + if (cookies[p] == '\\' && p + 1 < pos_dquote) + { + p++; + if (cookies[p] == '\\' || cookies[p] == '"') + value += cookies[p]; + else + { + value += '\\'; + value += cookies[p]; + } + } + else + value += cookies[p]; + } + } - template <typename T> - typename T::context& get() + ctx.jar.emplace(std::move(name), std::move(value)); + pos = cookies.find(";", pos_dquote+1); + if (pos == cookies.npos) + break; + pos++; + while(pos < cookies.size() && cookies[pos] == ' ') pos++; + if (pos == cookies.size()) + break; + } + else + { + size_t pos_semicolon = cookies.find(';', pos); + value = cookies.substr(pos, pos_semicolon - pos); + boost::trim(value); + ctx.jar.emplace(std::move(name), std::move(value)); + pos = pos_semicolon; + if (pos == cookies.npos) + break; + pos ++; + while(pos < cookies.size() && cookies[pos] == ' ') pos++; + if (pos == cookies.size()) + break; + } + } + } + + void after_handle(request& req, response& res, context& ctx) + { + for(auto& cookie:ctx.cookies_to_add) { - return static_cast<typename T::context&>(*this); + res.add_header("Set-Cookie", cookie.first + "=" + cookie.second); } + } + }; + + /* + App<CookieParser, AnotherJarMW> app; + A B C + A::context + int aa; + + ctx1 : public A::context + ctx2 : public ctx1, public B::context + ctx3 : public ctx2, public C::context + + C depends on A + + C::handle + context.aaa + + App::context : private CookieParser::contetx, ... + { + jar - template <int N> - using partial = typename partial_context<Middlewares...>::template partial<N>; - }; } + + SimpleApp + */ } @@ -6860,7 +7176,7 @@ namespace crow void complete_request() { - CROW_LOG_INFO << "Response: " << this << ' ' << req_.url << ' ' << res.code << ' ' << close_connection_; + CROW_LOG_INFO << "Response: " << this << ' ' << req_.raw_url << ' ' << res.code << ' ' << close_connection_; if (need_to_call_after_handlers_) { |