Skip to content

Commit 91bb3c7

Browse files
simbabqueoalders
authored andcommitted
Support for Brotli encoding
This commit adds encoding to the code written by trizen with #164, which added decoding. It also adds tests. Also makes sure recommended modules are installed during the Linux github action build, which we were already doing on Mac. Closes #163.
1 parent b03c67b commit 91bb3c7

5 files changed

Lines changed: 59 additions & 1 deletion

File tree

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ jobs:
6969
cpm install -g
7070
--cpanfile cpanfile
7171
--with-develop
72+
--with-recommends
7273
--with-suggests
7374
--show-build-log-on-failure
7475
- name: Run Tests

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Revision history for HTTP-Message
22

33
{{$NEXT}}
4+
- Support for Brotli "br" encoding (GH#163) (trizen and Julien Fiegehenn)
45
- Don't test Perl > 5.32 on Windows in GH Actions (GH#174) (Olaf Alders)
56

67
6.36 2022-01-05 14:39:42Z

dist.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Encode = 3.01
1515
Encode::Locale = 1
1616
Exporter = 5.57
1717
HTTP::Date = 6
18+
IO::Compress::Brotli = 0.004001
1819
IO::Compress::Bzip2 = 2.021
20+
IO::Uncompress::Brotli = 0.004001
1921
IO::Uncompress::Bunzip2 = 2.021
2022
LWP::MediaTypes = 6
2123
MIME::Base64 = 2.1
@@ -39,6 +41,12 @@ to_relationship = suggests
3941
copy_to = develop.requires
4042
module = Clone
4143

44+
[Prereqs::Soften / Brotli]
45+
to_relationship = recommends
46+
copy_to = test.recommends
47+
module = IO::Compress::Brotli
48+
module = IO::Uncompress::Brotli
49+
4250
[Test::Compile]
4351
bail_out_on_fail = 1
4452
xt_mode = 1

lib/HTTP/Message.pm

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,13 @@ sub encode
503503
or die "Can't bzip2 content: $IO::Compress::Bzip2::Bzip2Error";
504504
$content = $output;
505505
}
506+
elsif ($encoding eq "br") {
507+
require IO::Compress::Brotli;
508+
my $output;
509+
eval { $output = IO::Compress::Brotli::bro($content) }
510+
or die "Can't brotli content: $@";
511+
$content = $output;
512+
}
506513
elsif ($encoding eq "rot13") { # for the fun of it
507514
$content =~ tr/A-Za-z/N-ZA-Mn-za-m/;
508515
}
@@ -991,7 +998,7 @@ want to process its content as a string.
991998
Apply the given encodings to the content of the message. Returns TRUE
992999
if successful. The "identity" (non-)encoding is always supported; other
9931000
currently supported encodings, subject to availability of required
994-
additional modules, are "gzip", "deflate", "x-bzip2" and "base64".
1001+
additional modules, are "gzip", "deflate", "x-bzip2", "base64" and "br".
9951002
9961003
A successful call to this function will set the C<Content-Encoding>
9971004
header.

t/message-brotli.t

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#! perl -w
2+
3+
use strict;
4+
use warnings;
5+
6+
use Test::More;
7+
use Test::Needs 'IO::Compress::Brotli', 'IO::Uncompress::Brotli';
8+
9+
require HTTP::Message;
10+
11+
subtest "decoding" => sub {
12+
13+
my $m = HTTP::Message->new(
14+
[
15+
"Content-Type" => "text/plain",
16+
"Content-Encoding" => "br, base64",
17+
],
18+
"CwaASGVsbG8gd29ybGQhCgM=\n"
19+
);
20+
is( $m->decoded_content, "Hello world!\n", "decoded_content() works" );
21+
ok( $m->decode, "decode() works" );
22+
is( $m->content, "Hello world!\n", "... and content() is correct" );
23+
};
24+
25+
subtest "encoding" => sub {
26+
my $m = HTTP::Message->new(
27+
[
28+
"Content-Type" => "text/plain",
29+
],
30+
"Hello world!"
31+
);
32+
ok( $m->encode("br"), "set encoding to 'br" );
33+
is( $m->header("Content-Encoding"),
34+
"br", "... and Content-Encoding is set" );
35+
isnt( $m->content, "Hello world!", "... and the content has changed" );
36+
is( $m->decoded_content, "Hello world!", "decoded_content() works" );
37+
ok( $m->decode, "decode() works" );
38+
is( $m->content, "Hello world!", "... and content() is correct" );
39+
};
40+
41+
done_testing;

0 commit comments

Comments
 (0)