Mojo/Content/Single.pm

filter: Statementstmt 100% 34 / 34 Branchbran 87.5% 7 / 8 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 90.2% 65 / 72 CC 8 SCAR 9.0
CC
8
Cov
92%
body_size
5.0
parse
3.0
clone
2.0
CRAP 2.4
« Mojo/DynamicMethods.pm ↑ Summary Mojolicious/Plugins.pm »
1 package Mojo::Content::Single;
2 30 ▶use Mojo::Base 'Mojo::Content';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 30 ▶use Mojo::Asset::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Mojo::Content::MultiPart;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 529 ▶has asset => sub { Mojo::Asset::Memory->new(auto_upgrade => 1) };
Subroutine __ANON__: called CC=1 SCAR=0.0
8 30 has auto_upgrade => 1;
9  
10 32 ▶sub body_contains { shift->asset->contains(shift) >= 0 }
Subroutine body_contains: called CC=1 SCAR=0.0
Pod documented
11  
12 587 ▶sub body_size {
Subroutine body_size: called CC=4 SCAR=16.1
Pod documented
13 587 my $self = shift;
14 587 ▶ return ($self->headers->content_length || 0) if $self->is_dynamic;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_dynamic 0 587 587
Condition0%0/2
$self->headers->content_length || 0
l!l
00
MC/DC0%0/1
$self->headers->content_length || 0
$self->headers->content_length
Truth table0%0/2
A
A= $self->headers->content_length
Aresult
11
00
15 587 ▶ return $self->{body_size} //= $self->asset->size;
Condition66%2/3
$self->{"body_size"} //= $self->asset->size
l!l&&r!l&&!r
2143730
MC/DC0%0/2
$self->{"body_size"} //= $self->asset->size
$self->{"body_size"} $self->asset->size
Truth table66%2/3
A //= B
A= $self->{"body_size"}B= $self->asset->size
ABresult
1X1
011
000
16 }
17  
18 17 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
19 17 my $self = shift;
20 17 ▶ return undef unless my $clone = $self->SUPER::clone();
Branch: 1 branch
BranchTrueFalseTotal
unless my $clone = $self->SUPER::clone 4 13 17
21 13 return $clone->asset($self->asset);
22 }
23  
24 439 ▶sub get_body_chunk {
Subroutine get_body_chunk: called CC=2 SCAR=6.9
Pod documented
25 439 my ($self, $offset) = @_;
26 439 ▶ return $self->generate_body_chunk($offset) if $self->is_dynamic;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_dynamic 58 381 439
27 381 return $self->asset->get_chunk($offset);
28 }
29  
30 1214 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
31 1214 my $self = shift->SUPER::new(@_);
32 1214 ▶ $self->{read} = $self->on(read => sub { $_[0]->asset($_[0]->asset->add_chunk($_[1])) });
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1214 return $self;
34 }
35  
36 984 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
37 984 my $self = shift;
38  
39 # Parse headers
40 984 $self->_parse_until_body(@_);
41  
42 # Parse body
43 984 ▶ return $self->SUPER::parse unless $self->auto_upgrade && defined $self->boundary;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auto_upgrade and defined $self->boundary 957 27 984
Condition100%3/3
$self->auto_upgrade and defined $self->boundary
!ll&&!rl&&r
1893927
MC/DC100%2/2
$self->auto_upgrade and defined $self->boundary
$self->auto_upgrade defined $self->boundary
Truth table100%3/3
A and B
A= $self->auto_upgradeB= defined $self->boundary
ABresult
0X0
100
111
44  
45 # Content needs to be upgraded to multipart
46 27 $self->unsubscribe(read => $self->{read});
47 27 my $multi = Mojo::Content::MultiPart->new(%$self);
48 27 $self->emit(upgrade => $multi);
49 27 return $multi->parse;
50 }
51  
52 30 1;
53  
54 =encoding utf8
55  
56 =head1 NAME
57  
58 Mojo::Content::Single - HTTP content
59  
60 =head1 SYNOPSIS
61  
62 use Mojo::Content::Single;
63  
64 my $single = Mojo::Content::Single->new;
65 $single->parse("Content-Length: 12\x0d\x0a\x0d\x0aHello World!");
66 say $single->headers->content_length;
67  
68 =head1 DESCRIPTION
69  
70 L<Mojo::Content::Single> is a container for HTTP content, based on L<RFC 7230|https://tools.ietf.org/html/rfc7230> and
71 L<RFC 7231|https://tools.ietf.org/html/rfc7231>.
72  
73 =head1 EVENTS
74  
75 L<Mojo::Content::Single> inherits all events from L<Mojo::Content> and can emit the following new ones.
76  
77 =head2 upgrade
78  
79 $single->on(upgrade => sub ($single, $multi) {...});
80  
81 Emitted when content gets upgraded to a L<Mojo::Content::MultiPart> object.
82  
83 $single->on(upgrade => sub ($single, $multi) {
84 return unless $multi->headers->content_type =~ /multipart\/([^;]+)/i;
85 say "Multipart: $1";
86 });
87  
88 =head1 ATTRIBUTES
89  
90 L<Mojo::Content::Single> inherits all attributes from L<Mojo::Content> and implements the following new ones.
91  
92 =head2 asset
93  
94 my $asset = $single->asset;
95 $single = $single->asset(Mojo::Asset::Memory->new);
96  
97 The actual content, defaults to a L<Mojo::Asset::Memory> object with L<Mojo::Asset::Memory/"auto_upgrade"> enabled.
98  
99 =head2 auto_upgrade
100  
101 my $bool = $single->auto_upgrade;
102 $single = $single->auto_upgrade($bool);
103  
104 Try to detect multipart content and automatically upgrade to a L<Mojo::Content::MultiPart> object, defaults to a true
105 value.
106  
107 =head1 METHODS
108  
109 L<Mojo::Content::Single> inherits all methods from L<Mojo::Content> and implements the following new ones.
110  
111 =head2 body_contains
112  
113 my $bool = $single->body_contains('1234567');
114  
115 Check if content contains a specific string.
116  
117 =head2 body_size
118  
119 my $size = $single->body_size;
120  
121 Content size in bytes.
122  
123 =head2 clone
124  
125 my $clone = $single->clone;
126  
127 Return a new L<Mojo::Content::Single> object cloned from this content if possible, otherwise return C<undef>.
128  
129 =head2 get_body_chunk
130  
131 my $bytes = $single->get_body_chunk(0);
132  
133 Get a chunk of content starting from a specific position. Note that it might not be possible to get the same chunk
134 twice if content was generated dynamically.
135  
136 =head2 new
137  
138 my $single = Mojo::Content::Single->new;
139 my $single = Mojo::Content::Single->new(asset => Mojo::Asset::File->new);
140 my $single = Mojo::Content::Single->new({asset => Mojo::Asset::File->new});
141  
142 Construct a new L<Mojo::Content::Single> object and subscribe to event L<Mojo::Content/"read"> with default content
143 parser.
144  
145 =head2 parse
146  
147 $single = $single->parse("Content-Length: 12\x0d\x0a\x0d\x0aHello World!");
148 my $multi = $single->parse("Content-Type: multipart/form-data\x0d\x0a\x0d\x0a");
149  
150 Parse content chunk and upgrade to L<Mojo::Content::MultiPart> object if necessary.
151  
152 =head1 SEE ALSO
153  
154 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
155  
156 =cut
« Mojo/DynamicMethods.pm ↑ Summary Mojolicious/Plugins.pm »