Line data Source code
1 : //
2 : // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2025 Mohammad Nejati
4 : //
5 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 : //
8 : // Official repository: https://github.com/cppalliance/http_proto
9 : //
10 :
11 : #ifndef BOOST_HTTP_PROTO_RESPONSE_PARSER_HPP
12 : #define BOOST_HTTP_PROTO_RESPONSE_PARSER_HPP
13 :
14 : #include <boost/http_proto/detail/config.hpp>
15 : #include <boost/http_proto/error.hpp>
16 : #include <boost/http_proto/parser.hpp>
17 : #include <boost/http_proto/response_view.hpp>
18 : #include <boost/http_proto/status.hpp>
19 :
20 : namespace boost {
21 : namespace http_proto {
22 :
23 : /// @copydoc parser
24 : /// @brief A parser for HTTP/1 responses.
25 : /// @see @ref request_parser.
26 : class response_parser
27 : : public parser
28 : {
29 : public:
30 : /** Configuration settings for response_parser.
31 :
32 : @see
33 : @ref install_parser_service,
34 : @ref response_parser.
35 : */
36 : struct config : config_base
37 : {
38 : /** Constructor.
39 : */
40 14 : config() noexcept
41 14 : {
42 14 : body_limit = 1024 * 1024;
43 14 : }
44 : };
45 :
46 : /** Constructor.
47 :
48 : Constructs a parser that uses the @ref
49 : config parameters installed on the
50 : provided `ctx`.
51 :
52 : The parser will attempt to allocate
53 : the required space on startup, with the
54 : amount depending on the @ref config
55 : parameters, and will not perform any
56 : further allocations, except for Brotli
57 : decoder instances, if enabled.
58 :
59 : Depending on which compression algorithms
60 : are enabled in the @ref config, the parser
61 : will attempt to access the corresponding
62 : decoder services on the same `ctx`.
63 :
64 : @par Example
65 : @code
66 : response_parser sr(ctx);
67 : @endcode
68 :
69 : @par Complexity
70 : Constant.
71 :
72 : @par Exception Safety
73 : Calls to allocate may throw.
74 :
75 : @param ctx Context from which the
76 : response_parser will access registered
77 : services. The caller is responsible for
78 : ensuring that the provided ctx remains
79 : valid for the lifetime of the response_parser.
80 :
81 : @see
82 : @ref install_parser_service,
83 : @ref config.
84 : */
85 : BOOST_HTTP_PROTO_DECL
86 : explicit
87 : response_parser(const rts::context& ctx);
88 :
89 : /** Constructor.
90 :
91 : The states of `other` are transferred
92 : to the newly constructed object,
93 : including the allocated buffer.
94 : After construction, the only valid
95 : operations on the moved-from object
96 : are destruction and assignemt.
97 :
98 : Buffer sequences previously obtained
99 : using @ref prepare or @ref pull_body
100 : remain valid.
101 :
102 : @par Complexity
103 : Constant.
104 :
105 : @param other The parser to move from.
106 : */
107 1 : response_parser(
108 : response_parser&& other) noexcept = default;
109 :
110 : /** Destructor.
111 :
112 : Any views or buffers obtained from this
113 : parser become invalid.
114 : */
115 31 : ~response_parser() = default;
116 :
117 : /** Prepare for the next message on the stream.
118 :
119 : This informs the parser not to read a
120 : payload for the next message, regardless
121 : of the presence or absence of certain
122 : fields such as Content-Length or a chunked
123 : Transfer-Encoding. Depending on the request,
124 : some responses do not carry a body. For
125 : example, a 200 response to a CONNECT
126 : request from a tunneling proxy, or a
127 : response to a HEAD request. In these
128 : cases, callers may use this function
129 : inform the parser that no body is
130 : expected. The parser will consider the
131 : message complete after the header has
132 : been received.
133 :
134 : @par Preconditions
135 : No previous call to @ref start for the new message.
136 :
137 : @see
138 : https://datatracker.ietf.org/doc/html/rfc7230#section-3.3
139 : */
140 : void
141 : start_head_response()
142 : {
143 : start_impl(true);
144 : }
145 :
146 : /** Return a read-only view to the parsed response headers.
147 :
148 : The returned view remains valid until:
149 : @li @ref start or @ref start_head_response is called
150 : @li @ref reset is called
151 : @li The parser instance is destroyed
152 :
153 : @par Preconditions
154 : @code
155 : this->got_header() == true
156 : @endcode
157 :
158 : @par Exception Safety
159 : Strong guarantee.
160 :
161 : @see
162 : @ref got_header.
163 : */
164 : BOOST_HTTP_PROTO_DECL
165 : response_view
166 : get() const;
167 : };
168 :
169 : } // http_proto
170 : } // boost
171 :
172 : #endif
|