/usr/share/perl5/Dancer2/Core/Role/Headers.pm is in libdancer2-perl 0.152000+dfsg-1.
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 | # ABSTRACT: Role for handling headers
package Dancer2::Core::Role::Headers;
$Dancer2::Core::Role::Headers::VERSION = '0.152000';
use Moo::Role;
use Dancer2::Core::Types;
use HTTP::Headers;
has headers => (
is => 'rw',
isa => InstanceOf ['HTTP::Headers'],
lazy => 1,
coerce => sub {
my ($value) = @_;
return $value if ref($value) eq 'HTTP::Headers';
HTTP::Headers->new( @{$value} );
},
default => sub {
HTTP::Headers->new();
},
);
sub header {
my $self = shift;
my $header = shift;
if (@_) {
$self->headers->header( $header => @_ );
}
else {
return $self->headers->header($header);
}
}
sub push_header {
my $self = shift;
my $header = shift;
if (@_) {
foreach my $h (@_) {
$self->headers->push_header( $header => $h );
}
}
else {
return $self->headers->header($header);
}
}
sub headers_to_array {
my $self = shift;
my $headers = [
map {
my $k = $_;
map {
my $v = $_;
$v =~ s/^(.+)\r?\n(.*)$/$1\r\n $2/;
( $k => $v )
} $self->headers->header($_);
} $self->headers->header_field_names
];
return $headers;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dancer2::Core::Role::Headers - Role for handling headers
=head1 VERSION
version 0.152000
=head1 DESCRIPTION
When a class consumes this role, it gets a C<headers> attribute and all the
helper methods to manipulate it.
This logic is contained in this role in order to reuse the code between
L<Dancer2::Core::Response> and L<Dancer2::Core::Request> objects.
=head1 ATTRIBUTES
=head2 headers
The attribute that store the headers in a L<HTTP::Headers> object.
That attribute coerces from ArrayRef and defaults to an empty L<HTTP::Headers>
instance.
=head1 METHODS
=head2 header($name)
Return the value of the given header, if present. If the header has multiple
values, returns the list of values if called in list context, the first one
if in scalar context.
=head2 push_header
Add the header no matter if it already exists or not.
$self->push_header( 'X-Wing' => '1' );
It can also be called with multiple values to add many times the same header
with different values:
$self->push_header( 'X-Wing' => 1, 2, 3 );
=head2 headers_to_array
Convert the C<headers> attribute to an ArrayRef.
=head1 AUTHOR
Dancer Core Developers
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|