2016-05-07 17:06:46 -04:00
|
|
|
//
|
2017-02-06 20:07:03 -05:00
|
|
|
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2016-05-07 17:06:46 -04:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-06-24 22:24:39 -07:00
|
|
|
#ifndef BEAST_EXAMPLE_COMMON_FILE_BODY_HPP
|
|
|
|
#define BEAST_EXAMPLE_COMMON_FILE_BODY_HPP
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
#include <beast/core/error.hpp>
|
|
|
|
#include <beast/http/message.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <boost/filesystem.hpp>
|
2017-06-08 22:03:58 -07:00
|
|
|
#include <boost/assert.hpp>
|
2017-05-08 12:41:45 -07:00
|
|
|
#include <boost/optional.hpp>
|
2017-06-08 05:43:57 -07:00
|
|
|
#include <algorithm>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdint>
|
2017-06-08 22:03:58 -07:00
|
|
|
#include <utility>
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-06-14 20:26:44 -07:00
|
|
|
//[example_http_file_body_1
|
2017-06-08 22:03:58 -07:00
|
|
|
|
2017-06-12 19:16:39 -07:00
|
|
|
/** A message body represented by a file on the filesystem.
|
|
|
|
|
|
|
|
Messages with this type have bodies represented by a
|
|
|
|
file on the file system. When parsing a message using
|
|
|
|
this body type, the data is stored in the file pointed
|
|
|
|
to by the path, which must be writable. When serializing,
|
|
|
|
the implementation will read the file and present those
|
|
|
|
octets as the body content. This may be used to serve
|
|
|
|
content from a directory as part of a web service.
|
|
|
|
*/
|
2017-07-20 08:01:46 -07:00
|
|
|
struct file_body
|
|
|
|
{
|
2017-06-08 22:03:58 -07:00
|
|
|
/** Algorithm for retrieving buffers when serializing.
|
|
|
|
|
|
|
|
Objects of this type are created during serialization
|
|
|
|
to extract the buffers representing the body.
|
|
|
|
*/
|
|
|
|
class reader;
|
|
|
|
|
|
|
|
/** Algorithm for storing buffers when parsing.
|
|
|
|
|
|
|
|
Objects of this type are created during parsing
|
|
|
|
to store incoming buffers representing the body.
|
|
|
|
*/
|
|
|
|
class writer;
|
2017-07-02 15:52:57 -07:00
|
|
|
|
|
|
|
/** The type of the @ref message::body member.
|
|
|
|
|
|
|
|
Messages declared using `file_body` will have this
|
|
|
|
type for the body member. This rich class interface
|
|
|
|
allow the file to be opened with the file handle
|
|
|
|
maintained directly in the object, which is attached
|
|
|
|
to the message.
|
|
|
|
*/
|
|
|
|
class value_type;
|
2017-06-08 22:03:58 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//]
|
|
|
|
|
2017-06-14 20:26:44 -07:00
|
|
|
//[example_http_file_body_2
|
2017-06-08 22:03:58 -07:00
|
|
|
|
2017-07-02 15:52:57 -07:00
|
|
|
// The body container holds a handle to the file if
|
|
|
|
// it is open, and we also cache the size upon opening.
|
|
|
|
//
|
|
|
|
class file_body::value_type
|
2017-06-08 22:03:58 -07:00
|
|
|
{
|
2017-07-02 15:52:57 -07:00
|
|
|
friend class reader;
|
|
|
|
friend class writer;
|
|
|
|
|
|
|
|
FILE* file_ = nullptr;
|
|
|
|
std::uint64_t size_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** Destructor.
|
|
|
|
|
|
|
|
If the file handle is open, it is closed first.
|
|
|
|
*/
|
|
|
|
~value_type()
|
|
|
|
{
|
|
|
|
if(file_)
|
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Default constructor
|
|
|
|
value_type() = default;
|
|
|
|
|
|
|
|
/// Move constructor
|
|
|
|
value_type(value_type&& other)
|
|
|
|
: file_(other.file_)
|
|
|
|
, size_(other.size_)
|
|
|
|
{
|
|
|
|
other.file_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Move assignment
|
|
|
|
value_type& operator=(value_type&& other)
|
|
|
|
{
|
|
|
|
file_ = other.file_;
|
|
|
|
size_ = other.size_;
|
|
|
|
other.file_ = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the file is open
|
|
|
|
bool
|
|
|
|
is_open() const
|
|
|
|
{
|
|
|
|
return file_ != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Open a file for reading or writing.
|
|
|
|
|
|
|
|
@param path The path to the file
|
|
|
|
|
|
|
|
@param mode The open mode used with fopen()
|
|
|
|
|
|
|
|
@param ec Set to the error, if any occurred
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
open(boost::filesystem::path const& path,
|
|
|
|
char const* mode, beast::error_code& ec)
|
|
|
|
{
|
|
|
|
// Attempt to open the file for reading
|
|
|
|
file_ = fopen(path.string().c_str(), mode);
|
|
|
|
|
|
|
|
if(! file_)
|
|
|
|
{
|
|
|
|
// Convert the old-school `errno` into
|
|
|
|
// an error code using the generic category.
|
|
|
|
ec = beast::error_code{errno, beast::generic_category()};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file was opened successfully.
|
|
|
|
// If we are reading, cache the file size.
|
|
|
|
if(std::string{mode} == "rb")
|
|
|
|
{
|
|
|
|
size_ = boost::filesystem::file_size(path, ec);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This is required by the error_code specification
|
|
|
|
ec = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the size of the file.
|
|
|
|
|
|
|
|
Preconditions:
|
|
|
|
|
|
|
|
* The file must be open for binary reading (mode=="rb")
|
|
|
|
|
|
|
|
@return The size of the file if a file is open, else undefined.
|
|
|
|
*/
|
|
|
|
std::uint64_t
|
|
|
|
size() const
|
|
|
|
{
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
};
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
//]
|
|
|
|
|
2017-06-14 20:26:44 -07:00
|
|
|
//[example_http_file_body_3
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
class file_body::reader
|
|
|
|
{
|
2017-07-02 15:52:57 -07:00
|
|
|
FILE* file_; // The file handle
|
|
|
|
std::uint64_t remain_; // The number of unread bytes
|
2017-06-08 22:03:58 -07:00
|
|
|
char buf_[4096]; // Small buffer for reading
|
|
|
|
|
|
|
|
public:
|
|
|
|
// The type of buffer sequence returned by `get`.
|
|
|
|
//
|
|
|
|
using const_buffers_type =
|
|
|
|
boost::asio::const_buffers_1;
|
|
|
|
|
|
|
|
// Constructor.
|
|
|
|
//
|
|
|
|
// `m` holds the message we are sending, which will
|
|
|
|
// always have the `file_body` as the body type.
|
|
|
|
//
|
|
|
|
template<bool isRequest, class Fields>
|
2017-06-25 08:28:41 -07:00
|
|
|
reader(beast::http::message<isRequest, file_body, Fields> const& m,
|
|
|
|
beast::error_code& ec);
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
// This function is called zero or more times to
|
|
|
|
// retrieve buffers. A return value of `boost::none`
|
|
|
|
// means there are no more buffers. Otherwise,
|
|
|
|
// the contained pair will have the next buffer
|
|
|
|
// to serialize, and a `bool` indicating whether
|
|
|
|
// or not there may be additional buffers.
|
|
|
|
boost::optional<std::pair<const_buffers_type, bool>>
|
2017-06-12 19:16:39 -07:00
|
|
|
get(beast::error_code& ec);
|
2017-06-08 22:03:58 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//]
|
|
|
|
|
2017-06-14 20:26:44 -07:00
|
|
|
//[example_http_file_body_4
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
// Here we just stash a reference to the path for later.
|
|
|
|
// Rather than dealing with messy constructor exceptions,
|
|
|
|
// we save the things that might fail for the call to `init`.
|
|
|
|
//
|
|
|
|
template<bool isRequest, class Fields>
|
|
|
|
file_body::reader::
|
2017-06-25 08:28:41 -07:00
|
|
|
reader(beast::http::message<isRequest, file_body, Fields> const& m,
|
|
|
|
beast::error_code& ec)
|
2017-07-02 15:52:57 -07:00
|
|
|
: file_(m.body.file_)
|
|
|
|
, remain_(m.body.size())
|
2017-06-08 22:03:58 -07:00
|
|
|
{
|
2017-07-02 15:52:57 -07:00
|
|
|
// The file must already be open
|
|
|
|
BOOST_ASSERT(file_ != nullptr);
|
2017-06-05 21:11:33 -07:00
|
|
|
|
2017-06-25 08:28:41 -07:00
|
|
|
// This is required by the error_code specification
|
|
|
|
ec = {};
|
2017-06-08 22:03:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function is called repeatedly by the serializer to
|
|
|
|
// retrieve the buffers representing the body. Our strategy
|
|
|
|
// is to read into our buffer and return it until we have
|
|
|
|
// read through the whole file.
|
|
|
|
//
|
|
|
|
inline
|
|
|
|
auto
|
|
|
|
file_body::reader::
|
2017-06-12 19:16:39 -07:00
|
|
|
get(beast::error_code& ec) ->
|
2017-06-08 22:03:58 -07:00
|
|
|
boost::optional<std::pair<const_buffers_type, bool>>
|
|
|
|
{
|
|
|
|
// Calculate the smaller of our buffer size,
|
|
|
|
// or the amount of unread data in the file.
|
2017-06-17 13:04:01 -07:00
|
|
|
auto const amount = remain_ > sizeof(buf_) ?
|
|
|
|
sizeof(buf_) : static_cast<std::size_t>(remain_);
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
// Check for an empty file
|
|
|
|
if(amount == 0)
|
2017-06-13 07:08:52 -07:00
|
|
|
{
|
|
|
|
ec = {};
|
2017-06-08 22:03:58 -07:00
|
|
|
return boost::none;
|
2017-06-13 07:08:52 -07:00
|
|
|
}
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
// Now read the next buffer
|
|
|
|
auto const nread = fread(buf_, 1, amount, file_);
|
|
|
|
|
|
|
|
// Handle any errors
|
|
|
|
if(ferror(file_))
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-06-17 08:35:09 -07:00
|
|
|
// Convert the old-school `errno` into
|
|
|
|
// an error code using the generic category.
|
|
|
|
ec = beast::error_code{errno, beast::generic_category()};
|
2017-06-08 22:03:58 -07:00
|
|
|
return boost::none;
|
|
|
|
}
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-06-08 22:03:58 -07:00
|
|
|
// Make sure there is forward progress
|
|
|
|
BOOST_ASSERT(nread != 0);
|
|
|
|
BOOST_ASSERT(nread <= remain_);
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-06-08 22:03:58 -07:00
|
|
|
// Update the amount remaining based on what we got
|
|
|
|
remain_ -= nread;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-06-08 22:03:58 -07:00
|
|
|
// Return the buffer to the caller.
|
|
|
|
//
|
|
|
|
// The second element of the pair indicates whether or
|
|
|
|
// not there is more data. As long as there is some
|
|
|
|
// unread bytes, there will be more data. Otherwise,
|
|
|
|
// we set this bool to `false` so we will not be called
|
|
|
|
// again.
|
|
|
|
//
|
2017-06-13 07:08:52 -07:00
|
|
|
ec = {};
|
2017-06-08 22:03:58 -07:00
|
|
|
return {{
|
|
|
|
const_buffers_type{buf_, nread}, // buffer to return.
|
|
|
|
remain_ > 0 // `true` if there are more buffers.
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
//]
|
|
|
|
|
2017-06-14 20:26:44 -07:00
|
|
|
//[example_http_file_body_5
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
class file_body::writer
|
|
|
|
{
|
2017-07-02 15:52:57 -07:00
|
|
|
FILE* file_; // The file handle
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Constructor.
|
|
|
|
//
|
|
|
|
// This is called after the header is parsed and
|
|
|
|
// indicates that a non-zero sized body may be present.
|
|
|
|
// `m` holds the message we are receiving, which will
|
|
|
|
// always have the `file_body` as the body type.
|
|
|
|
//
|
|
|
|
template<bool isRequest, class Fields>
|
|
|
|
explicit
|
2017-06-25 08:28:41 -07:00
|
|
|
writer(beast::http::message<isRequest, file_body, Fields>& m,
|
|
|
|
boost::optional<std::uint64_t> const& content_length,
|
|
|
|
beast::error_code& ec);
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
// This function is called one or more times to store
|
|
|
|
// buffer sequences corresponding to the incoming body.
|
|
|
|
//
|
|
|
|
template<class ConstBufferSequence>
|
2017-07-02 06:32:19 -07:00
|
|
|
std::size_t
|
2017-06-12 19:16:39 -07:00
|
|
|
put(ConstBufferSequence const& buffers, beast::error_code& ec);
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
// This function is called when writing is complete.
|
|
|
|
// It is an opportunity to perform any final actions
|
|
|
|
// which might fail, in order to return an error code.
|
|
|
|
// Operations that might fail should not be attemped in
|
|
|
|
// destructors, since an exception thrown from there
|
|
|
|
// would terminate the program.
|
|
|
|
//
|
|
|
|
void
|
2017-06-12 19:16:39 -07:00
|
|
|
finish(beast::error_code& ec);
|
2017-06-08 22:03:58 -07:00
|
|
|
};
|
2017-06-09 14:55:44 -07:00
|
|
|
|
2017-06-08 22:03:58 -07:00
|
|
|
//]
|
|
|
|
|
2017-06-14 20:26:44 -07:00
|
|
|
//[example_http_file_body_6
|
2017-06-08 22:03:58 -07:00
|
|
|
|
|
|
|
// Just stash a reference to the path so we can open the file later.
|
|
|
|
template<bool isRequest, class Fields>
|
|
|
|
file_body::writer::
|
2017-06-25 08:28:41 -07:00
|
|
|
writer(beast::http::message<isRequest, file_body, Fields>& m,
|
|
|
|
boost::optional<std::uint64_t> const& content_length,
|
|
|
|
beast::error_code& ec)
|
2017-07-02 15:52:57 -07:00
|
|
|
: file_(m.body.file_)
|
2017-06-08 22:03:58 -07:00
|
|
|
{
|
2017-07-02 15:52:57 -07:00
|
|
|
// We don't do anything with this but a sophisticated
|
|
|
|
// application might check available space on the device
|
|
|
|
// to see if there is enough room to store the body.
|
2017-06-22 10:27:45 -07:00
|
|
|
boost::ignore_unused(content_length);
|
|
|
|
|
2017-07-02 15:52:57 -07:00
|
|
|
// The file must already be open for writing
|
|
|
|
BOOST_ASSERT(file_ != nullptr);
|
2017-06-13 07:08:52 -07:00
|
|
|
|
2017-06-25 08:28:41 -07:00
|
|
|
// This is required by the error_code specification
|
2017-06-13 07:08:52 -07:00
|
|
|
ec = {};
|
2017-06-08 22:03:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This will get called one or more times with body buffers
|
|
|
|
//
|
|
|
|
template<class ConstBufferSequence>
|
2017-07-02 06:32:19 -07:00
|
|
|
std::size_t
|
2017-06-08 22:03:58 -07:00
|
|
|
file_body::writer::
|
2017-06-12 19:16:39 -07:00
|
|
|
put(ConstBufferSequence const& buffers, beast::error_code& ec)
|
2017-06-08 22:03:58 -07:00
|
|
|
{
|
2017-07-02 06:32:19 -07:00
|
|
|
// This function must return the total number of
|
|
|
|
// bytes transferred from the input buffers.
|
|
|
|
std::size_t bytes_transferred = 0;
|
|
|
|
|
2017-06-08 22:03:58 -07:00
|
|
|
// Loop over all the buffers in the sequence,
|
|
|
|
// and write each one to the file.
|
2017-06-13 11:53:06 -07:00
|
|
|
for(boost::asio::const_buffer buffer : buffers)
|
2017-06-08 22:03:58 -07:00
|
|
|
{
|
|
|
|
// Write this buffer to the file
|
2017-07-02 06:32:19 -07:00
|
|
|
bytes_transferred += fwrite(
|
2017-06-08 22:03:58 -07:00
|
|
|
boost::asio::buffer_cast<void const*>(buffer), 1,
|
|
|
|
boost::asio::buffer_size(buffer),
|
|
|
|
file_);
|
|
|
|
|
|
|
|
// Handle any errors
|
|
|
|
if(ferror(file_))
|
2017-06-09 14:55:44 -07:00
|
|
|
{
|
2017-06-17 08:35:09 -07:00
|
|
|
// Convert the old-school `errno` into
|
|
|
|
// an error code using the generic category.
|
|
|
|
ec = beast::error_code{errno, beast::generic_category()};
|
2017-07-02 06:32:19 -07:00
|
|
|
return bytes_transferred;
|
2017-06-09 14:55:44 -07:00
|
|
|
}
|
2017-06-08 22:03:58 -07:00
|
|
|
}
|
2017-06-13 07:08:52 -07:00
|
|
|
|
|
|
|
// Indicate success
|
|
|
|
ec = {};
|
2017-07-02 06:32:19 -07:00
|
|
|
|
|
|
|
return bytes_transferred;
|
2017-06-08 22:03:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called after writing is done when there's no error.
|
|
|
|
inline
|
|
|
|
void
|
|
|
|
file_body::writer::
|
2017-06-12 19:16:39 -07:00
|
|
|
finish(beast::error_code& ec)
|
2017-06-08 22:03:58 -07:00
|
|
|
{
|
2017-06-12 19:16:39 -07:00
|
|
|
// This has to be cleared before returning, to
|
|
|
|
// indicate no error. The specification requires it.
|
2017-06-13 07:08:52 -07:00
|
|
|
ec = {};
|
2017-06-08 22:03:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//]
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
#endif
|