Skip to content

Commit

Permalink
Add http method conversion function
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy-peak committed Jun 30, 2023
1 parent 61073b4 commit 39707c0
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 94 deletions.
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(std::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

0 comments on commit 39707c0

Please sign in to comment.