User-defined types are possible for the message body, where the type meets the __Body__ requirements. This simplified class declaration shows the customization points available to user-defined body types: ``` /// Defines a Body type struct body {
An optional nested type meeting the requirements of __BodyReader__, which provides the algorithm for storing a forward range of buffer sequences in the body representation. If present, this body type may be used with a __parser__.
An optional nested type meeting the requirements of __BodyWriter__, which provides the algorithm for converting the body representation to a forward range of buffer sequences. If present this body type may be used with a __serializer__.
The `value_type` nested type allows the body to define the declaration of the body type as it appears in the message. This can be any type. For example, a body's value type may specify `std::vector<char>` or `std::list<std::string>`. A custom body may even set the value type to something that is not a container for body octets, such as a [@boost:/libs/filesystem/doc/reference.html#class-path `boost::filesystem::path`]. Or, a more structured container may be chosen. This declares a body's value type as a JSON tree structure produced from a [@boost:/doc/html/json/input_output.html#json.input_output.parsing.streaming_parser `boost::json::stream_parser`]: ``` #include <boost/json/stream_parser.hpp>
As long as a suitable reader or writer is available to provide the algorithm for transferring buffers in and out of the value type, those bodies may be parsed or serialized.
Use of the flexible __Body__ concept customization point enables authors to preserve the self-contained nature of the __message__ object while allowing domain specific behaviors. Common operations for HTTP servers include sending responses which deliver file contents, and allowing for file uploads. In this example we build the [link beast.ref.boost__beast__http__basic_file_body `basic_file_body`] type which supports both reading and writing to a file on the file system. The interface is a class templated on the type of file used to access the file system, which must meet the requirements of __File__.
We will start with the definition of the `value_type`. Our strategy will be to store the file object directly in the message container through the `value_type` field. To use this body it will be necessary to call `msg.body.file().open()` first with the required information such as the path and open mode. This ensures that the file exists throughout the operation and prevent the race condition where the file is removed from the file system in between calls.
Our implementation of __BodyWriter__ will contain a small buffer from which the file contents are read. The buffer is provided to the implementation on each call until everything has been read in.