05_parser_streams.qbk 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 Parser Stream Operations]
  8. Non-trivial algorithms need to do more than receive entire messages
  9. at once, such as:
  10. * Receive the header first and body later.
  11. * Receive a large body using a fixed-size buffer.
  12. * Receive a message incrementally: bounded work in each I/O cycle.
  13. * Defer the commitment to a __Body__ type until after reading the header.
  14. These types of operations require callers to manage the lifetime of
  15. associated state, by constructing a class derived from __basic_parser__.
  16. Beast comes with the derived instance __parser__ which creates complete
  17. __message__ objects using the __basic_fields__ Fields container.
  18. [table Parser
  19. [[Name][Description]]
  20. [[
  21. __parser__
  22. ][
  23. ```
  24. /// An HTTP/1 parser for producing a message.
  25. template<
  26. bool isRequest, // `true` to parse an HTTP request
  27. class Body, // The Body type for the resulting message
  28. class Allocator = std::allocator<char>> // The type of allocator for the header
  29. class parser
  30. : public basic_parser<...>;
  31. ```
  32. ]]
  33. [[
  34. [link beast.ref.boost__beast__http__request_parser `request_parser`]
  35. ][
  36. ```
  37. /// An HTTP/1 parser for producing a request message.
  38. template<class Body, class Allocator = std::allocator<char>>
  39. using request_parser = parser<true, Body, Allocator>;
  40. ```
  41. ]]
  42. [[
  43. [link beast.ref.boost__beast__http__response_parser `response_parser`]
  44. ][
  45. ```
  46. /// An HTTP/1 parser for producing a response message.
  47. template<class Body, class Allocator = std::allocator<char>>
  48. using response_parser = parser<false, Body, Allocator>;
  49. ```
  50. ]]
  51. ]
  52. [note
  53. The __basic_parser__ and classes derived from it handle octet streams
  54. serialized in the HTTP/1 format described in __rfc7230__.
  55. ]
  56. The stream operations which work on parsers are:
  57. [table Parser Stream Operations
  58. [[Name][Description]]
  59. [[
  60. [link beast.ref.boost__beast__http__read.overload1 [*read]]
  61. ][
  62. Read everything into a parser from a __SyncWriteStream__.
  63. ]]
  64. [[
  65. [link beast.ref.boost__beast__http__async_read.overload1 [*async_read]]
  66. ][
  67. Read everything into a parser asynchronously from an __AsyncWriteStream__.
  68. ]]
  69. [[
  70. [link beast.ref.boost__beast__http__read_header.overload1 [*read_header]]
  71. ][
  72. Read only the header octets into a parser from a __SyncWriteStream__.
  73. ]]
  74. [[
  75. [link beast.ref.boost__beast__http__async_read_header [*async_read_header]]
  76. ][
  77. Read only the header octets into a parser asynchronously from an __AsyncWriteStream__.
  78. ]]
  79. [[
  80. [link beast.ref.boost__beast__http__read_some.overload1 [*read_some]]
  81. ][
  82. Read some octets into a parser from a __SyncReadStream__.
  83. ]]
  84. [[
  85. [link beast.ref.boost__beast__http__async_read_some [*async_read_some]]
  86. ][
  87. Read some octets into a parser asynchronously from an __AsyncWriteStream__.
  88. ]]
  89. ]
  90. As with message stream operations, parser stream operations require a
  91. persisted __DynamicBuffer__ for holding unused octets from the stream.
  92. The basic parser implementation is optimized for the case where this dynamic
  93. buffer stores its input sequence in a single contiguous memory buffer. It is
  94. advised to use an instance of __flat_buffer__, __flat_static_buffer__, or
  95. __flat_static_buffer_base__ for this purpose, although a user defined instance
  96. of __DynamicBuffer__ which produces input sequences of length one is also
  97. suitable.
  98. The parser contains a message constructed internally. Arguments passed
  99. to the parser's constructor are forwarded into the message container.
  100. The caller can access the message inside the parser by calling
  101. [link beast.ref.boost__beast__http__parser.get `parser::get`].
  102. If the `Fields` and `Body` types are [*MoveConstructible], the caller
  103. can take ownership of the message by calling
  104. [link beast.ref.boost__beast__http__parser.release `parser::release`]. In this example
  105. we read an HTTP response with a string body using a parser, then print
  106. the response:
  107. [http_snippet_13]
  108. [section:incremental_read Incremental Read __example__]
  109. This function uses
  110. [link beast.ref.boost__beast__http__buffer_body `buffer_body`]
  111. and parser stream operations to read a message body progressively
  112. using a small, fixed-size buffer:
  113. [example_incremental_read]
  114. [endsect]
  115. [endsect]