02_message.qbk 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 Message Containers]
  8. Beast provides a single class template __message__ and some aliases which
  9. model HTTP/1 and
  10. [@https://tools.ietf.org/html/rfc7540 HTTP/2]
  11. messages:
  12. [table Message
  13. [[Name][Description]]
  14. [[
  15. __message__
  16. ][
  17. ```
  18. /// An HTTP message
  19. template<
  20. bool isRequest, // `true` for requests, `false` for responses
  21. class Body, // Controls the container and algorithms used for the body
  22. class Fields = fields> // The type of container to store the fields
  23. class message;
  24. ```
  25. ]]
  26. [[
  27. [link beast.ref.boost__beast__http__request `request`]
  28. ][
  29. ```
  30. /// A typical HTTP request
  31. template<class Body, class Fields = fields>
  32. using request = message<true, Body, Fields>;
  33. ```
  34. ]]
  35. [[
  36. [link beast.ref.boost__beast__http__response `response`]
  37. ][
  38. ```
  39. /// A typical HTTP response
  40. template<class Body, class Fields = fields>
  41. using response = message<false, Body, Fields>;
  42. ```
  43. ]]
  44. ]
  45. The container offers value semantics including move and copy if supported
  46. by __Body__ and __Fields__. User defined template function parameters can
  47. accept any message, or can use partial specialization to accept just
  48. requests or responses. The default __fields__ is a provided associative
  49. container using the standard allocator and supporting modification and
  50. inspection of fields. As per __rfc7230__, a non-case-sensitive comparison
  51. is used for field names. User defined types for fields are possible.
  52. The `Body` type determines the type of the container used to represent the
  53. body as well as the algorithms for transferring buffers to and from the
  54. container. The library comes with a collection of common body types. As
  55. with fields, user defined body types are possible.
  56. Sometimes it is desired to only work with a header. Beast provides a single
  57. class template __header__ and some aliases to model HTTP/1 and HTTP/2 headers:
  58. [table Header
  59. [[Name][Description]]
  60. [[
  61. __header__
  62. ][
  63. ```
  64. /// An HTTP header
  65. template<
  66. bool isRequest, // `true` for requests, `false` for responses
  67. class Fields = fields> // The type of container to store the fields
  68. class header;
  69. ```
  70. ]]
  71. [[
  72. [link beast.ref.boost__beast__http__request_header `request_header`]
  73. ][
  74. ```
  75. /// A typical HTTP request header
  76. template<class Fields>
  77. using request_header = header<true, Fields>;
  78. ```
  79. ]]
  80. [[
  81. [link beast.ref.boost__beast__http__response_header `response_header`]
  82. ][
  83. ```
  84. /// A typical HTTP response header
  85. template<class Fields>
  86. using response_header = header<false, Fields>;
  87. ```
  88. ]]
  89. ]
  90. Requests and responses share the version, fields, and body but have
  91. a few members unique to the type. This is implemented by declaring the
  92. header classes as partial specializations of `isRequest`. __message__
  93. is derived from __header__; a message may be passed as an argument to
  94. a function taking a suitably typed header as a parameter. Additionally,
  95. `header` is publicly derived from `Fields`; a message inherits all the
  96. member functions of `Fields`. This diagram shows the inheritance
  97. relationship between header and message, along with some of the
  98. notable differences in members in each partial specialization:
  99. [$beast/images/message.png [width 730px] [height 459px]]
  100. [heading:body Body Types]
  101. Beast defines the __Body__ concept, which determines both the type of
  102. the [link beast.ref.boost__beast__http__message.body `message::body`] member
  103. (as seen in the diagram above) and may also include algorithms for
  104. transferring buffers in and out. These algorithms are used during
  105. parsing and serialization. Users may define their own body types which
  106. meet the requirements, or use the ones that come with the library:
  107. [table
  108. [[Name][Description]]
  109. [[
  110. [link beast.ref.boost__beast__http__buffer_body `buffer_body`]
  111. ][
  112. A body whose
  113. [link beast.ref.boost__beast__http__buffer_body__value_type `value_type`]
  114. holds a raw pointer and size to a caller-provided buffer.
  115. This allows for serialization of body data coming from
  116. external sources, and incremental parsing of message body
  117. content using a fixed size buffer.
  118. ]]
  119. [[
  120. [link beast.ref.boost__beast__http__dynamic_body `dynamic_body`]
  121. [link beast.ref.boost__beast__http__basic_dynamic_body `basic_dynamic_body`]
  122. ][
  123. A body whose `value_type` is a __DynamicBuffer__. It inherits
  124. the insertion complexity of the underlying choice of dynamic buffer.
  125. Messages with this body type may be serialized and parsed.
  126. ]]
  127. [[
  128. [link beast.ref.boost__beast__http__empty_body `empty_body`]
  129. ][
  130. A special body with an empty `value_type` indicating that the
  131. message has no body. Messages with this body may be serialized
  132. and parsed; however, body octets received while parsing a message
  133. with this body will generate a unique error.
  134. ]]
  135. [[
  136. [link beast.ref.boost__beast__http__file_body `file_body`]
  137. [link beast.ref.boost__beast__http__basic_file_body `basic_file_body`]
  138. ][
  139. This body is represented by a file opened for either reading or
  140. writing. Messages with this body may be serialized and parsed.
  141. HTTP algorithms will use the open file for reading and writing,
  142. for streaming and incremental sends and receives.
  143. ]]
  144. [[
  145. [link beast.ref.boost__beast__http__span_body `span_body`]
  146. ][
  147. A body whose `value_type` is a
  148. [link beast.ref.boost__beast__span `span`],
  149. a non-owning reference to a single linear buffer of bytes.
  150. Messages with this body type may be serialized and parsed.
  151. ]]
  152. [[
  153. [link beast.ref.boost__beast__http__string_body `string_body`]
  154. [link beast.ref.boost__beast__http__basic_string_body `basic_string_body`]
  155. ][
  156. A body whose `value_type` is `std::basic_string` or `std::string`.
  157. Insertion complexity is amortized constant time, while capacity
  158. grows geometrically. Messages with this body type may be serialized
  159. and parsed. This is the type of body used in the examples.
  160. ]]
  161. [[
  162. [link beast.ref.boost__beast__http__vector_body `vector_body`]
  163. ][
  164. A body whose `value_type` is `std::vector`. Insertion complexity
  165. is amortized constant time, while capacity grows geometrically.
  166. Messages with this body type may be serialized and parsed.
  167. ]]
  168. ]
  169. [heading Usage]
  170. These examples show how to create and fill in request and response
  171. objects: Here we build an
  172. [@https://tools.ietf.org/html/rfc7231#section-4.3.1 HTTP GET]
  173. request with an empty message body:
  174. [table Create Request
  175. [[Statements] [Serialized Result]]
  176. [[
  177. [http_snippet_2]
  178. ][
  179. ```
  180. GET /index.htm HTTP/1.1\r\n
  181. Accept: text/html\r\n
  182. User-Agent: Beast\r\n
  183. \r\n
  184. ```
  185. ]]
  186. ]
  187. In this code we create an HTTP response with a status code indicating success.
  188. This message has a body with a non-zero length. The function
  189. [link beast.ref.boost__beast__http__message.prepare_payload `message::prepare_payload`]
  190. automatically sets the Content-Length or Transfer-Encoding field
  191. depending on the content and type of the `body` member. Use of this function
  192. is optional; these fields may also be set explicitly.
  193. [table Create Response
  194. [[Statements] [Serialized Result]]
  195. [[
  196. [http_snippet_3]
  197. ][
  198. ```
  199. HTTP/1.1 200 OK\r\n
  200. Server: Beast\r\n
  201. Content-Length: 13\r\n
  202. \r\n
  203. Hello, world!
  204. ```
  205. ]]
  206. ]
  207. The implementation will automatically fill in the obsolete
  208. [@https://tools.ietf.org/html/rfc7230#section-3.1.2 reason-phrase]
  209. from the status code when serializing a message. Or it may
  210. be set directly using
  211. [link beast.ref.boost__beast__http__header.reason.overload2 `header::reason`].
  212. [endsect]