2014-04-14 15:31:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
2014-10-23 17:45:34 +00:00
|
|
|
#include <vector>
|
2014-04-14 15:31:51 +00:00
|
|
|
#include <string>
|
2014-04-20 08:45:10 +00:00
|
|
|
#include <stdexcept>
|
2014-10-23 19:17:20 +00:00
|
|
|
#include <iostream>
|
2016-09-21 14:11:06 +00:00
|
|
|
#include "crow/utility.h"
|
2014-04-14 15:31:51 +00:00
|
|
|
|
2014-04-26 17:19:59 +00:00
|
|
|
namespace crow
|
2014-04-14 15:31:51 +00:00
|
|
|
{
|
2014-04-20 08:45:10 +00:00
|
|
|
enum class HTTPMethod
|
|
|
|
{
|
2016-08-27 05:15:16 +00:00
|
|
|
#ifndef DELETE
|
|
|
|
DELETE = 0,
|
2014-04-20 08:45:10 +00:00
|
|
|
GET,
|
|
|
|
HEAD,
|
|
|
|
POST,
|
|
|
|
PUT,
|
|
|
|
CONNECT,
|
|
|
|
OPTIONS,
|
|
|
|
TRACE,
|
2017-10-05 15:13:40 +00:00
|
|
|
PATCH = 24,
|
2016-08-27 05:15:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Delete = 0,
|
|
|
|
Get,
|
|
|
|
Head,
|
|
|
|
Post,
|
|
|
|
Put,
|
|
|
|
Connect,
|
|
|
|
Options,
|
|
|
|
Trace,
|
2017-10-05 15:13:40 +00:00
|
|
|
Patch = 24,
|
2014-04-20 08:45:10 +00:00
|
|
|
};
|
|
|
|
|
2014-08-06 20:25:18 +00:00
|
|
|
inline std::string method_name(HTTPMethod method)
|
2014-04-20 08:45:10 +00:00
|
|
|
{
|
|
|
|
switch(method)
|
|
|
|
{
|
2016-08-27 05:15:16 +00:00
|
|
|
case HTTPMethod::Delete:
|
2014-04-20 08:45:10 +00:00
|
|
|
return "DELETE";
|
2016-08-27 05:15:16 +00:00
|
|
|
case HTTPMethod::Get:
|
2014-04-20 08:45:10 +00:00
|
|
|
return "GET";
|
2016-08-27 05:15:16 +00:00
|
|
|
case HTTPMethod::Head:
|
2014-04-20 08:45:10 +00:00
|
|
|
return "HEAD";
|
2016-08-27 05:15:16 +00:00
|
|
|
case HTTPMethod::Post:
|
2014-04-20 08:45:10 +00:00
|
|
|
return "POST";
|
2016-08-27 05:15:16 +00:00
|
|
|
case HTTPMethod::Put:
|
2014-04-20 08:45:10 +00:00
|
|
|
return "PUT";
|
2016-08-27 05:15:16 +00:00
|
|
|
case HTTPMethod::Connect:
|
2014-04-20 08:45:10 +00:00
|
|
|
return "CONNECT";
|
2016-08-27 05:15:16 +00:00
|
|
|
case HTTPMethod::Options:
|
2014-04-20 08:45:10 +00:00
|
|
|
return "OPTIONS";
|
2016-08-27 05:15:16 +00:00
|
|
|
case HTTPMethod::Trace:
|
2014-04-20 08:45:10 +00:00
|
|
|
return "TRACE";
|
2017-10-05 15:13:40 +00:00
|
|
|
case HTTPMethod::Patch:
|
|
|
|
return "PATCH";
|
2014-04-20 08:45:10 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2014-04-15 17:57:18 +00:00
|
|
|
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
|
|
|
{
|
2014-04-14 16:43:25 +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
|
|
|
{
|
2014-04-14 16:43:25 +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
|
|
|
{
|
2014-04-14 16:43:25 +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
|
|
|
{
|
2014-04-14 16:43:25 +00:00
|
|
|
return string_params[index];
|
2014-04-14 15:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-20 08:45:10 +00:00
|
|
|
|
2015-02-20 02:47:51 +00:00
|
|
|
#ifndef CROW_MSVC_WORKAROUND
|
2016-08-27 09:03:49 +00:00
|
|
|
constexpr crow::HTTPMethod operator "" _method(const char* str, size_t /*len*/)
|
2014-04-20 08:45:10 +00:00
|
|
|
{
|
|
|
|
return
|
2016-08-27 05:15:16 +00:00
|
|
|
crow::black_magic::is_equ_p(str, "GET", 3) ? crow::HTTPMethod::Get :
|
|
|
|
crow::black_magic::is_equ_p(str, "DELETE", 6) ? crow::HTTPMethod::Delete :
|
|
|
|
crow::black_magic::is_equ_p(str, "HEAD", 4) ? crow::HTTPMethod::Head :
|
|
|
|
crow::black_magic::is_equ_p(str, "POST", 4) ? crow::HTTPMethod::Post :
|
|
|
|
crow::black_magic::is_equ_p(str, "PUT", 3) ? crow::HTTPMethod::Put :
|
|
|
|
crow::black_magic::is_equ_p(str, "OPTIONS", 7) ? crow::HTTPMethod::Options :
|
|
|
|
crow::black_magic::is_equ_p(str, "CONNECT", 7) ? crow::HTTPMethod::Connect :
|
|
|
|
crow::black_magic::is_equ_p(str, "TRACE", 5) ? crow::HTTPMethod::Trace :
|
2017-10-05 15:13:40 +00:00
|
|
|
crow::black_magic::is_equ_p(str, "PATCH", 5) ? crow::HTTPMethod::Patch :
|
2014-04-20 08:45:10 +00:00
|
|
|
throw std::runtime_error("invalid http method");
|
2016-08-27 09:03:49 +00:00
|
|
|
}
|
2015-02-20 02:47:51 +00:00
|
|
|
#endif
|