2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BEAST_EXAMPLE_FILE_BODY_H_INCLUDED
9 #define BEAST_EXAMPLE_FILE_BODY_H_INCLUDED
11 #include <beast/core/error.hpp>
12 #include <beast/http/message.hpp>
13 #include <boost/asio/buffer.hpp>
14 #include <boost/assert.hpp>
15 #include <boost/filesystem.hpp>
24 using value_type = std::string;
28 std::uint64_t size_ = 0;
29 std::uint64_t offset_ = 0;
30 std::string const& path_;
31 FILE* file_ = nullptr;
36 writer(writer const&) = delete;
37 writer& operator=(writer const&) = delete;
39 template<bool isRequest, class Fields>
40 writer(message<isRequest,
41 file_body, Fields> const& m) noexcept
53 init(error_code& ec) noexcept
55 file_ = fopen(path_.c_str(), "rb");
57 ec = error_code{errno,
60 size_ = boost::filesystem::file_size(path_);
64 content_length() const noexcept
69 template<class WriteFunction>
71 write(error_code& ec, WriteFunction&& wf) noexcept
73 if(size_ - offset_ < sizeof(buf_))
74 buf_len_ = static_cast<std::size_t>(
77 buf_len_ = sizeof(buf_);
78 auto const nread = fread(
79 buf_, 1, sizeof(buf_), file_);
82 ec = error_code(errno,
86 BOOST_ASSERT(nread != 0);
88 wf(boost::asio::buffer(buf_, nread));
89 return offset_ >= size_;