http_10_custom_parser.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #include <boost/beast/http/basic_parser.hpp>
  10. namespace boost {
  11. namespace beast {
  12. namespace http {
  13. //[code_http_10_custom_parser
  14. template<bool isRequest>
  15. class custom_parser : public basic_parser<isRequest>
  16. {
  17. private:
  18. /** Called after receiving the request-line.
  19. This virtual function is invoked after receiving a request-line
  20. when parsing HTTP requests.
  21. It can only be called when `isRequest == true`.
  22. @param method The verb enumeration. If the method string is not
  23. one of the predefined strings, this value will be @ref verb::unknown.
  24. @param method_str The unmodified string representing the verb.
  25. @param target The request-target.
  26. @param version The HTTP-version. This will be 10 for HTTP/1.0,
  27. and 11 for HTTP/1.1.
  28. @param ec An output parameter which the function may set to indicate
  29. an error. The error will be clear before this function is invoked.
  30. */
  31. void
  32. on_request_impl(
  33. verb method, // The method verb, verb::unknown if no match
  34. string_view method_str, // The method as a string
  35. string_view target, // The request-target
  36. int version, // The HTTP-version
  37. error_code& ec) override; // The error returned to the caller, if any
  38. /** Called after receiving the status-line.
  39. This virtual function is invoked after receiving a status-line
  40. when parsing HTTP responses.
  41. It can only be called when `isRequest == false`.
  42. @param code The numeric status code.
  43. @param reason The reason-phrase. Note that this value is
  44. now obsolete, and only provided for historical or diagnostic
  45. purposes.
  46. @param version The HTTP-version. This will be 10 for HTTP/1.0,
  47. and 11 for HTTP/1.1.
  48. @param ec An output parameter which the function may set to indicate
  49. an error. The error will be clear before this function is invoked.
  50. */
  51. void
  52. on_response_impl(
  53. int code, // The status-code
  54. string_view reason, // The obsolete reason-phrase
  55. int version, // The HTTP-version
  56. error_code& ec) override; // The error returned to the caller, if any
  57. /** Called once for each complete field in the HTTP header.
  58. This virtual function is invoked for each field that is received
  59. while parsing an HTTP message.
  60. @param name The known field enum value. If the name of the field
  61. is not recognized, this value will be @ref field::unknown.
  62. @param name_string The exact name of the field as received from
  63. the input, represented as a string.
  64. @param value A string holding the value of the field.
  65. @param ec An output parameter which the function may set to indicate
  66. an error. The error will be clear before this function is invoked.
  67. */
  68. void
  69. on_field_impl(
  70. field f, // The known-field enumeration constant
  71. string_view name, // The field name string.
  72. string_view value, // The field value
  73. error_code& ec) override; // The error returned to the caller, if any
  74. /** Called once after the complete HTTP header is received.
  75. This virtual function is invoked once, after the complete HTTP
  76. header is received while parsing a message.
  77. @param ec An output parameter which the function may set to indicate
  78. an error. The error will be clear before this function is invoked.
  79. */
  80. void
  81. on_header_impl(
  82. error_code& ec) override; // The error returned to the caller, if any
  83. /** Called once before the body is processed.
  84. This virtual function is invoked once, before the content body is
  85. processed (but after the complete header is received).
  86. @param content_length A value representing the content length in
  87. bytes if the length is known (this can include a zero length).
  88. Otherwise, the value will be `boost::none`.
  89. @param ec An output parameter which the function may set to indicate
  90. an error. The error will be clear before this function is invoked.
  91. */
  92. void
  93. on_body_init_impl(
  94. boost::optional<
  95. std::uint64_t> const&
  96. content_length, // Content length if known, else `boost::none`
  97. error_code& ec) override; // The error returned to the caller, if any
  98. /** Called each time additional data is received representing the content body.
  99. This virtual function is invoked for each piece of the body which is
  100. received while parsing of a message. This function is only used when
  101. no chunked transfer encoding is present.
  102. @param body A string holding the additional body contents. This may
  103. contain nulls or unprintable characters.
  104. @param ec An output parameter which the function may set to indicate
  105. an error. The error will be clear before this function is invoked.
  106. @see on_chunk_body_impl
  107. */
  108. std::size_t
  109. on_body_impl(
  110. string_view s, // A portion of the body
  111. error_code& ec) override; // The error returned to the caller, if any
  112. /** Called each time a new chunk header of a chunk encoded body is received.
  113. This function is invoked each time a new chunk header is received.
  114. The function is only used when the chunked transfer encoding is present.
  115. @param size The size of this chunk, in bytes.
  116. @param extensions A string containing the entire chunk extensions.
  117. This may be empty, indicating no extensions are present.
  118. @param ec An output parameter which the function may set to indicate
  119. an error. The error will be clear before this function is invoked.
  120. */
  121. void
  122. on_chunk_header_impl(
  123. std::uint64_t size, // The size of the upcoming chunk,
  124. // or zero for the last chunk
  125. string_view extension, // The chunk extensions (may be empty)
  126. error_code& ec) override; // The error returned to the caller, if any
  127. /** Called each time additional data is received representing part of a body chunk.
  128. This virtual function is invoked for each piece of the body which is
  129. received while parsing of a message. This function is only used when
  130. no chunked transfer encoding is present.
  131. @param remain The number of bytes remaining in this chunk. This includes
  132. the contents of passed `body`. If this value is zero, then this represents
  133. the final chunk.
  134. @param body A string holding the additional body contents. This may
  135. contain nulls or unprintable characters.
  136. @param ec An output parameter which the function may set to indicate
  137. an error. The error will be clear before this function is invoked.
  138. @return This function should return the number of bytes actually consumed
  139. from the `body` value. Any bytes that are not consumed on this call
  140. will be presented in a subsequent call.
  141. @see on_body_impl
  142. */
  143. std::size_t
  144. on_chunk_body_impl(
  145. std::uint64_t remain, // The number of bytes remaining in the chunk,
  146. // including what is being passed here.
  147. // or zero for the last chunk
  148. string_view body, // The next piece of the chunk body
  149. error_code& ec) override; // The error returned to the caller, if any
  150. /** Called once when the complete message is received.
  151. This virtual function is invoked once, after successfully parsing
  152. a complete HTTP message.
  153. @param ec An output parameter which the function may set to indicate
  154. an error. The error will be clear before this function is invoked.
  155. */
  156. void
  157. on_finish_impl(
  158. error_code& ec) override; // The error returned to the caller, if any
  159. public:
  160. custom_parser() = default;
  161. };
  162. //]
  163. // Definitions are not part of the docs but necessary to link
  164. template<bool isRequest>
  165. void custom_parser<isRequest>::
  166. on_request_impl(verb method, string_view method_str,
  167. string_view path, int version, error_code& ec)
  168. {
  169. boost::ignore_unused(method, method_str, path, version);
  170. ec = {};
  171. }
  172. template<bool isRequest>
  173. void custom_parser<isRequest>::
  174. on_response_impl(
  175. int status,
  176. string_view reason,
  177. int version,
  178. error_code& ec)
  179. {
  180. boost::ignore_unused(status, reason, version);
  181. ec = {};
  182. }
  183. template<bool isRequest>
  184. void custom_parser<isRequest>::
  185. on_field_impl(
  186. field f,
  187. string_view name,
  188. string_view value,
  189. error_code& ec)
  190. {
  191. boost::ignore_unused(f, name, value);
  192. ec = {};
  193. }
  194. template<bool isRequest>
  195. void custom_parser<isRequest>::
  196. on_header_impl(error_code& ec)
  197. {
  198. ec = {};
  199. }
  200. template<bool isRequest>
  201. void custom_parser<isRequest>::
  202. on_body_init_impl(
  203. boost::optional<std::uint64_t> const& content_length,
  204. error_code& ec)
  205. {
  206. boost::ignore_unused(content_length);
  207. ec = {};
  208. }
  209. template<bool isRequest>
  210. std::size_t custom_parser<isRequest>::
  211. on_body_impl(string_view body, error_code& ec)
  212. {
  213. boost::ignore_unused(body);
  214. ec = {};
  215. return body.size();
  216. }
  217. template<bool isRequest>
  218. void custom_parser<isRequest>::
  219. on_chunk_header_impl(
  220. std::uint64_t size,
  221. string_view extension,
  222. error_code& ec)
  223. {
  224. boost::ignore_unused(size, extension);
  225. ec = {};
  226. }
  227. template<bool isRequest>
  228. std::size_t custom_parser<isRequest>::
  229. on_chunk_body_impl(
  230. std::uint64_t remain,
  231. string_view body,
  232. error_code& ec)
  233. {
  234. boost::ignore_unused(remain);
  235. ec = {};
  236. return body.size();
  237. }
  238. template<bool isRequest>
  239. void custom_parser<isRequest>::
  240. on_finish_impl(error_code& ec)
  241. {
  242. ec = {};
  243. }
  244. template class custom_parser<true>;
  245. template class custom_parser<false>;
  246. } // http
  247. } // beast
  248. } // boost