From cc198db10eb4eba2f48afa204ae4100617079390 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 7 Jul 2017 23:25:23 -0700 Subject: [PATCH] serializing file_body is not const --- CHANGELOG.md | 1 + example/common/write_msg.hpp | 6 +++- .../http_server_threaded.cpp | 9 +++++- example/server-framework/http_sync_port.hpp | 3 +- include/beast/core/file_win32.hpp | 6 ++-- include/beast/core/impl/file_win32.ipp | 4 +-- include/beast/http/file_body.hpp | 29 +++++++++++++++---- 7 files changed, 45 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bca89eb3..a4859fef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Version 76: * Add serializer::get * Add serializer::chunked * Serializer members are not const +* serializing file_body is not const API Changes: diff --git a/example/common/write_msg.hpp b/example/common/write_msg.hpp index ebec85fe..038df599 100644 --- a/example/common/write_msg.hpp +++ b/example/common/write_msg.hpp @@ -46,12 +46,16 @@ class write_msg_op // beast::http::message msg; + // Serializer for the message + beast::http::serializer sr; + data( Handler& handler, AsyncWriteStream& stream_, beast::http::message&& msg_) : stream(stream_) , msg(std::move(msg_)) + , sr(msg) { boost::ignore_unused(handler); } @@ -96,7 +100,7 @@ public: { auto& d = *d_; beast::http::async_write( - d.stream, d.msg, std::move(*this)); + d.stream, d.sr, std::move(*this)); } // Completion handler diff --git a/example/http-server-threaded/http_server_threaded.cpp b/example/http-server-threaded/http_server_threaded.cpp index 2b2924fc..4389549c 100644 --- a/example/http-server-threaded/http_server_threaded.cpp +++ b/example/http-server-threaded/http_server_threaded.cpp @@ -141,11 +141,18 @@ private: auto res = get(full_path, file_ec); if(file_ec == beast::errc::no_such_file_or_directory) + { http::write(sock_, not_found(), ec); + } else if(ec) + { http::write(sock_, server_error(file_ec), ec); + } else - http::write(sock_, std::move(res), ec); + { + http::serializer sr{res}; + http::write(sock_, sr, ec); + } } void diff --git a/example/server-framework/http_sync_port.hpp b/example/server-framework/http_sync_port.hpp index 69fb1a68..2cd20477 100644 --- a/example/server-framework/http_sync_port.hpp +++ b/example/server-framework/http_sync_port.hpp @@ -157,7 +157,8 @@ private: operator()( beast::http::response&& res) const { - beast::http::write(self_.impl().stream(), res, ec_); + beast::http::serializer sr{res}; + beast::http::write(self_.impl().stream(), sr, ec_); } }; diff --git a/include/beast/core/file_win32.hpp b/include/beast/core/file_win32.hpp index fac82ccb..8aafd345 100644 --- a/include/beast/core/file_win32.hpp +++ b/include/beast/core/file_win32.hpp @@ -75,7 +75,7 @@ public: /// Returns the native handle associated with the file. native_handle_type - native_handle() const + native_handle() { return h_; } @@ -130,7 +130,7 @@ public: @return The offset in bytes from the beginning of the file */ std::uint64_t - pos(error_code& ec) const; + pos(error_code& ec); /** Adjust the current position in the open file @@ -150,7 +150,7 @@ public: @param ec Set to the error, if any occurred */ 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 diff --git a/include/beast/core/impl/file_win32.ipp b/include/beast/core/impl/file_win32.ipp index 5d36a55e..5b7a1647 100644 --- a/include/beast/core/impl/file_win32.ipp +++ b/include/beast/core/impl/file_win32.ipp @@ -226,7 +226,7 @@ size(error_code& ec) const inline std::uint64_t file_win32:: -pos(error_code& ec) const +pos(error_code& ec) { if(h_ == boost::detail::winapi::INVALID_HANDLE_VALUE_) { @@ -272,7 +272,7 @@ seek(std::uint64_t offset, error_code& ec) inline std::size_t 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_) { diff --git a/include/beast/http/file_body.hpp b/include/beast/http/file_body.hpp index 17970126..bd2753b7 100644 --- a/include/beast/http/file_body.hpp +++ b/include/beast/http/file_body.hpp @@ -219,9 +219,9 @@ size(value_type const& v) template class basic_file_body::reader { - value_type const& body_; // The body we are reading from - std::uint64_t remain_; // The number of unread bytes - char buf_[4096]; // Small buffer for reading + value_type& body_; // The body we are reading from + std::uint64_t remain_; // The number of unread bytes + char buf_[4096]; // Small buffer for reading public: // The type of buffer sequence returned by `get`. @@ -234,9 +234,28 @@ public: // `m` holds the message we are sending, which will // 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 reader(message< - isRequest, basic_file_body, Fields> const& m); + isRequest, basic_file_body, Fields>& m); // Initializer // @@ -269,7 +288,7 @@ template template basic_file_body:: reader:: -reader(message const& m) +reader(message& m) : body_(m.body) { // The file must already be open