Use field in basic_fields and call sites

This commit is contained in:
Vinnie Falco
2017-07-20 08:12:18 -07:00
parent d5f22adeca
commit ca25eee0dd
18 changed files with 220 additions and 69 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ connection_impl<_>::upgrade;
req.version = 11;
req.method(verb::upgrade);
req.target("/");
req.insert("User-Agent", "Beast");
req.insert(field::user_agent, "Beast");
req.prepare(connection::close, connection::upgrade);
@endcode
+84 -2
View File
@@ -12,6 +12,7 @@
#include <beast/core/string_view.hpp>
#include <beast/core/detail/ci_char_traits.hpp>
#include <beast/http/connection.hpp>
#include <beast/http/field.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/intrusive/list.hpp>
#include <boost/intrusive/set.hpp>
@@ -226,7 +227,19 @@ public:
void
clear() noexcept;
/** Remove a field.
/** Remove zero or more known fields.
If more than one field with the specified name exists, all
matching fields will be removed.
@param f The known field constant.
@return The number of fields removed.
*/
std::size_t
erase(field f);
/** Remove zero or more fields by name.
If more than one field with the specified name exists, all
matching fields will be removed.
@@ -238,7 +251,22 @@ public:
std::size_t
erase(string_view name);
/** Insert a field value.
/** Insert a value for a known field.
If a field with the same name already exists, the
existing field is untouched and a new field value pair
is inserted into the container.
@param f The known field constant.
@param name The name of the field.
@param value A string holding the value of the field.
*/
void
insert(field f, string_view value);
/** Insert a value for a field by name.
If a field with the same name already exists, the
existing field is untouched and a new field value pair
@@ -314,16 +342,70 @@ protected:
// for `header
//
/** Returns the stored request-method string.
@note This is called by the @ref header implementation.
*/
string_view method_impl() const;
/** Returns the stored request-target string.
@note This is called by the @ref header implementation.
*/
string_view target_impl() const;
/** Returns the stored obsolete reason-phrase string.
@note This is called by the @ref header implementation.
*/
string_view reason_impl() const;
/** Set or clear the stored request-method string.
@note This is called by the @ref header implementation.
*/
void method_impl(string_view s);
/** Set or clear the stored request-target string.
@note This is called by the @ref header implementation.
*/
void target_impl(string_view s);
/** Set or clear the stored obsolete reason-phrase string.
@note This is called by the @ref header implementation.
*/
void reason_impl(string_view s);
/** Set the Content-Length field to the specified value.
@note This is called by the @ref header implementation.
*/
void content_length_impl(std::uint64_t n);
/** Add close to the Connection field.
@note This is called by the @ref header implementation.
*/
void connection_impl(close_t);
/** Add keep-alive to the Connection field.
@note This is called by the @ref header implementation.
*/
void connection_impl(keep_alive_t);
/** Add upgrade to the Connection field.
@note This is called by the @ref header implementation.
*/
void connection_impl(upgrade_t);
/** Add chunked to the Transfer-Encoding field.
@note This is called by the @ref header implementation.
*/
void chunked_impl();
private:
+16
View File
@@ -300,6 +300,14 @@ clear() noexcept
list_.clear();
}
template<class Allocator>
std::size_t
basic_fields<Allocator>::
erase(field f)
{
return erase(to_string(f));
}
template<class Allocator>
std::size_t
basic_fields<Allocator>::
@@ -323,6 +331,14 @@ erase(string_view name)
return n;
}
template<class Allocator>
void
basic_fields<Allocator>::
insert(field f, string_view value)
{
insert(to_string(f), value);
}
template<class Allocator>
void
basic_fields<Allocator>::
+8
View File
@@ -103,6 +103,14 @@ size() const
Body, decltype(*this)>{});
}
template<bool isRequest, class Body, class Fields>
void
message<isRequest, Body, Fields>::
content_length(std::uint64_t n)
{
this->content_length_impl(n);
}
template<bool isRequest, class Body, class Fields>
template<class... Args>
void
+3 -1
View File
@@ -559,6 +559,8 @@ struct message : header<isRequest, Fields>
The value of the Content-Length field will be unconditionally
set to the specified number of octets.
@para n The number of octets to set for the Content-Length field.
*/
void
content_length(std::uint64_t n);
@@ -575,7 +577,7 @@ struct message : header<isRequest, Fields>
req.version = 11;
req.method(verb::upgrade);
req.target("/");
req.insert("User-Agent", "Beast");
req.insert(field::user_agent, "Beast");
req.prepare(connection::close, connection::upgrade);
@endcode
+12 -11
View File
@@ -163,12 +163,12 @@ build_request(detail::sec_ws_key_type& key,
req.target(target);
req.version = 11;
req.method(http::verb::get);
req.insert("Host", host);
req.insert("Upgrade", "websocket");
req.insert("Connection", "upgrade");
req.insert(http::field::host, host);
req.insert(http::field::upgrade, "websocket");
req.insert(http::field::connection, "upgrade");
detail::make_sec_ws_key(key, maskgen_);
req.insert("Sec-WebSocket-Key", key);
req.insert("Sec-WebSocket-Version", "13");
req.insert(http::field::sec_websocket_key, key);
req.insert(http::field::sec_websocket_version, "13");
if(pmd_opts_.client_enable)
{
detail::pmd_offer config;
@@ -187,7 +187,7 @@ build_request(detail::sec_ws_key_type& key,
if(! req.exists("User-Agent"))
{
static_string<20> s(BEAST_VERSION_STRING);
req.insert("User-Agent", s);
req.insert(http::field::user_agent, s);
}
return req;
}
@@ -205,8 +205,9 @@ build_response(http::header<true, Fields> const& req,
decorator(res);
if(! res.exists("Server"))
{
BOOST_STATIC_ASSERT(sizeof(BEAST_VERSION_STRING) < 20);
static_string<20> s(BEAST_VERSION_STRING);
res.insert("Server", s);
res.insert(http::field::server, s);
}
};
auto err =
@@ -245,7 +246,7 @@ build_response(http::header<true, Fields> const& req,
response_type res;
res.result(http::status::upgrade_required);
res.version = req.version;
res.insert("Sec-WebSocket-Version", "13");
res.insert(http::field::sec_websocket_version, "13");
res.prepare();
decorate(res);
return res;
@@ -261,12 +262,12 @@ build_response(http::header<true, Fields> const& req,
}
res.result(http::status::switching_protocols);
res.version = req.version;
res.insert("Upgrade", "websocket");
res.insert("Connection", "upgrade");
res.insert(http::field::upgrade, "websocket");
res.insert(http::field::connection, "upgrade");
{
detail::sec_ws_accept_type accept;
detail::make_sec_ws_accept(accept, key);
res.insert("Sec-WebSocket-Accept", accept);
res.insert(http::field::sec_websocket_accept, accept);
}
decorate(res);
return res;
+4 -4
View File
@@ -1563,7 +1563,7 @@ public:
ws.handshake("localhost", "/",
[](request_type& req)
{
req.insert("User-Agent", "Beast");
req.insert(field::user_agent, "Beast");
});
}
catch(...)
@@ -1625,7 +1625,7 @@ public:
ws.handshake(res, "localhost", "/",
[](request_type& req)
{
req.insert("User-Agent", "Beast");
req.insert(field::user_agent, "Beast");
});
}
catch(...)
@@ -1771,7 +1771,7 @@ public:
ws.handshake("localhost", "/",
[](request_type& req)
{
req.insert("User-Agent", "Beast");
req.insert(field::user_agent, "Beast");
},
ec);
if(ec)
@@ -1833,7 +1833,7 @@ public:
ws.handshake(res, "localhost", "/",
[](request_type& req)
{
req.insert("User-Agent", "Beast");
req.insert(field::user_agent, "Beast");
},
ec);
if(ec)