diff --git a/dist.ini b/dist.ini index 113652a3..c0a01f19 100644 --- a/dist.ini +++ b/dist.ini @@ -18,6 +18,7 @@ HTTP::Date = 6 IO::Compress::Brotli = 0.004001 IO::Compress::Bzip2 = 2.021 IO::Uncompress::Brotli = 0.004001 +IO::Compress::Zstd = 2.214 LWP::MediaTypes = 6 MIME::Base64 = 2.1 perl = 5.008001 diff --git a/lib/HTTP/Message.pm b/lib/HTTP/Message.pm index ede3eefb..2152529e 100644 --- a/lib/HTTP/Message.pm +++ b/lib/HTTP/Message.pm @@ -7,6 +7,8 @@ our $VERSION = '7.02'; require HTTP::Headers; require Carp; +use Module::Load (); +use Module::Load::Conditional (); our $MAXIMUM_BODY_SIZE; @@ -354,6 +356,25 @@ sub decoded_content $content_ref = \$output; $content_ref_iscopy++; } + elsif ($ce eq 'zstd') { + Module::Load::load('IO::Uncompress::UnZstd'); + my $buffer; + my $z; + if( defined $content_limit ) { + $z = IO::Uncompress::UnZstd->new( $content_ref, InputLength => $content_limit, Append => 1, Strict => 1, ) + or Carp::croak "IO::Compress::Zstd->new failed: $IO::Uncompress::UnZstd::UnZstdError\n"; + } else { + $z = IO::Uncompress::UnZstd->new( $content_ref, Append => 1, Strict => 1, ) + or Carp::croak "IO::Compress::Zstd->new failed: $IO::Uncompress::UnZstd::UnZstdError\n"; + } + my $status; + while( $status = $z->read($buffer) > 0 ) { } + if( $status < 0 ) { + Carp::croak "IO::Compress::Zstd::read failed: $IO::Uncompress::UnZstd::UnZstdError\n"; + } + $content_ref = \$buffer; + $content_ref_iscopy++; + } elsif ($ce eq "x-bzip2" or $ce eq "bzip2") { require Compress::Raw::Bzip2; @@ -509,6 +530,9 @@ sub decodable require IO::Uncompress::Brotli; push(@enc, 'br'); }; + if( Module::Load::Conditional::check_install( module => 'IO::Compress::Zstd') ) { + push(@enc, "zstd"); + } # we don't care about announcing the 'identity', 'base64' and # 'quoted-printable' stuff return wantarray ? @enc : join(", ", @enc); @@ -577,6 +601,16 @@ sub encode elsif ($encoding eq "rot13") { # for the fun of it $content =~ tr/A-Za-z/N-ZA-Mn-za-m/; } + elsif ($encoding eq 'zstd') { + Module::Load::load('IO::Compress::Zstd'); + my $output; + my $z = IO::Compress::Zstd->new( \$output, Level => 3, Append => 0, Strict => 1, ) + or Carp::croak "IO::Compress::Zstd failed: $IO::Compress::Zstd::ZstdError\n"; + $z->write($content) + or Carp::croak "IO::Compress::Zstd::write failed: $IO::Compress::Zstd::ZstdError\n"; + $z->flush(); + $content = $output; + } else { return 0; } @@ -1062,7 +1096,14 @@ want to process its content as a string. Apply the given encodings to the content of the message. Returns TRUE if successful. The "identity" (non-)encoding is always supported; other currently supported encodings, subject to availability of required -additional modules, are "gzip", "deflate", "x-bzip2", "base64" and "br". +additional modules, are "gzip", "deflate", "x-bzip2", "br", "zstd" +and "base64". + +N.B. Base64 encoding is not in L. +It is HTTP::Message specific option. +You can use it to ensure that content is always ASCII, and console +friendly. A successful call to this function will set the C header. diff --git a/t/files/lorem_ipsum.txt b/t/files/lorem_ipsum.txt new file mode 100644 index 00000000..123c7903 --- /dev/null +++ b/t/files/lorem_ipsum.txt @@ -0,0 +1,9 @@ +The standard Lorem Ipsum passage, used since the 1500s + +"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC + +"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" +1914 translation by H. Rackham + +"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" diff --git a/t/files/lorem_ipsum.txt-zst-b64 b/t/files/lorem_ipsum.txt-zst-b64 new file mode 100644 index 00000000..30714ec4 --- /dev/null +++ b/t/files/lorem_ipsum.txt-zst-b64 @@ -0,0 +1,22 @@ +KLUv/QBYnCUAylzEDSMwT5M2aLa7+3tLAlbpt5uID1xhy0abYUBaMxb+GfgGDqqywM0A0gDWALe0 +OZexMibPeHtt48XMQ4IrtLlOa3kh1HiuazTjbakROnPbtfMhe9y4eGmUj+OdT+2gKaaL6fFu2no3 +7vpeI7iCqx15MqU0L/uDfTnu3x5+L00qhPqYx8Vn1B7sp5U38XvteJLq0B5Ph5d+6unQzhSuqHYc +qhc2Kt3YbpqLQuc+Y587Z/CuuSTjn0zbZ8/zY/uED9kbeGZvuVPXZmNji2mesKU6314T5bkEArEB +AhRYuCl8M3j7Y9tPrFG7tTz1LgyeTLPBj9bB4r0pEwL7ZGy6ad/TZux2cNf5Tp67UG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LYPSjwgeF5 +9+j/MIhCavNMJhdGuq6mJ3Qonpe2/WynN7SA0l3FW6DdfITxAJMOxod1OqtldeimsZLIFmq53wMj +fheeNAP4rW8RSQXW6a8ufvg+ElVcppzpxMeRAi7j1wDHPGNnyWAorgmAHeNqY7bcOt/goQKl+FNk +IIrbirHSJaV5AaGXb55/ diff --git a/t/message-zstd.t b/t/message-zstd.t new file mode 100644 index 00000000..e89b7fba --- /dev/null +++ b/t/message-zstd.t @@ -0,0 +1,98 @@ +#! perl + +use strict; +use warnings; + +use English qw( -no_match_vars ); + +use Test::More; +use Test::Needs 'IO::Compress::Zstd'; +use Path::Tiny qw( path ); + +require MIME::Base64; +require HTTP::Message; + +my $files = path($PROGRAM_NAME)->parent->child('files'); +my $lorem_ipsum_clear = $files->child('lorem_ipsum.txt')->slurp_utf8; +my $lorem_ipsum_zstd_b64 = $files->child('lorem_ipsum.txt-zst-b64')->slurp_raw; +my $lorem_ipsum_zstd = MIME::Base64::decode($lorem_ipsum_zstd_b64); + +subtest "no decoding" => sub { + + my $m = HTTP::Message->new( + [ + "Content-Type" => "text/plain", + "Content-Encoding" => "", + ], + $lorem_ipsum_clear + ); + is( $m->decoded_content, $lorem_ipsum_clear, "decoded_content() works, is same as content" ); + ok( $m->decode, "decode() works" ); + is( $m->content, $lorem_ipsum_clear, "... and content() is correct" ); +}; + +subtest "decoding zstd" => sub { + + my $m = HTTP::Message->new( + [ + "Content-Type" => "text/plain", + "Content-Encoding" => "zstd", + ], + $lorem_ipsum_zstd + ); + is( $m->decoded_content, $lorem_ipsum_clear, "decoded_content() works" ); + ok( $m->decode, "decode() works" ); + is( $m->content, $lorem_ipsum_clear, "... and content() is correct" ); +}; + +subtest "decoding zstd in base64" => sub { + + my $m = HTTP::Message->new( + [ + "Content-Type" => "text/plain", + "Content-Encoding" => "zstd, base64", + ], + $lorem_ipsum_zstd_b64 + ); + is( $m->decoded_content, $lorem_ipsum_clear, "decoded_content() works" ); + ok( $m->decode, "decode() works" ); + is( $m->content, $lorem_ipsum_clear, "... and content() is correct" ); +}; + +subtest "encoding to zstd" => sub { + my $m = HTTP::Message->new( + [ + "Content-Type" => "text/plain", + ], + $lorem_ipsum_clear + ); + is( $m->content, $lorem_ipsum_clear, "the content is the original" ); + ok( $m->encode("zstd"), "set encoding to 'zstd'" ); + is( $m->header("Content-Encoding"), + "zstd", "... and Content-Encoding is set" ); + isnt( $m->content, $lorem_ipsum_clear, "... and the content has changed" ); + is( $m->content, $lorem_ipsum_zstd, "... and the content is correct" ); + is( $m->decoded_content, $lorem_ipsum_clear, "decoded_content() works" ); + ok( $m->decode, "decode() works" ); + is( $m->content, $lorem_ipsum_clear, "... and content() is correct" ); +}; + +subtest "encoding to zstd in base64" => sub { + my $m = HTTP::Message->new( + [ + "Content-Type" => "text/plain", + ], + $lorem_ipsum_clear + ); + is( $m->content, $lorem_ipsum_clear, "the content is the original" ); + ok( $m->encode("zstd", "base64"), "set encoding to 'zstd' in 'base64'" ); + is( $m->header("Content-Encoding"), + "zstd, base64", "... and Content-Encoding is set" ); + isnt( $m->content, $lorem_ipsum_clear, "... and the content has changed" ); + is( $m->content, $lorem_ipsum_zstd_b64, "... and the content is correct" ); + is( $m->decoded_content, $lorem_ipsum_clear, "decoded_content() works" ); + ok( $m->decode, "decode() works" ); + is( $m->content, $lorem_ipsum_clear, "... and content() is correct" ); +}; + +done_testing;