Crow/common.h

124 lines
3.2 KiB
C
Raw Normal View History

2014-04-14 15:31:51 +00:00
#pragma once
#include <string>
#include <stdexcept>
#include "utility.h"
2014-04-14 15:31:51 +00:00
namespace flask
{
enum class HTTPMethod
{
DELETE,
GET,
HEAD,
POST,
PUT,
CONNECT,
OPTIONS,
TRACE,
};
std::string method_name(HTTPMethod method)
{
switch(method)
{
case HTTPMethod::DELETE:
return "DELETE";
case HTTPMethod::GET:
return "GET";
case HTTPMethod::HEAD:
return "HEAD";
case HTTPMethod::POST:
return "POST";
case HTTPMethod::PUT:
return "PUT";
case HTTPMethod::CONNECT:
return "CONNECT";
case HTTPMethod::OPTIONS:
return "OPTIONS";
case HTTPMethod::TRACE:
return "TRACE";
}
return "invalid";
}
2014-04-14 15:31:51 +00:00
enum class ParamType
{
INT,
UINT,
DOUBLE,
STRING,
PATH,
MAX
};
struct routing_params
{
std::vector<int64_t> int_params;
std::vector<uint64_t> uint_params;
std::vector<double> double_params;
std::vector<std::string> string_params;
void debug_print() const
{
std::cerr << "routing_params" << std::endl;
for(auto i:int_params)
std::cerr<<i <<", " ;
std::cerr<<std::endl;
for(auto i:uint_params)
std::cerr<<i <<", " ;
std::cerr<<std::endl;
for(auto i:double_params)
std::cerr<<i <<", " ;
std::cerr<<std::endl;
for(auto& i:string_params)
std::cerr<<i <<", " ;
std::cerr<<std::endl;
}
2014-04-14 15:31:51 +00:00
template <typename T>
T get(unsigned) const;
};
template<>
2014-04-17 09:18:02 +00:00
inline int64_t routing_params::get<int64_t>(unsigned index) const
2014-04-14 15:31:51 +00:00
{
return int_params[index];
2014-04-14 15:31:51 +00:00
}
template<>
2014-04-17 09:18:02 +00:00
inline uint64_t routing_params::get<uint64_t>(unsigned index) const
2014-04-14 15:31:51 +00:00
{
return uint_params[index];
2014-04-14 15:31:51 +00:00
}
template<>
2014-04-17 09:18:02 +00:00
inline double routing_params::get<double>(unsigned index) const
2014-04-14 15:31:51 +00:00
{
return double_params[index];
2014-04-14 15:31:51 +00:00
}
template<>
2014-04-17 09:18:02 +00:00
inline std::string routing_params::get<std::string>(unsigned index) const
2014-04-14 15:31:51 +00:00
{
return string_params[index];
2014-04-14 15:31:51 +00:00
}
}
constexpr flask::HTTPMethod operator "" _method(const char* str, size_t len)
{
return
flask::black_magic::is_equ_p(str, "GET", 3) ? flask::HTTPMethod::GET :
flask::black_magic::is_equ_p(str, "DELETE", 6) ? flask::HTTPMethod::DELETE :
flask::black_magic::is_equ_p(str, "HEAD", 4) ? flask::HTTPMethod::HEAD :
flask::black_magic::is_equ_p(str, "POST", 4) ? flask::HTTPMethod::POST :
flask::black_magic::is_equ_p(str, "PUT", 3) ? flask::HTTPMethod::PUT :
flask::black_magic::is_equ_p(str, "OPTIONS", 7) ? flask::HTTPMethod::OPTIONS :
flask::black_magic::is_equ_p(str, "CONNECT", 7) ? flask::HTTPMethod::CONNECT :
flask::black_magic::is_equ_p(str, "TRACE", 5) ? flask::HTTPMethod::TRACE :
throw std::runtime_error("invalid http method");
};