Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser optimization #332

Merged
merged 8 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 164 additions & 27 deletions include/crow/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

namespace crow
{
const char cr = '\r';
const char lf = '\n';
const std::string crlf("\r\n");

Comment on lines +11 to +14
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

came from multipart.h, connection.h, and the parser.

enum class HTTPMethod : char
{
#ifndef DELETE
Expand All @@ -16,29 +20,148 @@ namespace crow
HEAD,
POST,
PUT,

CONNECT,
OPTIONS,
TRACE,

PATCH,
PURGE,

COPY,
LOCK,
MKCOL,
MOVE,
PROPFIND,
PROPPATCH,
SEARCH,
UNLOCK,
BIND,
REBIND,
UNBIND,
ACL,

REPORT,
MKACTIVITY,
CHECKOUT,
MERGE,

MSEARCH,
NOTIFY,
SUBSCRIBE,
UNSUBSCRIBE,

MKCALENDAR,

LINK,
UNLINK,

SOURCE,
Comment on lines +30 to +59
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Came from the parser

#endif

Delete = 0,
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,

MSearch,
Notify,
Subscribe,
Unsubscribe,

Mkcalendar,

Link,
Unlink,

Source,


InternalMethodCount,
// should not add an item below this line: used for array count
};

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";
}

Comment on lines +110 to +164
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of inspired by the parser

// clang-format off

enum status
Expand Down Expand Up @@ -85,25 +208,6 @@ namespace crow
VARIANT_ALSO_NEGOTIATES = 506
};

inline 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";
case HTTPMethod::Patch: return "PATCH";
case HTTPMethod::Purge: return "PURGE";
default: return "invalid";
}
return "invalid";
}

// clang-format on

enum class ParamType : char
Expand Down Expand Up @@ -170,19 +274,52 @@ namespace crow
}
} // namespace crow

// clang-format off
#ifndef CROW_MSVC_WORKAROUND
constexpr crow::HTTPMethod operator"" _method(const char* str, size_t /*len*/)
{
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 :
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 :

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 :
crow::black_magic::is_equ_p(str, "PATCH", 5) ? crow::HTTPMethod::Patch :
crow::black_magic::is_equ_p(str, "PURGE", 5) ? crow::HTTPMethod::Purge :
throw std::runtime_error("invalid http method");

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");
}
#endif
// clang-format on
Loading