Message.pm

filter: Statementstmt 98.7% 463 / 469 Branchbran 91.9% 298 / 324 Conditioncond 87.0% 87 / 100 MC/DCmcdc 74.5% 44 / 59 Subroutinesub 97.3% 37 / 38 Podpod 90.9% 20 / 22 totaltotal 93.7% 949 / 1012 CC 201 SCAR 33.0
CC
201
Cov
95%
decoded_content
67.4
content_charset
30.0
encode
20.1
CRAP 27.1
↑ Summary Request/Common.pm »
1 package HTTP::Message;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 our $VERSION = '7.04';
7  
8 17 require HTTP::Headers;
9 17 require Carp;
10  
11 17 our $MAXIMUM_BODY_SIZE;
12  
13 17 my $CRLF = "\015\012"; # "\r\n" is not portable
14 17 ▶unless ($HTTP::URI_CLASS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($HTTP::URI_CLASS) 17 0 17
15 17 ▶ if ($ENV{PERL_HTTP_URI_CLASS}
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'PERL_HTTP_URI_CLASS'} and $ENV{'PERL_HTTP_URI_CLASS'} =~ /^([\w:]+)$/) { } 0 17 17
Condition33%1/3
$ENV{"PERL_HTTP_URI_CLASS"} and $ENV{"PERL_HTTP_URI_CLASS"} =~ /^([\w:]+)$/
!ll&&!rl&&r
1700
MC/DC0%0/2
$ENV{"PERL_HTTP_URI_CLASS"} and $ENV{"PERL_HTTP_URI_CLASS"} =~ /^([\w:]+)$/
$ENV{"PERL_HTTP_URI_CLASS"} $ENV{"PERL_HTTP_URI_CLASS"} =~ /^([\w:]+)$/
Truth table33%1/3
A and B
A= $ENV{"PERL_HTTP_URI_CLASS"}B= $ENV{"PERL_HTTP_URI_CLASS"} =~ /^([\w:]+)$/
ABresult
0X0
100
111
16 && $ENV{PERL_HTTP_URI_CLASS} =~ /^([\w:]+)$/) {
17 0 $HTTP::URI_CLASS = $1;
18 } else {
19 17 $HTTP::URI_CLASS = "URI";
20 }
21 }
22 17 ▶eval "require $HTTP::URI_CLASS"; die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 17 17
23  
24 *_utf8_downgrade = defined(&utf8::downgrade) ?
25 231 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
26 231 ▶ utf8::downgrade($_[0], 1) or
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($_[0], 1) or Carp::croak("HTTP::Message content must be bytes") 228 3 231
27 Carp::croak("HTTP::Message content must be bytes")
28 }
29 :
30 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
31 17 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
defined &utf8::downgrade ? : 17 0 17
32  
33 sub new
34 190 ▶{
Subroutine new: called CC=5 SCAR=16.1
Pod documented
35 190 my($class, $header, $content) = @_;
36 190 ▶ if (defined $header) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header) { } 102 88 190
37 102 ▶ Carp::croak("Bad header argument") unless ref $header;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $header 1 101 102
38 101 ▶ if (ref($header) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $header eq 'ARRAY') { } 79 22 101
39 79 $header = HTTP::Headers->new(@$header);
40 }
41 else {
42 22 $header = $header->clone;
43 }
44 }
45 else {
46 88 $header = HTTP::Headers->new;
47 }
48 189 ▶ if (defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) { } 97 92 189
49 97 _utf8_downgrade($content);
50 }
51 else {
52 92 $content = '';
53 }
54  
55 188 bless {
56 '_headers' => $header,
57 '_content' => $content,
58 '_max_body_size' => $HTTP::Message::MAXIMUM_BODY_SIZE,
59 }, $class;
60 }
61  
62 sub parse
63 47 ▶{
Subroutine parse: called CC=4 SCAR=13.9
Pod documented
64 47 my($class, $str) = @_;
65  
66 47 my @hdr;
67 47 while (1) {
68 81 ▶ if ($str =~ s/^([^\s:]+)[ \t]*: ?(.*)\n?//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ s/^([^\s:]+)[ \t]*: ?(.*)\n?//) { } 29 52 81
elsif (@hdr and $str =~ s/^([ \t].*)\n?//) { } 5 47 52
69 29 push(@hdr, $1, $2);
70 29 $hdr[-1] =~ s/\r\z//;
71 }
72 ▶ elsif (@hdr && $str =~ s/^([ \t].*)\n?//) {
Condition100%3/3
@hdr and $str =~ s/^([ \t].*)\n?//
!ll&&!rl&&r
29185
MC/DC100%2/2
@hdr and $str =~ s/^([ \t].*)\n?//
@hdr $str =~ s/^([ \t].*)\n?//
Truth table100%3/3
A and B
A= @hdrB= $str =~ s/^([ \t].*)\n?//
ABresult
0X0
100
111
73 5 $hdr[-1] .= "\n$1";
74 5 $hdr[-1] =~ s/\r\z//;
75 }
76 else {
77 47 $str =~ s/^\r?\n//;
78 47 last;
79 }
80 }
81 47 local $HTTP::Headers::TRANSLATE_UNDERSCORE;
82 47 new($class, \@hdr, $str);
83 }
84  
85  
86 sub clone
87 8 ▶{
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
88 8 my $self = shift;
89 8 my $clone = HTTP::Message->new($self->headers,
90 $self->content);
91 8 $clone->protocol($self->protocol);
92 8 $clone;
93 }
94  
95  
96 4 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
97 4 my $self = shift;
98 4 $self->{_headers}->clear;
99 4 $self->content("");
100 4 delete $self->{_parts};
101 4 return;
102 }
103  
104  
105 79 ▶sub protocol {
Subroutine protocol: called CC=1 SCAR=0.0
Pod documented
106 79 shift->_elem('_protocol', @_);
107 }
108  
109 1153 ▶sub headers {
Subroutine headers: called CC=2 SCAR=6.9
Pod documented
110 1153 my $self = shift;
111  
112 # recalculation of _content might change headers, so we
113 # need to force it now
114 1153 ▶ $self->_content unless exists $self->{_content};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_content"} 3 1150 1153
115  
116 1153 $self->{_headers};
117 }
118  
119 3 ▶sub headers_as_string {
Subroutine headers_as_string: called CC=1 SCAR=0.0
Pod documented
120 3 shift->headers->as_string(@_);
121 }
122  
123  
124 354 ▶sub content {
Subroutine content: called CC=7 SCAR=19.5
Pod documented
125  
126 354 my $self = $_[0];
127 354 ▶ if (defined(wantarray)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 246 108 354
128 246 ▶ $self->_content unless exists $self->{_content};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_content"} 6 240 246
129 246 my $old = $self->{_content};
130 246 ▶ $old = $$old if ref($old) eq "SCALAR";
Branch: 1 branch
BranchTrueFalseTotal
if ref $old eq "SCALAR" 5 241 246
131 246 ▶ &_set_content if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 6 240 246
132 246 return $old;
133 }
134  
135 108 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 106 2 108
136 106 &_set_content;
137 }
138 else {
139 2 ▶ Carp::carp("Useless content call in void context") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 1 1 2
140 }
141 }
142  
143  
144 122 ▶sub _set_content {
Subroutine _set_content: called CC=7 SCAR=19.5
145 122 my $self = $_[0];
146 122 _utf8_downgrade($_[1]);
147 121 ▶ if (!ref($_[1]) && ref($self->{_content}) eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[1] and ref $self->{'_content'} eq 'SCALAR') { } 3 118 121
Condition100%3/3
not ref $_[1] and ref $self->{"_content"} eq "SCALAR"
!ll&&!rl&&r
41143
MC/DC100%2/2
not ref $_[1] and ref $self->{"_content"} eq "SCALAR"
not ref $_[1] ref $self->{"_content"} eq "SCALAR"
Truth table100%3/3
not(A) and B
A= not ref $_[1]B= ref $self->{"_content"} eq "SCALAR"
ABresult
0X0
100
111
148 3 ▶ ${$self->{_content}} = defined( $_[1] ) ? $_[1] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 3 0 3
149 }
150 else {
151 118 ▶ die "Can't set content to be a scalar reference" if ref($_[1]) eq "SCALAR";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "SCALAR" 1 117 118
152 117 ▶ $self->{_content} = defined( $_[1] ) ? $_[1] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 115 2 117
153 117 delete $self->{_content_ref};
154 }
155 119 ▶ delete $self->{_parts} unless $_[2];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[2] 109 10 119
156 }
157  
158  
159 sub add_content
160 12 ▶{
Subroutine add_content: called CC=5 SCAR=16.1
Pod documented
161 12 my $self = shift;
162 12 ▶ $self->_content unless exists $self->{_content};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_content"} 0 12 12
163 12 my $chunkref = \$_[0];
164 12 ▶ $chunkref = $$chunkref if ref($$chunkref); # legacy
Branch: 1 branch
BranchTrueFalseTotal
if ref $$chunkref 2 10 12
165  
166 12 _utf8_downgrade($$chunkref);
167  
168 11 my $ref = ref($self->{_content});
169 11 ▶ if (!$ref) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ref) { } 9 2 11
elsif ($ref eq 'SCALAR') { } 1 1 2
170 9 $self->{_content} .= $$chunkref;
171 }
172 elsif ($ref eq "SCALAR") {
173 1 ${$self->{_content}} .= $$chunkref;
174 }
175 else {
176 1 Carp::croak("Can't append to $ref content");
177 }
178 10 delete $self->{_parts};
179 }
180  
181 2 ▶sub add_content_utf8 {
Subroutine add_content_utf8: called CC=1 SCAR=0.0
Pod documented
182 2 my($self, $buf) = @_;
183 2 utf8::upgrade($buf);
184 2 utf8::encode($buf);
185 2 $self->add_content($buf);
186 }
187  
188 sub content_ref
189 140 ▶{
Subroutine content_ref: called CC=5 SCAR=16.1
Pod documented
190 140 my $self = shift;
191 140 ▶ $self->_content unless exists $self->{_content};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_content"} 0 140 140
192 140 delete $self->{_parts};
193 140 my $old = \$self->{_content};
194 140 my $old_cref = $self->{_content_ref};
195 140 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 134 140
196 6 my $new = shift;
197 6 ▶ Carp::croak("Setting content_ref to a non-ref") unless ref($new);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $new 1 5 6
198 5 delete $self->{_content}; # avoid modifying $$old
199 5 $self->{_content} = $new;
200 5 $self->{_content_ref}++;
201 }
202 139 ▶ $old = $$old if $old_cref;
Branch: 1 branch
BranchTrueFalseTotal
if $old_cref 6 133 139
203 139 return $old;
204 }
205  
206  
207 sub content_charset
208 53 ▶{
Subroutine content_charset: called CC=30 SCAR=34.0
Pod documented
209 53 my $self = shift;
210 53 ▶ if (my $charset = $self->content_type_charset) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $charset = $self->content_type_charset) 1 52 53
211 1 return $charset;
212 }
213  
214 # time to start guessing
215 52 my $cref = $self->decoded_content(ref => 1, charset => "none");
216  
217 # Unicode BOM
218 52 for ($$cref) {
219 52 ▶ return "UTF-8" if /^\xEF\xBB\xBF/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\xEF\xBB\xBF/ 2 50 52
220 50 ▶ return "UTF-32LE" if /^\xFF\xFE\x00\x00/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\xFF\xFE\x00\x00/ 1 49 50
221 49 ▶ return "UTF-32BE" if /^\x00\x00\xFE\xFF/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\x00\x00\xFE\xFF/ 1 48 49
222 48 ▶ return "UTF-16LE" if /^\xFF\xFE/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\xFF\xFE/ 4 44 48
223 44 ▶ return "UTF-16BE" if /^\xFE\xFF/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\xFE\xFF/ 1 43 44
224 }
225  
226 43 ▶ if ($self->content_is_xml) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->content_is_xml) { } 15 28 43
elsif ($self->content_is_html) { } 4 24 28
elsif ($self->content_type eq 'application/json') { } 6 18 24
227 # http://www.w3.org/TR/2006/REC-xml-20060816/#sec-guessing
228 # XML entity not accompanied by external encoding information and not
229 # in UTF-8 or UTF-16 encoding must begin with an XML encoding declaration,
230 # in which the first characters must be '<?xml'
231 15 for ($$cref) {
232 15 ▶ return "UTF-32BE" if /^\x00\x00\x00</;
Branch: 1 branch
BranchTrueFalseTotal
if /^\x00\x00\x00</ 1 14 15
233 14 ▶ return "UTF-32LE" if /^<\x00\x00\x00/;
Branch: 1 branch
BranchTrueFalseTotal
if /^<\x00\x00\x00/ 1 13 14
234 13 ▶ return "UTF-16BE" if /^(?:\x00\s)*\x00</;
Branch: 1 branch
BranchTrueFalseTotal
if /^(?:\x00\s)*\x00</ 1 12 13
235 12 ▶ return "UTF-16LE" if /^(?:\s\x00)*<\x00/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(?:\s\x00)*<\x00/ 1 11 12
236 11 ▶ if (/^\s*(<\?xml[^\x00]*?\?>)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(<\?xml[^\x00]*?\?>)/) 4 7 11
237 4 ▶ if ($1 =~ /\sencoding\s*=\s*(["'])(.*?)\1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 =~ /\sencoding\s*=\s*(["'])(.*?)\1/) 4 0 4
238 4 my $enc = $2;
239 4 $enc =~ s/^\s+//; $enc =~ s/\s+\z//;
240 4 ▶ return $enc if $enc;
Branch: 1 branch
BranchTrueFalseTotal
if $enc 3 1 4
241 }
242 }
243 }
244 8 return "UTF-8";
245 }
246 elsif ($self->content_is_html) {
247 # look for <META charset="..."> or <META content="...">
248 # http://dev.w3.org/html5/spec/Overview.html#determining-the-character-encoding
249 4 require IO::HTML;
250 # Use relaxed search to match previous versions of HTTP::Message:
251 4 my $encoding = IO::HTML::find_charset_in($$cref, { encoding => 1,
252 need_pragma => 0 });
253 4 ▶ return $encoding->mime_name if $encoding;
Branch: 1 branch
BranchTrueFalseTotal
if $encoding 3 1 4
254 }
255 elsif ($self->content_type eq "application/json") {
256 6 for ($$cref) {
257 # RFC 4627, ch 3
258 6 ▶ return "UTF-32BE" if /^\x00\x00\x00./s;
Branch: 1 branch
BranchTrueFalseTotal
if /^\x00\x00\x00./s 1 5 6
259 5 ▶ return "UTF-32LE" if /^.\x00\x00\x00/s;
Branch: 1 branch
BranchTrueFalseTotal
if /^.\x00\x00\x00/s 2 3 5
260 3 ▶ return "UTF-16BE" if /^\x00.\x00./s;
Branch: 1 branch
BranchTrueFalseTotal
if /^\x00.\x00./s 1 2 3
261 2 ▶ return "UTF-16LE" if /^.\x00.\x00/s;
Branch: 1 branch
BranchTrueFalseTotal
if /^.\x00.\x00/s 1 1 2
262 1 return "UTF-8";
263 }
264 }
265 19 ▶ if ($self->content_type =~ /^text\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content_type =~ m[^text/]) 18 1 19
266 18 for ($$cref) {
267 18 ▶ if (length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 17 1 18
268 17 ▶ return "US-ASCII" unless /[\x80-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
unless /[\x80-\xFF]/ 14 3 17
269 3 require Encode;
270 3 eval {
271 3 Encode::decode_utf8($_, Encode::FB_CROAK() | Encode::LEAVE_SRC());
272 };
273 3 ▶ return "UTF-8" unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 2 1 3
274 1 return "ISO-8859-1";
275 }
276 }
277 }
278  
279 2 return undef;
280 }
281  
282 138 ▶sub max_body_size {
Subroutine max_body_size: called CC=2 SCAR=6.9
Pod undocumented
283 138 my $self = $_[0];
284 138 my $old = $self->{_max_body_size};
285 138 ▶ $self->_set_max_body_size($_[1]) if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 7 131 138
286 138 return $old;
287 }
288  
289 7 ▶sub _set_max_body_size {
Subroutine _set_max_body_size: called CC=1 SCAR=0.0
290 7 my $self = $_[0];
291 7 $self->{_max_body_size} = $_[1];
292 }
293  
294 sub decoded_content
295 120 ▶{
Subroutine decoded_content: called CC=65 SCAR=42.1
Pod documented
296 120 my($self, %opt) = @_;
297 120 my $content_ref;
298 my $content_ref_iscopy;
299  
300 120 eval {
301 120 $content_ref = $self->content_ref;
302 120 ▶ die "Can't decode ref content" if ref($content_ref) ne "SCALAR";
Branch: 1 branch
BranchTrueFalseTotal
if ref $content_ref ne "SCALAR" 0 120 120
303  
304 my $content_limit = exists $opt{ max_body_size } ? $opt{ max_body_size }
305 120 ▶ : defined $self->max_body_size ? $self->max_body_size
Branch: 2 branches
BranchTrueFalseTotal
exists $opt{'max_body_size'} ? : 3 117 120
defined $self->max_body_size ? : 5 112 117
306 : undef
307 ;
308 120 my %limiter_options;
309 120 ▶ if( defined $content_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_limit) 8 112 120
310 8 %limiter_options = (LimitOutput => 1, Bufsize => $content_limit);
311 };
312 120 ▶ if (my $h = $self->header("Content-Encoding")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $self->header("Content-Encoding")) 62 58 120
313 62 $h =~ s/^\s+//;
314 62 $h =~ s/\s+$//;
315 62 for my $ce (reverse split(/\s*,\s*/, lc($h))) {
316 107 ▶ next unless $ce;
Branch: 1 branch
BranchTrueFalseTotal
unless $ce 0 107 107
317 107 ▶ next if $ce eq "identity" || $ce eq "none";
Branch: 1 branch
BranchTrueFalseTotal
if $ce eq "identity" or $ce eq "none" 4 103 107
Condition100%3/3
$ce eq "identity" or $ce eq "none"
l!l&&r!l&&!r
22103
MC/DC100%2/2
$ce eq "identity" or $ce eq "none"
$ce eq "identity" $ce eq "none"
Truth table100%3/3
A or B
A= $ce eq "identity"B= $ce eq "none"
ABresult
1X1
011
000
318 103 ▶ if ($ce eq "gzip" || $ce eq "x-gzip") {
Branch: 7 branches
BranchTrueFalseTotal
if ($ce eq 'gzip' or $ce eq 'x-gzip') { } 30 73 103
elsif ($ce eq 'br') { } 14 59 73
elsif ($ce eq 'x-bzip2' or $ce eq 'bzip2') { } 28 31 59
elsif ($ce eq 'deflate') { } 6 25 31
elsif ($ce eq 'compress' or $ce eq 'x-compress') { } 2 23 25
elsif ($ce eq 'base64') { } 19 4 23
elsif ($ce eq 'quoted-printable') { } 1 3 4
Condition100%3/3
$ce eq "gzip" or $ce eq "x-gzip"
l!l&&r!l&&!r
24673
MC/DC100%2/2
$ce eq "gzip" or $ce eq "x-gzip"
$ce eq "gzip" $ce eq "x-gzip"
Truth table100%3/3
A or B
A= $ce eq "gzip"B= $ce eq "x-gzip"
ABresult
1X1
011
000
319 30 require Compress::Raw::Zlib; # 'WANT_GZIP_OR_ZLIB', 'Z_BUF_ERROR';
320  
321 30 ▶ if( ! $content_ref_iscopy and keys %limiter_options) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $content_ref_iscopy and keys %limiter_options) 2 28 30
Condition100%3/3
not $content_ref_iscopy and keys %limiter_options
!ll&&!rl&&r
16122
MC/DC100%2/2
not $content_ref_iscopy and keys %limiter_options
not $content_ref_iscopy keys %limiter_options
Truth table100%3/3
not(A) and B
A= not $content_ref_iscopyB= keys %limiter_options
ABresult
0X0
100
111
322 # Create a copy of the input because Zlib will overwrite it
323 # :-(
324 2 my $input = "$$content_ref";
325 2 $content_ref = \$input;
326 2 $content_ref_iscopy++;
327 };
328 30 my ($i, $status) = Compress::Raw::Zlib::Inflate->new(
329 %limiter_options,
330 ConsumeInput => 0, # overridden by Zlib if we have %limiter_options :-(
331 WindowBits => Compress::Raw::Zlib::WANT_GZIP_OR_ZLIB(),
332 );
333 30 my $res = $i->inflate( $content_ref, \my $output );
334 30 ▶ $res == Compress::Raw::Zlib::Z_BUF_ERROR()
Branch: 1 branch
BranchTrueFalseTotal
$res == Compress::Raw::Zlib::Z_BUF_ERROR() and Carp::croak("Decoded content would be larger than $content_limit octets") 2 28 30
335 and Carp::croak("Decoded content would be larger than $content_limit octets");
336 28 ▶ $res == Compress::Raw::Zlib::Z_OK()
Branch: 1 branch
BranchTrueFalseTotal
$res == Compress::Raw::Zlib::Z_OK() or $res == Compress::Raw::Zlib::Z_STREAM_END() or die "Can't gunzip content: $res" 28 0 28
Condition33%1/3
$res == Compress::Raw::Zlib::Z_OK() or $res == Compress::Raw::Zlib::Z_STREAM_END()
l!l&&r!l&&!r
0280
MC/DC0%0/2
$res == Compress::Raw::Zlib::Z_OK() or $res == Compress::Raw::Zlib::Z_STREAM_END()
$res == Compress::Raw::Zlib::Z_OK() $res == Compress::Raw::Zlib::Z_STREAM_END()
Truth table33%1/3
A or B
A= $res == Compress::Raw::Zlib::Z_OK()B= $res == Compress::Raw::Zlib::Z_STREAM_END()
ABresult
1X1
011
000
337 or $res == Compress::Raw::Zlib::Z_STREAM_END()
338 or die "Can't gunzip content: $res";
339 28 $content_ref = \$output;
340 28 $content_ref_iscopy++;
341 }
342 elsif ($ce eq 'br') {
343 14 require IO::Uncompress::Brotli;
344  
345 14 my $output;
346 14 ▶ if( defined $content_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_limit) { } 4 10 14
347 # unbro() is the only brotli interface that takes a size
348 # limit; the streaming decompress() method cannot stop
349 # short. It decodes into a buffer of $content_limit
350 # octets, so the limit bounds the allocation directly.
351 4 $output = eval {
352 4 IO::Uncompress::Brotli::unbro($$content_ref, $content_limit);
353 };
354 # Brotli reports "output buffer too small" and "this is
355 # not valid brotli" identically, so, unlike the gzip and
356 # bzip2 branches, we cannot say for certain which it was.
357 4 ▶ $@ and $@ =~ /BrotliDecoderDecompress/
Branch: 1 branch
BranchTrueFalseTotal
$@ and $@ =~ /BrotliDecoderDecompress/ and Carp::croak("Can't unbrotli content: it is corrupt, or would decode to more than $content_limit octets") 2 2 4
Condition66%2/3
$@ and $@ =~ /BrotliDecoderDecompress/
!ll&&!rl&&r
202
MC/DC50%1/2
$@ and $@ =~ /BrotliDecoderDecompress/
$@ $@ =~ /BrotliDecoderDecompress/
Truth table66%2/3
A and B
A= $@B= $@ =~ /BrotliDecoderDecompress/
ABresult
0X0
100
111
358 and Carp::croak("Can't unbrotli content: it is corrupt, or would decode to more than $content_limit octets");
359 } else {
360 10 my $bro = IO::Uncompress::Brotli->create;
361 10 $output = eval { $bro->decompress($$content_ref) };
362 }
363  
364 12 ▶ $@ and die "Can't unbrotli content: $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "Can't unbrotli content: $@" 0 12 12
365 12 $content_ref = \$output;
366 12 $content_ref_iscopy++;
367 }
368 ▶ elsif ($ce eq "x-bzip2" or $ce eq "bzip2") {
Condition100%3/3
$ce eq "x-bzip2" or $ce eq "bzip2"
l!l&&r!l&&!r
62231
MC/DC100%2/2
$ce eq "x-bzip2" or $ce eq "bzip2"
$ce eq "x-bzip2" $ce eq "bzip2"
Truth table100%3/3
A or B
A= $ce eq "x-bzip2"B= $ce eq "bzip2"
ABresult
1X1
011
000
369 28 require Compress::Raw::Bzip2;
370  
371 28 ▶ if( ! $content_ref_iscopy ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content_ref_iscopy) 12 16 28
372 # Create a copy of the input because Bzlib2 will overwrite it
373 # :-(
374 12 my $input = "$$content_ref";
375 12 $content_ref = \$input;
376 12 $content_ref_iscopy++;
377 };
378 my ($i, $status) = Compress::Raw::Bunzip2->new(
379 1, # appendInput
380 0, # consumeInput
381 0, # small
382 28 ▶ $limiter_options{ LimitOutput } || 0,
Condition100%2/2
$limiter_options{'LimitOutput'} || 0
l!l
424
MC/DC100%1/1
$limiter_options{'LimitOutput'} || 0
$limiter_options{'LimitOutput'}
Truth table100%2/2
A
A= $limiter_options{'LimitOutput'}
Aresult
11
00
383 );
384 28 my $output;
385 $output = "\0" x $limiter_options{ Bufsize }
386 28 ▶ if $limiter_options{ Bufsize };
Branch: 1 branch
BranchTrueFalseTotal
if $limiter_options{"Bufsize"} 4 24 28
387 28 my $res = $i->bzinflate( $content_ref, \$output );
388 28 ▶ $res == Compress::Raw::Bzip2::BZ_OUTBUFF_FULL()
Branch: 1 branch
BranchTrueFalseTotal
$res == Compress::Raw::Bzip2::BZ_OUTBUFF_FULL() and Carp::croak("Decoded content would be larger than $content_limit octets") 0 28 28
389 and Carp::croak("Decoded content would be larger than $content_limit octets");
390 28 ▶ $res == Compress::Raw::Bzip2::BZ_OK()
Branch: 1 branch
BranchTrueFalseTotal
$res == Compress::Raw::Bzip2::BZ_OK() or $res == Compress::Raw::Bzip2::BZ_STREAM_END() or die "Can't bunzip content: $res" 26 2 28
Condition66%2/3
$res == Compress::Raw::Bzip2::BZ_OK() or $res == Compress::Raw::Bzip2::BZ_STREAM_END()
l!l&&r!l&&!r
0262
MC/DC50%1/2
$res == Compress::Raw::Bzip2::BZ_OK() or $res == Compress::Raw::Bzip2::BZ_STREAM_END()
$res == Compress::Raw::Bzip2::BZ_OK() $res == Compress::Raw::Bzip2::BZ_STREAM_END()
Truth table66%2/3
A or B
A= $res == Compress::Raw::Bzip2::BZ_OK()B= $res == Compress::Raw::Bzip2::BZ_STREAM_END()
ABresult
1X1
011
000
391 or $res == Compress::Raw::Bzip2::BZ_STREAM_END()
392 or die "Can't bunzip content: $res";
393 26 $content_ref = \$output;
394 26 $content_ref_iscopy++;
395 }
396 elsif ($ce eq "deflate") {
397 6 require IO::Uncompress::Inflate;
398 6 my $output;
399 6 my $status = IO::Uncompress::Inflate::inflate($content_ref, \$output, Transparent => 0);
400 6 my $error = $IO::Uncompress::Inflate::InflateError;
401 6 ▶ unless ($status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 2 4 6
402 # "Content-Encoding: deflate" is supposed to mean the
403 # "zlib" format of RFC 1950, but Microsoft got that
404 # wrong, so some servers sends the raw compressed
405 # "deflate" data. This tries to inflate this format.
406 2 $output = undef;
407 2 require IO::Uncompress::RawInflate;
408 2 ▶ unless (IO::Uncompress::RawInflate::rawinflate($content_ref, \$output)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (IO::Uncompress::RawInflate::rawinflate($content_ref, \$output)) 0 2 2
409 0 $self->push_header("Client-Warning" =>
410 "Could not raw inflate content: $IO::Uncompress::RawInflate::RawInflateError");
411 0 $output = undef;
412 }
413 }
414 6 ▶ die "Can't inflate content: $error" unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 6 6
415 6 $content_ref = \$output;
416 6 $content_ref_iscopy++;
417 }
418 ▶ elsif ($ce eq "compress" || $ce eq "x-compress") {
Condition100%3/3
$ce eq "compress" or $ce eq "x-compress"
l!l&&r!l&&!r
1123
MC/DC100%2/2
$ce eq "compress" or $ce eq "x-compress"
$ce eq "compress" $ce eq "x-compress"
Truth table100%3/3
A or B
A= $ce eq "compress"B= $ce eq "x-compress"
ABresult
1X1
011
000
419 2 die "Can't uncompress content";
420 }
421 elsif ($ce eq "base64") { # not really C-T-E, but should be harmless
422 19 require MIME::Base64;
423 19 $content_ref = \MIME::Base64::decode($$content_ref);
424 19 $content_ref_iscopy++;
425 }
426 elsif ($ce eq "quoted-printable") { # not really C-T-E, but should be harmless
427 1 require MIME::QuotedPrint;
428 1 $content_ref = \MIME::QuotedPrint::decode($$content_ref);
429 1 $content_ref_iscopy++;
430 }
431 else {
432 3 die "Don't know how to decode Content-Encoding '$ce'";
433 }
434 }
435 }
436  
437 109 ▶ if ($self->content_is_text || (my $is_xml = $self->content_is_xml)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content_is_text or my $is_xml = $self->content_is_xml) 100 9 109
Condition100%3/3
$self->content_is_text or my $is_xml = $self->content_is_xml
l!l&&r!l&&!r
71299
MC/DC100%2/2
$self->content_is_text or my $is_xml = $self->content_is_xml
$self->content_is_text my $is_xml = $self->content_is_xml
Truth table100%3/3
A or B
A= $self->content_is_textB= my $is_xml = $self->content_is_xml
ABresult
1X1
011
000
438 my $charset = lc(
439 $opt{charset} ||
440 $self->content_type_charset ||
441 $opt{default_charset} ||
442 100 ▶ $self->content_charset ||
Condition81%9/11
$opt{'charset'} || $self->content_type_charset || $opt{'default_charset'} || $self->content_charset || 'ISO-8859-1'
l!l
1000
$opt{"charset"} or $self->content_type_charset or $opt{"default_charset"} or $self->content_charset
l!l&&r!l&&!r
77230
$opt{"charset"} or $self->content_type_charset or $opt{"default_charset"}
l!l&&r!l&&!r
75223
$opt{"charset"} or $self->content_type_charset
l!l&&r!l&&!r
611425
MC/DC0%0/4
$opt{'charset'} || $self->content_type_charset || $opt{'default_charset'} || $self->content_charset || 'ISO-8859-1'
$opt{"charset"} $self->content_type_charset $opt{"default_charset"} $self->content_charset
Truth table80%4/5
A or B or C or D
A= $opt{"charset"}B= $self->content_type_charsetC= $opt{"default_charset"}D= $self->content_charset
ABCDresult
1XXX1
01XX1
001X1
00011
00000
443 "ISO-8859-1"
444 );
445 100 ▶ if ($charset eq "none") {
Branch: 2 branches
BranchTrueFalseTotal
if ($charset eq 'none') { } 58 42 100
elsif ($charset eq 'us-ascii' or $charset eq 'iso-8859-1') { } 15 27 42
446 # leave it as is
447 }
448 ▶ elsif ($charset eq "us-ascii" || $charset eq "iso-8859-1") {
Condition100%3/3
$charset eq "us-ascii" or $charset eq "iso-8859-1"
l!l&&r!l&&!r
12327
MC/DC100%2/2
$charset eq "us-ascii" or $charset eq "iso-8859-1"
$charset eq "us-ascii" $charset eq "iso-8859-1"
Truth table100%3/3
A or B
A= $charset eq "us-ascii"B= $charset eq "iso-8859-1"
ABresult
1X1
011
000
449 15 ▶ if ($$content_ref =~ /[^\x00-\x7F]/ && defined &utf8::upgrade) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$content_ref =~ /[^\x00-\x7F]/ and defined &utf8::upgrade) 3 12 15
Condition66%2/3
$$content_ref =~ /[^\x00-\x7F]/ and defined &utf8::upgrade
!ll&&!rl&&r
1203
MC/DC50%1/2
$$content_ref =~ /[^\x00-\x7F]/ and defined &utf8::upgrade
$$content_ref =~ /[^\x00-\x7F]/ defined &utf8::upgrade
Truth table66%2/3
A and B
A= $$content_ref =~ /[^\x00-\x7F]/B= defined &utf8::upgrade
ABresult
0X0
100
111
450 3 ▶ unless ($content_ref_iscopy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content_ref_iscopy) 3 0 3
451 3 my $copy = $$content_ref;
452 3 $content_ref = \$copy;
453 3 $content_ref_iscopy++;
454 }
455 3 utf8::upgrade($$content_ref);
456 }
457 }
458 else {
459 27 require Encode;
460 27 eval {
461 $content_ref = \Encode::decode($charset, $$content_ref,
462 27 ▶ ($opt{charset_strict} ? Encode::FB_CROAK() : 0) | Encode::LEAVE_SRC());
Branch: 1 branch
BranchTrueFalseTotal
$opt{'charset_strict'} ? : 1 26 27
463 };
464 27 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 23 27
465 4 my $retried;
466 4 ▶ if ($@ =~ /^Unknown encoding/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^Unknown encoding/) 3 1 4
467 3 ▶ my $alt_charset = lc($opt{alt_charset} || "");
Condition100%2/2
$opt{'alt_charset'} || ''
l!l
21
MC/DC100%1/1
$opt{'alt_charset'} || ''
$opt{'alt_charset'}
Truth table100%2/2
A
A= $opt{'alt_charset'}
Aresult
11
00
468 3 ▶ if ($alt_charset && $charset ne $alt_charset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alt_charset and $charset ne $alt_charset) 2 1 3
Condition66%2/3
$alt_charset and $charset ne $alt_charset
!ll&&!rl&&r
102
MC/DC50%1/2
$alt_charset and $charset ne $alt_charset
$alt_charset $charset ne $alt_charset
Truth table66%2/3
A and B
A= $alt_charsetB= $charset ne $alt_charset
ABresult
0X0
100
111
469 # Retry decoding with the alternative charset
470 $content_ref = \Encode::decode($alt_charset, $$content_ref,
471 2 ▶ ($opt{charset_strict} ? Encode::FB_CROAK() : 0) | Encode::LEAVE_SRC())
Branch: 2 branches
BranchTrueFalseTotal
unless $alt_charset eq "none" 1 1 2
$opt{'charset_strict'} ? : 0 1 1
472 unless $alt_charset eq "none";
473 2 $retried++;
474 }
475 }
476 4 ▶ die unless $retried;
Branch: 1 branch
BranchTrueFalseTotal
unless $retried 2 2 4
477 }
478 25 ▶ die "Encode::decode() returned undef improperly" unless defined $$content_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$content_ref 0 25 25
479 25 ▶ if ($is_xml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_xml) 10 15 25
480 # Get rid of the XML encoding declaration if present
481 10 $$content_ref =~ s/^\x{FEFF}//;
482 10 ▶ if ($$content_ref =~ /^(\s*<\?xml[^\x00]*?\?>)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$content_ref =~ /^(\s*<\?xml[^\x00]*?\?>)/) 4 6 10
483 4 substr($$content_ref, 0, length($1)) =~ s/\sencoding\s*=\s*(["']).*?\1//;
484 }
485 }
486 }
487 }
488 };
489 120 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 13 107 120
490 13 ▶ Carp::croak($@) if $opt{raise_error};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"raise_error"} 9 4 13
491 4 return undef;
492 }
493  
494 107 ▶ return $opt{ref} ? $content_ref : $$content_ref;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'ref'} ? : 52 55 107
495 }
496  
497  
498 sub decodable
499 4 ▶{
Subroutine decodable: called CC=2 SCAR=6.9
Pod documented
500 # should match the Content-Encoding values that decoded_content can deal with
501 4 my $self = shift;
502 4 my @enc;
503 4 local $@;
504 # XXX preferably we should determine if the modules are available without loading
505 # them here
506 4 eval {
507 4 require Compress::Raw::Zlib;
508 4 push(@enc, "gzip", "x-gzip");
509 };
510 4 eval {
511 4 require IO::Uncompress::Inflate;
512 4 require IO::Uncompress::RawInflate;
513 4 push(@enc, "deflate");
514 };
515 4 eval {
516 4 require Compress::Raw::Bzip2;
517 4 push(@enc, "x-bzip2", "bzip2");
518 };
519 4 eval {
520 4 require IO::Uncompress::Brotli;
521 4 push(@enc, 'br');
522 };
523 # we don't care about announcing the 'identity', 'base64' and
524 # 'quoted-printable' stuff
525 4 ▶ return wantarray ? @enc : join(", ", @enc);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
526 }
527  
528  
529 sub decode
530 12 ▶{
Subroutine decode: called CC=3 SCAR=11.0
Pod documented
531 12 my $self = shift;
532 12 ▶ return 1 unless $self->header("Content-Encoding");
Branch: 1 branch
BranchTrueFalseTotal
unless $self->header("Content-Encoding") 1 11 12
533 11 ▶ if (defined(my $content = $self->decoded_content(charset => "none"))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $content = $self->decoded_content("charset", "none"))) 10 1 11
534 10 $self->remove_header("Content-Encoding", "Content-Length", "Content-MD5");
535 10 $self->content($content);
536 10 return 1;
537 }
538 1 return 0;
539 }
540  
541  
542 sub encode
543 13 ▶{
Subroutine encode: called CC=20 SCAR=30.0
Pod documented
544 13 my($self, @enc) = @_;
545  
546 13 ▶ Carp::croak("Can't encode multipart/* messages") if $self->content_type =~ m,^multipart/,;
Branch: 1 branch
BranchTrueFalseTotal
if $self->content_type =~ m[^multipart/] 1 12 13
547 12 ▶ Carp::croak("Can't encode message/* messages") if $self->content_type =~ m,^message/,;
Branch: 1 branch
BranchTrueFalseTotal
if $self->content_type =~ m[^message/] 1 11 12
548  
549 11 ▶ return 1 unless @enc; # nothing to do
Branch: 1 branch
BranchTrueFalseTotal
unless @enc 1 10 11
550  
551 10 my $content = $self->content;
552 10 for my $encoding (@enc) {
553 12 ▶ if ($encoding eq "identity" || $encoding eq "none") {
Branch: 7 branches
BranchTrueFalseTotal
if ($encoding eq 'identity' or $encoding eq 'none') { } 2 10 12
elsif ($encoding eq 'base64') { } 2 8 10
elsif ($encoding eq 'gzip' or $encoding eq 'x-gzip') { } 2 6 8
elsif ($encoding eq 'deflate') { } 1 5 6
elsif ($encoding eq 'x-bzip2' or $encoding eq 'bzip2') { } 2 3 5
elsif ($encoding eq 'br') { } 1 2 3
elsif ($encoding eq 'rot13') { } 1 1 2
Condition100%3/3
$encoding eq "identity" or $encoding eq "none"
l!l&&r!l&&!r
1110
MC/DC100%2/2
$encoding eq "identity" or $encoding eq "none"
$encoding eq "identity" $encoding eq "none"
Truth table100%3/3
A or B
A= $encoding eq "identity"B= $encoding eq "none"
ABresult
1X1
011
000
554 # nothing to do
555 }
556 elsif ($encoding eq "base64") {
557 2 require MIME::Base64;
558 2 $content = MIME::Base64::encode($content);
559 }
560 ▶ elsif ($encoding eq "gzip" || $encoding eq "x-gzip") {
Condition100%3/3
$encoding eq "gzip" or $encoding eq "x-gzip"
l!l&&r!l&&!r
116
MC/DC100%2/2
$encoding eq "gzip" or $encoding eq "x-gzip"
$encoding eq "gzip" $encoding eq "x-gzip"
Truth table100%3/3
A or B
A= $encoding eq "gzip"B= $encoding eq "x-gzip"
ABresult
1X1
011
000
561 2 require IO::Compress::Gzip;
562 2 my $output;
563 2 ▶ IO::Compress::Gzip::gzip(\$content, \$output, Minimal => 1)
Branch: 1 branch
BranchTrueFalseTotal
IO::Compress::Gzip::gzip(\$content, \$output, "Minimal", 1) or die "Can't gzip content: $IO::Compress::Gzip::GzipError" 2 0 2
564 or die "Can't gzip content: $IO::Compress::Gzip::GzipError";
565 2 $content = $output;
566 }
567 elsif ($encoding eq "deflate") {
568 1 require IO::Compress::Deflate;
569 1 my $output;
570 1 ▶ IO::Compress::Deflate::deflate(\$content, \$output)
Branch: 1 branch
BranchTrueFalseTotal
IO::Compress::Deflate::deflate(\$content, \$output) or die "Can't deflate content: $IO::Compress::Deflate::DeflateError" 1 0 1
571 or die "Can't deflate content: $IO::Compress::Deflate::DeflateError";
572 1 $content = $output;
573 }
574 ▶ elsif ($encoding eq "x-bzip2" || $encoding eq "bzip2") {
Condition100%3/3
$encoding eq "x-bzip2" or $encoding eq "bzip2"
l!l&&r!l&&!r
113
MC/DC100%2/2
$encoding eq "x-bzip2" or $encoding eq "bzip2"
$encoding eq "x-bzip2" $encoding eq "bzip2"
Truth table100%3/3
A or B
A= $encoding eq "x-bzip2"B= $encoding eq "bzip2"
ABresult
1X1
011
000
575 2 require IO::Compress::Bzip2;
576 2 my $output;
577 2 ▶ IO::Compress::Bzip2::bzip2(\$content, \$output)
Branch: 1 branch
BranchTrueFalseTotal
IO::Compress::Bzip2::bzip2(\$content, \$output) or die "Can't bzip2 content: $IO::Compress::Bzip2::Bzip2Error" 2 0 2
578 or die "Can't bzip2 content: $IO::Compress::Bzip2::Bzip2Error";
579 2 $content = $output;
580 }
581 elsif ($encoding eq "br") {
582 1 require IO::Compress::Brotli;
583 1 my $output;
584 1 ▶ eval { $output = IO::Compress::Brotli::bro($content) }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $output = IO::Compress::Brotli::bro($content) } } or die "Can't brotli content: $@" 1 0 1
585 or die "Can't brotli content: $@";
586 1 $content = $output;
587 }
588 elsif ($encoding eq "rot13") { # for the fun of it
589 1 $content =~ tr/A-Za-z/N-ZA-Mn-za-m/;
590 }
591 else {
592 1 return 0;
593 }
594 }
595 9 my $h = $self->header("Content-Encoding");
596 9 ▶ unshift(@enc, $h) if $h;
Branch: 1 branch
BranchTrueFalseTotal
if $h 2 7 9
597 9 $self->header("Content-Encoding", join(", ", @enc));
598 9 $self->remove_header("Content-Length", "Content-MD5");
599 9 $self->content($content);
600 9 return 1;
601 }
602  
603  
604 sub as_string
605 91 ▶{
Subroutine as_string: called CC=5 SCAR=16.1
Pod documented
606 91 my($self, $eol) = @_;
607 91 ▶ $eol = "\n" unless defined $eol;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $eol 34 57 91
608  
609 # The calculation of content might update the headers
610 # so we need to do that first.
611 91 my $content = $self->content;
612  
613 91 ▶ return join("", $self->{'_headers'}->as_string($eol),
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && length $content && !($content =~ /\n\z/) ? : 6 85 91
Condition100%6/6
@_ == 1 and length $content and not $content =~ /\n\z/
!ll&&!rl&&r
70156
@_ == 1 and length $content
!ll&&!rl&&r
571321
MC/DC100%3/3
@_ == 1 and length $content and not $content =~ /\n\z/
@_ == 1 length $content not $content =~ /\n\z/
Truth table100%4/4
A and B and not(C)
A= @_ == 1B= length $contentC= not $content =~ /\n\z/
ABCresult
0XX0
10X0
1100
1111
614 $eol,
615 $content,
616 (@_ == 1 && length($content) &&
617 $content !~ /\n\z/) ? "\n" : "",
618 );
619 }
620  
621  
622 sub dump
623 15 ▶{
Subroutine dump: called CC=12 SCAR=24.9
Pod documented
624 15 my($self, %opt) = @_;
625 15 my $content = $self->content;
626 15 my $chopped = 0;
627 15 ▶ if (!ref($content)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $content) 15 0 15
628 15 my $maxlen = $opt{maxlength};
629 15 ▶ $maxlen = 512 unless defined($maxlen);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxlen 13 2 15
630 15 ▶ if ($maxlen && length($content) > $maxlen * 1.1 + 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxlen and length $content > $maxlen * "1.1" + 3) 1 14 15
Condition100%3/3
$maxlen and length $content > $maxlen * "1.1" + 3
!ll&&!rl&&r
1131
MC/DC100%2/2
$maxlen and length $content > $maxlen * "1.1" + 3
$maxlen length $content > $maxlen * "1.1" + 3
Truth table100%3/3
A and B
A= $maxlenB= length $content > $maxlen * "1.1" + 3
ABresult
0X0
100
111
631 1 $chopped = length($content) - $maxlen;
632 1 $content = substr($content, 0, $maxlen) . "...";
633 }
634  
635 15 $content =~ s/\\/\\\\/g;
636 15 $content =~ s/\t/\\t/g;
637 15 $content =~ s/\r/\\r/g;
638  
639 # no need for 3 digits in escape for these
640 15 $content =~ s/([\0-\11\13-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
641  
642 15 $content =~ s/([\0-\11\13-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
643 0 $content =~ s/([^\12\040-\176])/sprintf('\\x{%X}',ord($1))/eg;
644  
645 # remaining whitespace
646 0 $content =~ s/( +)\n/("\\40" x length($1)) . "\n"/eg;
647 0 $content =~ s/(\n+)\n/("\\n" x length($1)) . "\n"/eg;
648 15 $content =~ s/\n\z/\\n/;
649  
650 15 my $no_content = $opt{no_content};
651 15 ▶ $no_content = "(no content)" unless defined $no_content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $no_content 11 4 15
652 15 ▶ if ($content eq $no_content) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content eq $no_content) { } 2 13 15
elsif ($content eq '') { } 8 5 13
653 # escape our $no_content marker
654 2 $content =~ s/^(.)/sprintf('\\x%02X',ord($1))/eg;
655 }
656 elsif ($content eq "") {
657 8 $content = $no_content;
658 }
659 }
660  
661 15 my @dump;
662 15 ▶ push(@dump, $opt{preheader}) if $opt{preheader};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"preheader"} 6 9 15
663 15 push(@dump, $self->{_headers}->as_string, $content);
664 15 ▶ push(@dump, "(+ $chopped more bytes not shown)") if $chopped;
Branch: 1 branch
BranchTrueFalseTotal
if $chopped 1 14 15
665  
666 15 my $dump = join("\n", @dump, "");
667 15 ▶ $dump =~ s/^/$opt{prefix}/gm if $opt{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"prefix"} 3 12 15
668  
669 15 ▶ print $dump unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 2 13 15
670 15 return $dump;
671 }
672  
673 # allow subclasses to override what will handle individual parts
674 9 ▶sub _part_class {
Subroutine _part_class: called CC=1 SCAR=0.0
675 9 return __PACKAGE__;
676 }
677  
678 27 ▶sub parts {
Subroutine parts: called CC=11 SCAR=24.0
Pod documented
679 27 my $self = shift;
680 27 ▶ if (defined(wantarray) && (!exists $self->{_parts} || ref($self->{_content}) eq "SCALAR")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray and !exists $self->{"_parts"} || ref $self->{"_content"} eq "SCALAR") 13 14 27
Condition100%6/6
defined wantarray and !exists $self->{"_parts"} || ref $self->{"_content"} eq "SCALAR"
!ll&&!rl&&r
8613
not exists $self->{"_parts"} or ref $self->{"_content"} eq "SCALAR"
l!l&&r!l&&!r
1216
MC/DC100%3/3
defined wantarray and !exists $self->{"_parts"} || ref $self->{"_content"} eq "SCALAR"
defined wantarray not exists $self->{"_parts"} ref $self->{"_content"} eq "SCALAR"
Truth table100%4/4
A and not(B) or C
A= defined wantarrayB= not exists $self->{"_parts"}C= ref $self->{"_content"} eq "SCALAR"
ABCresult
0XX0
1000
11X1
1011
681 13 $self->_parts;
682 }
683 27 my $old = $self->{_parts};
684 27 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 19 27
685 8 ▶ my @parts = map { ref($_) eq 'ARRAY' ? @$_ : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 2 10 12
686 8 ▶ my $ct = $self->content_type || "";
Condition100%2/2
$self->content_type || ''
l!l
53
MC/DC100%1/1
$self->content_type || ''
$self->content_type
Truth table100%2/2
A
A= $self->content_type
Aresult
11
00
687 8 ▶ if ($ct =~ m,^message/,) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ct =~ m[^message/]) { } 3 5 8
elsif (not $ct =~ m[^multipart/]) { } 3 2 5
688 3 ▶ Carp::croak("Only one part allowed for $ct content")
Branch: 1 branch
BranchTrueFalseTotal
if @parts > 1 1 2 3
689 if @parts > 1;
690 }
691 elsif ($ct !~ m,^multipart/,) {
692 3 $self->remove_content_headers;
693 3 $self->content_type("multipart/mixed");
694 }
695 7 $self->{_parts} = \@parts;
696 7 _stale_content($self);
697 }
698 26 ▶ return @$old if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 15 11 26
699 11 return $old->[0];
700 }
701  
702 4 ▶sub add_part {
Subroutine add_part: called CC=7 SCAR=19.5
Pod documented
703 4 my $self = shift;
704 4 ▶ if (($self->content_type || "") !~ m,^multipart/,) {
Branch: 2 branches
BranchTrueFalseTotal
if (not +($self->content_type || '') =~ m[^multipart/]) { } 2 2 4
elsif (not exists $self->{'_parts'} or ref $self->{'_content'} eq 'SCALAR') { } 1 1 2
Condition100%2/2
$self->content_type || ''
l!l
31
MC/DC100%1/1
$self->content_type || ''
$self->content_type
Truth table100%2/2
A
A= $self->content_type
Aresult
11
00
705 2 my $p = $self->_part_class->new(
706 $self->remove_content_headers,
707 $self->content(""),
708 );
709 2 $self->content_type("multipart/mixed");
710 2 $self->{_parts} = [];
711 2 ▶ if ($p->headers->header_field_names || $p->content ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->headers->header_field_names or $p->content ne "") 1 1 2
Condition66%2/3
$p->headers->header_field_names or $p->content ne ""
l!l&&r!l&&!r
101
MC/DC50%1/2
$p->headers->header_field_names or $p->content ne ""
$p->headers->header_field_names $p->content ne ""
Truth table66%2/3
A or B
A= $p->headers->header_field_namesB= $p->content ne ""
ABresult
1X1
011
000
712 1 push(@{$self->{_parts}}, $p);
713 }
714 }
715 ▶ elsif (!exists $self->{_parts} || ref($self->{_content}) eq "SCALAR") {
Condition66%2/3
not exists $self->{"_parts"} or ref $self->{"_content"} eq "SCALAR"
l!l&&r!l&&!r
101
MC/DC50%1/2
not exists $self->{"_parts"} or ref $self->{"_content"} eq "SCALAR"
not exists $self->{"_parts"} ref $self->{"_content"} eq "SCALAR"
Truth table66%2/3
not(A) or B
A= not exists $self->{"_parts"}B= ref $self->{"_content"} eq "SCALAR"
ABresult
1X1
011
000
716 1 $self->_parts;
717 }
718  
719 4 push(@{$self->{_parts}}, @_);
720 4 _stale_content($self);
721 4 return;
722 }
723  
724 11 ▶sub _stale_content {
Subroutine _stale_content: called CC=2 SCAR=6.9
725 11 my $self = shift;
726 11 ▶ if (ref($self->{_content}) eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'_content'} eq 'SCALAR') { } 1 10 11
727 # must recalculate now
728 1 $self->_content;
729 }
730 else {
731 # just invalidate cache
732 10 delete $self->{_content};
733 10 delete $self->{_content_ref};
734 }
735 }
736  
737 # delegate all other method calls to the headers object.
738 17 our $AUTOLOAD;
739  
740 75 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
741 75 my ( $package, $method ) = $AUTOLOAD =~ m/\A(.+)::([^:]*)\z/;
742 75 my $code = $_[0]->can($method);
743 75 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
unless $code 3 72 75
744 qq(Can't locate object method "$method" via package "$package"))
745 unless $code;
746 72 goto &$code;
747 }
748  
749 129 ▶sub can {
Subroutine can: called CC=4 SCAR=13.9
Pod undocumented
750 129 my ( $self, $method ) = @_;
751  
752 129 ▶ if ( my $own_method = $self->SUPER::can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $own_method = $self->SUPER::can($method)) 20 109 129
753 20 return $own_method;
754 }
755  
756 109 ▶ my $headers = ref($self) ? $self->headers : 'HTTP::Headers';
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 106 3 109
757 109 ▶ if ( $headers->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->can($method)) 74 35 109
758  
759 # We create the function here so that it will not need to be
760 # autoloaded or recreated the next time.
761 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
762 1027 ▶ *$method = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
763 1027 local $Carp::Internal{ +__PACKAGE__ } = 1;
764 1027 shift->headers->$method(@_);
765 74 };
766 74 return \&$method;
767 }
768  
769 35 return undef;
770 }
771  
772 1 ▶sub DESTROY { } # avoid AUTOLOADing it
Subroutine DESTROY: called CC=1 SCAR=0.0
773  
774 # Private method to access members in %$self
775 sub _elem
776 404 ▶{
Subroutine _elem: called CC=2 SCAR=6.9
777 404 my $self = shift;
778 404 my $elem = shift;
779 404 my $old = $self->{$elem};
780 404 ▶ $self->{$elem} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 169 235 404
781 404 return $old;
782 }
783  
784  
785 # Create private _parts attribute from current _content
786 14 ▶sub _parts {
Subroutine _parts: called CC=9 SCAR=22.0
787 14 my $self = shift;
788 14 my $ct = $self->content_type;
789 14 ▶ if ($ct =~ m,^multipart/,) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ct =~ m[^multipart/]) { } 4 10 14
elsif ($ct eq 'message/http') { } 4 6 10
elsif ($ct =~ m[^message/]) { } 2 4 6
790 4 require HTTP::Headers::Util;
791 4 my @h = HTTP::Headers::Util::split_header_words($self->header("Content-Type"));
792 4 ▶ die "Assert" unless @h;
Branch: 1 branch
BranchTrueFalseTotal
unless @h 0 4 4
793 4 my %h = @{$h[0]};
794 4 ▶ if (defined(my $b = $h{boundary})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $b = $h{"boundary"})) 3 1 4
795 3 my $str = $self->content;
796 3 $str =~ s/\r?\n--\Q$b\E--.*//s;
797 3 ▶ if ($str =~ s/(^|.*?\r?\n)--\Q$b\E\r?\n//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/(^|.*?\r?\n)--\Q$b\E\r?\n//s) 3 0 3
798 3 $self->{_parts} = [map $self->_part_class->parse($_),
799 split(/\r?\n--\Q$b\E\r?\n/, $str)]
800 }
801 }
802 }
803 elsif ($ct eq "message/http") {
804 4 require HTTP::Request;
805 4 require HTTP::Response;
806 4 my $content = $self->content;
807 4 ▶ my $class = ($content =~ m,^(HTTP/.*)\n,) ?
Branch: 1 branch
BranchTrueFalseTotal
$content =~ m[^(HTTP/.*)\n] ? : 2 2 4
808 "HTTP::Response" : "HTTP::Request";
809 4 $self->{_parts} = [$class->parse($content)];
810 }
811 elsif ($ct =~ m,^message/,) {
812 2 $self->{_parts} = [ $self->_part_class->parse($self->content) ];
813 }
814  
815 14 ▶ $self->{_parts} ||= [];
Condition100%2/2
$self->{"_parts"} ||= []
l!l
95
MC/DC100%1/1
$self->{"_parts"} ||= []
$self->{"_parts"}
Truth table100%2/2
A
A= $self->{"_parts"}
Aresult
11
00
816 }
817  
818  
819 # Create private _content attribute from current _parts
820 10 ▶sub _content {
Subroutine _content: called CC=10 SCAR=23.0
821 10 my $self = shift;
822 10 ▶ my $ct = $self->{_headers}->header("Content-Type") || "multipart/mixed";
Condition50%1/2
$self->{'_headers'}->header('Content-Type') || 'multipart/mixed'
l!l
100
MC/DC0%0/1
$self->{'_headers'}->header('Content-Type') || 'multipart/mixed'
$self->{'_headers'}->header('Content-Type')
Truth table50%1/2
A
A= $self->{'_headers'}->header('Content-Type')
Aresult
11
00
823 10 ▶ if ($ct =~ m,^\s*message/,i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ m[^\s*message/]i) 2 8 10
824 2 _set_content($self, $self->{_parts}[0]->as_string($CRLF), 1);
825 2 return;
826 }
827  
828 8 require HTTP::Headers::Util;
829 8 my @v = HTTP::Headers::Util::split_header_words($ct);
830 8 ▶ Carp::carp("Multiple Content-Type headers") if @v > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @v > 1 0 8 8
831 8 @v = @{$v[0]};
832  
833 8 my $boundary;
834 my $boundary_index;
835 8 ▶ for (my @tmp = @v; @tmp;) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp) 10 6 16
836 10 my($k, $v) = splice(@tmp, 0, 2);
837 10 ▶ if ($k eq "boundary") {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "boundary") 2 8 10
838 2 $boundary = $v;
839 2 $boundary_index = @v - @tmp - 1;
840 2 last;
841 }
842 }
843  
844 8 my @parts = map $_->as_string($CRLF), @{$self->{_parts}};
845  
846 8 my $bno = 0;
847 8 ▶ $boundary = _boundary() unless defined $boundary;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $boundary 6 2 8
848 CHECK_BOUNDARY:
849 {
850 8 for (@parts) {
851 15 ▶ if (index($_, $boundary) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($_, $boundary) != -1) 1 14 15
852 # must have a better boundary
853 1 $boundary = _boundary(++$bno);
854 1 redo CHECK_BOUNDARY;
855 }
856 }
857 }
858  
859 8 ▶ if ($boundary_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($boundary_index) { } 2 6 8
860 2 $v[$boundary_index] = $boundary;
861 }
862 else {
863 6 push(@v, boundary => $boundary);
864 }
865  
866 8 $ct = HTTP::Headers::Util::join_header_words(@v);
867 8 $self->{_headers}->header("Content-Type", $ct);
868  
869 8 _set_content($self, "--$boundary$CRLF" .
870 join("$CRLF--$boundary$CRLF", @parts) .
871 "$CRLF--$boundary--$CRLF",
872 1);
873 }
874  
875  
876 sub _boundary
877 7 ▶{
Subroutine _boundary: called CC=2 SCAR=6.9
878 7 ▶ my $size = shift || return "xYzZY";
Condition100%2/2
shift() || (return 'xYzZY')
l!l
16
MC/DC100%1/1
shift() || (return 'xYzZY')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
879 1 require MIME::Base64;
880 1 my $b = MIME::Base64::encode(join("", map chr(rand(256)), 1..$size*3), "");
881 1 $b =~ s/[\W]/X/g; # ensure alnum only
882 1 $b;
883 }
884  
885  
886 17 1;
887  
888 =pod
889  
890 =encoding UTF-8
891  
892 =head1 NAME
893  
894 HTTP::Message - HTTP style message (base class)
895  
896 =head1 VERSION
897  
898 version 7.04
899  
900 =head1 SYNOPSIS
901  
902 use parent 'HTTP::Message';
903  
904 =head1 DESCRIPTION
905  
906 An C<HTTP::Message> object contains some headers and a content body.
907 The following methods are available:
908  
909 =over 4
910  
911 =item $mess = HTTP::Message->new
912  
913 =item $mess = HTTP::Message->new( $headers )
914  
915 =item $mess = HTTP::Message->new( $headers, $content )
916  
917 This constructs a new message object. Normally you would want
918 construct C<HTTP::Request> or C<HTTP::Response> objects instead.
919  
920 The optional $header argument should be a reference to an
921 C<HTTP::Headers> object or a plain array reference of key/value pairs.
922 If an C<HTTP::Headers> object is provided then a copy of it will be
923 embedded into the constructed message, i.e. it will not be owned and
924 can be modified afterwards without affecting the message.
925  
926 The optional $content argument should be a string of bytes.
927  
928 =item $mess = HTTP::Message->parse( $str )
929  
930 This constructs a new message object by parsing the given string.
931  
932 =item $mess->headers
933  
934 Returns the embedded C<HTTP::Headers> object.
935  
936 =item $mess->headers_as_string
937  
938 =item $mess->headers_as_string( $eol )
939  
940 Call the as_string() method for the headers in the
941 message. This will be the same as
942  
943 $mess->headers->as_string
944  
945 but it will make your program a whole character shorter :-)
946  
947 =item $mess->content
948  
949 =item $mess->content( $bytes )
950  
951 The content() method sets the raw content if an argument is given. If no
952 argument is given the content is not touched. In either case the
953 original raw content is returned.
954  
955 If the C<undef> argument is given, the content is reset to its default value,
956 which is an empty string.
957  
958 Note that the content should be a string of bytes. Strings in perl
959 can contain characters outside the range of a byte. The C<Encode>
960 module can be used to turn such strings into a string of bytes.
961  
962 =item $mess->add_content( $bytes )
963  
964 The add_content() methods appends more data bytes to the end of the
965 current content buffer.
966  
967 =item $mess->add_content_utf8( $string )
968  
969 The add_content_utf8() method appends the UTF-8 bytes representing the
970 string to the end of the current content buffer.
971  
972 =item $mess->content_ref
973  
974 =item $mess->content_ref( \$bytes )
975  
976 The content_ref() method will return a reference to content buffer string.
977 It can be more efficient to access the content this way if the content
978 is huge, and it can even be used for direct manipulation of the content,
979 for instance:
980  
981 ${$res->content_ref} =~ s/\bfoo\b/bar/g;
982  
983 This example would modify the content buffer in-place.
984  
985 If an argument is passed it will setup the content to reference some
986 external source. The content() and add_content() methods
987 will automatically dereference scalar references passed this way. For
988 other references content() will return the reference itself and
989 add_content() will refuse to do anything.
990  
991 =item $mess->content_charset
992  
993 This returns the charset used by the content in the message. The
994 charset is either found as the charset attribute of the
995 C<Content-Type> header or by guessing.
996  
997 See L<http://www.w3.org/TR/REC-html40/charset.html#spec-char-encoding>
998 for details about how charset is determined.
999  
1000 =item $mess->decoded_content( %options )
1001  
1002 Returns the content with any C<Content-Encoding> undone and, for textual content
1003 (C<Content-Type> values starting with C<text/>, exactly matching
1004 C<application/xml>, or ending with C<+xml>), the raw content's character set
1005 decoded into Perl's Unicode string format. Note that this
1006 L<does not currently|https://github.com/libwww-perl/HTTP-Message/pull/99>
1007 attempt to decode declared character sets for any other content types like
1008 C<application/json> or C<application/javascript>. If the C<Content-Encoding>
1009 or C<charset> of the message is unknown, this method will fail by returning
1010 C<undef>.
1011  
1012 The following options can be specified.
1013  
1014 =over
1015  
1016 =item C<charset>
1017  
1018 This overrides the charset parameter for text content. The value
1019 C<none> can used to suppress decoding of the charset.
1020  
1021 =item C<default_charset>
1022  
1023 This overrides the default charset guessed by content_charset() or
1024 if that fails "ISO-8859-1".
1025  
1026 =item C<alt_charset>
1027  
1028 If decoding fails because the charset specified in the Content-Type header
1029 isn't recognized by Perl's Encode module, then try decoding using this charset
1030 instead of failing. The C<alt_charset> might be specified as C<none> to simply
1031 return the string without any decoding of charset as alternative.
1032  
1033 =item C<charset_strict>
1034  
1035 Abort decoding if malformed characters is found in the content. By
1036 default you get the substitution character ("\x{FFFD}") in place of
1037 malformed characters.
1038  
1039 =item C<raise_error>
1040  
1041 If TRUE then raise an exception if not able to decode content. Reason
1042 might be that the specified C<Content-Encoding> or C<charset> is not
1043 supported. If this option is FALSE, then decoded_content() will return
1044 C<undef> on errors, but will still set $@.
1045  
1046 =item C<ref>
1047  
1048 If TRUE then a reference to decoded content is returned. This might
1049 be more efficient in cases where the decoded content is identical to
1050 the raw content as no data copying is required in this case.
1051  
1052 =back
1053  
1054 =item $mess->decodable
1055  
1056 =item HTTP::Message::decodable()
1057  
1058 This returns the encoding identifiers that decoded_content() can
1059 process. In scalar context returns a comma separated string of
1060 identifiers.
1061  
1062 This value is suitable for initializing the C<Accept-Encoding> request
1063 header field.
1064  
1065 =item $mess->decode
1066  
1067 This method tries to replace the content of the message with the
1068 decoded version and removes the C<Content-Encoding> header. Returns
1069 TRUE if successful and FALSE if not.
1070  
1071 If the message does not have a C<Content-Encoding> header this method
1072 does nothing and returns TRUE.
1073  
1074 Note that the content of the message is still bytes after this method
1075 has been called and you still need to call decoded_content() if you
1076 want to process its content as a string.
1077  
1078 =item $mess->encode( $encoding, ... )
1079  
1080 Apply the given encodings to the content of the message. Returns TRUE
1081 if successful. The "identity" (non-)encoding is always supported; other
1082 currently supported encodings, subject to availability of required
1083 additional modules, are "gzip", "deflate", "x-bzip2", "base64" and "br".
1084  
1085 A successful call to this function will set the C<Content-Encoding>
1086 header.
1087  
1088 Note that C<multipart/*> or C<message/*> messages can't be encoded and
1089 this method will croak if you try.
1090  
1091 =item $mess->parts
1092  
1093 =item $mess->parts( @parts )
1094  
1095 =item $mess->parts( \@parts )
1096  
1097 Messages can be composite, i.e. contain other messages. The composite
1098 messages have a content type of C<multipart/*> or C<message/*>. This
1099 method give access to the contained messages.
1100  
1101 The argumentless form will return a list of C<HTTP::Message> objects.
1102 If the content type of $msg is not C<multipart/*> or C<message/*> then
1103 this will return the empty list. In scalar context only the first
1104 object is returned. The returned message parts should be regarded as
1105 read-only (future versions of this library might make it possible
1106 to modify the parent by modifying the parts).
1107  
1108 If the content type of $msg is C<message/*> then there will only be
1109 one part returned.
1110  
1111 If the content type is C<message/http>, then the return value will be
1112 either an C<HTTP::Request> or an C<HTTP::Response> object.
1113  
1114 If a @parts argument is given, then the content of the message will be
1115 modified. The array reference form is provided so that an empty list
1116 can be provided. The @parts array should contain C<HTTP::Message>
1117 objects. The @parts objects are owned by $mess after this call and
1118 should not be modified or made part of other messages.
1119  
1120 When updating the message with this method and the old content type of
1121 $mess is not C<multipart/*> or C<message/*>, then the content type is
1122 set to C<multipart/mixed> and all other content headers are cleared.
1123  
1124 This method will croak if the content type is C<message/*> and more
1125 than one part is provided.
1126  
1127 =item $mess->add_part( $part )
1128  
1129 This will add a part to a message. The $part argument should be
1130 another C<HTTP::Message> object. If the previous content type of
1131 $mess is not C<multipart/*> then the old content (together with all
1132 content headers) will be made part #1 and the content type made
1133 C<multipart/mixed> before the new part is added. The $part object is
1134 owned by $mess after this call and should not be modified or made part
1135 of other messages.
1136  
1137 There is no return value.
1138  
1139 =item $mess->clear
1140  
1141 Will clear the headers and set the content to the empty string. There
1142 is no return value
1143  
1144 =item $mess->protocol
1145  
1146 =item $mess->protocol( $proto )
1147  
1148 Sets the HTTP protocol used for the message. The protocol() is a string
1149 like C<HTTP/1.0> or C<HTTP/1.1>.
1150  
1151 =item $mess->clone
1152  
1153 Returns a copy of the message object.
1154  
1155 =item $mess->as_string
1156  
1157 =item $mess->as_string( $eol )
1158  
1159 Returns the message formatted as a single string.
1160  
1161 The optional $eol parameter specifies the line ending sequence to use.
1162 The default is "\n". If no $eol is given then as_string will ensure
1163 that the returned string is newline terminated (even when the message
1164 content is not). No extra newline is appended if an explicit $eol is
1165 passed.
1166  
1167 =item $mess->dump( %opt )
1168  
1169 Returns the message formatted as a string. In void context print the string.
1170  
1171 This differs from C<< $mess->as_string >> in that it escapes the bytes
1172 of the content so that it's safe to print them and it limits how much
1173 content to print. The escapes syntax used is the same as for Perl's
1174 double quoted strings. If there is no content the string "(no
1175 content)" is shown in its place.
1176  
1177 Options to influence the output can be passed as key/value pairs. The
1178 following options are recognized:
1179  
1180 =over
1181  
1182 =item maxlength => $num
1183  
1184 How much of the content to show. The default is 512. Set this to 0
1185 for unlimited.
1186  
1187 If the content is longer then the string is chopped at the limit and
1188 the string "...\n(### more bytes not shown)" appended.
1189  
1190 =item no_content => $str
1191  
1192 Replaces the "(no content)" marker.
1193  
1194 =item prefix => $str
1195  
1196 A string that will be prefixed to each line of the dump.
1197  
1198 =back
1199  
1200 =back
1201  
1202 All methods unknown to C<HTTP::Message> itself are delegated to the
1203 C<HTTP::Headers> object that is part of every message. This allows
1204 convenient access to these methods. Refer to L<HTTP::Headers> for
1205 details of these methods:
1206  
1207 $mess->header( $field => $val )
1208 $mess->push_header( $field => $val )
1209 $mess->init_header( $field => $val )
1210 $mess->remove_header( $field )
1211 $mess->remove_content_headers
1212 $mess->header_field_names
1213 $mess->scan( \&doit )
1214  
1215 $mess->date
1216 $mess->expires
1217 $mess->if_modified_since
1218 $mess->if_unmodified_since
1219 $mess->last_modified
1220 $mess->content_type
1221 $mess->content_encoding
1222 $mess->content_length
1223 $mess->content_language
1224 $mess->title
1225 $mess->user_agent
1226 $mess->server
1227 $mess->from
1228 $mess->referer
1229 $mess->www_authenticate
1230 $mess->authorization
1231 $mess->proxy_authorization
1232 $mess->authorization_basic
1233 $mess->proxy_authorization_basic
1234  
1235 =head1 AUTHOR
1236  
1237 Gisle Aas <gisle@activestate.com>
1238  
1239 =head1 COPYRIGHT AND LICENSE
1240  
1241 This software is copyright (c) 1994 by Gisle Aas.
1242  
1243 This is free software; you can redistribute it and/or modify it under
1244 the same terms as the Perl 5 programming language system itself.
1245  
1246 =cut
1247  
1248 __END__
↑ Summary Request/Common.pm »