Line data Source code
1 : //
2 : // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2024 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
9 : //
10 :
11 : #ifndef BOOST_HTTP_CONFIG_HPP
12 : #define BOOST_HTTP_CONFIG_HPP
13 :
14 : #include <boost/http/detail/config.hpp>
15 : #include <boost/http/header_limits.hpp>
16 :
17 : #include <cstddef>
18 : #include <cstdint>
19 : #include <memory>
20 :
21 : namespace boost {
22 : namespace http {
23 :
24 : /** Parser configuration settings.
25 :
26 : @see @ref make_parser_config,
27 : @ref request_parser,
28 : @ref response_parser.
29 : */
30 : struct parser_config
31 : {
32 : /// Limits for HTTP headers.
33 : header_limits headers;
34 :
35 : /** Maximum content body size (after decoding).
36 :
37 : @see @ref parser::set_body_limit.
38 : */
39 : std::uint64_t body_limit;
40 :
41 : /** Enable Brotli Content-Encoding decoding.
42 : */
43 : bool apply_brotli_decoder = false;
44 :
45 : /** Enable Deflate Content-Encoding decoding.
46 : */
47 : bool apply_deflate_decoder = false;
48 :
49 : /** Enable Gzip Content-Encoding decoding.
50 : */
51 : bool apply_gzip_decoder = false;
52 :
53 : /** Zlib window bits (9-15).
54 :
55 : Must be >= the value used during compression.
56 : Larger windows improve decompression at the
57 : cost of memory.
58 : */
59 : int zlib_window_bits = 15;
60 :
61 : /** Minimum payload buffer size.
62 :
63 : Controls:
64 : @li Smallest read/decode buffer allocation
65 : @li Minimum guaranteed in-place body size
66 : @li Reserve size for dynamic buffers when
67 : payload size is unknown
68 :
69 : This cannot be zero.
70 : */
71 : std::size_t min_buffer = 4096;
72 :
73 : /** Maximum buffer size from @ref parser::prepare.
74 :
75 : This cannot be zero.
76 : */
77 : std::size_t max_prepare = std::size_t(-1);
78 :
79 : /** Space reserved for type-erased @ref sink objects.
80 : */
81 : std::size_t max_type_erase = 1024;
82 :
83 : /** Constructor.
84 :
85 : @param server True for server mode (parsing requests,
86 : 64KB body limit), false for client mode
87 : (parsing responses, 1MB body limit).
88 : */
89 : explicit
90 28 : parser_config(bool server) noexcept
91 28 : : body_limit(server ? 64 * 1024 : 1024 * 1024)
92 : {
93 28 : }
94 : };
95 :
96 : /** Parser configuration with computed fields.
97 :
98 : Derived from @ref parser_config with additional
99 : precomputed values for workspace allocation.
100 :
101 : @see @ref make_parser_config.
102 : */
103 : struct parser_config_impl : parser_config
104 : {
105 : /// Total workspace allocation size.
106 : std::size_t space_needed;
107 :
108 : /// Space for decompressor state.
109 : std::size_t max_codec;
110 :
111 : /// Maximum overread bytes.
112 : BOOST_HTTP_DECL
113 : std::size_t
114 : max_overread() const noexcept;
115 : };
116 :
117 : //------------------------------------------------
118 :
119 : /** Serializer configuration settings.
120 :
121 : @see @ref make_serializer_config,
122 : @ref serializer.
123 : */
124 : struct serializer_config
125 : {
126 : /** Enable Brotli Content-Encoding.
127 : */
128 : bool apply_brotli_encoder = false;
129 :
130 : /** Enable Deflate Content-Encoding.
131 : */
132 : bool apply_deflate_encoder = false;
133 :
134 : /** Enable Gzip Content-Encoding.
135 : */
136 : bool apply_gzip_encoder = false;
137 :
138 : /** Brotli compression quality (0-11).
139 :
140 : Higher values yield better but slower compression.
141 : */
142 : std::uint32_t brotli_comp_quality = 5;
143 :
144 : /** Brotli compression window size (10-24).
145 :
146 : Larger windows improve compression but increase
147 : memory usage.
148 : */
149 : std::uint32_t brotli_comp_window = 18;
150 :
151 : /** Zlib compression level (0-9).
152 :
153 : 0 = no compression, 1 = fastest, 9 = best.
154 : */
155 : int zlib_comp_level = 6;
156 :
157 : /** Zlib window bits (9-15).
158 :
159 : Controls the history buffer size.
160 : */
161 : int zlib_window_bits = 15;
162 :
163 : /** Zlib memory level (1-9).
164 :
165 : Higher values use more memory, but offer faster
166 : and more efficient compression.
167 : */
168 : int zlib_mem_level = 8;
169 :
170 : /** Minimum buffer size for payloads (must be > 0).
171 : */
172 : std::size_t payload_buffer = 8192;
173 :
174 : /** Reserved space for type-erasure storage.
175 : */
176 : std::size_t max_type_erase = 1024;
177 : };
178 :
179 : //------------------------------------------------
180 :
181 : struct parser_config_impl;
182 : struct serializer_config_impl;
183 :
184 : /** Shared pointer to immutable parser configuration.
185 :
186 : @see @ref parser_config_impl, @ref make_parser_config.
187 : */
188 : using shared_parser_config = std::shared_ptr<parser_config_impl const>;
189 :
190 : /** Shared pointer to immutable serializer configuration.
191 :
192 : @see @ref serializer_config_impl, @ref make_serializer_config.
193 : */
194 : using shared_serializer_config = std::shared_ptr<serializer_config_impl const>;
195 :
196 :
197 : /** Create parser configuration with computed values.
198 :
199 : @param cfg User-provided configuration settings.
200 :
201 : @return Shared pointer to configuration with
202 : precomputed fields.
203 :
204 : @see @ref parser_config,
205 : @ref request_parser,
206 : @ref response_parser.
207 : */
208 : BOOST_HTTP_DECL
209 : shared_parser_config
210 : make_parser_config(parser_config cfg);
211 :
212 : /** Serializer configuration with computed fields.
213 :
214 : Derived from @ref serializer_config with additional
215 : precomputed values for workspace allocation.
216 :
217 : @see @ref make_serializer_config.
218 : */
219 : struct serializer_config_impl : serializer_config
220 : {
221 : /// Total workspace allocation size.
222 : std::size_t space_needed;
223 : };
224 :
225 : /** Create serializer configuration with computed values.
226 :
227 : @param cfg User-provided configuration settings.
228 :
229 : @return Shared pointer to configuration with
230 : precomputed fields.
231 :
232 : @see @ref serializer_config,
233 : @ref serializer.
234 : */
235 : BOOST_HTTP_DECL
236 : shared_serializer_config
237 : make_serializer_config(serializer_config cfg);
238 :
239 : } // http
240 : } // boost
241 :
242 : #endif
|