diff options
author | ipkn <ipknhama@gmail.com> | 2014-11-05 03:07:15 +0900 |
---|---|---|
committer | ipkn <ipknhama@gmail.com> | 2014-11-05 03:07:15 +0900 |
commit | ccc5845c3e52814d28a2baf2cd90c6cd67c58e2c (patch) | |
tree | 4869beaf3d64e14666628f8acc89b15a27ada8d7 /amalgamate/crow_all.h | |
parent | 4b3b8070e75ce0fc181e5c012c47da2a1e7a918e (diff) | |
parent | 738d5d47b55fa612842060dfa4924abf476c0726 (diff) | |
download | crow-ccc5845c3e52814d28a2baf2cd90c6cd67c58e2c.tar.gz crow-ccc5845c3e52814d28a2baf2cd90c6cd67c58e2c.zip |
Merge pull request #36 from acron0/auto-json-content-type
Responses now auto to 'application/json' when constructed using json::wvalue
Diffstat (limited to 'amalgamate/crow_all.h')
-rw-r--r-- | amalgamate/crow_all.h | 1485 |
1 files changed, 751 insertions, 734 deletions
diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h index 2c7613d..2294438 100644 --- a/amalgamate/crow_all.h +++ b/amalgamate/crow_all.h @@ -1,5 +1,329 @@ #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.h> #include <string> @@ -311,6 +635,135 @@ namespace crow #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> @@ -321,6 +774,7 @@ namespace crow #include <boost/lexical_cast.hpp> #include <boost/algorithm/string/predicate.hpp> #include <boost/operators.hpp> +#include <vector> #if defined(__GNUG__) || defined(__clang__) #define crow_json_likely(x) __builtin_expect(x, 1) @@ -4897,274 +5351,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> - - - - -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 - #include <boost/asio.hpp> #include <deque> #include <functional> @@ -5256,308 +5442,90 @@ 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; + }; } #pragma once +#include <vector> #include <string> #include <stdexcept> +#include <iostream> @@ -5683,6 +5651,43 @@ constexpr crow::HTTPMethod operator "" _method(const char* str, size_t len) #pragma once +#include <boost/algorithm/string/predicate.hpp> +#include <boost/functional/hash.hpp> +#include <unordered_map> + +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 + @@ -5750,6 +5755,8 @@ namespace crow + + namespace crow { template <typename Handler> @@ -5951,8 +5958,11 @@ namespace crow explicit response(int code) : code(code) {} response(std::string body) : body(std::move(body)) {} response(json::wvalue&& json_value) : json_value(std::move(json_value)) {} - response(const json::wvalue& json_value) : body(json::dump(json_value)) {} response(int code, std::string body) : body(std::move(body)), code(code) {} + response(const json::wvalue& json_value) : body(json::dump(json_value)) + { + set_header("Content-Type", "application/json"); + } response(response&& r) { @@ -6024,185 +6034,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> @@ -6210,6 +6041,7 @@ namespace crow #include <unordered_map> #include <memory> #include <boost/lexical_cast.hpp> +#include <vector> @@ -6884,6 +6716,10 @@ public: + + + + namespace crow { namespace detail @@ -6941,12 +6777,192 @@ 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 <boost/asio.hpp> #include <boost/algorithm/string/predicate.hpp> #include <boost/lexical_cast.hpp> #include <boost/array.hpp> #include <atomic> #include <chrono> +#include <vector> @@ -7455,6 +7471,7 @@ namespace crow #include <cstdint> #include <atomic> #include <future> +#include <vector> #include <memory> |