aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRasie1 <rasielll@gmail.com>2017-03-16 19:27:34 +0300
committerRasie1 <rasielll@gmail.com>2017-03-16 19:27:34 +0300
commit15d377907fd8b63e2d8474dfeeeb1765fd8b729d (patch)
tree6145e7c5567bd19bac427019a09338feb94a2c8a
parent633dc82b8f8f80bf82c664120e097a2ebbcce877 (diff)
downloadcrow-15d377907fd8b63e2d8474dfeeeb1765fd8b729d.tar.gz
crow-15d377907fd8b63e2d8474dfeeeb1765fd8b729d.zip
Update amalgamate
-rw-r--r--amalgamate/crow_all.h6417
1 files changed, 3234 insertions, 3183 deletions
diff --git a/amalgamate/crow_all.h b/amalgamate/crow_all.h
index 6ffbe66..7f2dee1 100644
--- a/amalgamate/crow_all.h
+++ b/amalgamate/crow_all.h
@@ -1,40 +1,3 @@
-#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>;
-}
-
-
-
/*
*
* TinySHA1 - a header only implementation of the SHA1 algorithm in C++. Based
@@ -235,12 +198,722 @@ namespace sha1
#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
+
+/* #ifdef - enables ssl */
+//#define CROW_ENABLE_SSL
+
+/* #define - specifies log level */
+/*
+ Debug = 0
+ Info = 1
+ Warning = 2
+ Error = 3
+ Critical = 4
+
+ default to INFO
+*/
+#define CROW_LOG_LEVEL 1
+
+
+// compiler flags
+#if __cplusplus >= 201402L
+#define CROW_CAN_USE_CPP14
+#endif
+
+#if defined(_MSC_VER)
+#if _MSC_VER < 1900
+#define CROW_MSVC_WORKAROUND
+#define constexpr const
+#define noexcept throw()
+#endif
+#endif
+
+
+
+#pragma once
+
+#include <cstdint>
+#include <stdexcept>
+#include <tuple>
+#include <type_traits>
+#include <cstring>
+#include <functional>
+#include <string>
+
+
+
+
+namespace crow
+{
+ namespace black_magic
+ {
+#ifndef CROW_MSVC_WORKAROUND
+ 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);
+ }
+#endif
+ template <typename T>
+ struct parameter_tag
+ {
+ static const int value = 0;
+ };
+#define CROW_INTERNAL_PARAMETER_TAG(t, i) \
+template <> \
+struct parameter_tag<t> \
+{ \
+ static const int value = i; \
+}
+ CROW_INTERNAL_PARAMETER_TAG(int, 1);
+ CROW_INTERNAL_PARAMETER_TAG(char, 1);
+ CROW_INTERNAL_PARAMETER_TAG(short, 1);
+ CROW_INTERNAL_PARAMETER_TAG(long, 1);
+ CROW_INTERNAL_PARAMETER_TAG(long long, 1);
+ CROW_INTERNAL_PARAMETER_TAG(unsigned int, 2);
+ CROW_INTERNAL_PARAMETER_TAG(unsigned char, 2);
+ CROW_INTERNAL_PARAMETER_TAG(unsigned short, 2);
+ CROW_INTERNAL_PARAMETER_TAG(unsigned long, 2);
+ CROW_INTERNAL_PARAMETER_TAG(unsigned long long, 2);
+ CROW_INTERNAL_PARAMETER_TAG(double, 3);
+ CROW_INTERNAL_PARAMETER_TAG(std::string, 4);
+#undef CROW_INTERNAL_PARAMETER_TAG
+ template <typename ... Args>
+ struct compute_parameter_tag_from_args_list;
+
+ template <>
+ struct compute_parameter_tag_from_args_list<>
+ {
+ static const int value = 0;
+ };
+
+ template <typename Arg, typename ... Args>
+ struct compute_parameter_tag_from_args_list<Arg, Args...>
+ {
+ static const int sub_value =
+ compute_parameter_tag_from_args_list<Args...>::value;
+ static const int value =
+ parameter_tag<typename std::decay<Arg>::type>::value
+ ? sub_value* 6 + parameter_tag<typename std::decay<Arg>::type>::value
+ : sub_value;
+ };
+
+ static inline bool is_parameter_tag_compatible(uint64_t a, uint64_t b)
+ {
+ if (a == 0)
+ return b == 0;
+ if (b == 0)
+ return a == 0;
+ int sa = a%6;
+ int sb = a%6;
+ if (sa == 5) sa = 4;
+ if (sb == 5) sb = 4;
+ if (sa != sb)
+ return false;
+ return is_parameter_tag_compatible(a/6, b/6);
+ }
+
+ static inline unsigned find_closing_tag_runtime(const char* s, unsigned p)
+ {
+ return
+ s[p] == 0
+ ? throw std::runtime_error("unmatched tag <") :
+ s[p] == '>'
+ ? p : find_closing_tag_runtime(s, p + 1);
+ }
+
+ static inline uint64_t get_parameter_tag_runtime(const char* s, unsigned p = 0)
+ {
+ return
+ s[p] == 0
+ ? 0 :
+ s[p] == '<' ? (
+ std::strncmp(s+p, "<int>", 5) == 0
+ ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 1 :
+ std::strncmp(s+p, "<uint>", 6) == 0
+ ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 2 :
+ (std::strncmp(s+p, "<float>", 7) == 0 ||
+ std::strncmp(s+p, "<double>", 8) == 0)
+ ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 3 :
+ (std::strncmp(s+p, "<str>", 5) == 0 ||
+ std::strncmp(s+p, "<string>", 8) == 0)
+ ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 4 :
+ std::strncmp(s+p, "<path>", 6) == 0
+ ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 5 :
+ throw std::runtime_error("invalid parameter type")
+ ) :
+ get_parameter_tag_runtime(s, p+1);
+ }
+#ifndef CROW_MSVC_WORKAROUND
+ 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);
+ }
+#endif
+
+ 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
+ {
+ };
+
+ template <typename T>
+ struct promote
+ {
+ using type = T;
+ };
+
+#define CROW_INTERNAL_PROMOTE_TYPE(t1, t2) \
+ template<> \
+ struct promote<t1> \
+ { \
+ using type = t2; \
+ }
+
+ CROW_INTERNAL_PROMOTE_TYPE(char, int64_t);
+ CROW_INTERNAL_PROMOTE_TYPE(short, int64_t);
+ CROW_INTERNAL_PROMOTE_TYPE(int, int64_t);
+ CROW_INTERNAL_PROMOTE_TYPE(long, int64_t);
+ CROW_INTERNAL_PROMOTE_TYPE(long long, int64_t);
+ CROW_INTERNAL_PROMOTE_TYPE(unsigned char, uint64_t);
+ CROW_INTERNAL_PROMOTE_TYPE(unsigned short, uint64_t);
+ CROW_INTERNAL_PROMOTE_TYPE(unsigned int, uint64_t);
+ CROW_INTERNAL_PROMOTE_TYPE(unsigned long, uint64_t);
+ CROW_INTERNAL_PROMOTE_TYPE(unsigned long long, uint64_t);
+ CROW_INTERNAL_PROMOTE_TYPE(float, double);
+#undef CROW_INTERNAL_PROMOTE_TYPE
+
+ template <typename T>
+ using promote_t = typename promote<T>::type;
+
+ } // namespace black_magic
+
+ namespace detail
+ {
+
+ template <class T, std::size_t N, class... Args>
+ struct get_index_of_element_from_tuple_by_type_impl
+ {
+ static constexpr auto value = N;
+ };
+
+ template <class T, std::size_t N, class... Args>
+ struct get_index_of_element_from_tuple_by_type_impl<T, N, T, Args...>
+ {
+ static constexpr auto value = N;
+ };
+
+ template <class T, std::size_t N, class U, class... Args>
+ struct get_index_of_element_from_tuple_by_type_impl<T, N, U, Args...>
+ {
+ static constexpr auto value = get_index_of_element_from_tuple_by_type_impl<T, N + 1, Args...>::value;
+ };
+
+ } // namespace detail
+
+ namespace utility
+ {
+ template <class T, class... Args>
+ T& get_element_by_type(std::tuple<Args...>& t)
+ {
+ return std::get<detail::get_index_of_element_from_tuple_by_type_impl<T, 0, Args...>::value>(t);
+ }
+
+ template<typename T>
+ struct function_traits;
+
+#ifndef CROW_MSVC_WORKAROUND
+ template<typename T>
+ struct function_traits : public function_traits<decltype(&T::operator())>
+ {
+ using parent_t = function_traits<decltype(&T::operator())>;
+ static const size_t arity = parent_t::arity;
+ using result_type = typename parent_t::result_type;
+ template <size_t i>
+ using arg = typename parent_t::template arg<i>;
+
+ };
+#endif
+
+ template<typename ClassType, typename R, typename ...Args>
+ struct function_traits<R(ClassType::*)(Args...) const>
+ {
+ static const size_t arity = sizeof...(Args);
+
+ typedef R result_type;
+
+ template <size_t i>
+ using arg = typename std::tuple_element<i, std::tuple<Args...>>::type;
+ };
+
+ template<typename ClassType, typename R, typename ...Args>
+ struct function_traits<R(ClassType::*)(Args...)>
+ {
+ static const size_t arity = sizeof...(Args);
+
+ typedef R result_type;
+
+ template <size_t i>
+ using arg = typename std::tuple_element<i, std::tuple<Args...>>::type;
+ };
+
+ template<typename R, typename ...Args>
+ struct function_traits<std::function<R(Args...)>>
+ {
+ static const size_t arity = sizeof...(Args);
+
+ typedef R result_type;
+
+ template <size_t i>
+ using arg = typename std::tuple_element<i, std::tuple<Args...>>::type;
+ };
+
+ inline static std::string base64encode(const char* data, size_t size, const char* key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
+ {
+ std::string ret;
+ ret.resize((size+2) / 3 * 4);
+ auto it = ret.begin();
+ while(size >= 3)
+ {
+ *it++ = key[(((unsigned char)*data)&0xFC)>>2];
+ unsigned char h = (((unsigned char)*data++) & 0x03) << 4;
+ *it++ = key[h|((((unsigned char)*data)&0xF0)>>4)];
+ h = (((unsigned char)*data++) & 0x0F) << 2;
+ *it++ = key[h|((((unsigned char)*data)&0xC0)>>6)];
+ *it++ = key[((unsigned char)*data++)&0x3F];
+
+ size -= 3;
+ }
+ if (size == 1)
+ {
+ *it++ = key[(((unsigned char)*data)&0xFC)>>2];
+ unsigned char h = (((unsigned char)*data++) & 0x03) << 4;
+ *it++ = key[h];
+ *it++ = '=';
+ *it++ = '=';
+ }
+ else if (size == 2)
+ {
+ *it++ = key[(((unsigned char)*data)&0xFC)>>2];
+ unsigned char h = (((unsigned char)*data++) & 0x03) << 4;
+ *it++ = key[h|((((unsigned char)*data)&0xF0)>>4)];
+ h = (((unsigned char)*data++) & 0x0F) << 2;
+ *it++ = key[h];
+ *it++ = '=';
+ }
+ return ret;
+ }
+
+ inline static std::string base64encode_urlsafe(const char* data, size_t size)
+ {
+ return base64encode(data, size, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
+ }
+
+
+ } // namespace utility
+}
+
+
+
+#pragma once
+#include <boost/asio.hpp>
+#ifdef CROW_ENABLE_SSL
+#include <boost/asio/ssl.hpp>
+#endif
+
+
+namespace crow
+{
+ using namespace boost;
+ using tcp = asio::ip::tcp;
+
+ struct SocketAdaptor
+ {
+ using context = void;
+ SocketAdaptor(boost::asio::io_service& io_service, context*)
+ : socket_(io_service)
+ {
+ }
+
+ boost::asio::io_service& get_io_service()
+ {
+ return socket_.get_io_service();
+ }
+
+ tcp::socket& raw_socket()
+ {
+ return socket_;
+ }
+
+ tcp::socket& socket()
+ {
+ return socket_;
+ }
+
+ tcp::endpoint remote_endpoint()
+ {
+ return socket_.remote_endpoint();
+ }
+
+ bool is_open()
+ {
+ return socket_.is_open();
+ }
+
+ void close()
+ {
+ socket_.close();
+ }
+
+ template <typename F>
+ void start(F f)
+ {
+ f(boost::system::error_code());
+ }
+
+ tcp::socket socket_;
+ };
+
+#ifdef CROW_ENABLE_SSL
+ struct SSLAdaptor
+ {
+ using context = boost::asio::ssl::context;
+ using ssl_socket_t = boost::asio::ssl::stream<tcp::socket>;
+ SSLAdaptor(boost::asio::io_service& io_service, context* ctx)
+ : ssl_socket_(new ssl_socket_t(io_service, *ctx))
+ {
+ }
+
+ boost::asio::ssl::stream<tcp::socket>& socket()
+ {
+ return *ssl_socket_;
+ }
+
+ tcp::socket::lowest_layer_type&
+ raw_socket()
+ {
+ return ssl_socket_->lowest_layer();
+ }
+
+ tcp::endpoint remote_endpoint()
+ {
+ return raw_socket().remote_endpoint();
+ }
+
+ bool is_open()
+ {
+ return raw_socket().is_open();
+ }
+
+ void close()
+ {
+ raw_socket().close();
+ }
+
+ boost::asio::io_service& get_io_service()
+ {
+ return raw_socket().get_io_service();
+ }
+
+ template <typename F>
+ void start(F f)
+ {
+ ssl_socket_->async_handshake(boost::asio::ssl::stream_base::server,
+ [f](const boost::system::error_code& ec) {
+ f(ec);
+ });
+ }
+
+ std::unique_ptr<boost::asio::ssl::stream<tcp::socket>> ssl_socket_;
+ };
+#endif
+}
+
+
+
+#pragma once
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <iostream>
+#include <boost/optional.hpp>
namespace crow
{
@@ -433,6 +1106,37 @@ inline char * qs_k2v(const char * key, char * const * qs_kv, int qs_kv_size, int
return NULL;
}
+inline boost::optional<std::pair<std::string, std::string>> 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;
+
+ name_len = strlen(dict_name) + 1;
+
+#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(dict_name, qs_kv[i], name_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 boost::make_optional(std::make_pair(std::string(qs_kv[i] + 1),
+ std::string(qs_kv[i] + skip)));
+ else
+ --nth;
+ }
+ }
+#endif // _qsSORTING
+
+ return boost::none;
+}
+
inline char * qs_scanvalue(const char * key, const char * qs, char * val, size_t val_len)
{
@@ -573,6 +1277,25 @@ namespace crow
}
+ std::vector<std::pair<std::string, std::string>> get_dict (const std::string& name) const
+ {
+ std::vector<std::pair<std::string, std::string>> ret;
+
+ int count = 0;
+ while(1)
+ {
+ if (auto element = qs_dict_name2kv(name.c_str(), key_value_pairs_.data(), key_value_pairs_.size(), count++))
+ ret.push_back(*element);
+ else
+ break;
+ }
+ return ret;
+ }
+
+
+
+
+
private:
std::string url_;
std::vector<char*> key_value_pairs_;
@@ -582,6 +1305,2171 @@ namespace crow
+#pragma once
+
+#include <string>
+#include <cstdio>
+#include <cstdlib>
+#include <ctime>
+#include <iostream>
+#include <sstream>
+
+
+
+
+namespace crow
+{
+ enum class LogLevel
+ {
+#ifndef ERROR
+ DEBUG = 0,
+ INFO,
+ WARNING,
+ ERROR,
+ CRITICAL,
+#endif
+
+ Debug = 0,
+ 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);
+
+ tm my_tm;
+
+#ifdef _MSC_VER
+ gmtime_s(&my_tm, &t);
+#else
+ gmtime_r(&t, &my_tm);
+#endif
+
+ size_t sz = strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", &my_tm);
+ return std::string(date, date+sz);
+ }
+
+ 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>
+#include <unordered_map>
+#include <iostream>
+#include <algorithm>
+#include <memory>
+#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)
+#define crow_json_unlikely(x) __builtin_expect(x, 0)
+#else
+#define crow_json_likely(x) x
+#define crow_json_unlikely(x) x
+#endif
+
+
+namespace crow
+{
+ namespace mustache
+ {
+ class template_t;
+ }
+
+ namespace json
+ {
+ inline void escape(const std::string& str, std::string& ret)
+ {
+ ret.reserve(ret.size() + str.size()+str.size()/4);
+ for(char c:str)
+ {
+ switch(c)
+ {
+ case '"': ret += "\\\""; break;
+ case '\\': ret += "\\\\"; break;
+ case '\n': ret += "\\n"; break;
+ case '\b': ret += "\\b"; break;
+ case '\f': ret += "\\f"; break;
+ case '\r': ret += "\\r"; break;
+ case '\t': ret += "\\t"; break;
+ default:
+ if (0 <= c && c < 0x20)
+ {
+ ret += "\\u00";
+ auto to_hex = [](char c)
+ {
+ c = c&0xf;
+ if (c < 10)
+ return '0' + c;
+ return 'a'+c-10;
+ };
+ ret += to_hex(c/16);
+ ret += to_hex(c%16);
+ }
+ else
+ ret += c;
+ break;
+ }
+ }
+ }
+ inline std::string escape(const std::string& str)
+ {
+ std::string ret;
+ escape(str, ret);
+ return ret;
+ }
+
+ enum class type : char
+ {
+ Null,
+ False,
+ True,
+ Number,
+ String,
+ List,
+ Object,
+ };
+
+ inline const char* get_type_str(type t) {
+ switch(t){
+ case type::Number: return "Number";
+ case type::False: return "False";
+ case type::True: return "True";
+ case type::List: return "List";
+ case type::String: return "String";
+ case type::Object: return "Object";
+ default: return "Unknown";
+ }
+ }
+
+ class rvalue;
+ rvalue load(const char* data, size_t size);
+
+ namespace detail
+ {
+
+ struct r_string
+ : boost::less_than_comparable<r_string>,
+ boost::less_than_comparable<r_string, std::string>,
+ boost::equality_comparable<r_string>,
+ boost::equality_comparable<r_string, std::string>
+ {
+ r_string() {};
+ r_string(char* s, char* e)
+ : s_(s), e_(e)
+ {};
+ ~r_string()
+ {
+ if (owned_)
+ delete[] s_;
+ }
+
+ r_string(const r_string& r)
+ {
+ *this = r;
+ }
+
+ r_string(r_string&& r)
+ {
+ *this = r;
+ }
+
+ r_string& operator = (r_string&& r)
+ {
+ s_ = r.s_;
+ e_ = r.e_;
+ owned_ = r.owned_;
+ if (r.owned_)
+ r.owned_ = 0;
+ return *this;
+ }
+
+ r_string& operator = (const r_string& r)
+ {
+ s_ = r.s_;
+ e_ = r.e_;
+ owned_ = 0;
+ return *this;
+ }
+
+ operator std::string () const
+ {
+ return std::string(s_, e_);
+ }
+
+
+ const char* begin() const { return s_; }
+ const char* end() const { return e_; }
+ size_t size() const { return end() - begin(); }
+
+ using iterator = const char*;
+ using const_iterator = const char*;
+
+ char* s_;
+ mutable char* e_;
+ uint8_t owned_{0};
+ friend std::ostream& operator << (std::ostream& os, const r_string& s)
+ {
+ os << (std::string)s;
+ return os;
+ }
+ private:
+ void force(char* s, uint32_t /*length*/)
+ {
+ s_ = s;
+ owned_ = 1;
+ }
+ friend rvalue crow::json::load(const char* data, size_t size);
+ };
+
+ inline bool operator < (const r_string& l, const r_string& r)
+ {
+ return boost::lexicographical_compare(l,r);
+ }
+
+ inline bool operator < (const r_string& l, const std::string& r)
+ {
+ return boost::lexicographical_compare(l,r);
+ }
+
+ inline bool operator > (const r_string& l, const std::string& r)
+ {
+ return boost::lexicographical_compare(r,l);
+ }
+
+ inline bool operator == (const r_string& l, const r_string& r)
+ {
+ return boost::equals(l,r);
+ }
+
+ inline bool operator == (const r_string& l, const std::string& r)
+ {
+ return boost::equals(l,r);
+ }
+ }
+
+ class rvalue
+ {
+ static const int cached_bit = 2;
+ static const int error_bit = 4;
+ public:
+ rvalue() noexcept : option_{error_bit}
+ {}
+ rvalue(type t) noexcept
+ : lsize_{}, lremain_{}, t_{t}
+ {}
+ rvalue(type t, char* s, char* e) noexcept
+ : start_{s},
+ end_{e},
+ t_{t}
+ {}
+
+ rvalue(const rvalue& r)
+ : start_(r.start_),
+ end_(r.end_),
+ key_(r.key_),
+ t_(r.t_),
+ option_(r.option_)
+ {
+ copy_l(r);
+ }
+
+ rvalue(rvalue&& r) noexcept
+ {
+ *this = std::move(r);
+ }
+
+ rvalue& operator = (const rvalue& r)
+ {
+ start_ = r.start_;
+ end_ = r.end_;
+ key_ = r.key_;
+ copy_l(r);
+ t_ = r.t_;
+ option_ = r.option_;
+ return *this;
+ }
+ rvalue& operator = (rvalue&& r) noexcept
+ {
+ start_ = r.start_;
+ end_ = r.end_;
+ key_ = std::move(r.key_);
+ l_ = std::move(r.l_);
+ lsize_ = r.lsize_;
+ lremain_ = r.lremain_;
+ t_ = r.t_;
+ option_ = r.option_;
+ return *this;
+ }
+
+ explicit operator bool() const noexcept
+ {
+ return (option_ & error_bit) == 0;
+ }
+
+ explicit operator int64_t() const
+ {
+ return i();
+ }
+
+ explicit operator uint64_t() const
+ {
+ return u();
+ }
+
+ explicit operator int() const
+ {
+ return (int)i();
+ }
+
+ type t() const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ if (option_ & error_bit)
+ {
+ throw std::runtime_error("invalid json object");
+ }
+#endif
+ return t_;
+ }
+
+ int64_t i() const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ switch (t()) {
+ case type::Number:
+ case type::String:
+ return boost::lexical_cast<int64_t>(start_, end_-start_);
+ default:
+ const std::string msg = "expected number, got: "
+ + std::string(get_type_str(t()));
+ throw std::runtime_error(msg);
+ }
+#endif
+ return boost::lexical_cast<int64_t>(start_, end_-start_);
+ }
+
+ uint64_t u() const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ switch (t()) {
+ case type::Number:
+ case type::String:
+ return boost::lexical_cast<uint64_t>(start_, end_-start_);
+ default:
+ throw std::runtime_error(std::string("expected number, got: ") + get_type_str(t()));
+ }
+#endif
+ return boost::lexical_cast<uint64_t>(start_, end_-start_);
+ }
+
+ double d() const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ if (t() != type::Number)
+ throw std::runtime_error("value is not number");
+#endif
+ return boost::lexical_cast<double>(start_, end_-start_);
+ }
+
+ bool b() const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ if (t() != type::True && t() != type::False)
+ throw std::runtime_error("value is not boolean");
+#endif
+ return t() == type::True;
+ }
+
+ void unescape() const
+ {
+ if (*(start_-1))
+ {
+ char* head = start_;
+ char* tail = start_;
+ while(head != end_)
+ {
+ if (*head == '\\')
+ {
+ switch(*++head)
+ {
+ case '"': *tail++ = '"'; break;
+ case '\\': *tail++ = '\\'; break;
+ case '/': *tail++ = '/'; break;
+ case 'b': *tail++ = '\b'; break;
+ case 'f': *tail++ = '\f'; break;
+ case 'n': *tail++ = '\n'; break;
+ case 'r': *tail++ = '\r'; break;
+ case 't': *tail++ = '\t'; break;
+ case 'u':
+ {
+ auto from_hex = [](char c)
+ {
+ if (c >= 'a')
+ return c - 'a' + 10;
+ if (c >= 'A')
+ return c - 'A' + 10;
+ return c - '0';
+ };
+ unsigned int code =
+ (from_hex(head[1])<<12) +
+ (from_hex(head[2])<< 8) +
+ (from_hex(head[3])<< 4) +
+ from_hex(head[4]);
+ if (code >= 0x800)
+ {
+ *tail++ = 0xE0 | (code >> 12);
+ *tail++ = 0x80 | ((code >> 6) & 0x3F);
+ *tail++ = 0x80 | (code & 0x3F);
+ }
+ else if (code >= 0x80)
+ {
+ *tail++ = 0xC0 | (code >> 6);
+ *tail++ = 0x80 | (code & 0x3F);
+ }
+ else
+ {
+ *tail++ = code;
+ }
+ head += 4;
+ }
+ break;
+ }
+ }
+ else
+ *tail++ = *head;
+ head++;
+ }
+ end_ = tail;
+ *end_ = 0;
+ *(start_-1) = 0;
+ }
+ }
+
+ detail::r_string s() const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ if (t() != type::String)
+ throw std::runtime_error("value is not string");
+#endif
+ unescape();
+ return detail::r_string{start_, end_};
+ }
+
+ bool has(const char* str) const
+ {
+ return has(std::string(str));
+ }
+
+ bool has(const std::string& str) const
+ {
+ struct Pred
+ {
+ bool operator()(const rvalue& l, const rvalue& r) const
+ {
+ return l.key_ < r.key_;
+ };
+ bool operator()(const rvalue& l, const std::string& r) const
+ {
+ return l.key_ < r;
+ };
+ bool operator()(const std::string& l, const rvalue& r) const
+ {
+ return l < r.key_;
+ };
+ };
+ if (!is_cached())
+ {
+ std::sort(begin(), end(), Pred());
+ set_cached();
+ }
+ auto it = lower_bound(begin(), end(), str, Pred());
+ return it != end() && it->key_ == str;
+ }
+
+ int count(const std::string& str)
+ {
+ return has(str) ? 1 : 0;
+ }
+
+ rvalue* begin() const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ if (t() != type::Object && t() != type::List)
+ throw std::runtime_error("value is not a container");
+#endif
+ return l_.get();
+ }
+ rvalue* end() const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ if (t() != type::Object && t() != type::List)
+ throw std::runtime_error("value is not a container");
+#endif
+ return l_.get()+lsize_;
+ }
+
+ const detail::r_string& key() const
+ {
+ return key_;
+ }
+
+ size_t size() const
+ {
+ if (t() == type::String)
+ return s().size();
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ if (t() != type::Object && t() != type::List)
+ throw std::runtime_error("value is not a container");
+#endif
+ return lsize_;
+ }
+
+ const rvalue& operator[](int index) const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ if (t() != type::List)
+ throw std::runtime_error("value is not a list");
+ if (index >= (int)lsize_ || index < 0)
+ throw std::runtime_error("list out of bound");
+#endif
+ return l_[index];
+ }
+
+ const rvalue& operator[](size_t index) const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ if (t() != type::List)
+ throw std::runtime_error("value is not a list");
+ if (index >= lsize_)
+ throw std::runtime_error("list out of bound");
+#endif
+ return l_[index];
+ }
+
+ const rvalue& operator[](const char* str) const
+ {
+ return this->operator[](std::string(str));
+ }
+
+ const rvalue& operator[](const std::string& str) const
+ {
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ if (t() != type::Object)
+ throw std::runtime_error("value is not an object");
+#endif
+ struct Pred
+ {
+ bool operator()(const rvalue& l, const rvalue& r) const
+ {
+ return l.key_ < r.key_;
+ };
+ bool operator()(const rvalue& l, const std::string& r) const
+ {
+ return l.key_ < r;
+ };
+ bool operator()(const std::string& l, const rvalue& r) const
+ {
+ return l < r.key_;
+ };
+ };
+ if (!is_cached())
+ {
+ std::sort(begin(), end(), Pred());
+ set_cached();
+ }
+ auto it = lower_bound(begin(), end(), str, Pred());
+ if (it != end() && it->key_ == str)
+ return *it;
+#ifndef CROW_JSON_NO_ERROR_CHECK
+ throw std::runtime_error("cannot find key");
+#else
+ static rvalue nullValue;
+ return nullValue;
+#endif
+ }
+
+ void set_error()
+ {
+ option_|=error_bit;
+ }
+
+ bool error() const
+ {
+ return (option_&error_bit)!=0;
+ }
+ private:
+ bool is_cached() const
+ {
+ return (option_&cached_bit)!=0;
+ }
+ void set_cached() const
+ {
+ option_ |= cached_bit;
+ }
+ void copy_l(const rvalue& r)
+ {
+ if (r.t() != type::Object && r.t() != type::List)
+ return;
+ lsize_ = r.lsize_;
+ lremain_ = 0;
+ l_.reset(new rvalue[lsize_]);
+ std::copy(r.begin(), r.end(), begin());
+ }
+
+ void emplace_back(rvalue&& v)
+ {
+ if (!lremain_)
+ {
+ int new_size = lsize_ + lsize_;
+ if (new_size - lsize_ > 60000)
+ new_size = lsize_ + 60000;
+ if (new_size < 4)
+ new_size = 4;
+ rvalue* p = new rvalue[new_size];
+ rvalue* p2 = p;
+ for(auto& x : *this)
+ *p2++ = std::move(x);
+ l_.reset(p);
+ lremain_ = new_size - lsize_;
+ }
+ l_[lsize_++] = std::move(v);
+ lremain_ --;
+ }
+
+ mutable char* start_;
+ mutable char* end_;
+ detail::r_string key_;
+ std::unique_ptr<rvalue[]> l_;
+ uint32_t lsize_;
+ uint16_t lremain_;
+ type t_;
+ mutable uint8_t option_{0};
+
+ friend rvalue load_nocopy_internal(char* data, size_t size);
+ friend rvalue load(const char* data, size_t size);
+ friend std::ostream& operator <<(std::ostream& os, const rvalue& r)
+ {
+ switch(r.t_)
+ {
+
+ case type::Null: os << "null"; break;
+ case type::False: os << "false"; break;
+ case type::True: os << "true"; break;
+ case type::Number: os << r.d(); break;
+ case type::String: os << '"' << r.s() << '"'; break;
+ case type::List:
+ {
+ os << '[';
+ bool first = true;
+ for(auto& x : r)
+ {
+ if (!first)
+ os << ',';
+ first = false;
+ os << x;
+ }
+ os << ']';
+ }
+ break;
+ case type::Object:
+ {
+ os << '{';
+ bool first = true;
+ for(auto& x : r)
+ {
+ if (!first)
+ os << ',';
+ os << '"' << escape(x.key_) << "\":";
+ first = false;
+ os << x;
+ }
+ os << '}';
+ }
+ break;
+ }
+ return os;
+ }
+ };
+ namespace detail {
+ }
+
+ inline bool operator == (const rvalue& l, const std::string& r)
+ {
+ return l.s() == r;
+ }
+
+ inline bool operator == (const std::string& l, const rvalue& r)
+ {
+ return l == r.s();
+ }
+
+ inline bool operator != (const rvalue& l, const std::string& r)
+ {
+ return l.s() != r;
+ }
+
+ inline bool operator != (const std::string& l, const rvalue& r)
+ {
+ return l != r.s();
+ }
+
+ inline bool operator == (const rvalue& l, double r)
+ {
+ return l.d() == r;
+ }
+
+ inline bool operator == (double l, const rvalue& r)
+ {
+ return l == r.d();
+ }
+
+ inline bool operator != (const rvalue& l, double r)
+ {
+ return l.d() != r;
+ }
+
+ inline bool operator != (double l, const rvalue& r)
+ {
+ return l != r.d();
+ }
+
+
+ inline rvalue load_nocopy_internal(char* data, size_t size)
+ {
+ //static const char* escaped = "\"\\/\b\f\n\r\t";
+ struct Parser
+ {
+ Parser(char* data, size_t /*size*/)
+ : data(data)
+ {
+ }
+
+ bool consume(char c)
+ {
+ if (crow_json_unlikely(*data != c))
+ return false;
+ data++;
+ return true;
+ }
+
+ void ws_skip()
+ {
+ while(*data == ' ' || *data == '\t' || *data == '\r' || *data == '\n') ++data;
+ };
+
+ rvalue decode_string()
+ {
+ if (crow_json_unlikely(!consume('"')))
+ return {};
+ char* start = data;
+ uint8_t has_escaping = 0;
+ while(1)
+ {
+ if (crow_json_likely(*data != '"' && *data != '\\' && *data != '\0'))
+ {
+ data ++;
+ }
+ else if (*data == '"')
+ {
+ *data = 0;
+ *(start-1) = has_escaping;
+ data++;
+ return {type::String, start, data-1};
+ }
+ else if (*data == '\\')
+ {
+ has_escaping = 1;
+ data++;
+ switch(*data)
+ {
+ case 'u':
+ {
+ auto check = [](char c)
+ {
+ return
+ ('0' <= c && c <= '9') ||
+ ('a' <= c && c <= 'f') ||
+ ('A' <= c && c <= 'F');
+ };
+ if (!(check(*(data+1)) &&
+ check(*(data+2)) &&
+ check(*(data+3)) &&
+ check(*(data+4))))
+ return {};
+ }
+ data += 5;
+ break;
+ case '"':
+ case '\\':
+ case '/':
+ case 'b':
+ case 'f':
+ case 'n':
+ case 'r':
+ case 't':
+ data ++;
+ break;
+ default:
+ return {};
+ }
+ }
+ else
+ return {};
+ }
+ return {};
+ }
+
+ rvalue decode_list()
+ {
+ rvalue ret(type::List);
+ if (crow_json_unlikely(!consume('[')))
+ {
+ ret.set_error();
+ return ret;
+ }
+ ws_skip();
+ if (crow_json_unlikely(*data == ']'))
+ {
+ data++;
+ return ret;
+ }
+
+ while(1)
+ {
+ auto v = decode_value();
+ if (crow_json_unlikely(!v))
+ {
+ ret.set_error();
+ break;
+ }
+ ws_skip();
+ ret.emplace_back(std::move(v));
+ if (*data == ']')
+ {
+ data++;
+ break;
+ }
+ if (crow_json_unlikely(!consume(',')))
+ {
+ ret.set_error();
+ break;
+ }
+ ws_skip();
+ }
+ return ret;
+ }
+
+ rvalue decode_number()
+ {
+ char* start = data;
+
+ enum NumberParsingState
+ {
+ Minus,
+ AfterMinus,
+ ZeroFirst,
+ Digits,
+ DigitsAfterPoints,
+ E,
+ DigitsAfterE,
+ Invalid,
+ } state{Minus};
+ while(crow_json_likely(state != Invalid))
+ {
+ switch(*data)
+ {
+ case '0':
+ state = (NumberParsingState)"\2\2\7\3\4\6\6"[state];
+ /*if (state == NumberParsingState::Minus || state == NumberParsingState::AfterMinus)
+ {
+ state = NumberParsingState::ZeroFirst;
+ }
+ else if (state == NumberParsingState::Digits ||
+ state == NumberParsingState::DigitsAfterE ||
+ state == NumberParsingState::DigitsAfterPoints)
+ {
+ // ok; pass
+ }
+ else if (state == NumberParsingState::E)
+ {
+ state = NumberParsingState::DigitsAfterE;
+ }
+ else
+ return {};*/
+ break;
+ case '1': case '2': case '3':
+ case '4': case '5': case '6':
+ case '7': case '8': case '9':
+ state = (NumberParsingState)"\3\3\7\3\4\6\6"[state];
+ while(*(data+1) >= '0' && *(data+1) <= '9') data++;
+ /*if (state == NumberParsingState::Minus || state == NumberParsingState::AfterMinus)
+ {
+ state = NumberParsingState::Digits;
+ }
+ else if (state == NumberParsingState::Digits ||
+ state == NumberParsingState::DigitsAfterE ||
+ state == NumberParsingState::DigitsAfterPoints)
+ {
+ // ok; pass
+ }
+ else if (state == NumberParsingState::E)
+ {
+ state = NumberParsingState::DigitsAfterE;
+ }
+ else
+ return {};*/
+ break;
+ case '.':
+ state = (NumberParsingState)"\7\7\4\4\7\7\7"[state];
+ /*
+ if (state == NumberParsingState::Digits || state == NumberParsingState::ZeroFirst)
+ {
+ state = NumberParsingState::DigitsAfterPoints;
+ }
+ else
+ return {};
+ */
+ break;
+ case '-':
+ state = (NumberParsingState)"\1\7\7\7\7\6\7"[state];
+ /*if (state == NumberParsingState::Minus)
+ {
+ state = NumberParsingState::AfterMinus;
+ }
+ else if (state == NumberParsingState::E)
+ {
+ state = NumberParsingState::DigitsAfterE;
+ }
+ else
+ return {};*/
+ break;
+ case '+':
+ state = (NumberParsingState)"\7\7\7\7\7\6\7"[state];
+ /*if (state == NumberParsingState::E)
+ {
+ state = NumberParsingState::DigitsAfterE;
+ }
+ else
+ return {};*/
+ break;
+ case 'e': case 'E':
+ state = (NumberParsingState)"\7\7\7\5\5\7\7"[state];
+ /*if (state == NumberParsingState::Digits ||
+ state == NumberParsingState::DigitsAfterPoints)
+ {
+ state = NumberParsingState::E;
+ }
+ else
+ return {};*/
+ break;
+ default:
+ if (crow_json_likely(state == NumberParsingState::ZeroFirst ||
+ state == NumberParsingState::Digits ||
+ state == NumberParsingState::DigitsAfterPoints ||
+ state == NumberParsingState::DigitsAfterE))
+ return {type::Number, start, data};
+ else
+ return {};
+ }
+ data++;
+ }
+
+ return {};
+ }
+
+ rvalue decode_value()
+ {
+ switch(*data)
+ {
+ case '[':
+ return decode_list();
+ case '{':
+ return decode_object();
+ case '"':
+ return decode_string();
+ case 't':
+ if (//e-data >= 4 &&
+ data[1] == 'r' &&
+ data[2] == 'u' &&
+ data[3] == 'e')
+ {
+ data += 4;
+ return {type::True};
+ }
+ else
+ return {};
+ case 'f':
+ if (//e-data >= 5 &&
+ data[1] == 'a' &&
+ data[2] == 'l' &&
+ data[3] == 's' &&
+ data[4] == 'e')
+ {
+ data += 5;
+ return {type::False};
+ }
+ else
+ return {};
+ case 'n':
+ if (//e-data >= 4 &&
+ data[1] == 'u' &&
+ data[2] == 'l' &&
+ data[3] == 'l')
+ {
+ data += 4;
+ return {type::Null};
+ }
+ else
+ return {};
+ //case '1': case '2': case '3':
+ //case '4': case '5': case '6':
+ //case '7': case '8': case '9':
+ //case '0': case '-':
+ default:
+ return decode_number();
+ }
+ return {};
+ }
+
+ rvalue decode_object()
+ {
+ rvalue ret(type::Object);
+ if (crow_json_unlikely(!consume('{')))
+ {
+ ret.set_error();
+ return ret;
+ }
+
+ ws_skip();
+
+ if (crow_json_unlikely(*data == '}'))
+ {
+ data++;
+ return ret;
+ }
+
+ while(1)
+ {
+ auto t = decode_string();
+ if (crow_json_unlikely(!t))
+ {
+ ret.set_error();
+ break;
+ }
+
+ ws_skip();
+ if (crow_json_unlikely(!consume(':')))
+ {
+ ret.set_error();
+ break;
+ }
+
+ // TODO caching key to speed up (flyweight?)
+ auto key = t.s();
+
+ ws_skip();
+ auto v = decode_value();
+ if (crow_json_unlikely(!v))
+ {
+ ret.set_error();
+ break;
+ }
+ ws_skip();
+
+ v.key_ = std::move(key);
+ ret.emplace_back(std::move(v));
+ if (crow_json_unlikely(*data == '}'))
+ {
+ data++;
+ break;
+ }
+ if (crow_json_unlikely(!consume(',')))
+ {
+ ret.set_error();
+ break;
+ }
+ ws_skip();
+ }
+ return ret;
+ }
+
+ rvalue parse()
+ {
+ ws_skip();
+ auto ret = decode_value(); // or decode object?
+ ws_skip();
+ if (ret && *data != '\0')
+ ret.set_error();
+ return ret;
+ }
+
+ char* data;
+ };
+ return Parser(data, size).parse();
+ }
+ inline rvalue load(const char* data, size_t size)
+ {
+ char* s = new char[size+1];
+ memcpy(s, data, size);
+ s[size] = 0;
+ auto ret = load_nocopy_internal(s, size);
+ if (ret)
+ ret.key_.force(s, size);
+ else
+ delete[] s;
+ return ret;
+ }
+
+ inline rvalue load(const char* data)
+ {
+ return load(data, strlen(data));
+ }
+
+ inline rvalue load(const std::string& str)
+ {
+ return load(str.data(), str.size());
+ }
+
+ class wvalue
+ {
+ friend class crow::mustache::template_t;
+ public:
+ type t() const { return t_; }
+ private:
+ type t_{type::Null};
+ double d {};
+ std::string s;
+ std::unique_ptr<std::vector<wvalue>> l;
+ std::unique_ptr<std::unordered_map<std::string, wvalue>> o;
+ public:
+
+ wvalue() {}
+
+ wvalue(const rvalue& r)
+ {
+ t_ = r.t();
+ switch(r.t())
+ {
+ case type::Null:
+ case type::False:
+ case type::True:
+ return;
+ case type::Number:
+ d = r.d();
+ return;
+ case type::String:
+ s = r.s();
+ return;
+ case type::List:
+ l = std::unique_ptr<std::vector<wvalue>>(new std::vector<wvalue>{});
+ l->reserve(r.size());
+ for(auto it = r.begin(); it != r.end(); ++it)
+ l->emplace_back(*it);
+ return;
+ case type::Object:
+ o = std::unique_ptr<
+ std::unordered_map<std::string, wvalue>
+ >(
+ new std::unordered_map<std::string, wvalue>{});
+ for(auto it = r.begin(); it != r.end(); ++it)
+ o->emplace(it->key(), *it);
+ return;
+ }
+ }
+
+ wvalue(wvalue&& r)
+ {
+ *this = std::move(r);
+ }
+
+ wvalue& operator = (wvalue&& r)
+ {
+ t_ = r.t_;
+ d = r.d;
+ s = std::move(r.s);
+ l = std::move(r.l);
+ o = std::move(r.o);
+ return *this;
+ }
+
+ void clear()
+ {
+ t_ = type::Null;
+ l.reset();
+ o.reset();
+ }
+
+ void reset()
+ {
+ t_ = type::Null;
+ l.reset();
+ o.reset();
+ }
+
+ wvalue& operator = (std::nullptr_t)
+ {
+ reset();
+ return *this;
+ }
+ wvalue& operator = (bool value)
+ {
+ reset();
+ if (value)
+ t_ = type::True;
+ else
+ t_ = type::False;
+ return *this;
+ }
+
+ wvalue& operator = (double value)
+ {
+ reset();
+ t_ = type::Number;
+ d = value;
+ return *this;
+ }
+
+ wvalue& operator = (unsigned short value)
+ {
+ reset();
+ t_ = type::Number;
+ d = (double)value;
+ return *this;
+ }
+
+ wvalue& operator = (short value)
+ {
+ reset();
+ t_ = type::Number;
+ d = (double)value;
+ return *this;
+ }
+
+ wvalue& operator = (long long value)
+ {
+ reset();
+ t_ = type::Number;
+ d = (double)value;
+ return *this;
+ }
+
+ wvalue& operator = (long value)
+ {
+ reset();
+ t_ = type::Number;
+ d = (double)value;
+ return *this;
+ }
+
+ wvalue& operator = (int value)
+ {
+ reset();
+ t_ = type::Number;
+ d = (double)value;
+ return *this;
+ }
+
+ wvalue& operator = (unsigned long long value)
+ {
+ reset();
+ t_ = type::Number;
+ d = (double)value;
+ return *this;
+ }
+
+ wvalue& operator = (unsigned long value)
+ {
+ reset();
+ t_ = type::Number;
+ d = (double)value;
+ return *this;
+ }
+
+ wvalue& operator = (unsigned int value)
+ {
+ reset();
+ t_ = type::Number;
+ d = (double)value;
+ return *this;
+ }
+
+ wvalue& operator=(const char* str)
+ {
+ reset();
+ t_ = type::String;
+ s = str;
+ return *this;
+ }
+
+ wvalue& operator=(const std::string& str)
+ {
+ reset();
+ t_ = type::String;
+ s = str;
+ return *this;
+ }
+
+ template <typename T>
+ wvalue& operator=(const std::vector<T>& v)
+ {
+ if (t_ != type::List)
+ reset();
+ t_ = type::List;
+ if (!l)
+ l = std::unique_ptr<std::vector<wvalue>>(new std::vector<wvalue>{});
+ l->clear();
+ l->resize(v.size());
+ size_t idx = 0;
+ for(auto& x:v)
+ {
+ (*l)[idx++] = x;
+ }
+ return *this;
+ }
+
+ wvalue& operator[](unsigned index)
+ {
+ if (t_ != type::List)
+ reset();
+ t_ = type::List;
+ if (!l)
+ l = std::unique_ptr<std::vector<wvalue>>(new std::vector<wvalue>{});
+ if (l->size() < index+1)
+ l->resize(index+1);
+ return (*l)[index];
+ }
+
+ int count(const std::string& str)
+ {
+ if (t_ != type::Object)
+ return 0;
+ if (!o)
+ return 0;
+ return o->count(str);
+ }
+
+ wvalue& operator[](const std::string& str)
+ {
+ if (t_ != type::Object)
+ reset();
+ t_ = type::Object;
+ if (!o)
+ o = std::unique_ptr<
+ std::unordered_map<std::string, wvalue>
+ >(
+ new std::unordered_map<std::string, wvalue>{});
+ return (*o)[str];
+ }
+
+ size_t estimate_length() const
+ {
+ switch(t_)
+ {
+ case type::Null: return 4;
+ case type::False: return 5;
+ case type::True: return 4;
+ case type::Number: return 30;
+ case type::String: return 2+s.size()+s.size()/2;
+ case type::List:
+ {
+ size_t sum{};
+ if (l)
+ {
+ for(auto& x:*l)
+ {
+ sum += 1;
+ sum += x.estimate_length();
+ }
+ }
+ return sum+2;
+ }
+ case type::Object:
+ {
+ size_t sum{};
+ if (o)
+ {
+ for(auto& kv:*o)
+ {
+ sum += 2;
+ sum += 2+kv.first.size()+kv.first.size()/2;
+ sum += kv.second.estimate_length();
+ }
+ }
+ return sum+2;
+ }
+ }
+ return 1;
+ }
+
+
+ friend void dump_internal(const wvalue& v, std::string& out);
+ friend std::string dump(const wvalue& v);
+ };
+
+ inline void dump_string(const std::string& str, std::string& out)
+ {
+ out.push_back('"');
+ escape(str, out);
+ out.push_back('"');
+ }
+ inline void dump_internal(const wvalue& v, std::string& out)
+ {
+ switch(v.t_)
+ {
+ case type::Null: out += "null"; break;
+ case type::False: out += "false"; break;
+ case type::True: out += "true"; break;
+ case type::Number:
+ {
+ char outbuf[128];
+ sprintf(outbuf, "%g", v.d);
+ out += outbuf;
+ }
+ break;
+ case type::String: dump_string(v.s, out); break;
+ case type::List:
+ {
+ out.push_back('[');
+ if (v.l)
+ {
+ bool first = true;
+ for(auto& x:*v.l)
+ {
+ if (!first)
+ {
+ out.push_back(',');
+ }
+ first = false;
+ dump_internal(x, out);
+ }
+ }
+ out.push_back(']');
+ }
+ break;
+ case type::Object:
+ {
+ out.push_back('{');
+ if (v.o)
+ {
+ bool first = true;
+ for(auto& kv:*v.o)
+ {
+ if (!first)
+ {
+ out.push_back(',');
+ }
+ first = false;
+ dump_string(kv.first, out);
+ out.push_back(':');
+ dump_internal(kv.second, out);
+ }
+ }
+ out.push_back('}');
+ }
+ break;
+ }
+ }
+
+ inline std::string dump(const wvalue& v)
+ {
+ std::string ret;
+ ret.reserve(v.estimate_length());
+ dump_internal(v, ret);
+ return ret;
+ }
+
+ //std::vector<boost::asio::const_buffer> dump_ref(wvalue& v)
+ //{
+ //}
+ }
+}
+
+#undef crow_json_likely
+#undef crow_json_unlikely
+
+
+
+#pragma once
+#include <string>
+#include <vector>
+#include <fstream>
+#include <iterator>
+#include <functional>
+
+
+namespace crow
+{
+ namespace mustache
+ {
+ using context = json::wvalue;
+
+ template_t load(const std::string& filename);
+
+ class invalid_template_exception : public std::exception
+ {
+ public:
+ invalid_template_exception(const std::string& msg)
+ : msg("crow::mustache error: " + msg)
+ {
+ }
+ virtual const char* what() const throw()
+ {
+ return msg.c_str();
+ }
+ std::string msg;
+ };
+
+ enum class ActionType
+ {
+ Ignore,
+ Tag,
+ UnescapeTag,
+ OpenBlock,
+ CloseBlock,
+ ElseBlock,
+ Partial,
+ };
+
+ struct Action
+ {
+ int start;
+ int end;
+ int pos;
+ ActionType t;
+ Action(ActionType t, int start, int end, int pos = 0)
+ : start(start), end(end), pos(pos), t(t)
+ {}
+ };
+
+ class template_t
+ {
+ public:
+ template_t(std::string body)
+ : body_(std::move(body))
+ {
+ // {{ {{# {{/ {{^ {{! {{> {{=
+ parse();
+ }
+
+ private:
+ std::string tag_name(const Action& action)
+ {
+ return body_.substr(action.start, action.end - action.start);
+ }
+ auto find_context(const std::string& name, const std::vector<context*>& stack)->std::pair<bool, context&>
+ {
+ if (name == ".")
+ {
+ return {true, *stack.back()};
+ }
+ int dotPosition = name.find(".");
+ if (dotPosition == (int)name.npos)
+ {
+ for(auto it = stack.rbegin(); it != stack.rend(); ++it)
+ {
+ if ((*it)->t() == json::type::Object)
+ {
+ if ((*it)->count(name))
+ return {true, (**it)[name]};
+ }
+ }
+ }
+ else
+ {
+ std::vector<int> dotPositions;
+ dotPositions.push_back(-1);
+ while(dotPosition != (int)name.npos)
+ {
+ dotPositions.push_back(dotPosition);
+ dotPosition = name.find(".", dotPosition+1);
+ }
+ dotPositions.push_back(name.size());
+ std::vector<std::string> names;
+ names.reserve(dotPositions.size()-1);
+ for(int i = 1; i < (int)dotPositions.size(); i ++)
+ names.emplace_back(name.substr(dotPositions[i-1]+1, dotPositions[i]-dotPositions[i-1]-1));
+
+ for(auto it = stack.rbegin(); it != stack.rend(); ++it)
+ {
+ context* view = *it;
+ bool found = true;
+ for(auto jt = names.begin(); jt != names.end(); ++jt)
+ {
+ if (view->t() == json::type::Object &&
+ view->count(*jt))
+ {
+ view = &(*view)[*jt];
+ }
+ else
+ {
+ found = false;
+ break;
+ }
+ }
+ if (found)
+ return {true, *view};
+ }
+
+ }
+
+ static json::wvalue empty_str;
+ empty_str = "";
+ return {false, empty_str};
+ }
+
+ void escape(const std::string& in, std::string& out)
+ {
+ out.reserve(out.size() + in.size());
+ for(auto it = in.begin(); it != in.end(); ++it)
+ {
+ switch(*it)
+ {
+ case '&': out += "&amp;"; break;
+ case '<': out += "&lt;"; break;
+ case '>': out += "&gt;"; break;
+ case '"': out += "&quot;"; break;
+ case '\'': out += "&#39;"; break;
+ case '/': out += "&#x2F;"; break;
+ default: out += *it; break;
+ }
+ }
+ }
+
+ void render_internal(int actionBegin, int actionEnd, std::vector<context*>& stack, std::string& out, int indent)
+ {
+ int current = actionBegin;
+
+ if (indent)
+ out.insert(out.size(), indent, ' ');
+
+ while(current < actionEnd)
+ {
+ auto& fragment = fragments_[current];
+ auto& action = actions_[current];
+ render_fragment(fragment, indent, out);
+ switch(action.t)
+ {
+ case ActionType::Ignore:
+ // do nothing
+ break;
+ case ActionType::Partial:
+ {
+ std::string partial_name = tag_name(action);
+ auto partial_templ = load(partial_name);
+ int partial_indent = action.pos;
+ partial_templ.render_internal(0, partial_templ.fragments_.size()-1, stack, out, partial_indent?indent+partial_indent:0);
+ }
+ break;
+ case ActionType::UnescapeTag:
+ case ActionType::Tag:
+ {
+ auto optional_ctx = find_context(tag_name(action), stack);
+ auto& ctx = optional_ctx.second;
+ switch(ctx.t())
+ {
+ case json::type::Number:
+ out += json::dump(ctx);
+ break;
+ case json::type::String:
+ if (action.t == ActionType::Tag)
+ escape(ctx.s, out);
+ else
+ out += ctx.s;
+ break;
+ default:
+ throw std::runtime_error("not implemented tag type" + boost::lexical_cast<std::string>((int)ctx.t()));
+ }
+ }
+ break;
+ case ActionType::ElseBlock:
+ {
+ static context nullContext;
+ auto optional_ctx = find_context(tag_name(action), stack);
+ if (!optional_ctx.first)
+ {
+ stack.emplace_back(&nullContext);
+ break;
+ }
+
+ auto& ctx = optional_ctx.second;
+ switch(ctx.t())
+ {
+ case json::type::List:
+ if (ctx.l && !ctx.l->empty())
+ current = action.pos;
+ else
+ stack.emplace_back(&nullContext);
+ break;
+ case json::type::False:
+ case json::type::Null:
+ stack.emplace_back(&nullContext);
+ break;
+ default:
+ current = action.pos;
+ break;
+ }
+ break;
+ }
+ case ActionType::OpenBlock:
+ {
+ auto optional_ctx = find_context(tag_name(action), stack);
+ if (!optional_ctx.first)
+ {
+ current = action.pos;
+ break;
+ }
+
+ auto& ctx = optional_ctx.second;
+ switch(ctx.t())
+ {
+ case json::type::List:
+ if (ctx.l)
+ for(auto it = ctx.l->begin(); it != ctx.l->end(); ++it)
+ {
+ stack.push_back(&*it);
+ render_internal(current+1, action.pos, stack, out, indent);
+ stack.pop_back();
+ }
+ current = action.pos;
+ break;
+ case json::type::Number:
+ case json::type::String:
+ case json::type::Object:
+ case json::type::True:
+ stack.push_back(&ctx);
+ break;
+ case json::type::False:
+ case json::type::Null:
+ current = action.pos;
+ break;
+ default:
+ throw std::runtime_error("{{#: not implemented context type: " + boost::lexical_cast<std::string>((int)ctx.t()));
+ break;
+ }
+ break;
+ }
+ case ActionType::CloseBlock:
+ stack.pop_back();
+ break;
+ default:
+ throw std::runtime_error("not implemented " + boost::lexical_cast<std::string>((int)action.t));
+ }
+ current++;
+ }
+ auto& fragment = fragments_[actionEnd];
+ render_fragment(fragment, indent, out);
+ }
+ void render_fragment(const std::pair<int, int> fragment, int indent, std::string& out)
+ {
+ if (indent)
+ {
+ for(int i = fragment.first; i < fragment.second; i ++)
+ {
+ out += body_[i];
+ if (body_[i] == '\n' && i+1 != (int)body_.size())
+ out.insert(out.size(), indent, ' ');
+ }
+ }
+ else
+ out.insert(out.size(), body_, fragment.first, fragment.second-fragment.first);
+ }
+ public:
+ std::string render()
+ {
+ context empty_ctx;
+ std::vector<context*> stack;
+ stack.emplace_back(&empty_ctx);
+
+ std::string ret;
+ render_internal(0, fragments_.size()-1, stack, ret, 0);
+ return ret;
+ }
+ std::string render(context& ctx)
+ {
+ std::vector<context*> stack;
+ stack.emplace_back(&ctx);
+
+ std::string ret;
+ render_internal(0, fragments_.size()-1, stack, ret, 0);
+ return ret;
+ }
+
+ private:
+
+ void parse()
+ {
+ std::string tag_open = "{{";
+ std::string tag_close = "}}";
+
+ std::vector<int> blockPositions;
+
+ size_t current = 0;
+ while(1)
+ {
+ size_t idx = body_.find(tag_open, current);
+ if (idx == body_.npos)
+ {
+ fragments_.emplace_back(current, body_.size());
+ actions_.emplace_back(ActionType::Ignore, 0, 0);
+ break;
+ }
+ fragments_.emplace_back(current, idx);
+
+ idx += tag_open.size();
+ size_t endIdx = body_.find(tag_close, idx);
+ if (endIdx == idx)
+ {
+ throw invalid_template_exception("empty tag is not allowed");
+ }
+ if (endIdx == body_.npos)
+ {
+ // error, no matching tag
+ throw invalid_template_exception("not matched opening tag");
+ }
+ current = endIdx + tag_close.size();
+ switch(body_[idx])
+ {
+ case '#':
+ idx++;
+ while(body_[idx] == ' ') idx++;
+ while(body_[endIdx-1] == ' ') endIdx--;
+ blockPositions.emplace_back(actions_.size());
+ actions_.emplace_back(ActionType::OpenBlock, idx, endIdx);
+ break;
+ case '/':
+ idx++;
+ while(body_[idx] == ' ') idx++;
+ while(body_[endIdx-1] == ' ') endIdx--;
+ {
+ auto& matched = actions_[blockPositions.back()];
+ if (body_.compare(idx, endIdx-idx,
+ body_, matched.start, matched.end - matched.start) != 0)
+ {
+ throw invalid_template_exception("not matched {{# {{/ pair: " +
+ body_.substr(matched.start, matched.end - matched.start) + ", " +
+ body_.substr(idx, endIdx-idx));
+ }
+ matched.pos = actions_.size();
+ }
+ actions_.emplace_back(ActionType::CloseBlock, idx, endIdx, blockPositions.back());
+ blockPositions.pop_back();
+ break;
+ case '^':
+ idx++;
+ while(body_[idx] == ' ') idx++;
+ while(body_[endIdx-1] == ' ') endIdx--;
+ blockPositions.emplace_back(actions_.size());
+ actions_.emplace_back(ActionType::ElseBlock, idx, endIdx);
+ break;
+ case '!':
+ // do nothing action
+ actions_.emplace_back(ActionType::Ignore, idx+1, endIdx);
+ break;
+ case '>': // partial
+ idx++;
+ while(body_[idx] == ' ') idx++;
+ while(body_[endIdx-1] == ' ') endIdx--;
+ actions_.emplace_back(ActionType::Partial, idx, endIdx);
+ break;
+ case '{':
+ if (tag_open != "{{" || tag_close != "}}")
+ throw invalid_template_exception("cannot use triple mustache when delimiter changed");
+
+ idx ++;
+ if (body_[endIdx+2] != '}')
+ {
+ throw invalid_template_exception("{{{: }}} not matched");
+ }
+ while(body_[idx] == ' ') idx++;
+ while(body_[endIdx-1] == ' ') endIdx--;
+ actions_.emplace_back(ActionType::UnescapeTag, idx, endIdx);
+ current++;
+ break;
+ case '&':
+ idx ++;
+ while(body_[idx] == ' ') idx++;
+ while(body_[endIdx-1] == ' ') endIdx--;
+ actions_.emplace_back(ActionType::UnescapeTag, idx, endIdx);
+ break;
+ case '=':
+ // tag itself is no-op
+ idx ++;
+ actions_.emplace_back(ActionType::Ignore, idx, endIdx);
+ endIdx --;
+ if (body_[endIdx] != '=')
+ throw invalid_template_exception("{{=: not matching = tag: "+body_.substr(idx, endIdx-idx));
+ endIdx --;
+ while(body_[idx] == ' ') idx++;
+ while(body_[endIdx] == ' ') endIdx--;
+ endIdx++;
+ {
+ bool succeeded = false;
+ for(size_t i = idx; i < endIdx; i++)
+ {
+ if (body_[i] == ' ')
+ {
+ tag_open = body_.substr(idx, i-idx);
+ while(body_[i] == ' ') i++;
+ tag_close = body_.substr(i, endIdx-i);
+ if (tag_open.empty())
+ throw invalid_template_exception("{{=: empty open tag");
+ if (tag_close.empty())
+ throw invalid_template_exception("{{=: empty close tag");
+
+ if (tag_close.find(" ") != tag_close.npos)
+ throw invalid_template_exception("{{=: invalid open/close tag: "+tag_open+" " + tag_close);
+ succeeded = true;
+ break;
+ }
+ }
+ if (!succeeded)
+ throw invalid_template_exception("{{=: cannot find space between new open/close tags");
+ }
+ break;
+ default:
+ // normal tag case;
+ while(body_[idx] == ' ') idx++;
+ while(body_[endIdx-1] == ' ') endIdx--;
+ actions_.emplace_back(ActionType::Tag, idx, endIdx);
+ break;
+ }
+ }
+
+ // removing standalones
+ for(int i = actions_.size()-2; i >= 0; i --)
+ {
+ if (actions_[i].t == ActionType::Tag || actions_[i].t == ActionType::UnescapeTag)
+ continue;
+ auto& fragment_before = fragments_[i];
+ auto& fragment_after = fragments_[i+1];
+ bool is_last_action = i == (int)actions_.size()-2;
+ bool all_space_before = true;
+ int j, k;
+ for(j = fragment_before.second-1;j >= fragment_before.first;j--)
+ {
+ if (body_[j] != ' ')
+ {
+ all_space_before = false;
+ break;
+ }
+ }
+ if (all_space_before && i > 0)
+ continue;
+ if (!all_space_before && body_[j] != '\n')
+ continue;
+ bool all_space_after = true;
+ for(k = fragment_after.first; k < (int)body_.size() && k < fragment_after.second; k ++)
+ {
+ if (body_[k] != ' ')
+ {
+ all_space_after = false;
+ break;
+ }
+ }
+ if (all_space_after && !is_last_action)
+ continue;
+ if (!all_space_after &&
+ !(
+ body_[k] == '\n'
+ ||
+ (body_[k] == '\r' &&
+ k + 1 < (int)body_.size() &&
+ body_[k+1] == '\n')))
+ continue;
+ if (actions_[i].t == ActionType::Partial)
+ {
+ actions_[i].pos = fragment_before.second - j - 1;
+ }
+ fragment_before.second = j+1;
+ if (!all_space_after)
+ {
+ if (body_[k] == '\n')
+ k++;
+ else
+ k += 2;
+ fragment_after.first = k;
+ }
+ }
+ }
+
+ std::vector<std::pair<int,int>> fragments_;
+ std::vector<Action> actions_;
+ std::string body_;
+ };
+
+ inline template_t compile(const std::string& body)
+ {
+ return template_t(body);
+ }
+ namespace detail
+ {
+ inline std::string& get_template_base_directory_ref()
+ {
+ static std::string template_base_directory = "templates";
+ return template_base_directory;
+ }
+ }
+
+ inline std::string default_loader(const std::string& filename)
+ {
+ std::string path = detail::get_template_base_directory_ref();
+ if (!(path.back() == '/' || path.back() == '\\'))
+ path += '/';
+ path += filename;
+ std::ifstream inf(path);
+ if (!inf)
+ return {};
+ return {std::istreambuf_iterator<char>(inf), std::istreambuf_iterator<char>()};
+ }
+
+ namespace detail
+ {
+ inline std::function<std::string (std::string)>& get_loader_ref()
+ {
+ static std::function<std::string (std::string)> loader = default_loader;
+ return loader;
+ }
+ }
+
+ inline void set_base(const std::string& path)
+ {
+ auto& base = detail::get_template_base_directory_ref();
+ base = path;
+ if (base.back() != '\\' &&
+ base.back() != '/')
+ {
+ base += '/';
+ }
+ }
+
+ inline void set_loader(std::function<std::string(std::string)> loader)
+ {
+ detail::get_loader_ref() = std::move(loader);
+ }
+
+ inline std::string load_text(const std::string& filename)
+ {
+ return detail::get_loader_ref()(filename);
+ }
+
+ inline template_t load(const std::string& filename)
+ {
+ return compile(detail::get_loader_ref()(filename));
+ }
+ }
+}
+
+
+
/* merged revision: 5b951d74bd66ec9d38448e0a85b1cf8b85d97db3 */
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
@@ -3226,710 +6114,86 @@ http_parser_version(void) {
#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
-
-/* #ifdef - enables ssl */
-//#define CROW_ENABLE_SSL
-
-/* #define - specifies log level */
-/*
- Debug = 0
- Info = 1
- Warning = 2
- Error = 3
- Critical = 4
-
- default to INFO
-*/
-#define CROW_LOG_LEVEL 1
-
-
-// compiler flags
-#if __cplusplus >= 201402L
-#define CROW_CAN_USE_CPP14
-#endif
-
-#if defined(_MSC_VER)
-#if _MSC_VER < 1900
-#define CROW_MSVC_WORKAROUND
-#define constexpr const
-#define noexcept throw()
-#endif
-#endif
-
-
-#pragma once
#include <boost/asio.hpp>
-#ifdef CROW_ENABLE_SSL
-#include <boost/asio/ssl.hpp>
-#endif
-
-
-namespace crow
-{
- using namespace boost;
- using tcp = asio::ip::tcp;
-
- struct SocketAdaptor
- {
- using context = void;
- SocketAdaptor(boost::asio::io_service& io_service, context*)
- : socket_(io_service)
- {
- }
-
- boost::asio::io_service& get_io_service()
- {
- return socket_.get_io_service();
- }
-
- tcp::socket& raw_socket()
- {
- return socket_;
- }
-
- tcp::socket& socket()
- {
- return socket_;
- }
-
- tcp::endpoint remote_endpoint()
- {
- return socket_.remote_endpoint();
- }
-
- bool is_open()
- {
- return socket_.is_open();
- }
-
- void close()
- {
- socket_.close();
- }
-
- template <typename F>
- void start(F f)
- {
- f(boost::system::error_code());
- }
-
- tcp::socket socket_;
- };
-
-#ifdef CROW_ENABLE_SSL
- struct SSLAdaptor
- {
- using context = boost::asio::ssl::context;
- using ssl_socket_t = boost::asio::ssl::stream<tcp::socket>;
- SSLAdaptor(boost::asio::io_service& io_service, context* ctx)
- : ssl_socket_(new ssl_socket_t(io_service, *ctx))
- {
- }
-
- boost::asio::ssl::stream<tcp::socket>& socket()
- {
- return *ssl_socket_;
- }
-
- tcp::socket::lowest_layer_type&
- raw_socket()
- {
- return ssl_socket_->lowest_layer();
- }
-
- tcp::endpoint remote_endpoint()
- {
- return raw_socket().remote_endpoint();
- }
-
- bool is_open()
- {
- return raw_socket().is_open();
- }
-
- void close()
- {
- raw_socket().close();
- }
-
- boost::asio::io_service& get_io_service()
- {
- return raw_socket().get_io_service();
- }
-
- template <typename F>
- void start(F f)
- {
- ssl_socket_->async_handshake(boost::asio::ssl::stream_base::server,
- [f](const boost::system::error_code& ec) {
- f(ec);
- });
- }
-
- std::unique_ptr<boost::asio::ssl::stream<tcp::socket>> ssl_socket_;
- };
-#endif
-}
-
-
-
-#pragma once
-
-#include <cstdint>
-#include <stdexcept>
-#include <tuple>
-#include <type_traits>
-#include <cstring>
+#include <deque>
#include <functional>
-#include <string>
+#include <chrono>
+#include <thread>
namespace crow
{
- namespace black_magic
+ namespace detail
{
-#ifndef CROW_MSVC_WORKAROUND
- 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
+ // fast timer queue for fixed tick value.
+ class dumb_timer_queue
{
- const char * const begin_;
- unsigned size_;
+ public:
+ using key = std::pair<dumb_timer_queue*, int>;
- 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];
- }
+ void cancel(key& k)
+ {
+ auto self = k.first;
+ k.first = nullptr;
+ if (!self)
+ return;
- constexpr operator const char *() const {
- return begin_;
+ unsigned int index = (unsigned int)(k.second - self->step_);
+ if (index < self->dq_.size())
+ self->dq_[index].second = nullptr;
}
- constexpr const char* begin() const { return begin_; }
- constexpr const char* end() const { return begin_ + size_; }
+ key add(std::function<void()> f)
+ {
+ dq_.emplace_back(std::chrono::steady_clock::now(), std::move(f));
+ int ret = step_+dq_.size()-1;
- constexpr unsigned size() const {
- return size_;
+ CROW_LOG_DEBUG << "timer add inside: " << this << ' ' << ret ;
+ return {this, ret};
}
- };
-
- 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);
- }
-#endif
- template <typename T>
- struct parameter_tag
- {
- static const int value = 0;
- };
-#define CROW_INTERNAL_PARAMETER_TAG(t, i) \
-template <> \
-struct parameter_tag<t> \
-{ \
- static const int value = i; \
-}
- CROW_INTERNAL_PARAMETER_TAG(int, 1);
- CROW_INTERNAL_PARAMETER_TAG(char, 1);
- CROW_INTERNAL_PARAMETER_TAG(short, 1);
- CROW_INTERNAL_PARAMETER_TAG(long, 1);
- CROW_INTERNAL_PARAMETER_TAG(long long, 1);
- CROW_INTERNAL_PARAMETER_TAG(unsigned int, 2);
- CROW_INTERNAL_PARAMETER_TAG(unsigned char, 2);
- CROW_INTERNAL_PARAMETER_TAG(unsigned short, 2);
- CROW_INTERNAL_PARAMETER_TAG(unsigned long, 2);
- CROW_INTERNAL_PARAMETER_TAG(unsigned long long, 2);
- CROW_INTERNAL_PARAMETER_TAG(double, 3);
- CROW_INTERNAL_PARAMETER_TAG(std::string, 4);
-#undef CROW_INTERNAL_PARAMETER_TAG
- template <typename ... Args>
- struct compute_parameter_tag_from_args_list;
-
- template <>
- struct compute_parameter_tag_from_args_list<>
- {
- static const int value = 0;
- };
-
- template <typename Arg, typename ... Args>
- struct compute_parameter_tag_from_args_list<Arg, Args...>
- {
- static const int sub_value =
- compute_parameter_tag_from_args_list<Args...>::value;
- static const int value =
- parameter_tag<typename std::decay<Arg>::type>::value
- ? sub_value* 6 + parameter_tag<typename std::decay<Arg>::type>::value
- : sub_value;
- };
-
- static inline bool is_parameter_tag_compatible(uint64_t a, uint64_t b)
- {
- if (a == 0)
- return b == 0;
- if (b == 0)
- return a == 0;
- int sa = a%6;
- int sb = a%6;
- if (sa == 5) sa = 4;
- if (sb == 5) sb = 4;
- if (sa != sb)
- return false;
- return is_parameter_tag_compatible(a/6, b/6);
- }
-
- static inline unsigned find_closing_tag_runtime(const char* s, unsigned p)
- {
- return
- s[p] == 0
- ? throw std::runtime_error("unmatched tag <") :
- s[p] == '>'
- ? p : find_closing_tag_runtime(s, p + 1);
- }
-
- static inline uint64_t get_parameter_tag_runtime(const char* s, unsigned p = 0)
- {
- return
- s[p] == 0
- ? 0 :
- s[p] == '<' ? (
- std::strncmp(s+p, "<int>", 5) == 0
- ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 1 :
- std::strncmp(s+p, "<uint>", 6) == 0
- ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 2 :
- (std::strncmp(s+p, "<float>", 7) == 0 ||
- std::strncmp(s+p, "<double>", 8) == 0)
- ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 3 :
- (std::strncmp(s+p, "<str>", 5) == 0 ||
- std::strncmp(s+p, "<string>", 8) == 0)
- ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 4 :
- std::strncmp(s+p, "<path>", 6) == 0
- ? get_parameter_tag_runtime(s, find_closing_tag_runtime(s, p)) * 6 + 5 :
- throw std::runtime_error("invalid parameter type")
- ) :
- get_parameter_tag_runtime(s, p+1);
- }
-#ifndef CROW_MSVC_WORKAROUND
- 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);
- }
-#endif
-
- 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
- {
- };
-
- template <typename T>
- struct promote
- {
- using type = T;
- };
-
-#define CROW_INTERNAL_PROMOTE_TYPE(t1, t2) \
- template<> \
- struct promote<t1> \
- { \
- using type = t2; \
- }
-
- CROW_INTERNAL_PROMOTE_TYPE(char, int64_t);
- CROW_INTERNAL_PROMOTE_TYPE(short, int64_t);
- CROW_INTERNAL_PROMOTE_TYPE(int, int64_t);
- CROW_INTERNAL_PROMOTE_TYPE(long, int64_t);
- CROW_INTERNAL_PROMOTE_TYPE(long long, int64_t);
- CROW_INTERNAL_PROMOTE_TYPE(unsigned char, uint64_t);
- CROW_INTERNAL_PROMOTE_TYPE(unsigned short, uint64_t);
- CROW_INTERNAL_PROMOTE_TYPE(unsigned int, uint64_t);
- CROW_INTERNAL_PROMOTE_TYPE(unsigned long, uint64_t);
- CROW_INTERNAL_PROMOTE_TYPE(unsigned long long, uint64_t);
- CROW_INTERNAL_PROMOTE_TYPE(float, double);
-#undef CROW_INTERNAL_PROMOTE_TYPE
-
- template <typename T>
- using promote_t = typename promote<T>::type;
-
- } // namespace black_magic
-
- namespace detail
- {
-
- template <class T, std::size_t N, class... Args>
- struct get_index_of_element_from_tuple_by_type_impl
- {
- static constexpr auto value = N;
- };
-
- template <class T, std::size_t N, class... Args>
- struct get_index_of_element_from_tuple_by_type_impl<T, N, T, Args...>
- {
- static constexpr auto value = N;
- };
-
- template <class T, std::size_t N, class U, class... Args>
- struct get_index_of_element_from_tuple_by_type_impl<T, N, U, Args...>
- {
- static constexpr auto value = get_index_of_element_from_tuple_by_type_impl<T, N + 1, Args...>::value;
- };
-
- } // namespace detail
-
- namespace utility
- {
- template <class T, class... Args>
- T& get_element_by_type(std::tuple<Args...>& t)
- {
- return std::get<detail::get_index_of_element_from_tuple_by_type_impl<T, 0, Args...>::value>(t);
- }
-
- template<typename T>
- struct function_traits;
-
-#ifndef CROW_MSVC_WORKAROUND
- template<typename T>
- struct function_traits : public function_traits<decltype(&T::operator())>
- {
- using parent_t = function_traits<decltype(&T::operator())>;
- static const size_t arity = parent_t::arity;
- using result_type = typename parent_t::result_type;
- template <size_t i>
- using arg = typename parent_t::template arg<i>;
-
- };
-#endif
-
- template<typename ClassType, typename R, typename ...Args>
- struct function_traits<R(ClassType::*)(Args...) const>
- {
- static const size_t arity = sizeof...(Args);
-
- typedef R result_type;
-
- template <size_t i>
- using arg = typename std::tuple_element<i, std::tuple<Args...>>::type;
- };
-
- template<typename ClassType, typename R, typename ...Args>
- struct function_traits<R(ClassType::*)(Args...)>
- {
- static const size_t arity = sizeof...(Args);
-
- typedef R result_type;
-
- template <size_t i>
- using arg = typename std::tuple_element<i, std::tuple<Args...>>::type;
- };
-
- template<typename R, typename ...Args>
- struct function_traits<std::function<R(Args...)>>
- {
- static const size_t arity = sizeof...(Args);
-
- typedef R result_type;
-
- template <size_t i>
- using arg = typename std::tuple_element<i, std::tuple<Args...>>::type;
- };
-
- inline static std::string base64encode(const char* data, size_t size, const char* key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
- {
- std::string ret;
- ret.resize((size+2) / 3 * 4);
- auto it = ret.begin();
- while(size >= 3)
+ void process()
{
- *it++ = key[(((unsigned char)*data)&0xFC)>>2];
- unsigned char h = (((unsigned char)*data++) & 0x03) << 4;
- *it++ = key[h|((((unsigned char)*data)&0xF0)>>4)];
- h = (((unsigned char)*data++) & 0x0F) << 2;
- *it++ = key[h|((((unsigned char)*data)&0xC0)>>6)];
- *it++ = key[((unsigned char)*data++)&0x3F];
+ if (!io_service_)
+ return;
- size -= 3;
+ auto now = std::chrono::steady_clock::now();
+ while(!dq_.empty())
+ {
+ auto& x = dq_.front();
+ if (now - x.first < std::chrono::seconds(tick))
+ break;
+ if (x.second)
+ {
+ CROW_LOG_DEBUG << "timer call: " << this << ' ' << step_;
+ // we know that timer handlers are very simple currenty; call here
+ x.second();
+ }
+ dq_.pop_front();
+ step_++;
+ }
}
- if (size == 1)
+
+ void set_io_service(boost::asio::io_service& io_service)
{
- *it++ = key[(((unsigned char)*data)&0xFC)>>2];
- unsigned char h = (((unsigned char)*data++) & 0x03) << 4;
- *it++ = key[h];
- *it++ = '=';
- *it++ = '=';
+ io_service_ = &io_service;
}
- else if (size == 2)
+
+ dumb_timer_queue() noexcept
{
- *it++ = key[(((unsigned char)*data)&0xFC)>>2];
- unsigned char h = (((unsigned char)*data++) & 0x03) << 4;
- *it++ = key[h|((((unsigned char)*data)&0xF0)>>4)];
- h = (((unsigned char)*data++) & 0x0F) << 2;
- *it++ = key[h];
- *it++ = '=';
}
- return ret;
- }
-
- inline static std::string base64encode_urlsafe(const char* data, size_t size)
- {
- return base64encode(data, size, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
- }
+ private:
- } // namespace utility
+ int tick{5};
+ boost::asio::io_service* io_service_{};
+ std::deque<std::pair<decltype(std::chrono::steady_clock::now()), std::function<void()>>> dq_;
+ int step_{};
+ };
+ }
}
@@ -4077,2250 +6341,37 @@ constexpr crow::HTTPMethod operator "" _method(const char* str, size_t /*len*/)
#pragma once
-#include <string>
-#include <cstdio>
-#include <cstdlib>
-#include <ctime>
-#include <iostream>
-#include <sstream>
-
-
-
-
-namespace crow
-{
- enum class LogLevel
- {
-#ifndef ERROR
- DEBUG = 0,
- INFO,
- WARNING,
- ERROR,
- CRITICAL,
-#endif
-
- Debug = 0,
- 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);
-
- tm my_tm;
-
-#ifdef _MSC_VER
- gmtime_s(&my_tm, &t);
-#else
- gmtime_r(&t, &my_tm);
-#endif
-
- size_t sz = strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", &my_tm);
- return std::string(date, date+sz);
- }
-
- 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>
-#include <chrono>
-#include <thread>
-
-
-
-
-namespace crow
-{
- namespace detail
- {
- // fast timer queue for fixed tick value.
- class dumb_timer_queue
- {
- public:
- using key = std::pair<dumb_timer_queue*, int>;
-
- void cancel(key& k)
- {
- auto self = k.first;
- k.first = nullptr;
- if (!self)
- return;
-
- unsigned int index = (unsigned int)(k.second - self->step_);
- if (index < self->dq_.size())
- self->dq_[index].second = nullptr;
- }
-
- key add(std::function<void()> f)
- {
- dq_.emplace_back(std::chrono::steady_clock::now(), std::move(f));
- int ret = step_+dq_.size()-1;
-
- CROW_LOG_DEBUG << "timer add inside: " << this << ' ' << ret ;
- return {this, ret};
- }
-
- void process()
- {
- if (!io_service_)
- return;
-
- auto now = std::chrono::steady_clock::now();
- while(!dq_.empty())
- {
- auto& x = dq_.front();
- if (now - x.first < std::chrono::seconds(tick))
- break;
- if (x.second)
- {
- CROW_LOG_DEBUG << "timer call: " << this << ' ' << step_;
- // we know that timer handlers are very simple currenty; call here
- x.second();
- }
- dq_.pop_front();
- step_++;
- }
- }
-
- void set_io_service(boost::asio::io_service& io_service)
- {
- io_service_ = &io_service;
- }
-
- dumb_timer_queue() noexcept
- {
- }
-
- private:
-
- int tick{5};
- boost::asio::io_service* io_service_{};
- std::deque<std::pair<decltype(std::chrono::steady_clock::now()), std::function<void()>>> dq_;
- int step_{};
- };
- }
-}
-
-
-
-#pragma once
-
-//#define CROW_JSON_NO_ERROR_CHECK
-
-#include <string>
-#include <unordered_map>
-#include <iostream>
-#include <algorithm>
-#include <memory>
-#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)
-#define crow_json_unlikely(x) __builtin_expect(x, 0)
-#else
-#define crow_json_likely(x) x
-#define crow_json_unlikely(x) x
-#endif
-
+#include <boost/functional/hash.hpp>
+#include <unordered_map>
namespace crow
{
- namespace mustache
- {
- class template_t;
- }
-
- namespace json
+ struct ci_hash
{
- inline void escape(const std::string& str, std::string& ret)
- {
- ret.reserve(ret.size() + str.size()+str.size()/4);
- for(char c:str)
- {
- switch(c)
- {
- case '"': ret += "\\\""; break;
- case '\\': ret += "\\\\"; break;
- case '\n': ret += "\\n"; break;
- case '\b': ret += "\\b"; break;
- case '\f': ret += "\\f"; break;
- case '\r': ret += "\\r"; break;
- case '\t': ret += "\\t"; break;
- default:
- if (0 <= c && c < 0x20)
- {
- ret += "\\u00";
- auto to_hex = [](char c)
- {
- c = c&0xf;
- if (c < 10)
- return '0' + c;
- return 'a'+c-10;
- };
- ret += to_hex(c/16);
- ret += to_hex(c%16);
- }
- else
- ret += c;
- break;
- }
- }
- }
- inline std::string escape(const std::string& str)
- {
- std::string ret;
- escape(str, ret);
- return ret;
- }
-
- enum class type : char
- {
- Null,
- False,
- True,
- Number,
- String,
- List,
- Object,
- };
-
- inline const char* get_type_str(type t) {
- switch(t){
- case type::Number: return "Number";
- case type::False: return "False";
- case type::True: return "True";
- case type::List: return "List";
- case type::String: return "String";
- case type::Object: return "Object";
- default: return "Unknown";
- }
- }
-
- class rvalue;
- rvalue load(const char* data, size_t size);
-
- namespace detail
- {
-
- struct r_string
- : boost::less_than_comparable<r_string>,
- boost::less_than_comparable<r_string, std::string>,
- boost::equality_comparable<r_string>,
- boost::equality_comparable<r_string, std::string>
- {
- r_string() {};
- r_string(char* s, char* e)
- : s_(s), e_(e)
- {};
- ~r_string()
- {
- if (owned_)
- delete[] s_;
- }
-
- r_string(const r_string& r)
- {
- *this = r;
- }
-
- r_string(r_string&& r)
- {
- *this = r;
- }
-
- r_string& operator = (r_string&& r)
- {
- s_ = r.s_;
- e_ = r.e_;
- owned_ = r.owned_;
- if (r.owned_)
- r.owned_ = 0;
- return *this;
- }
-
- r_string& operator = (const r_string& r)
- {
- s_ = r.s_;
- e_ = r.e_;
- owned_ = 0;
- return *this;
- }
-
- operator std::string () const
- {
- return std::string(s_, e_);
- }
-
-
- const char* begin() const { return s_; }
- const char* end() const { return e_; }
- size_t size() const { return end() - begin(); }
-
- using iterator = const char*;
- using const_iterator = const char*;
-
- char* s_;
- mutable char* e_;
- uint8_t owned_{0};
- friend std::ostream& operator << (std::ostream& os, const r_string& s)
- {
- os << (std::string)s;
- return os;
- }
- private:
- void force(char* s, uint32_t /*length*/)
- {
- s_ = s;
- owned_ = 1;
- }
- friend rvalue crow::json::load(const char* data, size_t size);
- };
-
- inline bool operator < (const r_string& l, const r_string& r)
- {
- return boost::lexicographical_compare(l,r);
- }
-
- inline bool operator < (const r_string& l, const std::string& r)
- {
- return boost::lexicographical_compare(l,r);
- }
-
- inline bool operator > (const r_string& l, const std::string& r)
- {
- return boost::lexicographical_compare(r,l);
- }
-
- inline bool operator == (const r_string& l, const r_string& r)
- {
- return boost::equals(l,r);
- }
-
- inline bool operator == (const r_string& l, const std::string& r)
- {
- return boost::equals(l,r);
- }
- }
-
- class rvalue
- {
- static const int cached_bit = 2;
- static const int error_bit = 4;
- public:
- rvalue() noexcept : option_{error_bit}
- {}
- rvalue(type t) noexcept
- : lsize_{}, lremain_{}, t_{t}
- {}
- rvalue(type t, char* s, char* e) noexcept
- : start_{s},
- end_{e},
- t_{t}
- {}
-
- rvalue(const rvalue& r)
- : start_(r.start_),
- end_(r.end_),
- key_(r.key_),
- t_(r.t_),
- option_(r.option_)
- {
- copy_l(r);
- }
-
- rvalue(rvalue&& r) noexcept
- {
- *this = std::move(r);
- }
-
- rvalue& operator = (const rvalue& r)
- {
- start_ = r.start_;
- end_ = r.end_;
- key_ = r.key_;
- copy_l(r);
- t_ = r.t_;
- option_ = r.option_;
- return *this;
- }
- rvalue& operator = (rvalue&& r) noexcept
- {
- start_ = r.start_;
- end_ = r.end_;
- key_ = std::move(r.key_);
- l_ = std::move(r.l_);
- lsize_ = r.lsize_;
- lremain_ = r.lremain_;
- t_ = r.t_;
- option_ = r.option_;
- return *this;
- }
-
- explicit operator bool() const noexcept
- {
- return (option_ & error_bit) == 0;
- }
-
- explicit operator int64_t() const
- {
- return i();
- }
-
- explicit operator uint64_t() const
- {
- return u();
- }
-
- explicit operator int() const
- {
- return (int)i();
- }
-
- type t() const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- if (option_ & error_bit)
- {
- throw std::runtime_error("invalid json object");
- }
-#endif
- return t_;
- }
-
- int64_t i() const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- switch (t()) {
- case type::Number:
- case type::String:
- return boost::lexical_cast<int64_t>(start_, end_-start_);
- default:
- const std::string msg = "expected number, got: "
- + std::string(get_type_str(t()));
- throw std::runtime_error(msg);
- }
-#endif
- return boost::lexical_cast<int64_t>(start_, end_-start_);
- }
-
- uint64_t u() const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- switch (t()) {
- case type::Number:
- case type::String:
- return boost::lexical_cast<uint64_t>(start_, end_-start_);
- default:
- throw std::runtime_error(std::string("expected number, got: ") + get_type_str(t()));
- }
-#endif
- return boost::lexical_cast<uint64_t>(start_, end_-start_);
- }
-
- double d() const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- if (t() != type::Number)
- throw std::runtime_error("value is not number");
-#endif
- return boost::lexical_cast<double>(start_, end_-start_);
- }
-
- bool b() const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- if (t() != type::True && t() != type::False)
- throw std::runtime_error("value is not boolean");
-#endif
- return t() == type::True;
- }
-
- void unescape() const
- {
- if (*(start_-1))
- {
- char* head = start_;
- char* tail = start_;
- while(head != end_)
- {
- if (*head == '\\')
- {
- switch(*++head)
- {
- case '"': *tail++ = '"'; break;
- case '\\': *tail++ = '\\'; break;
- case '/': *tail++ = '/'; break;
- case 'b': *tail++ = '\b'; break;
- case 'f': *tail++ = '\f'; break;
- case 'n': *tail++ = '\n'; break;
- case 'r': *tail++ = '\r'; break;
- case 't': *tail++ = '\t'; break;
- case 'u':
- {
- auto from_hex = [](char c)
- {
- if (c >= 'a')
- return c - 'a' + 10;
- if (c >= 'A')
- return c - 'A' + 10;
- return c - '0';
- };
- unsigned int code =
- (from_hex(head[1])<<12) +
- (from_hex(head[2])<< 8) +
- (from_hex(head[3])<< 4) +
- from_hex(head[4]);
- if (code >= 0x800)
- {
- *tail++ = 0xE0 | (code >> 12);
- *tail++ = 0x80 | ((code >> 6) & 0x3F);
- *tail++ = 0x80 | (code & 0x3F);
- }
- else if (code >= 0x80)
- {
- *tail++ = 0xC0 | (code >> 6);
- *tail++ = 0x80 | (code & 0x3F);
- }
- else
- {
- *tail++ = code;
- }
- head += 4;
- }
- break;
- }
- }
- else
- *tail++ = *head;
- head++;
- }
- end_ = tail;
- *end_ = 0;
- *(start_-1) = 0;
- }
- }
-
- detail::r_string s() const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- if (t() != type::String)
- throw std::runtime_error("value is not string");
-#endif
- unescape();
- return detail::r_string{start_, end_};
- }
-
- bool has(const char* str) const
- {
- return has(std::string(str));
- }
-
- bool has(const std::string& str) const
- {
- struct Pred
- {
- bool operator()(const rvalue& l, const rvalue& r) const
- {
- return l.key_ < r.key_;
- };
- bool operator()(const rvalue& l, const std::string& r) const
- {
- return l.key_ < r;
- };
- bool operator()(const std::string& l, const rvalue& r) const
- {
- return l < r.key_;
- };
- };
- if (!is_cached())
- {
- std::sort(begin(), end(), Pred());
- set_cached();
- }
- auto it = lower_bound(begin(), end(), str, Pred());
- return it != end() && it->key_ == str;
- }
-
- int count(const std::string& str)
- {
- return has(str) ? 1 : 0;
- }
-
- rvalue* begin() const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- if (t() != type::Object && t() != type::List)
- throw std::runtime_error("value is not a container");
-#endif
- return l_.get();
- }
- rvalue* end() const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- if (t() != type::Object && t() != type::List)
- throw std::runtime_error("value is not a container");
-#endif
- return l_.get()+lsize_;
- }
-
- const detail::r_string& key() const
- {
- return key_;
- }
-
- size_t size() const
- {
- if (t() == type::String)
- return s().size();
-#ifndef CROW_JSON_NO_ERROR_CHECK
- if (t() != type::Object && t() != type::List)
- throw std::runtime_error("value is not a container");
-#endif
- return lsize_;
- }
-
- const rvalue& operator[](int index) const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- if (t() != type::List)
- throw std::runtime_error("value is not a list");
- if (index >= (int)lsize_ || index < 0)
- throw std::runtime_error("list out of bound");
-#endif
- return l_[index];
- }
-
- const rvalue& operator[](size_t index) const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- if (t() != type::List)
- throw std::runtime_error("value is not a list");
- if (index >= lsize_)
- throw std::runtime_error("list out of bound");
-#endif
- return l_[index];
- }
-
- const rvalue& operator[](const char* str) const
- {
- return this->operator[](std::string(str));
- }
-
- const rvalue& operator[](const std::string& str) const
- {
-#ifndef CROW_JSON_NO_ERROR_CHECK
- if (t() != type::Object)
- throw std::runtime_error("value is not an object");
-#endif
- struct Pred
- {
- bool operator()(const rvalue& l, const rvalue& r) const
- {
- return l.key_ < r.key_;
- };
- bool operator()(const rvalue& l, const std::string& r) const
- {
- return l.key_ < r;
- };
- bool operator()(const std::string& l, const rvalue& r) const
- {
- return l < r.key_;
- };
- };
- if (!is_cached())
- {
- std::sort(begin(), end(), Pred());
- set_cached();
- }
- auto it = lower_bound(begin(), end(), str, Pred());
- if (it != end() && it->key_ == str)
- return *it;
-#ifndef CROW_JSON_NO_ERROR_CHECK
- throw std::runtime_error("cannot find key");
-#else
- static rvalue nullValue;
- return nullValue;
-#endif
- }
-
- void set_error()
- {
- option_|=error_bit;
- }
-
- bool error() const
- {
- return (option_&error_bit)!=0;
- }
- private:
- bool is_cached() const
- {
- return (option_&cached_bit)!=0;
- }
- void set_cached() const
- {
- option_ |= cached_bit;
- }
- void copy_l(const rvalue& r)
- {
- if (r.t() != type::Object && r.t() != type::List)
- return;
- lsize_ = r.lsize_;
- lremain_ = 0;
- l_.reset(new rvalue[lsize_]);
- std::copy(r.begin(), r.end(), begin());
- }
-
- void emplace_back(rvalue&& v)
- {
- if (!lremain_)
- {
- int new_size = lsize_ + lsize_;
- if (new_size - lsize_ > 60000)
- new_size = lsize_ + 60000;
- if (new_size < 4)
- new_size = 4;
- rvalue* p = new rvalue[new_size];
- rvalue* p2 = p;
- for(auto& x : *this)
- *p2++ = std::move(x);
- l_.reset(p);
- lremain_ = new_size - lsize_;
- }
- l_[lsize_++] = std::move(v);
- lremain_ --;
- }
-
- mutable char* start_;
- mutable char* end_;
- detail::r_string key_;
- std::unique_ptr<rvalue[]> l_;
- uint32_t lsize_;
- uint16_t lremain_;
- type t_;
- mutable uint8_t option_{0};
-
- friend rvalue load_nocopy_internal(char* data, size_t size);
- friend rvalue load(const char* data, size_t size);
- friend std::ostream& operator <<(std::ostream& os, const rvalue& r)
- {
- switch(r.t_)
- {
-
- case type::Null: os << "null"; break;
- case type::False: os << "false"; break;
- case type::True: os << "true"; break;
- case type::Number: os << r.d(); break;
- case type::String: os << '"' << r.s() << '"'; break;
- case type::List:
- {
- os << '[';
- bool first = true;
- for(auto& x : r)
- {
- if (!first)
- os << ',';
- first = false;
- os << x;
- }
- os << ']';
- }
- break;
- case type::Object:
- {
- os << '{';
- bool first = true;
- for(auto& x : r)
- {
- if (!first)
- os << ',';
- os << '"' << escape(x.key_) << "\":";
- first = false;
- os << x;
- }
- os << '}';
- }
- break;
- }
- return os;
- }
- };
- namespace detail {
- }
-
- inline bool operator == (const rvalue& l, const std::string& r)
- {
- return l.s() == r;
- }
-
- inline bool operator == (const std::string& l, const rvalue& r)
- {
- return l == r.s();
- }
-
- inline bool operator != (const rvalue& l, const std::string& r)
- {
- return l.s() != r;
- }
-
- inline bool operator != (const std::string& l, const rvalue& r)
- {
- return l != r.s();
- }
-
- inline bool operator == (const rvalue& l, double r)
- {
- return l.d() == r;
- }
-
- inline bool operator == (double l, const rvalue& r)
- {
- return l == r.d();
- }
-
- inline bool operator != (const rvalue& l, double r)
- {
- return l.d() != r;
- }
-
- inline bool operator != (double l, const rvalue& r)
- {
- return l != r.d();
- }
-
-
- inline rvalue load_nocopy_internal(char* data, size_t size)
- {
- //static const char* escaped = "\"\\/\b\f\n\r\t";
- struct Parser
- {
- Parser(char* data, size_t /*size*/)
- : data(data)
- {
- }
-
- bool consume(char c)
- {
- if (crow_json_unlikely(*data != c))
- return false;
- data++;
- return true;
- }
-
- void ws_skip()
- {
- while(*data == ' ' || *data == '\t' || *data == '\r' || *data == '\n') ++data;
- };
-
- rvalue decode_string()
- {
- if (crow_json_unlikely(!consume('"')))
- return {};
- char* start = data;
- uint8_t has_escaping = 0;
- while(1)
- {
- if (crow_json_likely(*data != '"' && *data != '\\' && *data != '\0'))
- {
- data ++;
- }
- else if (*data == '"')
- {
- *data = 0;
- *(start-1) = has_escaping;
- data++;
- return {type::String, start, data-1};
- }
- else if (*data == '\\')
- {
- has_escaping = 1;
- data++;
- switch(*data)
- {
- case 'u':
- {
- auto check = [](char c)
- {
- return
- ('0' <= c && c <= '9') ||
- ('a' <= c && c <= 'f') ||
- ('A' <= c && c <= 'F');
- };
- if (!(check(*(data+1)) &&
- check(*(data+2)) &&
- check(*(data+3)) &&
- check(*(data+4))))
- return {};
- }
- data += 5;
- break;
- case '"':
- case '\\':
- case '/':
- case 'b':
- case 'f':
- case 'n':
- case 'r':
- case 't':
- data ++;
- break;
- default:
- return {};
- }
- }
- else
- return {};
- }
- return {};
- }
-
- rvalue decode_list()
- {
- rvalue ret(type::List);
- if (crow_json_unlikely(!consume('[')))
- {
- ret.set_error();
- return ret;
- }
- ws_skip();
- if (crow_json_unlikely(*data == ']'))
- {
- data++;
- return ret;
- }
-
- while(1)
- {
- auto v = decode_value();
- if (crow_json_unlikely(!v))
- {
- ret.set_error();
- break;
- }
- ws_skip();
- ret.emplace_back(std::move(v));
- if (*data == ']')
- {
- data++;
- break;
- }
- if (crow_json_unlikely(!consume(',')))
- {
- ret.set_error();
- break;
- }
- ws_skip();
- }
- return ret;
- }
-
- rvalue decode_number()
- {
- char* start = data;
-
- enum NumberParsingState
- {
- Minus,
- AfterMinus,
- ZeroFirst,
- Digits,
- DigitsAfterPoints,
- E,
- DigitsAfterE,
- Invalid,
- } state{Minus};
- while(crow_json_likely(state != Invalid))
- {
- switch(*data)
- {
- case '0':
- state = (NumberParsingState)"\2\2\7\3\4\6\6"[state];
- /*if (state == NumberParsingState::Minus || state == NumberParsingState::AfterMinus)
- {
- state = NumberParsingState::ZeroFirst;
- }
- else if (state == NumberParsingState::Digits ||
- state == NumberParsingState::DigitsAfterE ||
- state == NumberParsingState::DigitsAfterPoints)
- {
- // ok; pass
- }
- else if (state == NumberParsingState::E)
- {
- state = NumberParsingState::DigitsAfterE;
- }
- else
- return {};*/
- break;
- case '1': case '2': case '3':
- case '4': case '5': case '6':
- case '7': case '8': case '9':
- state = (NumberParsingState)"\3\3\7\3\4\6\6"[state];
- while(*(data+1) >= '0' && *(data+1) <= '9') data++;
- /*if (state == NumberParsingState::Minus || state == NumberParsingState::AfterMinus)
- {
- state = NumberParsingState::Digits;
- }
- else if (state == NumberParsingState::Digits ||
- state == NumberParsingState::DigitsAfterE ||
- state == NumberParsingState::DigitsAfterPoints)
- {
- // ok; pass
- }
- else if (state == NumberParsingState::E)
- {
- state = NumberParsingState::DigitsAfterE;
- }
- else
- return {};*/
- break;
- case '.':
- state = (NumberParsingState)"\7\7\4\4\7\7\7"[state];
- /*
- if (state == NumberParsingState::Digits || state == NumberParsingState::ZeroFirst)
- {
- state = NumberParsingState::DigitsAfterPoints;
- }
- else
- return {};
- */
- break;
- case '-':
- state = (NumberParsingState)"\1\7\7\7\7\6\7"[state];
- /*if (state == NumberParsingState::Minus)
- {
- state = NumberParsingState::AfterMinus;
- }
- else if (state == NumberParsingState::E)
- {
- state = NumberParsingState::DigitsAfterE;
- }
- else
- return {};*/
- break;
- case '+':
- state = (NumberParsingState)"\7\7\7\7\7\6\7"[state];
- /*if (state == NumberParsingState::E)
- {
- state = NumberParsingState::DigitsAfterE;
- }
- else
- return {};*/
- break;
- case 'e': case 'E':
- state = (NumberParsingState)"\7\7\7\5\5\7\7"[state];
- /*if (state == NumberParsingState::Digits ||
- state == NumberParsingState::DigitsAfterPoints)
- {
- state = NumberParsingState::E;
- }
- else
- return {};*/
- break;
- default:
- if (crow_json_likely(state == NumberParsingState::ZeroFirst ||
- state == NumberParsingState::Digits ||
- state == NumberParsingState::DigitsAfterPoints ||
- state == NumberParsingState::DigitsAfterE))
- return {type::Number, start, data};
- else
- return {};
- }
- data++;
- }
-
- return {};
- }
-
- rvalue decode_value()
- {
- switch(*data)
- {
- case '[':
- return decode_list();
- case '{':
- return decode_object();
- case '"':
- return decode_string();
- case 't':
- if (//e-data >= 4 &&
- data[1] == 'r' &&
- data[2] == 'u' &&
- data[3] == 'e')
- {
- data += 4;
- return {type::True};
- }
- else
- return {};
- case 'f':
- if (//e-data >= 5 &&
- data[1] == 'a' &&
- data[2] == 'l' &&
- data[3] == 's' &&
- data[4] == 'e')
- {
- data += 5;
- return {type::False};
- }
- else
- return {};
- case 'n':
- if (//e-data >= 4 &&
- data[1] == 'u' &&
- data[2] == 'l' &&
- data[3] == 'l')
- {
- data += 4;
- return {type::Null};
- }
- else
- return {};
- //case '1': case '2': case '3':
- //case '4': case '5': case '6':
- //case '7': case '8': case '9':
- //case '0': case '-':
- default:
- return decode_number();
- }
- return {};
- }
-
- rvalue decode_object()
- {
- rvalue ret(type::Object);
- if (crow_json_unlikely(!consume('{')))
- {
- ret.set_error();
- return ret;
- }
-
- ws_skip();
-
- if (crow_json_unlikely(*data == '}'))
- {
- data++;
- return ret;
- }
-
- while(1)
- {
- auto t = decode_string();
- if (crow_json_unlikely(!t))
- {
- ret.set_error();
- break;
- }
-
- ws_skip();
- if (crow_json_unlikely(!consume(':')))
- {
- ret.set_error();
- break;
- }
-
- // TODO caching key to speed up (flyweight?)
- auto key = t.s();
-
- ws_skip();
- auto v = decode_value();
- if (crow_json_unlikely(!v))
- {
- ret.set_error();
- break;
- }
- ws_skip();
-
- v.key_ = std::move(key);
- ret.emplace_back(std::move(v));
- if (crow_json_unlikely(*data == '}'))
- {
- data++;
- break;
- }
- if (crow_json_unlikely(!consume(',')))
- {
- ret.set_error();
- break;
- }
- ws_skip();
- }
- return ret;
- }
-
- rvalue parse()
- {
- ws_skip();
- auto ret = decode_value(); // or decode object?
- ws_skip();
- if (ret && *data != '\0')
- ret.set_error();
- return ret;
- }
-
- char* data;
- };
- return Parser(data, size).parse();
- }
- inline rvalue load(const char* data, size_t size)
- {
- char* s = new char[size+1];
- memcpy(s, data, size);
- s[size] = 0;
- auto ret = load_nocopy_internal(s, size);
- if (ret)
- ret.key_.force(s, size);
- else
- delete[] s;
- return ret;
- }
-
- inline rvalue load(const char* data)
- {
- return load(data, strlen(data));
- }
-
- inline rvalue load(const std::string& str)
- {
- return load(str.data(), str.size());
- }
-
- class wvalue
+ size_t operator()(const std::string& key) const
{
- friend class crow::mustache::template_t;
- public:
- type t() const { return t_; }
- private:
- type t_{type::Null};
- double d {};
- std::string s;
- std::unique_ptr<std::vector<wvalue>> l;
- std::unique_ptr<std::unordered_map<std::string, wvalue>> o;
- public:
-
- wvalue() {}
-
- wvalue(const rvalue& r)
- {
- t_ = r.t();
- switch(r.t())
- {
- case type::Null:
- case type::False:
- case type::True:
- return;
- case type::Number:
- d = r.d();
- return;
- case type::String:
- s = r.s();
- return;
- case type::List:
- l = std::unique_ptr<std::vector<wvalue>>(new std::vector<wvalue>{});
- l->reserve(r.size());
- for(auto it = r.begin(); it != r.end(); ++it)
- l->emplace_back(*it);
- return;
- case type::Object:
- o = std::unique_ptr<
- std::unordered_map<std::string, wvalue>
- >(
- new std::unordered_map<std::string, wvalue>{});
- for(auto it = r.begin(); it != r.end(); ++it)
- o->emplace(it->key(), *it);
- return;
- }
- }
-
- wvalue(wvalue&& r)
- {
- *this = std::move(r);
- }
-
- wvalue& operator = (wvalue&& r)
- {
- t_ = r.t_;
- d = r.d;
- s = std::move(r.s);
- l = std::move(r.l);
- o = std::move(r.o);
- return *this;
- }
-
- void clear()
- {
- t_ = type::Null;
- l.reset();
- o.reset();
- }
-
- void reset()
- {
- t_ = type::Null;
- l.reset();
- o.reset();
- }
-
- wvalue& operator = (std::nullptr_t)
- {
- reset();
- return *this;
- }
- wvalue& operator = (bool value)
- {
- reset();
- if (value)
- t_ = type::True;
- else
- t_ = type::False;
- return *this;
- }
-
- wvalue& operator = (double value)
- {
- reset();
- t_ = type::Number;
- d = value;
- return *this;
- }
-
- wvalue& operator = (unsigned short value)
- {
- reset();
- t_ = type::Number;
- d = (double)value;
- return *this;
- }
-
- wvalue& operator = (short value)
- {
- reset();
- t_ = type::Number;
- d = (double)value;
- return *this;
- }
-
- wvalue& operator = (long long value)
- {
- reset();
- t_ = type::Number;
- d = (double)value;
- return *this;
- }
-
- wvalue& operator = (long value)
- {
- reset();
- t_ = type::Number;
- d = (double)value;
- return *this;
- }
-
- wvalue& operator = (int value)
- {
- reset();
- t_ = type::Number;
- d = (double)value;
- return *this;
- }
-
- wvalue& operator = (unsigned long long value)
- {
- reset();
- t_ = type::Number;
- d = (double)value;
- return *this;
- }
-
- wvalue& operator = (unsigned long value)
- {
- reset();
- t_ = type::Number;
- d = (double)value;
- return *this;
- }
-
- wvalue& operator = (unsigned int value)
- {
- reset();
- t_ = type::Number;
- d = (double)value;
- return *this;
- }
-
- wvalue& operator=(const char* str)
- {
- reset();
- t_ = type::String;
- s = str;
- return *this;
- }
-
- wvalue& operator=(const std::string& str)
- {
- reset();
- t_ = type::String;
- s = str;
- return *this;
- }
-
- template <typename T>
- wvalue& operator=(const std::vector<T>& v)
- {
- if (t_ != type::List)
- reset();
- t_ = type::List;
- if (!l)
- l = std::unique_ptr<std::vector<wvalue>>(new std::vector<wvalue>{});
- l->clear();
- l->resize(v.size());
- size_t idx = 0;
- for(auto& x:v)
- {
- (*l)[idx++] = x;
- }
- return *this;
- }
-
- wvalue& operator[](unsigned index)
- {
- if (t_ != type::List)
- reset();
- t_ = type::List;
- if (!l)
- l = std::unique_ptr<std::vector<wvalue>>(new std::vector<wvalue>{});
- if (l->size() < index+1)
- l->resize(index+1);
- return (*l)[index];
- }
-
- int count(const std::string& str)
- {
- if (t_ != type::Object)
- return 0;
- if (!o)
- return 0;
- return o->count(str);
- }
-
- wvalue& operator[](const std::string& str)
- {
- if (t_ != type::Object)
- reset();
- t_ = type::Object;
- if (!o)
- o = std::unique_ptr<
- std::unordered_map<std::string, wvalue>
- >(
- new std::unordered_map<std::string, wvalue>{});
- return (*o)[str];
- }
-
- size_t estimate_length() const
- {
- switch(t_)
- {
- case type::Null: return 4;
- case type::False: return 5;
- case type::True: return 4;
- case type::Number: return 30;
- case type::String: return 2+s.size()+s.size()/2;
- case type::List:
- {
- size_t sum{};
- if (l)
- {
- for(auto& x:*l)
- {
- sum += 1;
- sum += x.estimate_length();
- }
- }
- return sum+2;
- }
- case type::Object:
- {
- size_t sum{};
- if (o)
- {
- for(auto& kv:*o)
- {
- sum += 2;
- sum += 2+kv.first.size()+kv.first.size()/2;
- sum += kv.second.estimate_length();
- }
- }
- return sum+2;
- }
- }
- return 1;
- }
-
-
- friend void dump_internal(const wvalue& v, std::string& out);
- friend std::string dump(const wvalue& v);
- };
+ std::size_t seed = 0;
+ std::locale locale;
- inline void dump_string(const std::string& str, std::string& out)
- {
- out.push_back('"');
- escape(str, out);
- out.push_back('"');
- }
- inline void dump_internal(const wvalue& v, std::string& out)
- {
- switch(v.t_)
+ for(auto c : key)
{
- case type::Null: out += "null"; break;
- case type::False: out += "false"; break;
- case type::True: out += "true"; break;
- case type::Number:
- {
- char outbuf[128];
- sprintf(outbuf, "%g", v.d);
- out += outbuf;
- }
- break;
- case type::String: dump_string(v.s, out); break;
- case type::List:
- {
- out.push_back('[');
- if (v.l)
- {
- bool first = true;
- for(auto& x:*v.l)
- {
- if (!first)
- {
- out.push_back(',');
- }
- first = false;
- dump_internal(x, out);
- }
- }
- out.push_back(']');
- }
- break;
- case type::Object:
- {
- out.push_back('{');
- if (v.o)
- {
- bool first = true;
- for(auto& kv:*v.o)
- {
- if (!first)
- {
- out.push_back(',');
- }
- first = false;
- dump_string(kv.first, out);
- out.push_back(':');
- dump_internal(kv.second, out);
- }
- }
- out.push_back('}');
- }
- break;
+ boost::hash_combine(seed, std::toupper(c, locale));
}
- }
- inline std::string dump(const wvalue& v)
- {
- std::string ret;
- ret.reserve(v.estimate_length());
- dump_internal(v, ret);
- return ret;
+ return seed;
}
+ };
- //std::vector<boost::asio::const_buffer> dump_ref(wvalue& v)
- //{
- //}
- }
-}
-
-#undef crow_json_likely
-#undef crow_json_unlikely
-
-
-
-#pragma once
-#include <string>
-#include <vector>
-#include <fstream>
-#include <iterator>
-#include <functional>
-
-
-namespace crow
-{
- namespace mustache
+ struct ci_key_eq
{
- using context = json::wvalue;
-
- template_t load(const std::string& filename);
-
- class invalid_template_exception : public std::exception
- {
- public:
- invalid_template_exception(const std::string& msg)
- : msg("crow::mustache error: " + msg)
- {
- }
- virtual const char* what() const throw()
- {
- return msg.c_str();
- }
- std::string msg;
- };
-
- enum class ActionType
- {
- Ignore,
- Tag,
- UnescapeTag,
- OpenBlock,
- CloseBlock,
- ElseBlock,
- Partial,
- };
-
- struct Action
- {
- int start;
- int end;
- int pos;
- ActionType t;
- Action(ActionType t, int start, int end, int pos = 0)
- : start(start), end(end), pos(pos), t(t)
- {}
- };
-
- class template_t
- {
- public:
- template_t(std::string body)
- : body_(std::move(body))
- {
- // {{ {{# {{/ {{^ {{! {{> {{=
- parse();
- }
-
- private:
- std::string tag_name(const Action& action)
- {
- return body_.substr(action.start, action.end - action.start);
- }
- auto find_context(const std::string& name, const std::vector<context*>& stack)->std::pair<bool, context&>
- {
- if (name == ".")
- {
- return {true, *stack.back()};
- }
- int dotPosition = name.find(".");
- if (dotPosition == (int)name.npos)
- {
- for(auto it = stack.rbegin(); it != stack.rend(); ++it)
- {
- if ((*it)->t() == json::type::Object)
- {
- if ((*it)->count(name))
- return {true, (**it)[name]};
- }
- }
- }
- else
- {
- std::vector<int> dotPositions;
- dotPositions.push_back(-1);
- while(dotPosition != (int)name.npos)
- {
- dotPositions.push_back(dotPosition);
- dotPosition = name.find(".", dotPosition+1);
- }
- dotPositions.push_back(name.size());
- std::vector<std::string> names;
- names.reserve(dotPositions.size()-1);
- for(int i = 1; i < (int)dotPositions.size(); i ++)
- names.emplace_back(name.substr(dotPositions[i-1]+1, dotPositions[i]-dotPositions[i-1]-1));
-
- for(auto it = stack.rbegin(); it != stack.rend(); ++it)
- {
- context* view = *it;
- bool found = true;
- for(auto jt = names.begin(); jt != names.end(); ++jt)
- {
- if (view->t() == json::type::Object &&
- view->count(*jt))
- {
- view = &(*view)[*jt];
- }
- else
- {
- found = false;
- break;
- }
- }
- if (found)
- return {true, *view};
- }
-
- }
-
- static json::wvalue empty_str;
- empty_str = "";
- return {false, empty_str};
- }
-
- void escape(const std::string& in, std::string& out)
- {
- out.reserve(out.size() + in.size());
- for(auto it = in.begin(); it != in.end(); ++it)
- {
- switch(*it)
- {
- case '&': out += "&amp;"; break;
- case '<': out += "&lt;"; break;
- case '>': out += "&gt;"; break;
- case '"': out += "&quot;"; break;
- case '\'': out += "&#39;"; break;
- case '/': out += "&#x2F;"; break;
- default: out += *it; break;
- }
- }
- }
-
- void render_internal(int actionBegin, int actionEnd, std::vector<context*>& stack, std::string& out, int indent)
- {
- int current = actionBegin;
-
- if (indent)
- out.insert(out.size(), indent, ' ');
-
- while(current < actionEnd)
- {
- auto& fragment = fragments_[current];
- auto& action = actions_[current];
- render_fragment(fragment, indent, out);
- switch(action.t)
- {
- case ActionType::Ignore:
- // do nothing
- break;
- case ActionType::Partial:
- {
- std::string partial_name = tag_name(action);
- auto partial_templ = load(partial_name);
- int partial_indent = action.pos;
- partial_templ.render_internal(0, partial_templ.fragments_.size()-1, stack, out, partial_indent?indent+partial_indent:0);
- }
- break;
- case ActionType::UnescapeTag:
- case ActionType::Tag:
- {
- auto optional_ctx = find_context(tag_name(action), stack);
- auto& ctx = optional_ctx.second;
- switch(ctx.t())
- {
- case json::type::Number:
- out += json::dump(ctx);
- break;
- case json::type::String:
- if (action.t == ActionType::Tag)
- escape(ctx.s, out);
- else
- out += ctx.s;
- break;
- default:
- throw std::runtime_error("not implemented tag type" + boost::lexical_cast<std::string>((int)ctx.t()));
- }
- }
- break;
- case ActionType::ElseBlock:
- {
- static context nullContext;
- auto optional_ctx = find_context(tag_name(action), stack);
- if (!optional_ctx.first)
- {
- stack.emplace_back(&nullContext);
- break;
- }
-
- auto& ctx = optional_ctx.second;
- switch(ctx.t())
- {
- case json::type::List:
- if (ctx.l && !ctx.l->empty())
- current = action.pos;
- else
- stack.emplace_back(&nullContext);
- break;
- case json::type::False:
- case json::type::Null:
- stack.emplace_back(&nullContext);
- break;
- default:
- current = action.pos;
- break;
- }
- break;
- }
- case ActionType::OpenBlock:
- {
- auto optional_ctx = find_context(tag_name(action), stack);
- if (!optional_ctx.first)
- {
- current = action.pos;
- break;
- }
-
- auto& ctx = optional_ctx.second;
- switch(ctx.t())
- {
- case json::type::List:
- if (ctx.l)
- for(auto it = ctx.l->begin(); it != ctx.l->end(); ++it)
- {
- stack.push_back(&*it);
- render_internal(current+1, action.pos, stack, out, indent);
- stack.pop_back();
- }
- current = action.pos;
- break;
- case json::type::Number:
- case json::type::String:
- case json::type::Object:
- case json::type::True:
- stack.push_back(&ctx);
- break;
- case json::type::False:
- case json::type::Null:
- current = action.pos;
- break;
- default:
- throw std::runtime_error("{{#: not implemented context type: " + boost::lexical_cast<std::string>((int)ctx.t()));
- break;
- }
- break;
- }
- case ActionType::CloseBlock:
- stack.pop_back();
- break;
- default:
- throw std::runtime_error("not implemented " + boost::lexical_cast<std::string>((int)action.t));
- }
- current++;
- }
- auto& fragment = fragments_[actionEnd];
- render_fragment(fragment, indent, out);
- }
- void render_fragment(const std::pair<int, int> fragment, int indent, std::string& out)
- {
- if (indent)
- {
- for(int i = fragment.first; i < fragment.second; i ++)
- {
- out += body_[i];
- if (body_[i] == '\n' && i+1 != (int)body_.size())
- out.insert(out.size(), indent, ' ');
- }
- }
- else
- out.insert(out.size(), body_, fragment.first, fragment.second-fragment.first);
- }
- public:
- std::string render()
- {
- context empty_ctx;
- std::vector<context*> stack;
- stack.emplace_back(&empty_ctx);
-
- std::string ret;
- render_internal(0, fragments_.size()-1, stack, ret, 0);
- return ret;
- }
- std::string render(context& ctx)
- {
- std::vector<context*> stack;
- stack.emplace_back(&ctx);
-
- std::string ret;
- render_internal(0, fragments_.size()-1, stack, ret, 0);
- return ret;
- }
-
- private:
-
- void parse()
- {
- std::string tag_open = "{{";
- std::string tag_close = "}}";
-
- std::vector<int> blockPositions;
-
- size_t current = 0;
- while(1)
- {
- size_t idx = body_.find(tag_open, current);
- if (idx == body_.npos)
- {
- fragments_.emplace_back(current, body_.size());
- actions_.emplace_back(ActionType::Ignore, 0, 0);
- break;
- }
- fragments_.emplace_back(current, idx);
-
- idx += tag_open.size();
- size_t endIdx = body_.find(tag_close, idx);
- if (endIdx == idx)
- {
- throw invalid_template_exception("empty tag is not allowed");
- }
- if (endIdx == body_.npos)
- {
- // error, no matching tag
- throw invalid_template_exception("not matched opening tag");
- }
- current = endIdx + tag_close.size();
- switch(body_[idx])
- {
- case '#':
- idx++;
- while(body_[idx] == ' ') idx++;
- while(body_[endIdx-1] == ' ') endIdx--;
- blockPositions.emplace_back(actions_.size());
- actions_.emplace_back(ActionType::OpenBlock, idx, endIdx);
- break;
- case '/':
- idx++;
- while(body_[idx] == ' ') idx++;
- while(body_[endIdx-1] == ' ') endIdx--;
- {
- auto& matched = actions_[blockPositions.back()];
- if (body_.compare(idx, endIdx-idx,
- body_, matched.start, matched.end - matched.start) != 0)
- {
- throw invalid_template_exception("not matched {{# {{/ pair: " +
- body_.substr(matched.start, matched.end - matched.start) + ", " +
- body_.substr(idx, endIdx-idx));
- }
- matched.pos = actions_.size();
- }
- actions_.emplace_back(ActionType::CloseBlock, idx, endIdx, blockPositions.back());
- blockPositions.pop_back();
- break;
- case '^':
- idx++;
- while(body_[idx] == ' ') idx++;
- while(body_[endIdx-1] == ' ') endIdx--;
- blockPositions.emplace_back(actions_.size());
- actions_.emplace_back(ActionType::ElseBlock, idx, endIdx);
- break;
- case '!':
- // do nothing action
- actions_.emplace_back(ActionType::Ignore, idx+1, endIdx);
- break;
- case '>': // partial
- idx++;
- while(body_[idx] == ' ') idx++;
- while(body_[endIdx-1] == ' ') endIdx--;
- actions_.emplace_back(ActionType::Partial, idx, endIdx);
- break;
- case '{':
- if (tag_open != "{{" || tag_close != "}}")
- throw invalid_template_exception("cannot use triple mustache when delimiter changed");
-
- idx ++;
- if (body_[endIdx+2] != '}')
- {
- throw invalid_template_exception("{{{: }}} not matched");
- }
- while(body_[idx] == ' ') idx++;
- while(body_[endIdx-1] == ' ') endIdx--;
- actions_.emplace_back(ActionType::UnescapeTag, idx, endIdx);
- current++;
- break;
- case '&':
- idx ++;
- while(body_[idx] == ' ') idx++;
- while(body_[endIdx-1] == ' ') endIdx--;
- actions_.emplace_back(ActionType::UnescapeTag, idx, endIdx);
- break;
- case '=':
- // tag itself is no-op
- idx ++;
- actions_.emplace_back(ActionType::Ignore, idx, endIdx);
- endIdx --;
- if (body_[endIdx] != '=')
- throw invalid_template_exception("{{=: not matching = tag: "+body_.substr(idx, endIdx-idx));
- endIdx --;
- while(body_[idx] == ' ') idx++;
- while(body_[endIdx] == ' ') endIdx--;
- endIdx++;
- {
- bool succeeded = false;
- for(size_t i = idx; i < endIdx; i++)
- {
- if (body_[i] == ' ')
- {
- tag_open = body_.substr(idx, i-idx);
- while(body_[i] == ' ') i++;
- tag_close = body_.substr(i, endIdx-i);
- if (tag_open.empty())
- throw invalid_template_exception("{{=: empty open tag");
- if (tag_close.empty())
- throw invalid_template_exception("{{=: empty close tag");
-
- if (tag_close.find(" ") != tag_close.npos)
- throw invalid_template_exception("{{=: invalid open/close tag: "+tag_open+" " + tag_close);
- succeeded = true;
- break;
- }
- }
- if (!succeeded)
- throw invalid_template_exception("{{=: cannot find space between new open/close tags");
- }
- break;
- default:
- // normal tag case;
- while(body_[idx] == ' ') idx++;
- while(body_[endIdx-1] == ' ') endIdx--;
- actions_.emplace_back(ActionType::Tag, idx, endIdx);
- break;
- }
- }
-
- // removing standalones
- for(int i = actions_.size()-2; i >= 0; i --)
- {
- if (actions_[i].t == ActionType::Tag || actions_[i].t == ActionType::UnescapeTag)
- continue;
- auto& fragment_before = fragments_[i];
- auto& fragment_after = fragments_[i+1];
- bool is_last_action = i == (int)actions_.size()-2;
- bool all_space_before = true;
- int j, k;
- for(j = fragment_before.second-1;j >= fragment_before.first;j--)
- {
- if (body_[j] != ' ')
- {
- all_space_before = false;
- break;
- }
- }
- if (all_space_before && i > 0)
- continue;
- if (!all_space_before && body_[j] != '\n')
- continue;
- bool all_space_after = true;
- for(k = fragment_after.first; k < (int)body_.size() && k < fragment_after.second; k ++)
- {
- if (body_[k] != ' ')
- {
- all_space_after = false;
- break;
- }
- }
- if (all_space_after && !is_last_action)
- continue;
- if (!all_space_after &&
- !(
- body_[k] == '\n'
- ||
- (body_[k] == '\r' &&
- k + 1 < (int)body_.size() &&
- body_[k+1] == '\n')))
- continue;
- if (actions_[i].t == ActionType::Partial)
- {
- actions_[i].pos = fragment_before.second - j - 1;
- }
- fragment_before.second = j+1;
- if (!all_space_after)
- {
- if (body_[k] == '\n')
- k++;
- else
- k += 2;
- fragment_after.first = k;
- }
- }
- }
-
- std::vector<std::pair<int,int>> fragments_;
- std::vector<Action> actions_;
- std::string body_;
- };
-
- inline template_t compile(const std::string& body)
- {
- return template_t(body);
- }
- namespace detail
- {
- inline std::string& get_template_base_directory_ref()
- {
- static std::string template_base_directory = "templates";
- return template_base_directory;
- }
- }
-
- inline std::string default_loader(const std::string& filename)
- {
- std::string path = detail::get_template_base_directory_ref();
- if (!(path.back() == '/' || path.back() == '\\'))
- path += '/';
- path += filename;
- std::ifstream inf(path);
- if (!inf)
- return {};
- return {std::istreambuf_iterator<char>(inf), std::istreambuf_iterator<char>()};
- }
-
- namespace detail
- {
- inline std::function<std::string (std::string)>& get_loader_ref()
- {
- static std::function<std::string (std::string)> loader = default_loader;
- return loader;
- }
- }
-
- inline void set_base(const std::string& path)
- {
- auto& base = detail::get_template_base_directory_ref();
- base = path;
- if (base.back() != '\\' &&
- base.back() != '/')
- {
- base += '/';
- }
- }
-
- inline void set_loader(std::function<std::string(std::string)> loader)
- {
- detail::get_loader_ref() = std::move(loader);
- }
-
- inline std::string load_text(const std::string& filename)
+ bool operator()(const std::string& l, const std::string& r) const
{
- return detail::get_loader_ref()(filename);
+ return boost::iequals(l, r);
}
+ };
- inline template_t load(const std::string& filename)
- {
- return compile(detail::get_loader_ref()(filename));
- }
- }
+ using ci_map = std::unordered_multimap<std::string, std::string, ci_hash, ci_key_eq>;
}
@@ -6401,179 +6452,6 @@ namespace crow
#pragma once
-
-#include <string>
-#include <unordered_map>
-#include <boost/algorithm/string.hpp>
-#include <boost/tokenizer.hpp>
-#include <algorithm>
-
-
-
-
-
-
-namespace crow
-{
- template <typename Handler>
- struct HTTPParser : public http_parser
- {
- static int on_message_begin(http_parser* self_)
- {
- HTTPParser* self = static_cast<HTTPParser*>(self_);
- self->clear();
- return 0;
- }
- static int on_url(http_parser* self_, const char* at, size_t length)
- {
- HTTPParser* self = static_cast<HTTPParser*>(self_);
- 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)
- {
- HTTPParser* self = static_cast<HTTPParser*>(self_);
- switch (self->header_building_state)
- {
- case 0:
- if (!self->header_value.empty())
- {
- self->headers.emplace(std::move(self->header_field), std::move(self->header_value));
- }
- self->header_field.assign(at, at+length);
- self->header_building_state = 1;
- break;
- case 1:
- self->header_field.insert(self->header_field.end(), at, at+length);
- break;
- }
- return 0;
- }
- static int on_header_value(http_parser* self_, const char* at, size_t length)
- {
- HTTPParser* self = static_cast<HTTPParser*>(self_);
- switch (self->header_building_state)
- {
- case 0:
- self->header_value.insert(self->header_value.end(), at, at+length);
- break;
- case 1:
- self->header_building_state = 0;
- self->header_value.assign(at, at+length);
- break;
- }
- return 0;
- }
- static int on_headers_complete(http_parser* self_)
- {
- HTTPParser* self = static_cast<HTTPParser*>(self_);
- if (!self->header_field.empty())
- {
- self->headers.emplace(std::move(self->header_field), std::move(self->header_value));
- }
- self->process_header();
- return 0;
- }
- static int on_body(http_parser* self_, const char* at, size_t length)
- {
- HTTPParser* self = static_cast<HTTPParser*>(self_);
- self->body.insert(self->body.end(), at, at+length);
- return 0;
- }
- 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;
- }
- HTTPParser(Handler* handler) :
- handler_(handler)
- {
- http_parser_init(this, HTTP_REQUEST);
- }
-
- // return false on error
- bool feed(const char* buffer, int length)
- {
- const static http_parser_settings settings_{
- on_message_begin,
- on_url,
- nullptr,
- on_header_field,
- on_header_value,
- on_headers_complete,
- on_body,
- on_message_complete,
- };
-
- int nparsed = http_parser_execute(this, &settings_, buffer, length);
- return nparsed == length;
- }
-
- bool done()
- {
- return feed(nullptr, 0);
- }
-
- void clear()
- {
- url.clear();
- raw_url.clear();
- header_building_state = 0;
- header_field.clear();
- header_value.clear();
- headers.clear();
- url_params.clear();
- body.clear();
- }
-
- void process_header()
- {
- handler_->handle_header();
- }
-
- void process_message()
- {
- handler_->handle();
- }
-
- request to_request() const
- {
- return request{(HTTPMethod)method, std::move(raw_url), std::move(url), std::move(url_params), std::move(headers), std::move(body)};
- }
-
- bool is_upgrade() const
- {
- return upgrade;
- }
-
- bool check_version(int major, int minor) const
- {
- 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_;
- };
-}
-
-
-
-#pragma once
#include <boost/algorithm/string/predicate.hpp>
@@ -7069,6 +6947,179 @@ namespace crow
#pragma once
+
+#include <string>
+#include <unordered_map>
+#include <boost/algorithm/string.hpp>
+#include <boost/tokenizer.hpp>
+#include <algorithm>
+
+
+
+
+
+
+namespace crow
+{
+ template <typename Handler>
+ struct HTTPParser : public http_parser
+ {
+ static int on_message_begin(http_parser* self_)
+ {
+ HTTPParser* self = static_cast<HTTPParser*>(self_);
+ self->clear();
+ return 0;
+ }
+ static int on_url(http_parser* self_, const char* at, size_t length)
+ {
+ HTTPParser* self = static_cast<HTTPParser*>(self_);
+ 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)
+ {
+ HTTPParser* self = static_cast<HTTPParser*>(self_);
+ switch (self->header_building_state)
+ {
+ case 0:
+ if (!self->header_value.empty())
+ {
+ self->headers.emplace(std::move(self->header_field), std::move(self->header_value));
+ }
+ self->header_field.assign(at, at+length);
+ self->header_building_state = 1;
+ break;
+ case 1:
+ self->header_field.insert(self->header_field.end(), at, at+length);
+ break;
+ }
+ return 0;
+ }
+ static int on_header_value(http_parser* self_, const char* at, size_t length)
+ {
+ HTTPParser* self = static_cast<HTTPParser*>(self_);
+ switch (self->header_building_state)
+ {
+ case 0:
+ self->header_value.insert(self->header_value.end(), at, at+length);
+ break;
+ case 1:
+ self->header_building_state = 0;
+ self->header_value.assign(at, at+length);
+ break;
+ }
+ return 0;
+ }
+ static int on_headers_complete(http_parser* self_)
+ {
+ HTTPParser* self = static_cast<HTTPParser*>(self_);
+ if (!self->header_field.empty())
+ {
+ self->headers.emplace(std::move(self->header_field), std::move(self->header_value));
+ }
+ self->process_header();
+ return 0;
+ }
+ static int on_body(http_parser* self_, const char* at, size_t length)
+ {
+ HTTPParser* self = static_cast<HTTPParser*>(self_);
+ self->body.insert(self->body.end(), at, at+length);
+ return 0;
+ }
+ 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;
+ }
+ HTTPParser(Handler* handler) :
+ handler_(handler)
+ {
+ http_parser_init(this, HTTP_REQUEST);
+ }
+
+ // return false on error
+ bool feed(const char* buffer, int length)
+ {
+ const static http_parser_settings settings_{
+ on_message_begin,
+ on_url,
+ nullptr,
+ on_header_field,
+ on_header_value,
+ on_headers_complete,
+ on_body,
+ on_message_complete,
+ };
+
+ int nparsed = http_parser_execute(this, &settings_, buffer, length);
+ return nparsed == length;
+ }
+
+ bool done()
+ {
+ return feed(nullptr, 0);
+ }
+
+ void clear()
+ {
+ url.clear();
+ raw_url.clear();
+ header_building_state = 0;
+ header_field.clear();
+ header_value.clear();
+ headers.clear();
+ url_params.clear();
+ body.clear();
+ }
+
+ void process_header()
+ {
+ handler_->handle_header();
+ }
+
+ void process_message()
+ {
+ handler_->handle();
+ }
+
+ request to_request() const
+ {
+ return request{(HTTPMethod)method, std::move(raw_url), std::move(url), std::move(url_params), std::move(headers), std::move(body)};
+ }
+
+ bool is_upgrade() const
+ {
+ return upgrade;
+ }
+
+ bool check_version(int major, int minor) const
+ {
+ 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_;
+ };
+}
+
+
+
+#pragma once
#include <string>
#include <unordered_map>
@@ -7367,7 +7418,7 @@ namespace crow
struct Wrapped
{
template <typename ... Args>
- void set(Func f, typename std::enable_if<
+ void set_(Func f, typename std::enable_if<
!std::is_same<typename std::tuple_element<0, std::tuple<Args..., void>>::type, const request&>::value
, int>::type = 0)
{
@@ -7401,7 +7452,7 @@ namespace crow
};
template <typename ... Args>
- void set(Func f, typename std::enable_if<
+ void set_(Func f, typename std::enable_if<
std::is_same<typename std::tuple_element<0, std::tuple<Args..., void>>::type, const request&>::value &&
!std::is_same<typename std::tuple_element<1, std::tuple<Args..., void, void>>::type, response&>::value
, int>::type = 0)
@@ -7416,7 +7467,7 @@ namespace crow
}
template <typename ... Args>
- void set(Func f, typename std::enable_if<
+ void set_(Func f, typename std::enable_if<
std::is_same<typename std::tuple_element<0, std::tuple<Args..., void>>::type, const request&>::value &&
std::is_same<typename std::tuple_element<1, std::tuple<Args..., void, void>>::type, response&>::value
, int>::type = 0)
@@ -8269,6 +8320,71 @@ public:
#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 <boost/algorithm/string/trim.hpp>
@@ -8448,71 +8564,6 @@ namespace crow
#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 <boost/asio.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>