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

Add http method conversion function #1659

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
94 changes: 94 additions & 0 deletions lib/inc/drogon/HttpTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,98 @@ inline trantor::LogStream &operator<<(trantor::LogStream &out,
{
return out << to_string_view(result);
}

inline string_view to_string_view(drogon::HttpMethod method)
{
switch (method)
{
case HttpMethod::Get:
return "GET";
case HttpMethod::Post:
return "POST";
case HttpMethod::Head:
return "HEAD";
case HttpMethod::Put:
return "PUT";
case HttpMethod::Delete:
return "DELETE";
case HttpMethod::Options:
return "OPTIONS";
case HttpMethod::Patch:
return "PATCH";
case HttpMethod::Invalid:
return "INVALID";
default:
return "UNKNOWN";
}
}

inline std::string to_string(drogon::HttpMethod method)
{
auto sv = to_string_view(method);
return std::string(sv.data(), sv.size());
}

inline drogon::HttpMethod to_http_method(string_view m)
{
switch (m.length())
{
case 3:
if (m == "GET")
{
return HttpMethod::Get;
}
else if (m == "PUT")
{
return HttpMethod::Put;
}
else
{
return HttpMethod::Invalid;
}
case 4:
if (m == "POST")
{
return HttpMethod::Post;
}
else if (m == "HEAD")
{
return HttpMethod::Head;
}
else
{
return HttpMethod::Invalid;
}
case 5:
if (m == "PATCH")
{
return HttpMethod::Patch;
}
else
{
return HttpMethod::Invalid;
}
case 6:
if (m == "DELETE")
{
return HttpMethod::Delete;
}
else
{
return HttpMethod::Invalid;
}
case 7:
if (m == "OPTIONS")
{
return HttpMethod::Options;
}
else
{
return HttpMethod::Invalid;
}
default:
return HttpMethod::Invalid;
}
}

} // namespace drogon
97 changes: 3 additions & 94 deletions lib/src/HttpRequestImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -597,105 +597,14 @@ const char *HttpRequestImpl::versionString() const

const char *HttpRequestImpl::methodString() const
{
const char *result = "UNKNOWN";
switch (method_)
{
case Get:
result = "GET";
break;
case Post:
result = "POST";
break;
case Head:
result = "HEAD";
break;
case Put:
result = "PUT";
break;
case Delete:
result = "DELETE";
break;
case Options:
result = "OPTIONS";
break;
case Patch:
result = "PATCH";
break;
default:
break;
}
return result;
auto sv = to_string_view(method_);
return sv.data();
}

bool HttpRequestImpl::setMethod(const char *start, const char *end)
{
assert(method_ == Invalid);
string_view m(start, end - start);
switch (m.length())
{
case 3:
if (m == "GET")
{
method_ = Get;
}
else if (m == "PUT")
{
method_ = Put;
}
else
{
method_ = Invalid;
}
break;
case 4:
if (m == "POST")
{
method_ = Post;
}
else if (m == "HEAD")
{
method_ = Head;
}
else
{
method_ = Invalid;
}
break;
case 5:
if (m == "PATCH")
{
method_ = Patch;
}
else
{
method_ = Invalid;
}
break;
case 6:
if (m == "DELETE")
{
method_ = Delete;
}
else
{
method_ = Invalid;
}
break;
case 7:
if (m == "OPTIONS")
{
method_ = Options;
}
else
{
method_ = Invalid;
}
break;
default:
method_ = Invalid;
break;
}

method_ = to_http_method(string_view(start, end - start));
// if (method_ != Invalid)
// {
// content_ = "";
Expand Down