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_REQUEST_PARSER_HPP
12 : #define BOOST_HTTP_PROTO_REQUEST_PARSER_HPP
13 :
14 : #include <boost/http_proto/detail/config.hpp>
15 : #include <boost/http_proto/error.hpp>
16 : #include <boost/http_proto/method.hpp>
17 : #include <boost/http_proto/parser.hpp>
18 : #include <boost/http_proto/static_request.hpp>
19 :
20 : #if defined(BOOST_MSVC)
21 : # pragma warning(push)
22 : # pragma warning(disable:4251)
23 : #endif
24 :
25 : namespace boost {
26 : namespace http_proto {
27 :
28 : /// @copydoc parser
29 : /// @brief A parser for HTTP/1 requests.
30 : /// @see @ref response_parser.
31 : class BOOST_HTTP_PROTO_DECL request_parser
32 : : public parser
33 : {
34 : public:
35 : /** Configuration settings for request_parser.
36 :
37 : @see
38 : @ref install_parser_service,
39 : @ref request_parser.
40 : */
41 : struct config : config_base
42 : {
43 : /** Constructor
44 : */
45 24 : config() noexcept
46 24 : {
47 24 : body_limit = 64 * 1024;
48 24 : }
49 : };
50 :
51 : /** Destructor.
52 :
53 : Any views or buffers obtained from this
54 : parser become invalid.
55 : */
56 1033 : ~request_parser() = default;
57 :
58 : /** Constructor.
59 :
60 : Default-constructed parsers do not reference any
61 : implementation and therefore must be assigned to before using.
62 : */
63 6 : request_parser() noexcept = default;
64 :
65 : /** Constructor.
66 :
67 : The states of `other` are transferred
68 : to the newly constructed object,
69 : including the allocated buffer.
70 : After construction, the only valid
71 : operations on the moved-from object
72 : are destruction and assignment.
73 :
74 : Buffer sequences previously obtained
75 : using @ref prepare or @ref pull_body
76 : remain valid.
77 :
78 : @par Complexity
79 : Constant.
80 :
81 : @param other The parser to move from.
82 : */
83 2 : request_parser(
84 : request_parser&& other) noexcept = default;
85 :
86 : /** Assignment.
87 : The states of `other` are transferred
88 : to this object, including the allocated
89 : buffer.
90 : After assignment, the only valid
91 : operations on the moved-from object
92 : are destruction and assignment.
93 : Buffer sequences previously obtained
94 : using @ref prepare or @ref pull_body
95 : remain valid.
96 : @par Complexity
97 : Constant.
98 : @param other The parser to move from.
99 : */
100 : request_parser&
101 2 : operator=(request_parser&& other) noexcept
102 : {
103 2 : assign(std::move(other));
104 2 : return *this;
105 : }
106 :
107 : /** Constructor.
108 :
109 : Constructs a parser that uses the @ref
110 : config parameters installed on the
111 : provided `ctx`.
112 :
113 : The parser will attempt to allocate
114 : the required space on startup, with the
115 : amount depending on the @ref config
116 : parameters, and will not perform any
117 : further allocations, except for Brotli
118 : decoder instances, if enabled.
119 :
120 : Depending on which compression algorithms
121 : are enabled in the @ref config, the parser
122 : will attempt to access the corresponding
123 : decoder services on the same `ctx`.
124 :
125 : @par Example
126 : @code
127 : request_parser sr(ctx);
128 : @endcode
129 :
130 : @par Complexity
131 : Constant.
132 :
133 : @par Exception Safety
134 : Calls to allocate may throw.
135 :
136 : @param ctx Context from which the
137 : request_parser will access registered
138 : services. The caller is responsible for
139 : ensuring that the provided ctx remains
140 : valid for the lifetime of the request_parser.
141 :
142 : @see
143 : @ref install_parser_service,
144 : @ref config.
145 : */
146 : explicit
147 : request_parser(capy::polystore& ctx);
148 :
149 : /** Return a reference to the parsed request headers.
150 :
151 : The returned reference remains valid until:
152 : @li @ref start is called
153 : @li @ref reset is called
154 : @li The parser instance is destroyed
155 :
156 : @par Preconditions
157 : @code
158 : this->got_header() == true
159 : @endcode
160 :
161 : @par Exception Safety
162 : Strong guarantee.
163 :
164 : @see
165 : @ref got_header.
166 : */
167 : static_request const&
168 : get() const;
169 : };
170 :
171 : #if defined(BOOST_MSVC)
172 : # pragma warning(pop)
173 : #endif
174 :
175 : } // http_proto
176 : } // boost
177 :
178 : #endif
|