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 : /** Constructor.
80 :
81 : @param server True for server mode (parsing requests,
82 : 64KB body limit), false for client mode
83 : (parsing responses, 1MB body limit).
84 : */
85 : explicit
86 30 : parser_config(bool server) noexcept
87 30 : : body_limit(server ? 64 * 1024 : 1024 * 1024)
88 : {
89 30 : }
90 : };
91 :
92 : /** Parser configuration with computed fields.
93 :
94 : Derived from @ref parser_config with additional
95 : precomputed values for workspace allocation.
96 :
97 : @see @ref make_parser_config.
98 : */
99 : struct parser_config_impl : parser_config
100 : {
101 : /// Total workspace allocation size.
102 : std::size_t space_needed;
103 :
104 : /// Space for decompressor state.
105 : std::size_t max_codec;
106 :
107 : /// Maximum overread bytes.
108 : BOOST_HTTP_DECL
109 : std::size_t
110 : max_overread() const noexcept;
111 : };
112 :
113 : //------------------------------------------------
114 :
115 : /** Serializer configuration settings.
116 :
117 : @see @ref make_serializer_config,
118 : @ref serializer.
119 : */
120 : struct serializer_config
121 : {
122 : /** Enable Brotli Content-Encoding.
123 : */
124 : bool apply_brotli_encoder = false;
125 :
126 : /** Enable Deflate Content-Encoding.
127 : */
128 : bool apply_deflate_encoder = false;
129 :
130 : /** Enable Gzip Content-Encoding.
131 : */
132 : bool apply_gzip_encoder = false;
133 :
134 : /** Brotli compression quality (0-11).
135 :
136 : Higher values yield better but slower compression.
137 : */
138 : std::uint32_t brotli_comp_quality = 5;
139 :
140 : /** Brotli compression window size (10-24).
141 :
142 : Larger windows improve compression but increase
143 : memory usage.
144 : */
145 : std::uint32_t brotli_comp_window = 18;
146 :
147 : /** Zlib compression level (0-9).
148 :
149 : 0 = no compression, 1 = fastest, 9 = best.
150 : */
151 : int zlib_comp_level = 6;
152 :
153 : /** Zlib window bits (9-15).
154 :
155 : Controls the history buffer size.
156 : */
157 : int zlib_window_bits = 15;
158 :
159 : /** Zlib memory level (1-9).
160 :
161 : Higher values use more memory, but offer faster
162 : and more efficient compression.
163 : */
164 : int zlib_mem_level = 8;
165 :
166 : /** Minimum buffer size for payloads (must be > 0).
167 : */
168 : std::size_t payload_buffer = 8192;
169 :
170 : };
171 :
172 : //------------------------------------------------
173 :
174 : struct parser_config_impl;
175 : struct serializer_config_impl;
176 :
177 : /** Shared pointer to immutable parser configuration.
178 :
179 : @see @ref parser_config_impl, @ref make_parser_config.
180 : */
181 : using shared_parser_config = std::shared_ptr<parser_config_impl const>;
182 :
183 : /** Shared pointer to immutable serializer configuration.
184 :
185 : @see @ref serializer_config_impl, @ref make_serializer_config.
186 : */
187 : using shared_serializer_config = std::shared_ptr<serializer_config_impl const>;
188 :
189 :
190 : /** Create parser configuration with computed values.
191 :
192 : @param cfg User-provided configuration settings.
193 :
194 : @return Shared pointer to configuration with
195 : precomputed fields.
196 :
197 : @see @ref parser_config,
198 : @ref request_parser,
199 : @ref response_parser.
200 : */
201 : BOOST_HTTP_DECL
202 : shared_parser_config
203 : make_parser_config(parser_config cfg);
204 :
205 : /** Serializer configuration with computed fields.
206 :
207 : Derived from @ref serializer_config with additional
208 : precomputed values for workspace allocation.
209 :
210 : @see @ref make_serializer_config.
211 : */
212 : struct serializer_config_impl : serializer_config
213 : {
214 : /// Total workspace allocation size.
215 : std::size_t space_needed;
216 : };
217 :
218 : /** Create serializer configuration with computed values.
219 :
220 : @param cfg User-provided configuration settings.
221 :
222 : @return Shared pointer to configuration with
223 : precomputed fields.
224 :
225 : @see @ref serializer_config,
226 : @ref serializer.
227 : */
228 : BOOST_HTTP_DECL
229 : shared_serializer_config
230 : make_serializer_config(serializer_config cfg);
231 :
232 : } // http
233 : } // boost
234 :
235 : #endif
|