_http.qbk 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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:using_http HTTP]
  8. [warning
  9. Higher level functions such as Basic
  10. Authentication, mime/multipart encoding, cookies, automatic handling
  11. of redirects, gzipped transfer encodings, caching, or proxying (to name
  12. a few) are not directly provided, but nothing stops users from creating
  13. these features using Beast's HTTP message types.
  14. ]
  15. This library offers programmers simple and performant models of HTTP messages
  16. and their associated operations including synchronous, asynchronous, and
  17. buffer-oriented parsing and serialization of messages in the HTTP/1 wire
  18. format using __Asio__. Specifically, the library provides:
  19. [variablelist
  20. [
  21. [Message Containers]
  22. [
  23. Complete HTTP messages are modeled using the __message__ class,
  24. with possible user customizations.
  25. ]
  26. ][
  27. [Stream Reading]
  28. [
  29. The functions
  30. [link beast.ref.boost__beast__http__read `read`],
  31. [link beast.ref.boost__beast__http__read_header `read_header`],
  32. [link beast.ref.boost__beast__http__read_some `read_some`],
  33. [link beast.ref.boost__beast__http__async_read `async_read`],
  34. [link beast.ref.boost__beast__http__async_read_header `async_read_header`], and
  35. [link beast.ref.boost__beast__http__async_read_some `async_read_some`]
  36. read HTTP/1 message data from a
  37. [link beast.concepts.streams stream].
  38. ]
  39. ][
  40. [Stream Writing]
  41. [
  42. The functions
  43. [link beast.ref.boost__beast__http__write `write`],
  44. [link beast.ref.boost__beast__http__write_header `write_header`],
  45. [link beast.ref.boost__beast__http__write_some `write_some`],
  46. [link beast.ref.boost__beast__http__async_write `async_write`],
  47. [link beast.ref.boost__beast__http__async_write_header `async_write_header`], and
  48. [link beast.ref.boost__beast__http__async_write_some `async_write_some`]
  49. write HTTP/1 message data to a
  50. [link beast.concepts.streams stream].
  51. ]
  52. ][
  53. [Serialization]
  54. [
  55. The __serializer__ produces a series of octet buffers
  56. conforming to the __rfc7230__ wire representation of
  57. a __message__.
  58. ]
  59. ][
  60. [Parsing]
  61. [
  62. The __parser__ attempts to convert a series of octet
  63. buffers into a __message__.
  64. ]
  65. ]
  66. ]
  67. Interfaces for operating on HTTP messages are structured into several
  68. layers. The highest level provides ease of use, while lower levels provide
  69. progressively more control, options, and flexibility. At the lowest level
  70. customization points are provided, where user defined types can replace
  71. parts of the implementation. The layers are arranged thusly:
  72. [table
  73. [[Level][Read/Write What][Description]]
  74. [
  75. [[*6]]
  76. [
  77. __message__
  78. ][
  79. At the highest level, these free functions send or receive
  80. a complete HTTP message in one call. They are designed for
  81. ease of use:
  82. [link beast.ref.boost__beast__http__read.overload4 `read`],
  83. [link beast.ref.boost__beast__http__write.overload4 `write`],
  84. [link beast.ref.boost__beast__http__async_read.overload2 `async_read`], and
  85. [link beast.ref.boost__beast__http__async_write.overload2 `async_write`].
  86. ]
  87. ][
  88. [[*5]]
  89. [
  90. __parser__, __serializer__
  91. ][
  92. For more control, callers may take responsibility for managing the
  93. required __parser__ or __serializer__ transient state objects.
  94. This allows additional configuration such as limiting the number
  95. of bytes for message components during parsing, or regulating the
  96. size of buffers emitted during output. These functions send or
  97. receive complete messages using a serializer or parser:
  98. [link beast.ref.boost__beast__http__read.overload2 `read`],
  99. [link beast.ref.boost__beast__http__write.overload2 `write`],
  100. [link beast.ref.boost__beast__http__async_read.overload1 `async_read`], and
  101. [link beast.ref.boost__beast__http__async_write.overload1 `async_write`].
  102. ]
  103. ][
  104. [[*4]]
  105. [
  106. __header__
  107. ][
  108. Sometimes it is necessary to first send or receive the HTTP
  109. header. For example, to read the header and take action before
  110. continuing to read the body. These functions use a __parser__
  111. or __serializer__ to read or write the header:
  112. [link beast.ref.boost__beast__http__read_header.overload2 `read_header`],
  113. [link beast.ref.boost__beast__http__write_header.overload2 `write_header`],
  114. [link beast.ref.boost__beast__http__async_read_header `async_read_header`], and
  115. [link beast.ref.boost__beast__http__async_write_header `async_write_header`].
  116. ]
  117. ][
  118. [[*3]]
  119. [
  120. partial __message__
  121. ][
  122. All of the stream operations at higher levels thus far have operated
  123. on a complete header or message. At this level it is possible to
  124. send and receive messages incrementally. This allows resource
  125. constrained implementations to perform work bounded on storage,
  126. or allows better control when setting timeouts for example.
  127. These functions read or write bounded amounts of data and return
  128. the number of bytes transacted:
  129. [link beast.ref.boost__beast__http__read_some.overload2 `read_some`],
  130. [link beast.ref.boost__beast__http__write_some.overload2 `write_some`],
  131. [link beast.ref.boost__beast__http__async_read_some `async_read_some`], and
  132. [link beast.ref.boost__beast__http__async_write_some `async_write_some`].
  133. ]
  134. ][
  135. [[*2]]
  136. [
  137. [@https://tools.ietf.org/html/rfc7230#section-4.1 ['chunked-body]]
  138. ][
  139. Until now parse and serialize operations apply or remove the chunked
  140. transfer coding as needed for message payloads whose size is not known
  141. ahead of time. For some domain specific niches, it is necessary
  142. to assume direct control over incoming or outgoing chunks in a chunk
  143. encoded message payload.
  144. For parsing this is achieved by setting hooks using the functions
  145. [link beast.ref.boost__beast__http__parser.on_chunk_header `on_chunk_header`] and/or
  146. [link beast.ref.boost__beast__http__parser.on_chunk_body `on_chunk_body`].
  147. For serializing callers may first emit the header, and then use
  148. these buffer sequence adapters to control the contents of each chunk
  149. including
  150. [@https://tools.ietf.org/html/rfc7230#section-4.1.1 ['chunk extensions]]
  151. and the
  152. [@https://tools.ietf.org/html/rfc7230#section-4.1.2 ['trailer-part]]:
  153. [link beast.ref.boost__beast__http__chunk_body `chunk_body`],
  154. [link beast.ref.boost__beast__http__chunk_crlf `chunk_crlf`],
  155. [link beast.ref.boost__beast__http__chunk_header `chunk_header`], and
  156. [link beast.ref.boost__beast__http__chunk_last `chunk_last`].
  157. ]
  158. ][
  159. [[*1]]
  160. [
  161. buffers
  162. ][
  163. For ultimate control, the use of library stream algorithms may
  164. be bypassed entirely and instead work directly with buffers by
  165. calling members of __parser__ or __serializer__.
  166. ]
  167. ][
  168. [[*0]]
  169. [
  170. ['user-defined]
  171. ][
  172. In addition to the typical customization points of __Stream__
  173. and __DynamicBuffer__, user-defined types may replace parts of
  174. the library implementation at the lowest level. The customization
  175. points include __Fields__ for creating a container to store HTTP
  176. fields, __Body__ for defining containers and algorithms used for
  177. HTTP message payloads, and user-defined subclasses of
  178. __basic_parser__ for implementing custom message representation
  179. strategies.
  180. ]
  181. ]]
  182. [note
  183. This documentation assumes some familiarity with __Asio__ and
  184. the HTTP protocol specification described in __rfc7230__. Sample
  185. code and identifiers mentioned in this section is written as if
  186. these declarations are in effect:
  187. [http_snippet_1]
  188. ]
  189. [include 01_primer.qbk]
  190. [include 02_message.qbk]
  191. [include 03_streams.qbk]
  192. [include 04_serializer_streams.qbk]
  193. [include 05_parser_streams.qbk]
  194. [include 06_serializer_buffers.qbk]
  195. [include 07_parser_buffers.qbk]
  196. [include 08_chunked_encoding.qbk]
  197. [include 09_custom_body.qbk]
  198. [include 10_custom_parsers.qbk]
  199. [endsect]