2017-07-20 08:01:46 -07:00
|
|
|
//
|
|
|
|
|
// Copyright (c) 2013-2016 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)
|
|
|
|
|
//
|
|
|
|
|
|
2016-10-09 06:34:35 -04:00
|
|
|
#ifndef BEAST_HTTP_IMPL_MESSAGE_IPP
|
|
|
|
|
#define BEAST_HTTP_IMPL_MESSAGE_IPP
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2016-06-20 09:40:35 -04:00
|
|
|
#include <beast/core/error.hpp>
|
2016-10-15 13:40:17 -04:00
|
|
|
#include <beast/http/concepts.hpp>
|
2016-05-24 06:17:04 -04:00
|
|
|
#include <beast/http/rfc7230.hpp>
|
2016-09-30 08:44:49 -04:00
|
|
|
#include <beast/core/detail/ci_char_traits.hpp>
|
2016-10-13 12:32:01 +10:00
|
|
|
#include <beast/core/detail/type_traits.hpp>
|
2016-10-09 06:34:35 -04:00
|
|
|
#include <boost/assert.hpp>
|
2016-04-29 06:04:40 -04:00
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
#include <stdexcept>
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
|
namespace beast {
|
|
|
|
|
namespace http {
|
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<class Fields>
|
2016-11-07 12:20:39 -05:00
|
|
|
void
|
|
|
|
|
swap(
|
2016-11-10 05:34:49 -05:00
|
|
|
header<true, Fields>& m1,
|
|
|
|
|
header<true, Fields>& m2)
|
2016-11-07 12:20:39 -05:00
|
|
|
{
|
|
|
|
|
using std::swap;
|
|
|
|
|
swap(m1.version, m2.version);
|
|
|
|
|
swap(m1.method, m2.method);
|
|
|
|
|
swap(m1.url, m2.url);
|
2016-11-10 05:34:49 -05:00
|
|
|
swap(m1.fields, m2.fields);
|
2016-11-07 12:20:39 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<class Fields>
|
2016-11-07 12:20:39 -05:00
|
|
|
void
|
|
|
|
|
swap(
|
2016-11-10 05:34:49 -05:00
|
|
|
header<false, Fields>& a,
|
|
|
|
|
header<false, Fields>& b)
|
2016-11-07 12:20:39 -05:00
|
|
|
{
|
|
|
|
|
using std::swap;
|
|
|
|
|
swap(a.version, b.version);
|
|
|
|
|
swap(a.status, b.status);
|
|
|
|
|
swap(a.reason, b.reason);
|
2016-11-10 05:34:49 -05:00
|
|
|
swap(a.fields, b.fields);
|
2016-11-07 12:20:39 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Body, class Fields>
|
2016-11-07 12:20:39 -05:00
|
|
|
void
|
|
|
|
|
swap(
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields>& m1,
|
|
|
|
|
message<isRequest, Body, Fields>& m2)
|
2016-11-07 12:20:39 -05:00
|
|
|
{
|
|
|
|
|
using std::swap;
|
|
|
|
|
swap(m1.base(), m2.base());
|
|
|
|
|
swap(m1.body, m2.body);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Fields>
|
2017-07-20 08:01:46 -07:00
|
|
|
bool
|
2016-11-10 05:34:49 -05:00
|
|
|
is_keep_alive(header<isRequest, Fields> const& msg)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-10-09 06:34:35 -04:00
|
|
|
BOOST_ASSERT(msg.version == 10 || msg.version == 11);
|
|
|
|
|
if(msg.version == 11)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
if(token_list{msg.fields["Connection"]}.exists("close"))
|
2017-07-20 08:01:46 -07:00
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-11-10 05:34:49 -05:00
|
|
|
if(token_list{msg.fields["Connection"]}.exists("keep-alive"))
|
2017-07-20 08:01:46 -07:00
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Fields>
|
2017-07-20 08:01:46 -07:00
|
|
|
bool
|
2016-11-10 05:34:49 -05:00
|
|
|
is_upgrade(header<isRequest, Fields> const& msg)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-10-09 06:34:35 -04:00
|
|
|
BOOST_ASSERT(msg.version == 10 || msg.version == 11);
|
|
|
|
|
if(msg.version == 10)
|
2017-07-20 08:01:46 -07:00
|
|
|
return false;
|
2016-11-10 05:34:49 -05:00
|
|
|
if(token_list{msg.fields["Connection"]}.exists("upgrade"))
|
2017-07-20 08:01:46 -07:00
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 06:04:40 -04:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
|
|
struct prepare_info
|
|
|
|
|
{
|
|
|
|
|
boost::optional<connection> connection_value;
|
|
|
|
|
boost::optional<std::uint64_t> content_length;
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Body, class Fields>
|
2016-04-29 06:04:40 -04:00
|
|
|
inline
|
|
|
|
|
void
|
|
|
|
|
prepare_options(prepare_info& pi,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields>& msg)
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
2016-10-13 12:32:01 +10:00
|
|
|
beast::detail::ignore_unused(pi, msg);
|
2016-04-29 06:04:40 -04:00
|
|
|
}
|
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Body, class Fields>
|
2016-04-29 06:04:40 -04:00
|
|
|
void
|
|
|
|
|
prepare_option(prepare_info& pi,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields>& msg,
|
2016-04-29 06:04:40 -04:00
|
|
|
connection value)
|
|
|
|
|
{
|
2016-10-13 12:32:01 +10:00
|
|
|
beast::detail::ignore_unused(msg);
|
2016-04-29 06:04:40 -04:00
|
|
|
pi.connection_value = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Body, class Fields,
|
2016-04-29 06:04:40 -04:00
|
|
|
class Opt, class... Opts>
|
|
|
|
|
void
|
|
|
|
|
prepare_options(prepare_info& pi,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields>& msg,
|
2016-04-29 06:04:40 -04:00
|
|
|
Opt&& opt, Opts&&... opts)
|
|
|
|
|
{
|
|
|
|
|
prepare_option(pi, msg, opt);
|
|
|
|
|
prepare_options(pi, msg,
|
|
|
|
|
std::forward<Opts>(opts)...);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Body, class Fields>
|
2016-04-29 06:04:40 -04:00
|
|
|
void
|
|
|
|
|
prepare_content_length(prepare_info& pi,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields> const& msg,
|
2016-04-29 06:04:40 -04:00
|
|
|
std::true_type)
|
|
|
|
|
{
|
|
|
|
|
typename Body::writer w(msg);
|
2016-06-20 09:40:35 -04:00
|
|
|
// VFALCO This is a design problem!
|
|
|
|
|
error_code ec;
|
|
|
|
|
w.init(ec);
|
|
|
|
|
if(ec)
|
|
|
|
|
throw system_error{ec};
|
2016-04-29 06:04:40 -04:00
|
|
|
pi.content_length = w.content_length();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Body, class Fields>
|
2016-04-29 06:04:40 -04:00
|
|
|
void
|
|
|
|
|
prepare_content_length(prepare_info& pi,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields> const& msg,
|
2016-04-29 06:04:40 -04:00
|
|
|
std::false_type)
|
|
|
|
|
{
|
2016-10-13 12:32:01 +10:00
|
|
|
beast::detail::ignore_unused(msg);
|
2016-04-29 06:04:40 -04:00
|
|
|
pi.content_length = boost::none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
|
|
template<
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Body, class Fields,
|
2016-04-29 06:04:40 -04:00
|
|
|
class... Options>
|
|
|
|
|
void
|
2016-11-10 05:34:49 -05:00
|
|
|
prepare(message<isRequest, Body, Fields>& msg,
|
2016-04-29 06:04:40 -04:00
|
|
|
Options&&... options)
|
|
|
|
|
{
|
2016-11-02 16:02:43 -04:00
|
|
|
using beast::detail::make_exception;
|
|
|
|
|
|
2016-04-29 06:04:40 -04:00
|
|
|
// VFALCO TODO
|
2016-10-15 13:40:17 -04:00
|
|
|
static_assert(is_Body<Body>::value,
|
|
|
|
|
"Body requirements not met");
|
|
|
|
|
static_assert(has_writer<Body>::value,
|
|
|
|
|
"Body has no writer");
|
|
|
|
|
static_assert(is_Writer<typename Body::writer,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields>>::value,
|
2016-10-15 13:40:17 -04:00
|
|
|
"Writer requirements not met");
|
2016-04-29 06:04:40 -04:00
|
|
|
detail::prepare_info pi;
|
|
|
|
|
detail::prepare_content_length(pi, msg,
|
|
|
|
|
detail::has_content_length<typename Body::writer>{});
|
|
|
|
|
detail::prepare_options(pi, msg,
|
|
|
|
|
std::forward<Options>(options)...);
|
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
if(msg.fields.exists("Connection"))
|
2016-11-02 16:02:43 -04:00
|
|
|
throw make_exception<std::invalid_argument>(
|
|
|
|
|
"prepare called with Connection field set", __FILE__, __LINE__);
|
2016-04-29 06:04:40 -04:00
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
if(msg.fields.exists("Content-Length"))
|
2016-11-02 16:02:43 -04:00
|
|
|
throw make_exception<std::invalid_argument>(
|
|
|
|
|
"prepare called with Content-Length field set", __FILE__, __LINE__);
|
2016-04-29 06:04:40 -04:00
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
if(token_list{msg.fields["Transfer-Encoding"]}.exists("chunked"))
|
2016-11-02 16:02:43 -04:00
|
|
|
throw make_exception<std::invalid_argument>(
|
|
|
|
|
"prepare called with Transfer-Encoding: chunked set", __FILE__, __LINE__);
|
2016-04-29 06:04:40 -04:00
|
|
|
|
|
|
|
|
if(pi.connection_value != connection::upgrade)
|
|
|
|
|
{
|
|
|
|
|
if(pi.content_length)
|
|
|
|
|
{
|
2016-09-30 08:44:49 -04:00
|
|
|
struct set_field
|
|
|
|
|
{
|
|
|
|
|
void
|
2016-11-10 05:34:49 -05:00
|
|
|
operator()(message<true, Body, Fields>& msg,
|
2016-09-30 08:44:49 -04:00
|
|
|
detail::prepare_info const& pi) const
|
|
|
|
|
{
|
|
|
|
|
using beast::detail::ci_equal;
|
|
|
|
|
if(*pi.content_length > 0 ||
|
|
|
|
|
ci_equal(msg.method, "POST"))
|
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
msg.fields.insert(
|
2016-11-09 10:40:09 -05:00
|
|
|
"Content-Length", *pi.content_length);
|
2016-09-30 08:44:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-11-10 05:34:49 -05:00
|
|
|
operator()(message<false, Body, Fields>& msg,
|
2016-09-30 08:44:49 -04:00
|
|
|
detail::prepare_info const& pi) const
|
|
|
|
|
{
|
|
|
|
|
if((msg.status / 100 ) != 1 &&
|
|
|
|
|
msg.status != 204 &&
|
|
|
|
|
msg.status != 304)
|
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
msg.fields.insert(
|
2016-11-09 10:40:09 -05:00
|
|
|
"Content-Length", *pi.content_length);
|
2016-09-30 08:44:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
set_field{}(msg, pi);
|
2016-04-29 06:04:40 -04:00
|
|
|
}
|
|
|
|
|
else if(msg.version >= 11)
|
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
msg.fields.insert("Transfer-Encoding", "chunked");
|
2016-04-29 06:04:40 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto const content_length =
|
2016-11-10 05:34:49 -05:00
|
|
|
msg.fields.exists("Content-Length");
|
2016-04-29 06:04:40 -04:00
|
|
|
|
|
|
|
|
if(pi.connection_value)
|
|
|
|
|
{
|
|
|
|
|
switch(*pi.connection_value)
|
|
|
|
|
{
|
|
|
|
|
case connection::upgrade:
|
2016-11-10 05:34:49 -05:00
|
|
|
msg.fields.insert("Connection", "upgrade");
|
2016-04-29 06:04:40 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case connection::keep_alive:
|
|
|
|
|
if(msg.version < 11)
|
|
|
|
|
{
|
|
|
|
|
if(content_length)
|
2016-11-10 05:34:49 -05:00
|
|
|
msg.fields.insert("Connection", "keep-alive");
|
2016-04-29 06:04:40 -04:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case connection::close:
|
|
|
|
|
if(msg.version >= 11)
|
2016-11-10 05:34:49 -05:00
|
|
|
msg.fields.insert("Connection", "close");
|
2016-04-29 06:04:40 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rfc7230 6.7.
|
2016-05-24 06:17:04 -04:00
|
|
|
if(msg.version < 11 && token_list{
|
2016-11-10 05:34:49 -05:00
|
|
|
msg.fields["Connection"]}.exists("upgrade"))
|
2016-11-02 16:02:43 -04:00
|
|
|
throw make_exception<std::invalid_argument>(
|
|
|
|
|
"invalid version for Connection: upgrade", __FILE__, __LINE__);
|
2016-04-29 06:04:40 -04:00
|
|
|
}
|
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
} // http
|
|
|
|
|
} // beast
|
|
|
|
|
|
|
|
|
|
#endif
|