RESTinio
Loading...
Searching...
No Matches
zlib.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
11#pragma once
12
14
16
21
22#include <zlib.h>
23
24#include <string>
25#include <cstring>
26
27namespace restinio
28{
29
30namespace transforms
31{
32
33namespace zlib
34{
35
38constexpr std::size_t default_output_reserve_buffer_size = 256 * 1024;
39
50
51//
52// params_t
53//
54
56/*
57 @since v.0.4.4
58
59 \note There is a special case for compression format: format_t::identity.
60 If this format is set that zlib transformator is transparently copies
61 input to output and so all other params are ignored.
62*/
64{
65 public:
67 enum class operation_t
68 {
73 };
74
76 enum class format_t
77 {
79 deflate,
81 gzip,
83 /*
84 Means that no compression will be used and no header/trailer will be applied.
85 */
87 };
88
90
98 operation_t op,
100 format_t f,
102 int l = -1 )
103 : m_operation{ op }
104 , m_format{ f }
105 {
106 level( l );
107 }
108
113 {
114 level( -1 );
115 }
116
119
121 format_t format() const { return m_format; }
122
124
127 int level() const { return m_level; }
128
130
135 params_t &
136 level( int level_value ) &
137 {
138 if( level_value < -1 || level_value > 9 )
139 {
140 throw exception_t{
141 fmt::format(
143 "invalid compression level: {}, must be "
144 "an integer value in the range of -1 to 9" ),
145 level_value ) };
146 }
147
148 m_level = level_value;
149
150 return reference_to_self();
151 }
152
154 params_t &&
155 level( int level_value ) &&
156 {
157 return std::move( this->level( level_value ) );
158 }
159
161 int window_bits() const { return m_window_bits; }
162
164
171 params_t &
172 window_bits( int window_bits_value ) &
173 {
174 // From https://zlib.net/manual.html:
175 // For the current implementation of deflate(),
176 // a windowBits value of 8 (a window size of 256 bytes)
177 // is not supported. As a result, a request for 8 will result in 9
178 // (a 512-byte window). In that case, providing 8 to inflateInit2()
179 // will result in an error when the zlib header with 9 is
180 // checked against the initialization of inflate().
181 // The remedy is to not use 8 with deflateInit2()
182 // with this initialization, or at least in that case use 9
183 // with inflateInit2().
184 // ...
185 // windowBits can also be zero to request that inflate
186 // use the window size in the zlib header of the compressed
187 // stream.
188
189 if( ( window_bits_value < 8 || window_bits_value > MAX_WBITS ) &&
190 ( 0 != window_bits_value || operation_t::decompress != operation() ) )
191 {
192 throw exception_t{
193 fmt::format(
195 "invalid window_bits: {}, must be "
196 "an integer value in the range of 8 to {} or "
197 "0 for decompress operation" ),
198 window_bits_value,
199 MAX_WBITS ) };
200 }
201
202 if( 8 == window_bits_value )
203 window_bits_value = 9;
204
205 m_window_bits = window_bits_value;
206
207 return reference_to_self();
208 }
209
211 params_t &&
212 window_bits( int window_bits_value ) &&
213 {
214 return std::move( this->window_bits( window_bits_value ) );
215 }
216
218
221 int mem_level() const { return m_mem_level; }
222
224
233 params_t &
234 mem_level( int mem_level_value ) &
235 {
236 if( mem_level_value < 1 || mem_level_value > MAX_MEM_LEVEL )
237 {
238 throw exception_t{
239 fmt::format(
241 "invalid compression mem_level: {}, must be "
242 "an integer value in the range of 1 to {}" ),
243 mem_level_value,
244 MAX_MEM_LEVEL ) };
245 }
246
247 m_mem_level = mem_level_value;
248
249 return reference_to_self();
250 }
251
253 params_t &&
254 mem_level( int mem_level_value ) &&
255 {
256 return std::move( this->mem_level( mem_level_value ) );
257 }
258
260
263 int strategy() const { return m_strategy; }
264
266
273 params_t &
274 strategy( int strategy_value ) &
275 {
276 if( Z_DEFAULT_STRATEGY != strategy_value &&
277 Z_FILTERED != strategy_value &&
278 Z_HUFFMAN_ONLY != strategy_value &&
279 Z_RLE != strategy_value )
280 {
281 throw exception_t{
282 fmt::format(
284 "invalid compression strategy: {}, must be "
285 "one of: "
286 "Z_DEFAULT_STRATEGY({}), "
287 "Z_FILTERED({}), "
288 "Z_HUFFMAN_ONLY({}), "
289 "Z_RLE({})" ),
290 strategy_value,
294 Z_RLE ) };
295 }
296
297 m_strategy = strategy_value;
298
299 return reference_to_self();
300 }
301
303 params_t &&
304 strategy( int strategy_value ) &&
305 {
306 return std::move( this->strategy( strategy_value ) );
307 }
308
310
317 std::size_t reserve_buffer_size() const { return m_reserve_buffer_size; }
318
320 params_t &
321 reserve_buffer_size( std::size_t size ) &
322 {
323 if( size < 10UL )
324 {
325 throw exception_t{ "too small reserve buffer size" };
326 }
327
329
330 return reference_to_self();
331 }
332
334 params_t &&
335 reserve_buffer_size( std::size_t size ) &&
336 {
337 return std::move( this->reserve_buffer_size( size ) );
338 }
339
340 private:
342 params_t & reference_to_self() { return *this; }
343
346
349
351
355
359
362};
363
383inline params_t
384make_deflate_compress_params( int compression_level = -1 )
385{
386 return params_t{
389 compression_level };
390}
391
392inline params_t
394{
395 return params_t{
398}
399
400inline params_t
401make_gzip_compress_params( int compression_level = -1 )
402{
403 return params_t{
406 compression_level };
407}
408
409inline params_t
411{
412 return params_t{
415}
416
417inline params_t
419{
420 return params_t{};
421}
423
424//
425// zlib_t
426//
427
429
479{
480 public:
481 zlib_t( const params_t & transformation_params )
482 : m_params{ transformation_params }
483 {
484 if( !is_identity() )
485 {
486 // Setting allocator stuff before initializing
487 // TODO: allocation can be done with user defined allocator.
491
492 // Track initialization result.
493 int init_result;
494
495 // Compression.
496 auto current_window_bits = m_params.window_bits();
497
499 {
500 current_window_bits += 16;
501 }
502
504 {
505 // zlib format.
506 init_result =
509 m_params.level(),
511 current_window_bits,
513 m_params.strategy() );
514 }
515 else
516 {
517 init_result =
520 current_window_bits );
521 }
522
523 if( Z_OK != init_result )
524 {
525 throw exception_t{
526 fmt::format(
528 "Failed to initialize zlib stream: {}, {}" ),
529 init_result,
530 get_error_msg() ) };
531 }
532
534
535 // Reserve initial buffer.
536 inc_buffer();
537 }
538 // else => Nothing to initialize and to reserve.
539 }
540
541 zlib_t( const zlib_t & ) = delete;
542 zlib_t( zlib_t && ) = delete;
543 zlib_t & operator = ( const zlib_t & ) = delete;
544 zlib_t & operator = ( zlib_t && ) = delete;
545
547 {
549 {
551 {
553 }
554 else
555 {
557 }
558 }
559 }
560
562 const params_t & params() const { return m_params; }
563
565
569 void
571 {
573
574 if( is_identity() )
575 {
576 m_out_buffer.append( input.data(), input.size() );
577 m_write_pos = m_out_buffer.size();
578 }
579 else
580 {
581 if( std::numeric_limits< decltype( m_zlib_stream.avail_in ) >::max() < input.size() )
582 {
583 throw exception_t{
584 fmt::format(
586 "input data is too large: {} (max possible: {}), "
587 "try to break large data into pieces" ),
588 input.size(),
589 std::numeric_limits< decltype( m_zlib_stream.avail_in ) >::max() ) };
590 }
591
592 if( 0 < input.size() )
593 {
595 reinterpret_cast< Bytef* >( const_cast< char* >( input.data() ) );
596
597 m_zlib_stream.avail_in = static_cast< uInt >( input.size() );
598
600 {
602 }
603 else
604 {
606 }
607 }
608 }
609 }
610
612
616 void
618 {
620
621 if( !is_identity() )
622 {
623 m_zlib_stream.next_in = nullptr;
624 m_zlib_stream.avail_in = static_cast< uInt >( 0 );
625
627 {
629 }
630 else
631 {
633 }
634 }
635 }
636
638 void
640 {
642
643 if( !is_identity() )
644 {
645 m_zlib_stream.next_in = nullptr;
646 m_zlib_stream.avail_in = static_cast< uInt >( 0 );
647
649 {
651 }
652 else
653 {
655 }
656 }
657
659 }
660
662
683 std::string
685 {
686 std::string result;
687 const auto data_size = m_write_pos;
688 std::swap( result, m_out_buffer );
689 m_write_pos = 0;
690 result.resize( data_size ); // Shrink output data.
691 return result;
692 }
693
695 auto output_size() const { return m_write_pos; }
696
698 bool is_completed() const { return m_operation_is_complete; }
699
700 private:
701 bool is_identity() const
702 {
704 }
705
707 const char *
709 {
710 const char * err_msg = "<no zlib error description>";
711 if( m_zlib_stream.msg )
712 err_msg = m_zlib_stream.msg;
713
714 return err_msg;
715 }
716
718 void
720 {
721 if( is_completed() )
722 throw exception_t{ "zlib operation is already completed" };
723 }
724
726 void
728 {
729 m_out_buffer.resize(
731 }
732
734 auto
736 {
738 reinterpret_cast< Bytef* >(
739 const_cast< char* >( m_out_buffer.data() + m_write_pos ) );
740
741 const auto provided_out_buffer_size =
742 m_out_buffer.size() - m_write_pos;
744 static_cast<uInt>( provided_out_buffer_size );
745
746 return provided_out_buffer_size;
747 }
748
750 /*
751 Data and its size must be already in
752 `m_zlib_stream.next_in`, `m_zlib_stream.avail_in`.
753 */
754 void
756 {
757 while( true )
758 {
759 const auto provided_out_buffer_size = prepare_out_buffer();
760
761 int operation_result = deflate( &m_zlib_stream, flush );
762
763 if( !( Z_OK == operation_result ||
764 Z_BUF_ERROR == operation_result ||
765 ( Z_STREAM_END == operation_result && Z_FINISH == flush ) ) )
766 {
767 const char * err_msg = "<no error desc>";
768 if( m_zlib_stream.msg )
769 err_msg = m_zlib_stream.msg;
770
771 throw exception_t{
772 fmt::format(
774 "unexpected result of deflate() (zlib): {}, {}" ),
775 operation_result,
776 err_msg ) };
777 }
778
779 m_write_pos += provided_out_buffer_size - m_zlib_stream.avail_out;
780
781 if( 0 == m_zlib_stream.avail_out && Z_STREAM_END != operation_result )
782 {
783 // Looks like not all the output was obtained.
784 // There is a minor chance that it just happened to
785 // occupy exactly the same space that was available,
786 // in that case it would go for a one redundant call to deflate.
787 inc_buffer();
788 continue;
789 }
790
791 if( 0 == m_zlib_stream.avail_in )
792 {
793 // All the input was consumed.
794 break;
795 }
796 }
797 }
798
800 /*
801 Data and its size must be already in
802 `m_zlib_stream.next_in`, `m_zlib_stream.avail_in`.
803 */
804 void
806 {
807 while( true )
808 {
809 const auto provided_out_buffer_size = prepare_out_buffer();
810
811 int operation_result = inflate( &m_zlib_stream, flush );
812 if( !( Z_OK == operation_result ||
813 Z_BUF_ERROR == operation_result ||
814 Z_STREAM_END == operation_result ) )
815 {
816 throw exception_t{
817 fmt::format(
819 "unexpected result of inflate() (zlib): {}, {}" ),
820 operation_result,
821 get_error_msg() ) };
822 }
823
824 m_write_pos += provided_out_buffer_size - m_zlib_stream.avail_out;
825
826 if( 0 == m_zlib_stream.avail_out && Z_STREAM_END != operation_result )
827 {
828 // Looks like not all the output was obtained.
829 // There is a minor chance that it just happened to
830 // occupy exactly the same space that was available,
831 // in that case it would go for a one redundant call to deflate.
832 inc_buffer();
833 continue;
834 }
835
836 if( 0 == m_zlib_stream.avail_in )
837 {
838 // All the input was consumed.
839 break;
840 }
841 }
842 }
843
846
848
853
856
858 std::string m_out_buffer;
860 std::size_t m_write_pos{ 0 };
861
863};
864
883
885inline std::string
886transform( string_view_t input, const params_t & params )
887{
888 zlib_t z{ params };
889 z.write( input );
890 z.complete();
891
892 return z.giveaway_output();
893}
894
895inline std::string
896deflate_compress( string_view_t input, int compression_level = -1 )
897{
898 return transform( input, make_deflate_compress_params( compression_level ) );
899}
900
901inline std::string
903{
904 return transform( input, make_deflate_decompress_params() );
905}
906
907inline std::string
908gzip_compress( string_view_t input, int compression_level = -1 )
909{
910 return transform( input, make_gzip_compress_params( compression_level ) );
911}
912
913inline std::string
915{
916 return transform( input, make_gzip_decompress_params() );
917}
919
920//
921// body_appender_t
922//
923
924template < typename Response_Output_Strategy >
926{
927 body_appender_t() = delete;
928};
929
930namespace impl
931{
932
935{
937 {
938 throw exception_t{ "operation is not copress" };
939 }
940}
941
943inline void ensure_valid_transforator( zlib_t * ztransformator )
944{
945 if( nullptr == ztransformator )
946 {
947 throw exception_t{ "invalid body appender" };
948 }
949}
950
953{
954 std::string result{ "identity" };
955
957 {
958 result.assign( "deflate" );
959 }
960 if( params_t::format_t::gzip == f )
961 {
962 result.assign( "gzip" );
963 }
964
965 return result;
966}
967
968} /* namespace impl */
969
970//
971// body_appender_base_t
972//
973
975template < typename Response_Output_Strategy, typename Descendant >
977{
978 public:
980
981 body_appender_base_t( const params_t & params, resp_t & resp )
982 : m_ztransformator{ std::make_unique< zlib_t >( params ) }
983 , m_resp{ resp }
984 {
986 m_ztransformator->params().operation() );
987
988 m_resp.append_header(
989 restinio::http_field::content_encoding,
991 m_ztransformator->params().format() ) );
992 }
993
997
999 : m_ztransformator{ std::move( ba.m_ztransformator ) }
1000 , m_resp{ ba.m_resp }
1001 {}
1002
1004
1005 protected:
1006 std::unique_ptr< zlib_t > m_ztransformator;
1008};
1009
1011template < typename X_Controlled_Output, typename Descendant >
1013 : public body_appender_base_t< X_Controlled_Output, Descendant >
1014{
1015 public:
1017
1018 using base_type_t::base_type_t;
1019
1021 Descendant &
1023 {
1025 this->m_ztransformator->write( input );
1026 return static_cast< Descendant & >( *this );
1027 }
1028
1030 void
1032 {
1034
1035 this->m_ztransformator->complete();
1036
1037 this->m_resp.append_body( this->m_ztransformator->giveaway_output() );
1038 }
1039};
1040
1063template <>
1066 restinio_controlled_output_t,
1067 body_appender_t< restinio_controlled_output_t > >
1068{
1069 public:
1074
1076 auto
1077 size() const
1078 {
1079 impl::ensure_valid_transforator( m_ztransformator.get() );
1080
1081 return m_ztransformator->output_size();
1082 }
1083
1084 using base_type_t::base_type_t;
1085};
1086
1110template <>
1113 user_controlled_output_t,
1114 body_appender_t< user_controlled_output_t > >
1115{
1116 public:
1121
1123
1124 auto &
1126 {
1127 impl::ensure_valid_transforator( m_ztransformator.get() );
1128 m_ztransformator->flush();
1129 m_resp
1130 .append_body( m_ztransformator->giveaway_output() )
1131 .flush();
1132
1133 return *this;
1134 }
1135};
1136
1137
1170template <>
1172 : public body_appender_base_t<
1173 chunked_output_t,
1174 body_appender_t< chunked_output_t > >
1175{
1176 public:
1181
1182 using base_type_t::base_type_t;
1183
1185
1189 auto &
1191 {
1192 impl::ensure_valid_transforator( m_ztransformator.get() );
1193
1194 m_ztransformator->write( input );
1195 return *this;
1196 }
1197
1200
1205 auto &
1207 {
1208 append( input ); // m_ztransformator is checked here.
1209
1210 m_ztransformator->flush(); // Flush already compressed data.
1211
1212 // Create a chunk with current output.
1213 m_resp.append_chunk( m_ztransformator->giveaway_output() );
1214
1215 return *this;
1216 }
1217
1220 void
1222 {
1223 impl::ensure_valid_transforator( m_ztransformator.get() );
1224
1225 if( !m_ztransformator->is_completed() )
1226 {
1227 m_ztransformator->flush();
1228 m_resp.append_chunk( m_ztransformator->giveaway_output() );
1229 }
1230
1231 m_resp.flush();
1232 }
1233
1235 void
1237 {
1238 impl::ensure_valid_transforator( m_ztransformator.get() );
1239 m_ztransformator->complete();
1240 m_resp.append_chunk( m_ztransformator->giveaway_output() );
1241 }
1242};
1243
1246template < typename Response_Output_Strategy >
1247body_appender_t< Response_Output_Strategy >
1250 const params_t & params )
1251{
1252 return body_appender_t< Response_Output_Strategy >{ params, resp };
1253}
1254
1257template < typename Response_Output_Strategy >
1258body_appender_t< Response_Output_Strategy >
1261 int compression_level = -1 )
1262{
1263 return body_appender( resp, make_deflate_compress_params( compression_level ) );
1264}
1265
1268template < typename Response_Output_Strategy >
1269inline body_appender_t< Response_Output_Strategy >
1272 int compression_level = -1 )
1273{
1274 return body_appender( resp, make_gzip_compress_params( compression_level ) );
1275}
1276
1279template < typename Response_Output_Strategy >
1280inline body_appender_t< Response_Output_Strategy >
1283 int = -1 )
1284{
1285 return body_appender( resp, make_identity_params() );
1286}
1287
1289
1323template < typename Extra_Data, typename Handler >
1324decltype(auto)
1327 Handler && handler )
1328{
1330
1331 const auto content_encoding =
1332 req.header().get_field_or( restinio::http_field::content_encoding, "identity" );
1333
1334 if( is_equal_caseless( content_encoding, "deflate" ) )
1335 {
1336 return handler( deflate_decompress( req.body() ) );
1337 }
1338 else if( is_equal_caseless( content_encoding, "gzip" ) )
1339 {
1340 return handler( gzip_decompress( req.body() ) );
1341 }
1342 else if( !is_equal_caseless( content_encoding, "identity" ) )
1343 {
1344 throw exception_t{
1345 fmt::format(
1346 RESTINIO_FMT_FORMAT_STRING( "content-encoding '{}' not supported" ),
1347 content_encoding )
1348 };
1349 }
1350
1351 return handler( req.body() );
1352}
1353
1354} /* namespace zlib */
1355
1356} /* namespace transforms */
1357
1358} /* namespace restinio */
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
const http_request_header_t & header() const noexcept
Get request header.
const std::string & body() const noexcept
Get request body.
std::string get_field_or(string_view_t field_name, string_view_t default_value) const
Get field value by field name or default value if the field not found.
Forbid arbitrary response_builder_t instantiations.
Base class for body appenders.
Definition: zlib.hpp:977
std::unique_ptr< zlib_t > m_ztransformator
Definition: zlib.hpp:1006
body_appender_base_t(body_appender_base_t &&ba) noexcept
Definition: zlib.hpp:998
body_appender_base_t(const body_appender_base_t &)=delete
body_appender_base_t(const params_t &params, resp_t &resp)
Definition: zlib.hpp:981
body_appender_base_t & operator=(const body_appender_base_t &)=delete
void flush()
Flushes currently available compressed data with possibly creating new chunk and then flushes target ...
Definition: zlib.hpp:1221
auto & append(string_view_t input)
Append data to be compressed.
Definition: zlib.hpp:1190
auto & make_chunk(string_view_t input=string_view_t{})
Append data to be compressed and adds current zlib transformator output as a new chunk.
Definition: zlib.hpp:1206
void complete()
Complete zlib transformation operation.
Definition: zlib.hpp:1236
Parameters of performing data transformation with zlib.
Definition: zlib.hpp:64
params_t && strategy(int strategy_value) &&
Set compression strategy.
Definition: zlib.hpp:304
params_t && window_bits(int window_bits_value) &&
Set window_bits.
Definition: zlib.hpp:212
params_t & window_bits(int window_bits_value) &
Set window_bits.
Definition: zlib.hpp:172
format_t format() const
Get format.
Definition: zlib.hpp:121
params_t()
Default constructor for identiry transformator.
Definition: zlib.hpp:110
params_t && level(int level_value) &&
Set compression level.
Definition: zlib.hpp:155
int window_bits() const
Get window_bits.
Definition: zlib.hpp:161
format_t
Formats of compressed data.
Definition: zlib.hpp:77
@ identity
Identity. With semantics descrobed here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Ac...
params_t & mem_level(int mem_level_value) &
Set compression mem_level.
Definition: zlib.hpp:234
operation_t m_operation
Transformation type.
Definition: zlib.hpp:345
int mem_level() const
Get compression mem_level.
Definition: zlib.hpp:221
int m_level
Compression level.
Definition: zlib.hpp:354
std::size_t m_reserve_buffer_size
Size initially reserved for buffer.
Definition: zlib.hpp:361
format_t m_format
Format to be used for compressed data.
Definition: zlib.hpp:348
params_t & reserve_buffer_size(std::size_t size) &
Set the size initially reserved for buffer.
Definition: zlib.hpp:321
int level() const
Get compression level.
Definition: zlib.hpp:127
std::size_t reserve_buffer_size() const
Get the size initially reserved for buffer.
Definition: zlib.hpp:317
params_t && reserve_buffer_size(std::size_t size) &&
Set the size initially reserved for buffer.
Definition: zlib.hpp:335
params_t(operation_t op, format_t f, int l=-1)
Init constructor.
Definition: zlib.hpp:96
params_t & level(int level_value) &
Set compression level.
Definition: zlib.hpp:136
params_t & reference_to_self()
Get the reference to self.
Definition: zlib.hpp:342
params_t && mem_level(int mem_level_value) &&
Set compression mem_level.
Definition: zlib.hpp:254
int strategy() const
Get compression strategy.
Definition: zlib.hpp:263
operation_t
Types of transformation.
Definition: zlib.hpp:68
operation_t operation() const
Get operation.
Definition: zlib.hpp:118
params_t & strategy(int strategy_value) &
Set compression strategy.
Definition: zlib.hpp:274
Base class for body appenders with restinio or user controlled output.
Definition: zlib.hpp:1014
Descendant & append(string_view_t input)
Append a piece of data to response.
Definition: zlib.hpp:1022
void complete()
Complete zlib transformation operation.
Definition: zlib.hpp:1031
Zlib transformator.
Definition: zlib.hpp:479
zlib_t(const zlib_t &)=delete
auto output_size() const
Get current output size.
Definition: zlib.hpp:695
bool m_zlib_stream_initialized
Flag: was m_zlib_stream initialized properly.
Definition: zlib.hpp:852
void inc_buffer()
Increment internal buffer for receiving output.
Definition: zlib.hpp:727
std::size_t m_write_pos
Next write pos in out buffer.
Definition: zlib.hpp:860
bool is_completed() const
Is operation complete?
Definition: zlib.hpp:698
void write(string_view_t input)
Append input data.
Definition: zlib.hpp:570
void write_compress_impl(int flush)
Handle incoming data for compression operation.
Definition: zlib.hpp:755
void flush()
Flush the zlib stream.
Definition: zlib.hpp:617
const char * get_error_msg() const
Get zlib error message if it exists.
Definition: zlib.hpp:708
void ensure_operation_in_not_completed() const
Checks completion flag and throws if operation is is already completed.
Definition: zlib.hpp:719
auto prepare_out_buffer()
Prepare out buffer for receiving data.
Definition: zlib.hpp:735
const params_t m_params
Parameters for zlib.
Definition: zlib.hpp:845
std::string m_out_buffer
Output buffer.
Definition: zlib.hpp:858
zlib_t(const params_t &transformation_params)
Definition: zlib.hpp:481
std::string giveaway_output()
Get current accumulated output data.
Definition: zlib.hpp:684
void write_decompress_impl(int flush)
Handle incoming data for decompression operation.
Definition: zlib.hpp:805
void complete()
Complete the stream.
Definition: zlib.hpp:639
zlib_t & operator=(const zlib_t &)=delete
const params_t & params() const
Get parameters of current transformation.
Definition: zlib.hpp:562
z_stream m_zlib_stream
zlib stream.
Definition: zlib.hpp:855
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:1076
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:763
A special wrapper around fmtlib include files.
#define RESTINIO_FMT_FORMAT_STRING(s)
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: inflate.c:622
int ZEXPORT inflateEnd(z_streamp strm)
Definition: inflate.c:1277
bool is_equal_caseless(const char *a, const char *b, std::size_t size) noexcept
Comparator for fields names.
std::string content_encoding_token(params_t::format_t f)
Get token for copression format.
Definition: zlib.hpp:952
void ensure_is_compression_operation(params_t::operation_t op)
Check if operation is a copression, and throw if it is not.
Definition: zlib.hpp:934
void ensure_valid_transforator(zlib_t *ztransformator)
Check if a pointer to zlib transformator is valid.
Definition: zlib.hpp:943
params_t make_gzip_compress_params(int compression_level=-1)
Definition: zlib.hpp:401
params_t make_gzip_decompress_params()
Definition: zlib.hpp:410
std::string gzip_decompress(string_view_t input)
Definition: zlib.hpp:914
constexpr int default_mem_level
Definition: zlib.hpp:47
std::string transform(string_view_t input, const params_t &params)
Do a specified zlib transformation.
Definition: zlib.hpp:886
params_t make_deflate_compress_params(int compression_level=-1)
Definition: zlib.hpp:384
constexpr int default_strategy
Definition: zlib.hpp:48
std::string deflate_decompress(string_view_t input)
Definition: zlib.hpp:902
decltype(auto) handle_body(const generic_request_t< Extra_Data > &req, Handler &&handler)
Call a handler over a request body.
Definition: zlib.hpp:1325
std::string deflate_compress(string_view_t input, int compression_level=-1)
Definition: zlib.hpp:896
body_appender_t< Response_Output_Strategy > deflate_body_appender(response_builder_t< Response_Output_Strategy > &resp, int compression_level=-1)
Create body appender with deflate transformation and a given compression level.
Definition: zlib.hpp:1259
body_appender_t< Response_Output_Strategy > gzip_body_appender(response_builder_t< Response_Output_Strategy > &resp, int compression_level=-1)
Create body appender with gzip transformation and a given compression level.
Definition: zlib.hpp:1270
constexpr std::size_t default_output_reserve_buffer_size
Default reserve buffer size for zlib transformator.
Definition: zlib.hpp:38
body_appender_t< Response_Output_Strategy > identity_body_appender(response_builder_t< Response_Output_Strategy > &resp, int=-1)
Create body appender with gzip transformation and a given compression level.
Definition: zlib.hpp:1281
params_t make_identity_params()
Definition: zlib.hpp:418
std::string gzip_compress(string_view_t input, int compression_level=-1)
Definition: zlib.hpp:908
body_appender_t< Response_Output_Strategy > body_appender(response_builder_t< Response_Output_Strategy > &resp, const params_t &params)
Create body appender with given zlib transformation parameters.
Definition: zlib.hpp:1248
params_t make_deflate_decompress_params()
Definition: zlib.hpp:393
constexpr int default_window_bits
Definition: zlib.hpp:46
nonstd::string_view string_view_t
Definition: string_view.hpp:19
STL namespace.
Helpers for caseless comparison of strings.
Tag type for chunked output response builder.
Tag type for RESTinio controlled output response builder.
Tag type for user controlled output response builder.
uInt avail_in
Definition: zlib.h:88
alloc_func zalloc
Definition: zlib.h:98
uInt avail_out
Definition: zlib.h:92
z_const Bytef * next_in
Definition: zlib.h:87
free_func zfree
Definition: zlib.h:99
voidpf opaque
Definition: zlib.h:100
Bytef * next_out
Definition: zlib.h:91
z_const char * msg
Definition: zlib.h:95
unsigned int uInt
Definition: zconf.h:393
#define MAX_MEM_LEVEL
Definition: zconf.h:260
#define MAX_WBITS
Definition: zconf.h:270
Byte FAR Bytef
Definition: zconf.h:400
#define Z_HUFFMAN_ONLY
Definition: zlib.h:197
#define Z_DEFLATED
Definition: zlib.h:209
#define Z_BUF_ERROR
Definition: zlib.h:184
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:200
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
Definition: zlib.h:1797
#define inflateInit2(strm, windowBits)
Definition: zlib.h:1800
#define Z_STREAM_END
Definition: zlib.h:178
#define Z_FINISH
Definition: zlib.h:172
#define Z_OK
Definition: zlib.h:177
#define Z_SYNC_FLUSH
Definition: zlib.h:170
#define Z_NO_FLUSH
Definition: zlib.h:168
#define Z_NULL
Definition: zlib.h:212
#define Z_FILTERED
Definition: zlib.h:196
#define Z_RLE
Definition: zlib.h:198