/usr/share/perl5/Mojo/Content/Single.pm is in libmojolicious-perl 2.98+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | package Mojo::Content::Single;
use Mojo::Base 'Mojo::Content';
use Mojo::Asset::Memory;
use Mojo::Content::MultiPart;
has asset => sub { Mojo::Asset::Memory->new(auto_upgrade => 1) };
has auto_upgrade => 1;
sub new {
my $self = shift->SUPER::new(@_);
# Default content parser
$self->{read} = $self->on(
read => sub {
my ($self, $chunk) = @_;
$self->asset($self->asset->add_chunk($chunk));
}
);
return $self;
}
sub body_contains { shift->asset->contains(shift) >= 0 }
sub body_size {
my $self = shift;
return ($self->headers->content_length || 0) if $self->{dynamic};
return $self->asset->size;
}
sub clone {
my $self = shift;
return unless my $clone = $self->SUPER::clone();
return $clone->asset($self->asset);
}
sub get_body_chunk {
my ($self, $offset) = @_;
# Body generator
return $self->generate_body_chunk($offset) if $self->{dynamic};
# Normal content
return $self->asset->get_chunk($offset);
}
sub parse {
my $self = shift;
# Parse headers
$self->parse_until_body(@_);
# Parse body
return $self->SUPER::parse
unless $self->auto_upgrade && defined($self->boundary);
# Content needs to be upgraded to multipart
$self->unsubscribe(read => $self->{read});
my $multi = Mojo::Content::MultiPart->new($self);
$self->emit(upgrade => $multi);
return $multi->parse;
}
1;
=head1 NAME
Mojo::Content::Single - HTTP 1.1 content container
=head1 SYNOPSIS
use Mojo::Content::Single;
my $single = Mojo::Content::Single->new;
$single->parse("Content-Length: 12\r\n\r\nHello World!");
=head1 DESCRIPTION
L<Mojo::Content::Single> is a container for HTTP 1.1 content as described in
RFC 2616.
=head1 EVENTS
L<Mojo::Content::Single> inherits all events from L<Mojo::Content> and can
emit the following new ones.
=head2 C<upgrade>
$single->on(upgrade => sub {
my ($single, $multi) = @_;
...
});
Emitted when content gets upgraded to a L<Mojo::Content::MultiPart> object.
$single->on(upgrade => sub {
my ($single, $multi) = @_;
return unless $multi->headers->content_type =~ /multipart\/([^;]+)/i;
say "Multipart: $1";
});
=head1 ATTRIBUTES
L<Mojo::Content::Single> inherits all attributes from L<Mojo::Content> and
implements the following new ones.
=head2 C<asset>
my $asset = $single->asset;
$single = $single->asset(Mojo::Asset::Memory->new);
The actual content, defaults to a L<Mojo::Asset::Memory> object with
C<auto_upgrade> enabled.
=head2 C<auto_upgrade>
my $upgrade = $single->auto_upgrade;
$single = $single->auto_upgrade(0);
Try to detect multipart content and automatically upgrade to a
L<Mojo::Content::MultiPart> object, defaults to C<1>.
=head1 METHODS
L<Mojo::Content::Single> inherits all methods from L<Mojo::Content> and
implements the following new ones.
=head2 C<new>
my $single = Mojo::Content::Single->new;
Construct a new L<Mojo::Content::Single> object and subscribe to C<read> event
with default content parser.
=head2 C<body_contains>
my $success = $single->body_contains('1234567');
Check if content contains a specific string.
=head2 C<body_size>
my $size = $single->body_size;
Content size in bytes.
=head2 C<clone>
my $clone = $single->clone;
Clone content if possible, otherwise return C<undef>.
=head2 C<get_body_chunk>
my $chunk = $single->get_body_chunk(0);
Get a chunk of content starting from a specfic position.
=head2 C<parse>
$single = $single->parse("Content-Length: 12\r\n\r\nHello World!");
my $multi = $single->parse("Content-Type: multipart/form-data\r\n\r\n");
Parse content chunk and upgrade to L<Mojo::Content::MultiPart> object if
possible.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|