Refactor method and verb (API Change):

The verb interfaces now use verb::unknown instead of
boost::optional<verb> == boost::none to indicate that
the request-method is an unrecognized string.

The http::header interface is modified to focus more on the
verb enum rather than the string. For recognized verbs, the
implementation stores an integer instead of the string.
Unknown verbs are still stored as strings.

* header::method now returns a verb
* header::method_string returns the method text
This commit is contained in:
Vinnie Falco
2017-07-20 08:12:18 -07:00
parent d3049fa03b
commit 048ee7523c
15 changed files with 205 additions and 87 deletions
+3 -21
View File
@@ -100,7 +100,6 @@ basic_fields(basic_fields&& other)
std::move(other.member()))
, detail::basic_fields_base(
std::move(other.set_), std::move(other.list_))
, verb_(other.verb_)
{
}
@@ -115,7 +114,6 @@ operator=(basic_fields&& other) ->
clear();
move_assign(other, std::integral_constant<bool,
alloc_traits::propagate_on_container_move_assignment::value>{});
verb_ = other.verb_;
return *this;
}
@@ -126,7 +124,6 @@ basic_fields(basic_fields const& other)
select_on_container_copy_construction(other.member()))
{
copy_from(other);
verb_ = other.verb_;
}
template<class Allocator>
@@ -138,7 +135,6 @@ operator=(basic_fields const& other) ->
clear();
copy_assign(other, std::integral_constant<bool,
alloc_traits::propagate_on_container_copy_assignment::value>{});
verb_ = other.verb_;
return *this;
}
@@ -148,7 +144,6 @@ basic_fields<Allocator>::
basic_fields(basic_fields<OtherAlloc> const& other)
{
copy_from(other);
verb_ = other.verb_;
}
template<class Allocator>
@@ -160,7 +155,6 @@ operator=(basic_fields<OtherAlloc> const& other) ->
{
clear();
copy_from(other);
verb_ = other.verb_;
return *this;
}
@@ -260,29 +254,17 @@ replace(string_view const& name,
template<class Allocator>
string_view
basic_fields<Allocator>::
method() const
method_string() const
{
if(verb_)
return to_string(*verb_);
return (*this)[":method"];
}
template<class Allocator>
void
basic_fields<Allocator>::
method(verb v)
method_string(string_view s)
{
verb_ = v;
this->erase(":method");
}
template<class Allocator>
void
basic_fields<Allocator>::
method(string_view const& s)
{
verb_ = string_to_verb(s);
if(verb_)
if(s.empty())
this->erase(":method");
else
this->replace(":method", s);
+39 -1
View File
@@ -22,6 +22,43 @@
namespace beast {
namespace http {
template<class Fields>
template<class>
string_view
header<true, Fields>::
get_method_string() const
{
if(method_ != verb::unknown)
return to_string(method_);
return fields.method_string();
}
template<class Fields>
template<class>
void
header<true, Fields>::
set_method(verb v)
{
if(v == verb::unknown)
BOOST_THROW_EXCEPTION(
std::invalid_argument{"unknown verb"});
method_ = v;
fields.method_string({});
}
template<class Fields>
template<class>
void
header<true, Fields>::
set_method(string_view s)
{
method_ = string_to_verb(s);
if(method_ != verb::unknown)
fields.method_string({});
else
fields.method_string(s);
}
template<class Fields>
void
swap(
@@ -31,6 +68,7 @@ swap(
using std::swap;
swap(m1.version, m2.version);
swap(m1.fields, m2.fields);
swap(m1.method_, m2.method_);
}
template<class Fields>
@@ -193,7 +231,7 @@ prepare(message<isRequest, Body, Fields>& msg,
{
using beast::detail::ci_equal;
if(*pi.content_length > 0 ||
ci_equal(msg.method(), "POST"))
msg.method() == verb::post)
{
msg.fields.insert(
"Content-Length", *pi.content_length);
+11 -7
View File
@@ -62,13 +62,16 @@ verb_to_string(verb v)
case verb::link: return "LINK";
case verb::unlink: return "UNLINK";
case verb::unknown:
return "<unknown>";
}
BOOST_THROW_EXCEPTION(std::logic_error{"unknown method"});
BOOST_THROW_EXCEPTION(std::invalid_argument{"unknown verb"});
}
template<class = void>
boost::optional<verb>
verb
string_to_verb(string_view v)
{
/*
@@ -107,7 +110,8 @@ string_to_verb(string_view v)
UNSUBSCRIBE
*/
if(v.size() < 3)
return boost::none;
return verb::unknown;
// s must be null terminated
auto const eq =
[](string_view sv, char const* s)
{
@@ -118,8 +122,8 @@ string_to_verb(string_view v)
return false;
++s;
++p;
if(*s == 0)
return *p == 0;
if(! *s)
return p == sv.end();
}
};
auto c = v[0];
@@ -303,7 +307,7 @@ string_to_verb(string_view v)
break;
}
return boost::none;
return verb::unknown;
}
} // detail
@@ -316,7 +320,7 @@ to_string(verb v)
}
inline
boost::optional<verb>
verb
string_to_verb(string_view s)
{
return detail::string_to_verb(s);