09_custom_body.qbk 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. [/
  2. Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. Official repository: https://github.com/boostorg/beast
  6. ]
  7. [section Custom Body Types]
  8. [block'''<?dbhtml stop-chunking?>''']
  9. User-defined types are possible for the message body, where the type meets the
  10. __Body__ requirements. This simplified class declaration
  11. shows the customization points available to user-defined body types:
  12. ```
  13. /// Defines a Body type
  14. struct body
  15. {
  16. /// This determines the return type of the `message::body` member function
  17. using value_type = ...;
  18. /// An optional function, returns the body's payload size (which may be zero)
  19. static
  20. std::uint64_t
  21. size(value_type const& v);
  22. /// The algorithm used for extracting buffers
  23. class reader;
  24. /// The algorithm used for inserting buffers
  25. class writer;
  26. }
  27. ```
  28. The meaning of the nested types is as follows
  29. [table Body Type Members
  30. [[Name][Description]]
  31. [
  32. [`value_type`]
  33. [
  34. Determines the type of the
  35. [link beast.ref.boost__beast__http__message.body `message::body`]
  36. member.
  37. ]
  38. ][
  39. [`reader`]
  40. [
  41. An optional nested type meeting the requirements of __BodyReader__,
  42. which provides the algorithm for storing a forward range of buffer
  43. sequences in the body representation.
  44. If present, this body type may be used with a __parser__.
  45. ]
  46. ][
  47. [`writer`]
  48. [
  49. An optional nested type meeting the requirements of __BodyWriter__,
  50. which provides the algorithm for converting the body representation
  51. to a forward range of buffer sequences.
  52. If present this body type may be used with a __serializer__.
  53. ]
  54. ]
  55. ]
  56. [heading Value Type]
  57. The `value_type` nested type allows the body to define the declaration of
  58. the body type as it appears in the message. This can be any type. For
  59. example, a body's value type may specify `std::vector<char>` or
  60. `std::list<std::string>`. A custom body may even set the value type to
  61. something that is not a container for body octets, such as a
  62. [@boost:/libs/filesystem/doc/reference.html#class-path `boost::filesystem::path`].
  63. Or, a more structured container may be chosen. This declares a body's
  64. value type as a JSON tree structure produced from a
  65. [@boost:/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser `boost::property_tree::json_parser`]:
  66. ```
  67. #include <boost/property_tree/ptree.hpp>
  68. #include <boost/property_tree/json_parser.hpp>
  69. struct Body
  70. {
  71. using value_type = boost::property_tree::ptree;
  72. class reader
  73. class writer;
  74. };
  75. ```
  76. As long as a suitable reader or writer is available to provide the
  77. algorithm for transferring buffers in and out of the value type,
  78. those bodies may be parsed or serialized.
  79. [section:file_body File Body __example__]
  80. Use of the flexible __Body__ concept customization point enables authors to
  81. preserve the self-contained nature of the __message__ object while allowing
  82. domain specific behaviors. Common operations for HTTP servers include sending
  83. responses which deliver file contents, and allowing for file uploads. In this
  84. example we build the
  85. [link beast.ref.boost__beast__http__basic_file_body `basic_file_body`]
  86. type which supports both reading and
  87. writing to a file on the file system. The interface is a class templated
  88. on the type of file used to access the file system, which must meet the
  89. requirements of __File__.
  90. First we declare the type with its nested types:
  91. [example_http_file_body_1]
  92. We will start with the definition of the `value_type`. Our strategy
  93. will be to store the file object directly in the message container
  94. through the `value_type` field. To use this body it will be necessary
  95. to call `msg.body.file().open()` first with the required information
  96. such as the path and open mode. This ensures that the file exists
  97. throughout the operation and prevent the race condition where the
  98. file is removed from the file system in between calls.
  99. [example_http_file_body_2]
  100. Our implementation of __BodyWriter__ will contain a small buffer
  101. from which the file contents are read. The buffer is provided to
  102. the implementation on each call until everything has been read in.
  103. [example_http_file_body_3]
  104. And here are the definitions for the functions we have declared:
  105. [example_http_file_body_4]
  106. Files can be read now, and the next step is to allow writing to files
  107. by implementing the __BodyReader__. The style is similar to the writer,
  108. except that buffers are incoming instead of outgoing. Here's the
  109. declaration:
  110. [example_http_file_body_5]
  111. Finally, here is the implementation of the reader member functions:
  112. [example_http_file_body_6]
  113. We have created a full featured body type capable of reading and
  114. writing files on the filesystem, integrating seamlessly with the
  115. HTTP algorithms and message container. The body type works with
  116. any file implementation meeting the requirements of __File__ so
  117. it may be transparently used with solutions optimized for particular
  118. platforms. Example HTTP servers which use file bodies are available
  119. in the example directory.
  120. [endsect]
  121. [endsect]