serializing file_body is not const

This commit is contained in:
Vinnie Falco
2017-07-07 23:25:23 -07:00
parent e06a055503
commit cc198db10e
7 changed files with 45 additions and 13 deletions

View File

@@ -6,6 +6,7 @@ Version 76:
* Add serializer::get * Add serializer::get
* Add serializer::chunked * Add serializer::chunked
* Serializer members are not const * Serializer members are not const
* serializing file_body is not const
API Changes: API Changes:

View File

@@ -46,12 +46,16 @@ class write_msg_op
// //
beast::http::message<isRequest, Body, Fields> msg; beast::http::message<isRequest, Body, Fields> msg;
// Serializer for the message
beast::http::serializer<isRequest, Body, Fields> sr;
data( data(
Handler& handler, Handler& handler,
AsyncWriteStream& stream_, AsyncWriteStream& stream_,
beast::http::message<isRequest, Body, Fields>&& msg_) beast::http::message<isRequest, Body, Fields>&& msg_)
: stream(stream_) : stream(stream_)
, msg(std::move(msg_)) , msg(std::move(msg_))
, sr(msg)
{ {
boost::ignore_unused(handler); boost::ignore_unused(handler);
} }
@@ -96,7 +100,7 @@ public:
{ {
auto& d = *d_; auto& d = *d_;
beast::http::async_write( beast::http::async_write(
d.stream, d.msg, std::move(*this)); d.stream, d.sr, std::move(*this));
} }
// Completion handler // Completion handler

View File

@@ -141,11 +141,18 @@ private:
auto res = get(full_path, file_ec); auto res = get(full_path, file_ec);
if(file_ec == beast::errc::no_such_file_or_directory) if(file_ec == beast::errc::no_such_file_or_directory)
{
http::write(sock_, not_found(), ec); http::write(sock_, not_found(), ec);
}
else if(ec) else if(ec)
{
http::write(sock_, server_error(file_ec), ec); http::write(sock_, server_error(file_ec), ec);
}
else else
http::write(sock_, std::move(res), ec); {
http::serializer<false, decltype(res)::body_type> sr{res};
http::write(sock_, sr, ec);
}
} }
void void

View File

@@ -157,7 +157,8 @@ private:
operator()( operator()(
beast::http::response<Body, Fields>&& res) const beast::http::response<Body, Fields>&& res) const
{ {
beast::http::write(self_.impl().stream(), res, ec_); beast::http::serializer<false, Body, Fields> sr{res};
beast::http::write(self_.impl().stream(), sr, ec_);
} }
}; };

View File

@@ -75,7 +75,7 @@ public:
/// Returns the native handle associated with the file. /// Returns the native handle associated with the file.
native_handle_type native_handle_type
native_handle() const native_handle()
{ {
return h_; return h_;
} }
@@ -130,7 +130,7 @@ public:
@return The offset in bytes from the beginning of the file @return The offset in bytes from the beginning of the file
*/ */
std::uint64_t std::uint64_t
pos(error_code& ec) const; pos(error_code& ec);
/** Adjust the current position in the open file /** Adjust the current position in the open file
@@ -150,7 +150,7 @@ public:
@param ec Set to the error, if any occurred @param ec Set to the error, if any occurred
*/ */
std::size_t std::size_t
read(void* buffer, std::size_t n, error_code& ec) const; read(void* buffer, std::size_t n, error_code& ec);
/** Write to the open file /** Write to the open file

View File

@@ -226,7 +226,7 @@ size(error_code& ec) const
inline inline
std::uint64_t std::uint64_t
file_win32:: file_win32::
pos(error_code& ec) const pos(error_code& ec)
{ {
if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_) if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_)
{ {
@@ -272,7 +272,7 @@ seek(std::uint64_t offset, error_code& ec)
inline inline
std::size_t std::size_t
file_win32:: file_win32::
read(void* buffer, std::size_t n, error_code& ec) const read(void* buffer, std::size_t n, error_code& ec)
{ {
if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_) if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_)
{ {

View File

@@ -219,7 +219,7 @@ size(value_type const& v)
template<class File> template<class File>
class basic_file_body<File>::reader class basic_file_body<File>::reader
{ {
value_type const& body_; // The body we are reading from value_type& body_; // The body we are reading from
std::uint64_t remain_; // The number of unread bytes std::uint64_t remain_; // The number of unread bytes
char buf_[4096]; // Small buffer for reading char buf_[4096]; // Small buffer for reading
@@ -234,9 +234,28 @@ public:
// `m` holds the message we are sending, which will // `m` holds the message we are sending, which will
// always have the `file_body` as the body type. // always have the `file_body` as the body type.
// //
// Note that the message is passed by non-const reference.
// This is intentional, because reading from the file
// changes its "current position" which counts makes the
// operation logically not-const (although it is bitwise
// const).
//
// The BodyReader concept allows the reader to choose
// whether to take the message by const reference or
// non-const reference. Depending on the choice, a
// serializer constructed using that body type will
// require the same const or non-const reference to
// construct.
//
// Readers which accept const messages usually allow
// the same body to be serialized by multiple threads
// concurrently, while readers accepting non-const
// messages may only be serialized by one thread at
// a time.
//
template<bool isRequest, class Fields> template<bool isRequest, class Fields>
reader(message< reader(message<
isRequest, basic_file_body, Fields> const& m); isRequest, basic_file_body, Fields>& m);
// Initializer // Initializer
// //
@@ -269,7 +288,7 @@ template<class File>
template<bool isRequest, class Fields> template<bool isRequest, class Fields>
basic_file_body<File>:: basic_file_body<File>::
reader:: reader::
reader(message<isRequest, basic_file_body, Fields> const& m) reader(message<isRequest, basic_file_body, Fields>& m)
: body_(m.body) : body_(m.body)
{ {
// The file must already be open // The file must already be open