Refactor treatment of request-method (API Change):

fix #397

method enum class is added to represent all known request methods.
Functions are provided to convert from strings to and from the method
enumeration.

The fields container is modified to also work with the enum.
This commit is contained in:
Vinnie Falco committed 2017-07-20 08:12:17 -07:00
1 parent cd2363e951
commit f7211da154
21 files changed
+737 -79

No files matched your search

+37 -9
View File
@@ -100,6 +100,7 @@ basic_fields(basic_fields&& other)
std::move(other.member()))
, detail::basic_fields_base(
std::move(other.set_), std::move(other.list_))
, verb_(other.verb_)
{
}
@@ -114,6 +115,7 @@ 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;
}
@@ -124,6 +126,7 @@ basic_fields(basic_fields const& other)
select_on_container_copy_construction(other.member()))
{
copy_from(other);
verb_ = other.verb_;
}
template<class Allocator>
@@ -135,6 +138,7 @@ 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;
}
@@ -144,6 +148,7 @@ basic_fields<Allocator>::
basic_fields(basic_fields<OtherAlloc> const& other)
{
copy_from(other);
verb_ = other.verb_;
}
template<class Allocator>
@@ -155,18 +160,10 @@ operator=(basic_fields<OtherAlloc> const& other) ->
{
clear();
copy_from(other);
verb_ = other.verb_;
return *this;
}
template<class Allocator>
template<class FwdIt>
basic_fields<Allocator>::
basic_fields(FwdIt first, FwdIt last)
{
for(;first != last; ++first)
insert(first->name(), first->value());
}
template<class Allocator>
std::size_t
basic_fields<Allocator>::
@@ -260,6 +257,37 @@ replace(string_view const& name,
insert(name, value);
}
template<class Allocator>
string_view
basic_fields<Allocator>::
method() const
{
if(verb_)
return to_string(*verb_);
return (*this)[":method"];
}
template<class Allocator>
void
basic_fields<Allocator>::
method(verb v)
{
verb_ = v;
this->erase(":method");
}
template<class Allocator>
void
basic_fields<Allocator>::
method(string_view const& s)
{
verb_ = string_to_verb(s);
if(verb_)
this->erase(":method");
else
this->replace(":method", s);
}
} // http
} // beast
+328
View File
@@ -0,0 +1,328 @@
//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_HTTP_IMPL_VERB_IPP
#define BEAST_HTTP_IMPL_VERB_IPP
#include <boost/throw_exception.hpp>
#include <stdexcept>
namespace beast {
namespace http {
namespace detail {
template<class = void>
inline
string_view
verb_to_string(verb v)
{
switch(v)
{
case verb::delete_: return "DELETE";
case verb::get: return "GET";
case verb::head: return "HEAD";
case verb::post: return "POST";
case verb::put: return "PUT";
case verb::connect: return "CONNECT";
case verb::options: return "OPTIONS";
case verb::trace: return "TRACE";
case verb::copy: return "COPY";
case verb::lock: return "LOCK";
case verb::mkcol: return "MKCOL";
case verb::move: return "MOVE";
case verb::propfind: return "PROPFIND";
case verb::proppatch: return "PROPPATCH";
case verb::search: return "SEARCH";
case verb::unlock: return "UNLOCK";
case verb::bind: return "BIND";
case verb::rebind: return "REBIND";
case verb::unbind: return "UNBIND";
case verb::acl: return "ACL";
case verb::report: return "REPORT";
case verb::mkactivity: return "MKACTIVITY";
case verb::checkout: return "CHECKOUT";
case verb::merge: return "MERGE";
case verb::msearch: return "M-SEARCH";
case verb::notify: return "NOTIFY";
case verb::subscribe: return "SUBSCRIBE";
case verb::unsubscribe: return "UNSUBSCRIBE";
case verb::patch: return "PATCH";
case verb::purge: return "PURGE";
case verb::mkcalendar: return "MKCALENDAR";
case verb::link: return "LINK";
case verb::unlink: return "UNLINK";
}
BOOST_THROW_EXCEPTION(std::logic_error{"unknown method"});
}
template<class = void>
boost::optional<verb>
string_to_verb(string_view v)
{
/*
ACL
BIND
CHECKOUT
CONNECT
COPY
DELETE
GET
HEAD
LINK
LOCK
M-SEARCH
MERGE
MKACTIVITY
MKCALENDAR
MKCOL
MOVE
NOTIFY
OPTIONS
PATCH
POST
PROPFIND
PROPPATCH
PURGE
PUT
REBIND
REPORT
SEARCH
SUBSCRIBE
TRACE
UNBIND
UNLINK
UNLOCK
UNSUBSCRIBE
*/
if(v.size() < 3)
return boost::none;
auto const eq =
[](string_view sv, char const* s)
{
auto p = sv.data();
for(;;)
{
if(*s != *p)
return false;
++s;
++p;
if(*s == 0)
return *p == 0;
}
};
auto c = v[0];
v.remove_prefix(1);
switch(c)
{
case 'A':
if(v == "CL")
return verb::acl;
break;
case 'B':
if(v == "IND")
return verb::bind;
break;
case 'C':
c = v[0];
v.remove_prefix(1);
switch(c)
{
case 'H':
if(eq(v, "ECKOUT"))
return verb::checkout;
break;
case 'O':
if(eq(v, "NNECT"))
return verb::connect;
if(eq(v, "PY"))
return verb::copy;
// [[fallthrough]]
default:
break;
}
break;
case 'D':
if(eq(v, "ELETE"))
return verb::delete_;
break;
case 'G':
if(eq(v, "ET"))
return verb::get;
break;
case 'H':
if(eq(v, "EAD"))
return verb::head;
break;
case 'L':
if(eq(v, "INK"))
return verb::link;
if(eq(v, "OCK"))
return verb::lock;
break;
case 'M':
c = v[0];
v.remove_prefix(1);
switch(c)
{
case '-':
if(eq(v, "SEARCH"))
return verb::msearch;
break;
case 'E':
if(eq(v, "RGE"))
return verb::merge;
break;
case 'K':
if(eq(v, "ACTIVITY"))
return verb::mkactivity;
if(v[0] == 'C')
{
v.remove_prefix(1);
if(eq(v, "ALENDAR"))
return verb::mkcalendar;
if(eq(v, "OL"))
return verb::mkcol;
break;
}
break;
case 'O':
if(eq(v, "VE"))
return verb::move;
// [[fallthrough]]
default:
break;
}
break;
case 'N':
if(eq(v, "OTIFY"))
return verb::notify;
break;
case 'O':
if(eq(v, "PTIONS"))
return verb::options;
break;
case 'P':
c = v[0];
v.remove_prefix(1);
switch(c)
{
case 'A':
if(eq(v, "TCH"))
return verb::patch;
break;
case 'O':
if(eq(v, "ST"))
return verb::post;
break;
case 'R':
if(eq(v, "OPFIND"))
return verb::propfind;
if(eq(v, "OPPATCH"))
return verb::proppatch;
break;
case 'U':
if(eq(v, "RGE"))
return verb::purge;
if(eq(v, "T"))
return verb::put;
// [[fallthrough]]
default:
break;
}
break;
case 'R':
if(v[0] != 'E')
break;
v.remove_prefix(1);
if(eq(v, "BIND"))
return verb::rebind;
if(eq(v, "PORT"))
return verb::report;
break;
case 'S':
if(eq(v, "EARCH"))
return verb::search;
if(eq(v, "UBSCRIBE"))
return verb::subscribe;
break;
case 'T':
if(eq(v, "RACE"))
return verb::trace;
break;
case 'U':
if(v[0] != 'N')
break;
v.remove_prefix(1);
if(eq(v, "BIND"))
return verb::unbind;
if(eq(v, "LINK"))
return verb::unlink;
if(eq(v, "LOCK"))
return verb::unlock;
if(eq(v, "SUBSCRIBE"))
return verb::unsubscribe;
break;
default:
break;
}
return boost::none;
}
} // detail
inline
string_view
to_string(verb v)
{
return detail::verb_to_string(v);
}
inline
boost::optional<verb>
string_to_verb(string_view s)
{
return detail::string_to_verb(s);
}
} // http
} // beast
#endif