Skip to content

Commit c111a17

Browse files
committed
Refactor deflate/inflate code in serializer and parser
1 parent 2d6bc80 commit c111a17

22 files changed

+438
-1058
lines changed

include/boost/http_proto/message_view_base.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//
22
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
33
// Copyright (c) 2024 Christian Mazakas
4+
// Copyright (c) 2025 Mohammad Nejati
45
//
56
// Distributed under the Boost Software License, Version 1.0. (See accompanying
67
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -13,10 +14,6 @@
1314

1415
#include <boost/http_proto/detail/config.hpp>
1516
#include <boost/http_proto/fields_view_base.hpp>
16-
#include <boost/url/grammar/recycled.hpp>
17-
#include <boost/url/grammar/type_traits.hpp>
18-
#include <memory>
19-
#include <string>
2017

2118
namespace boost {
2219
namespace http_proto {

include/boost/http_proto/parser.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ class parser
535535
private:
536536
friend class request_parser;
537537
friend class response_parser;
538+
friend class parser_service;
539+
class filter;
538540

539541
BOOST_HTTP_PROTO_DECL
540542
parser(context&, detail::kind);
@@ -602,7 +604,7 @@ class parser
602604
buffers::mutable_buffer_pair mbp_;
603605
buffers::const_buffer_pair cbp_;
604606

605-
detail::filter* filter_;
607+
filter* filter_;
606608
buffers::any_dynamic_buffer* eb_;
607609
sink* sink_;
608610

include/boost/http_proto/serializer.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//
22
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3+
// Copyright (c) 2025 Mohammad Nejati
34
//
45
// Distributed under the Boost Software License, Version 1.0. (See accompanying
56
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -24,7 +25,6 @@
2425
#include <boost/buffers/type_traits.hpp>
2526
#include <boost/system/result.hpp>
2627

27-
#include <memory>
2828
#include <numeric>
2929
#include <type_traits>
3030
#include <utility>
@@ -34,9 +34,6 @@ namespace http_proto {
3434

3535
#ifndef BOOST_HTTP_PROTO_DOCS
3636
class message_view_base;
37-
namespace detail {
38-
class filter;
39-
} // namespace detail
4037
#endif
4138

4239
/** A serializer for HTTP/1 messages
@@ -219,8 +216,8 @@ class serializer
219216
consume(std::size_t n);
220217

221218
private:
219+
class filter;
222220
class const_buf_gen_base;
223-
224221
template<class>
225222
class const_buf_gen;
226223

@@ -288,7 +285,7 @@ class serializer
288285
detail::workspace ws_;
289286

290287
const_buf_gen_base* buf_gen_;
291-
detail::filter* filter_;
288+
filter* filter_;
292289
source* source_;
293290

294291
buffers::circular_buffer cb0_;

include/boost/http_proto/service/deflate_service.hpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,26 @@ namespace zlib {
2121

2222
/** Provides the ZLib compression API
2323
*/
24-
struct deflate_service
24+
25+
struct BOOST_SYMBOL_VISIBLE
26+
deflate_service
27+
: public http_proto::service
2528
{
2629
virtual char const* version() const noexcept = 0;
27-
virtual int init(stream_t& st, int level) const = 0;
28-
virtual int init2(stream_t& st, int level, int method,
30+
virtual int init(stream& st, int level) const = 0;
31+
virtual int init2(stream& st, int level, int method,
2932
int windowBits, int memLevel, int strategy) const = 0;
30-
virtual int set_dict(stream_t& st, unsigned char const* dict, unsigned len) const = 0;
31-
virtual int get_dict(stream_t& st, unsigned char* dest, unsigned* len) const = 0;
32-
virtual int dup(stream_t& dest, stream_t& src) const = 0;
33-
virtual int deflate(stream_t& st, int flush) const = 0;
34-
virtual int deflate_end(stream_t& st) const = 0;
35-
virtual int reset(stream_t& st) const = 0;
36-
virtual int params(stream_t& st, int level, int strategy) const = 0;
37-
virtual std::size_t bound(stream_t& st, unsigned long sourceLen) const = 0;
38-
virtual int pending(stream_t& st, unsigned* pending, int* bits) const = 0;
39-
virtual int prime(stream_t& st, int bits, int value) const = 0;
40-
virtual int set_header(stream_t& st, void* header) const = 0;
33+
virtual int set_dict(stream& st, unsigned char const* dict, unsigned len) const = 0;
34+
virtual int get_dict(stream& st, unsigned char* dest, unsigned* len) const = 0;
35+
virtual int dup(stream& dest, stream& src) const = 0;
36+
virtual int deflate(stream& st, int flush) const = 0;
37+
virtual int deflate_end(stream& st) const = 0;
38+
virtual int reset(stream& st) const = 0;
39+
virtual int params(stream& st, int level, int strategy) const = 0;
40+
virtual std::size_t bound(stream& st, unsigned long sourceLen) const = 0;
41+
virtual int pending(stream& st, unsigned* pending, int* bits) const = 0;
42+
virtual int prime(stream& st, int bits, int value) const = 0;
43+
virtual int set_header(stream& st, void* header) const = 0;
4144
};
4245

4346
BOOST_HTTP_PROTO_ZLIB_DECL

include/boost/http_proto/service/inflate_service.hpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//
22
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3+
// Copyright (c) 2025 Mohammad Nejati
34
//
45
// Distributed under the Boost Software License, Version 1.0. (See accompanying
56
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -19,24 +20,26 @@ namespace zlib {
1920

2021
/** Provides the ZLib decompression API
2122
*/
22-
struct inflate_service
23+
struct BOOST_SYMBOL_VISIBLE
24+
inflate_service
25+
: public http_proto::service
2326
{
2427
virtual char const* version() const noexcept = 0;
25-
virtual int init(stream_t& st) const = 0;
26-
virtual int init2(stream_t& st, int windowBits) const = 0;
27-
virtual int inflate(stream_t& st, int flush) const = 0;
28-
virtual int inflate_end(stream_t& st) const = 0;
29-
virtual int set_dict(stream_t& st, unsigned char const* dict, unsigned len) const = 0;
30-
virtual int get_dict(stream_t& st, unsigned char* dest, unsigned* len) const = 0;
31-
virtual int sync(stream_t& st) const = 0;
32-
virtual int dup(stream_t& dest, stream_t& src) const = 0;
33-
virtual int reset(stream_t& st) const = 0;
34-
virtual int reset2(stream_t& st, int windowBits) const = 0;
35-
virtual int prime(stream_t& st, int bits, int value) const = 0;
36-
virtual long mark(stream_t& st) const = 0;
37-
virtual int get_header(stream_t& st, void* header) const = 0;
38-
virtual int back_init(stream_t& st, int windowBits, unsigned char* window) const = 0;
39-
virtual int back_end(stream_t& st) const = 0;
28+
virtual int init(stream& st) const = 0;
29+
virtual int init2(stream& st, int windowBits) const = 0;
30+
virtual int inflate(stream& st, int flush) const = 0;
31+
virtual int inflate_end(stream& st) const = 0;
32+
virtual int set_dict(stream& st, unsigned char const* dict, unsigned len) const = 0;
33+
virtual int get_dict(stream& st, unsigned char* dest, unsigned* len) const = 0;
34+
virtual int sync(stream& st) const = 0;
35+
virtual int dup(stream& dest, stream& src) const = 0;
36+
virtual int reset(stream& st) const = 0;
37+
virtual int reset2(stream& st, int windowBits) const = 0;
38+
virtual int prime(stream& st, int bits, int value) const = 0;
39+
virtual long mark(stream& st) const = 0;
40+
virtual int get_header(stream& st, void* header) const = 0;
41+
virtual int back_init(stream& st, int windowBits, unsigned char* window) const = 0;
42+
virtual int back_end(stream& st) const = 0;
4043
virtual unsigned long compile_flags() const = 0;
4144
};
4245

include/boost/http_proto/service/zlib_service.hpp

Lines changed: 48 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,30 @@ namespace boost {
2121
namespace http_proto {
2222
namespace zlib {
2323

24-
struct stream_t
24+
struct stream
2525
{
2626
using alloc_func = void*(*)(void*, unsigned int, unsigned int);
2727
using free_func = void(*)(void*, void*);
2828

29-
unsigned char* next_in; // next input byte
30-
unsigned int avail_in; // number of bytes available at next_in
31-
unsigned long total_in; // total number of input bytes read so far
29+
unsigned char* next_in; // next input byte
30+
unsigned int avail_in; // number of bytes available at next_in
31+
unsigned long total_in; // total number of input bytes read so far
3232

33-
unsigned char* next_out; // next output byte will go here
34-
unsigned int avail_out; // remaining free space at next_out
35-
unsigned long total_out; // total number of bytes output so far
33+
unsigned char* next_out; // next output byte will go here
34+
unsigned int avail_out; // remaining free space at next_out
35+
unsigned long total_out; // total number of bytes output so far
3636

37-
char const* msg; // last error message, NULL if no error
38-
void* state; // not visible by applications
37+
char* msg; // last error message, NULL if no error
38+
void* state; // not visible by applications
3939

40-
alloc_func zalloc; // used to allocate internal state
41-
free_func zfree; // used to deallocate internal state
42-
void* opaque; // private data object passed to zalloc and zfree
40+
alloc_func zalloc; // used to allocate internal state
41+
free_func zfree; // used to deallocate internal state
42+
void* opaque; // private data object passed to zalloc and zfree
4343

44-
int data_type; // best guess about the data type: binary or text
44+
int data_type; // best guess about the data type: binary or text
4545
// for deflate, or the decoding state for inflate
46-
unsigned long adler; // Adler-32 or CRC-32 value of the uncompressed data
47-
unsigned long reserved; // reserved for future use
46+
unsigned long adler; // Adler-32 or CRC-32 value of the uncompressed data
47+
unsigned long reserved; // reserved for future use
4848
};
4949

5050
/** Error codes returned from compression/decompression functions.
@@ -65,140 +65,51 @@ enum class error
6565
version_err = -6
6666
};
6767

68-
/// Flush methods.
69-
enum class flush
68+
/// Flush methods
69+
enum flush
7070
{
71-
none,
72-
partial,
73-
sync,
74-
full,
75-
finish,
76-
block,
77-
trees
71+
no_flush = 0,
72+
partial_flush = 1,
73+
sync_flush = 2,
74+
full_flush = 3,
75+
finish = 4,
76+
block = 5,
77+
trees = 6
7878
};
7979

80-
/** Input and output buffers.
81-
82-
The application must update `next_in` and `avail_in` when `avail_in`
83-
has dropped to zero. It must update `next_out` and `avail_out` when
84-
`avail_out` has dropped to zero.
85-
*/
86-
struct params
80+
/// Compression levels
81+
enum compression_level
8782
{
88-
/// Next input byte
89-
void const* next_in;
90-
91-
/// Number of bytes available at `next_in`
92-
std::size_t avail_in;
93-
94-
/// Next output byte
95-
void* next_out;
96-
97-
/// Number of bytes remaining free at `next_out`
98-
std::size_t avail_out;
83+
default_compression = -1,
84+
no_compression = 0,
85+
best_speed = 1,
86+
best_compression = 9
9987
};
10088

101-
/// Abstract interface for deflator/inflator streams.
102-
struct stream
89+
/// Compression strategy
90+
enum compression_strategy
10391
{
104-
/** Call the underling compression/decompression algorithm.
105-
106-
@param p The input and output buffers.
107-
108-
@param f The flush method.
109-
110-
@return The result of operation that contains a value
111-
of @ref error.
112-
*/
113-
virtual system::error_code
114-
write(params& p, flush f) noexcept = 0;
92+
default_strategy = 0,
93+
filtered = 1,
94+
huffman_only = 2,
95+
rle = 3,
96+
fixed = 4
11597
};
11698

117-
/** Provides in-memory compression and decompression functions
118-
using zlib underneath.
119-
*/
120-
struct BOOST_SYMBOL_VISIBLE
121-
service
122-
: http_proto::service
99+
/// Possible values of the data_type field for deflate
100+
enum data_type
123101
{
124-
/** The memory requirements for deflator.
125-
126-
@param window_bits The window size.
127-
128-
@param mem_level The memory level.
129-
130-
@return The memory requirements in bytes.
131-
*/
132-
virtual
133-
std::size_t
134-
deflator_space_needed(
135-
int window_bits,
136-
int mem_level) const noexcept = 0;
137-
138-
/** The memory requirements for inflator.
139-
140-
@param window_bits The window size.
141-
142-
@return The memory requirements in bytes.
143-
*/
144-
virtual
145-
std::size_t
146-
inflator_space_needed(
147-
int window_bits) const noexcept = 0;
148-
149-
/** Create a deflator stream by calling zlib `deflateInit2()`.
150-
151-
@param ws A reference to the workspace used for constructing the
152-
deflator stream object and for storage used by zlib.
153-
154-
@param level The compression level.
155-
156-
@param window_bits The window size.
157-
158-
@param mem_level Specifies how much memory should be allocated
159-
for the internal compression state.
160-
161-
@return A reference to the created deflator stream.
162-
163-
@throws std::length_error If there is insufficient free space in
164-
@ref `http_proto::detail::workspace`.
165-
*/
166-
virtual stream&
167-
make_deflator(
168-
http_proto::detail::workspace& ws,
169-
int level,
170-
int window_bits,
171-
int mem_level) const = 0;
172-
173-
/** Create an inflator stream by calling zlib `inflateInit2()`.
174-
175-
@param ws A reference to the workspace used for constructing the
176-
inflator stream object and for storage used by zlib.
177-
178-
@param window_bits The window size.
179-
180-
@return A reference to the created inflator stream.
181-
182-
@throws std::length_error If there is insufficient free space in
183-
@ref `http_proto::detail::workspace`.
184-
*/
185-
virtual stream&
186-
make_inflator(
187-
http_proto::detail::workspace& ws,
188-
int window_bits) const = 0;
102+
binary = 0,
103+
text = 1,
104+
ascii = 1,
105+
unknown = 2
189106
};
190107

191-
/** Installs a zlib service on the provided context.
192-
193-
@param ctx A reference to the @ref context where the service
194-
will be installed.
195-
196-
@throw std::invalid_argument If the zlib service already
197-
exist on the context.
198-
*/
199-
BOOST_HTTP_PROTO_ZLIB_DECL
200-
void
201-
install_service(context& ctx);
108+
/// Compression method
109+
enum compression_method
110+
{
111+
deflated = 8
112+
};
202113

203114
} // zlib
204115
} // http_proto

0 commit comments

Comments
 (0)