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
|
|
|
|
2022-02-05 15:15:19 +00:00
|
|
|
// TODO(EDev): Adding C++20's [[likely]] and [[unlikely]] attributes might be useful
|
|
|
|
#if defined(__GNUG__) || defined(__clang__)
|
|
|
|
#define CROW_LIKELY(X) __builtin_expect(!!(X), 1)
|
|
|
|
#define CROW_UNLIKELY(X) __builtin_expect(!!(X), 0)
|
|
|
|
#else
|
|
|
|
#define CROW_LIKELY(X) (X)
|
|
|
|
#define CROW_UNLIKELY(X) (X)
|
|
|
|
#endif
|
|
|
|
|
2014-04-26 17:19:59 +00:00
|
|
|
namespace crow
|
2014-04-14 15:31:51 +00:00
|
|
|
{
|
2022-02-05 15:15:19 +00:00
|
|
|
const char cr = '\r';
|
|
|
|
const char lf = '\n';
|
|
|
|
const std::string crlf("\r\n");
|
|
|
|
|
2021-07-30 10:09:01 +00:00
|
|
|
enum class HTTPMethod : char
|
2014-04-20 08:45:10 +00:00
|
|
|
{
|
2016-08-27 05:15:16 +00:00
|
|
|
#ifndef DELETE
|
|
|
|
DELETE = 0,
|
2014-04-20 08:45:10 +00:00
|
|
|
GET,
|
|
|
|
HEAD,
|
|
|
|
POST,
|
|
|
|
PUT,
|
2022-02-05 15:15:19 +00:00
|
|
|
|
2014-04-20 08:45:10 +00:00
|
|
|
CONNECT,
|
|
|
|
OPTIONS,
|
|
|
|
TRACE,
|
2022-02-05 15:15:19 +00:00
|
|
|
|
2017-10-21 12:20:07 +00:00
|
|
|
PATCH,
|
|
|
|
PURGE,
|
2022-02-05 15:15:19 +00:00
|
|
|
|
|
|
|
COPY,
|
|
|
|
LOCK,
|
|
|
|
MKCOL,
|
|
|
|
MOVE,
|
|
|
|
PROPFIND,
|
|
|
|
PROPPATCH,
|
|
|
|
SEARCH,
|
|
|
|
UNLOCK,
|
|
|
|
BIND,
|
|
|
|
REBIND,
|
|
|
|
UNBIND,
|
|
|
|
ACL,
|
|
|
|
|
|
|
|
REPORT,
|
|
|
|
MKACTIVITY,
|
|
|
|
CHECKOUT,
|
|
|
|
MERGE,
|
|
|
|
|
|
|
|
MSEARCH,
|
|
|
|
NOTIFY,
|
|
|
|
SUBSCRIBE,
|
|
|
|
UNSUBSCRIBE,
|
|
|
|
|
|
|
|
MKCALENDAR,
|
|
|
|
|
|
|
|
LINK,
|
|
|
|
UNLINK,
|
|
|
|
|
|
|
|
SOURCE,
|
2016-08-27 05:15:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Delete = 0,
|
|
|
|
Get,
|
|
|
|
Head,
|
|
|
|
Post,
|
|
|
|
Put,
|
2022-02-05 15:15:19 +00:00
|
|
|
|
2016-08-27 05:15:16 +00:00
|
|
|
Connect,
|
|
|
|
Options,
|
|
|
|
Trace,
|
2022-02-05 15:15:19 +00:00
|
|
|
|
2017-10-21 12:20:07 +00:00
|
|
|
Patch,
|
|
|
|
Purge,
|
|
|
|
|
2022-02-05 15:15:19 +00:00
|
|
|
Copy,
|
|
|
|
Lock,
|
|
|
|
MkCol,
|
|
|
|
Move,
|
|
|
|
Propfind,
|
|
|
|
Proppatch,
|
|
|
|
Search,
|
|
|
|
Unlock,
|
|
|
|
Bind,
|
|
|
|
Rebind,
|
|
|
|
Unbind,
|
|
|
|
Acl,
|
|
|
|
|
|
|
|
Report,
|
|
|
|
MkActivity,
|
|
|
|
Checkout,
|
|
|
|
Merge,
|
|
|
|
|
|
|
|
MSearch,
|
|
|
|
Notify,
|
|
|
|
Subscribe,
|
|
|
|
Unsubscribe,
|
|
|
|
|
|
|
|
Mkcalendar,
|
|
|
|
|
|
|
|
Link,
|
|
|
|
Unlink,
|
|
|
|
|
|
|
|
Source,
|
|
|
|
|
2017-10-21 12:20:07 +00:00
|
|
|
|
|
|
|
InternalMethodCount,
|
|
|
|
// should not add an item below this line: used for array count
|
2014-04-20 08:45:10 +00:00
|
|
|
};
|
|
|
|
|
2022-02-05 15:15:19 +00:00
|
|
|
const char* method_strings[] =
|
|
|
|
{
|
|
|
|
"DELETE",
|
|
|
|
"GET",
|
|
|
|
"HEAD",
|
|
|
|
"POST",
|
|
|
|
"PUT",
|
|
|
|
|
|
|
|
"CONNECT",
|
|
|
|
"OPTIONS",
|
|
|
|
"TRACE",
|
|
|
|
|
|
|
|
"PATCH",
|
|
|
|
"PURGE",
|
|
|
|
|
|
|
|
"COPY",
|
|
|
|
"LOCK",
|
|
|
|
"MKCOL",
|
|
|
|
"MOVE",
|
|
|
|
"PROPFIND",
|
|
|
|
"PROPPATCH",
|
|
|
|
"SEARCH",
|
|
|
|
"UNLOCK",
|
|
|
|
"BIND",
|
|
|
|
"REBIND",
|
|
|
|
"UNBIND",
|
|
|
|
"ACL",
|
|
|
|
|
|
|
|
"REPORT",
|
|
|
|
"MKACTIVITY",
|
|
|
|
"CHECKOUT",
|
|
|
|
"MERGE",
|
|
|
|
|
|
|
|
"M-SEARCH",
|
|
|
|
"NOTIFY",
|
|
|
|
"SUBSCRIBE",
|
|
|
|
"UNSUBSCRIBE",
|
|
|
|
|
|
|
|
"MKCALENDAR",
|
|
|
|
|
|
|
|
"LINK",
|
|
|
|
"UNLINK",
|
|
|
|
|
|
|
|
"SOURCE"};
|
|
|
|
|
|
|
|
|
|
|
|
inline std::string method_name(HTTPMethod method)
|
|
|
|
{
|
|
|
|
if (CROW_LIKELY(method < HTTPMethod::InternalMethodCount))
|
|
|
|
{
|
|
|
|
return method_strings[(unsigned char)method];
|
|
|
|
}
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
// clang-format off
|
|
|
|
|
2021-09-30 17:38:23 +00:00
|
|
|
enum status
|
|
|
|
{
|
|
|
|
CONTINUE = 100,
|
|
|
|
SWITCHING_PROTOCOLS = 101,
|
|
|
|
|
|
|
|
OK = 200,
|
|
|
|
CREATED = 201,
|
|
|
|
ACCEPTED = 202,
|
|
|
|
NON_AUTHORITATIVE_INFORMATION = 203,
|
|
|
|
NO_CONTENT = 204,
|
|
|
|
RESET_CONTENT = 205,
|
|
|
|
PARTIAL_CONTENT = 206,
|
|
|
|
|
|
|
|
MULTIPLE_CHOICES = 300,
|
|
|
|
MOVED_PERMANENTLY = 301,
|
|
|
|
FOUND = 302,
|
|
|
|
SEE_OTHER = 303,
|
|
|
|
NOT_MODIFIED = 304,
|
|
|
|
TEMPORARY_REDIRECT = 307,
|
|
|
|
PERMANENT_REDIRECT = 308,
|
|
|
|
|
|
|
|
BAD_REQUEST = 400,
|
|
|
|
UNAUTHORIZED = 401,
|
|
|
|
FORBIDDEN = 403,
|
|
|
|
NOT_FOUND = 404,
|
|
|
|
METHOD_NOT_ALLOWED = 405,
|
|
|
|
PROXY_AUTHENTICATION_REQUIRED = 407,
|
|
|
|
CONFLICT = 409,
|
|
|
|
GONE = 410,
|
|
|
|
PAYLOAD_TOO_LARGE = 413,
|
|
|
|
UNSUPPORTED_MEDIA_TYPE = 415,
|
|
|
|
RANGE_NOT_SATISFIABLE = 416,
|
|
|
|
EXPECTATION_FAILED = 417,
|
|
|
|
PRECONDITION_REQUIRED = 428,
|
|
|
|
TOO_MANY_REQUESTS = 429,
|
|
|
|
UNAVAILABLE_FOR_LEGAL_REASONS = 451,
|
|
|
|
|
|
|
|
INTERNAL_SERVER_ERROR = 500,
|
|
|
|
NOT_IMPLEMENTED = 501,
|
|
|
|
BAD_GATEWAY = 502,
|
|
|
|
SERVICE_UNAVAILABLE = 503,
|
|
|
|
VARIANT_ALSO_NEGOTIATES = 506
|
|
|
|
};
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
// clang-format on
|
|
|
|
|
2021-07-30 10:09:01 +00:00
|
|
|
enum class ParamType : char
|
2014-04-14 15:31:51 +00:00
|
|
|
{
|
|
|
|
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;
|
2021-11-25 11:45:38 +00:00
|
|
|
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-15 17:57:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
template<typename T>
|
2014-04-14 15:31:51 +00:00
|
|
|
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
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
} // namespace crow
|
2014-04-20 08:45:10 +00:00
|
|
|
|
2022-02-06 19:29:46 +00:00
|
|
|
// clang-format off
|
2015-02-20 02:47:51 +00:00
|
|
|
#ifndef CROW_MSVC_WORKAROUND
|
2021-11-25 11:45:38 +00:00
|
|
|
constexpr crow::HTTPMethod operator"" _method(const char* str, size_t /*len*/)
|
2014-04-20 08:45:10 +00:00
|
|
|
{
|
2022-02-05 15:15:19 +00:00
|
|
|
return 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 :
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
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 :
|
2022-02-05 15:15:19 +00:00
|
|
|
|
|
|
|
crow::black_magic::is_equ_p(str, "PATCH", 5) ? crow::HTTPMethod::Patch :
|
|
|
|
crow::black_magic::is_equ_p(str, "PURGE", 5) ? crow::HTTPMethod::Purge :
|
|
|
|
crow::black_magic::is_equ_p(str, "COPY", 4) ? crow::HTTPMethod::Copy :
|
|
|
|
crow::black_magic::is_equ_p(str, "LOCK", 4) ? crow::HTTPMethod::Lock :
|
|
|
|
crow::black_magic::is_equ_p(str, "MKCOL", 5) ? crow::HTTPMethod::MkCol :
|
|
|
|
crow::black_magic::is_equ_p(str, "MOVE", 4) ? crow::HTTPMethod::Move :
|
|
|
|
crow::black_magic::is_equ_p(str, "PROPFIND", 8) ? crow::HTTPMethod::Propfind :
|
|
|
|
crow::black_magic::is_equ_p(str, "PROPPATCH", 9) ? crow::HTTPMethod::Proppatch :
|
|
|
|
crow::black_magic::is_equ_p(str, "SEARCH", 6) ? crow::HTTPMethod::Search :
|
|
|
|
crow::black_magic::is_equ_p(str, "UNLOCK", 6) ? crow::HTTPMethod::Unlock :
|
|
|
|
crow::black_magic::is_equ_p(str, "BIND", 4) ? crow::HTTPMethod::Bind :
|
|
|
|
crow::black_magic::is_equ_p(str, "REBIND", 6) ? crow::HTTPMethod::Rebind :
|
|
|
|
crow::black_magic::is_equ_p(str, "UNBIND", 6) ? crow::HTTPMethod::Unbind :
|
|
|
|
crow::black_magic::is_equ_p(str, "ACL", 3) ? crow::HTTPMethod::Acl :
|
|
|
|
|
|
|
|
crow::black_magic::is_equ_p(str, "REPORT", 6) ? crow::HTTPMethod::Report :
|
|
|
|
crow::black_magic::is_equ_p(str, "MKACTIVITY", 10) ? crow::HTTPMethod::MkActivity :
|
|
|
|
crow::black_magic::is_equ_p(str, "CHECKOUT", 8) ? crow::HTTPMethod::Checkout :
|
|
|
|
crow::black_magic::is_equ_p(str, "MERGE", 5) ? crow::HTTPMethod::Merge :
|
|
|
|
|
|
|
|
crow::black_magic::is_equ_p(str, "MSEARCH", 7) ? crow::HTTPMethod::MSearch :
|
|
|
|
crow::black_magic::is_equ_p(str, "NOTIFY", 6) ? crow::HTTPMethod::Notify :
|
|
|
|
crow::black_magic::is_equ_p(str, "SUBSCRIBE", 9) ? crow::HTTPMethod::Subscribe :
|
|
|
|
crow::black_magic::is_equ_p(str, "UNSUBSCRIBE", 11) ? crow::HTTPMethod::Unsubscribe :
|
|
|
|
|
|
|
|
crow::black_magic::is_equ_p(str, "MKCALENDAR", 10) ? crow::HTTPMethod::Mkcalendar :
|
|
|
|
|
|
|
|
crow::black_magic::is_equ_p(str, "LINK", 4) ? crow::HTTPMethod::Link :
|
|
|
|
crow::black_magic::is_equ_p(str, "UNLINK", 6) ? crow::HTTPMethod::Unlink :
|
|
|
|
|
|
|
|
crow::black_magic::is_equ_p(str, "SOURCE", 6) ? crow::HTTPMethod::Source :
|
|
|
|
throw std::runtime_error("invalid http method");
|
2016-08-27 09:03:49 +00:00
|
|
|
}
|
2015-02-20 02:47:51 +00:00
|
|
|
#endif
|
2022-02-06 19:29:46 +00:00
|
|
|
// clang-format on
|