/usr/share/perl5/Plack/Response.pm is in libplack-perl 1.0030-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 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | package Plack::Response;
use strict;
use warnings;
our $VERSION = '1.0030';
use Plack::Util::Accessor qw(body status);
use Carp ();
use Scalar::Util ();
use HTTP::Headers;
use URI::Escape ();
sub code { shift->status(@_) }
sub content { shift->body(@_) }
sub new {
my($class, $rc, $headers, $content) = @_;
my $self = bless {}, $class;
$self->status($rc) if defined $rc;
$self->headers($headers) if defined $headers;
$self->body($content) if defined $content;
$self;
}
sub headers {
my $self = shift;
if (@_) {
my $headers = shift;
if (ref $headers eq 'ARRAY') {
Carp::carp("Odd number of headers") if @$headers % 2 != 0;
$headers = HTTP::Headers->new(@$headers);
} elsif (ref $headers eq 'HASH') {
$headers = HTTP::Headers->new(%$headers);
}
return $self->{headers} = $headers;
} else {
return $self->{headers} ||= HTTP::Headers->new();
}
}
sub cookies {
my $self = shift;
if (@_) {
$self->{cookies} = shift;
} else {
return $self->{cookies} ||= +{ };
}
}
sub header { shift->headers->header(@_) } # shortcut
sub content_length {
shift->headers->content_length(@_);
}
sub content_type {
shift->headers->content_type(@_);
}
sub content_encoding {
shift->headers->content_encoding(@_);
}
sub location {
my $self = shift;
return $self->headers->header('Location' => @_);
}
sub redirect {
my $self = shift;
if (@_) {
my $url = shift;
my $status = shift || 302;
$self->location($url);
$self->status($status);
}
return $self->location;
}
sub finalize {
my $self = shift;
Carp::croak "missing status" unless $self->status();
my $headers = $self->headers;
my @headers;
$headers->scan(sub{
my ($k,$v) = @_;
$v =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP
$v =~ s/\015|\012//g; # remove CR and LF since the char is invalid here
push @headers, $k, $v;
});
$self->_finalize_cookies(\@headers);
return [
$self->status,
\@headers,
$self->_body,
];
}
sub to_app {
my $self = shift;
return sub { $self->finalize };
}
sub _body {
my $self = shift;
my $body = $self->body;
$body = [] unless defined $body;
if (!ref $body or Scalar::Util::blessed($body) && overload::Method($body, q("")) && !$body->can('getline')) {
return [ $body ];
} else {
return $body;
}
}
sub _finalize_cookies {
my($self, $headers) = @_;
while (my($name, $val) = each %{$self->cookies}) {
my $cookie = $self->_bake_cookie($name, $val);
push @$headers, 'Set-Cookie' => $cookie;
}
}
sub _bake_cookie {
my($self, $name, $val) = @_;
return '' unless defined $val;
$val = { value => $val } unless ref $val eq 'HASH';
my @cookie = ( URI::Escape::uri_escape($name) . "=" . URI::Escape::uri_escape($val->{value}) );
push @cookie, "domain=" . $val->{domain} if $val->{domain};
push @cookie, "path=" . $val->{path} if $val->{path};
push @cookie, "expires=" . $self->_date($val->{expires}) if $val->{expires};
push @cookie, "max-age=" . $val->{"max-age"} if $val->{"max-age"};
push @cookie, "secure" if $val->{secure};
push @cookie, "HttpOnly" if $val->{httponly};
return join "; ", @cookie;
}
my @MON = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my @WDAY = qw( Sun Mon Tue Wed Thu Fri Sat );
sub _date {
my($self, $expires) = @_;
if ($expires =~ /^\d+$/) {
# all numbers -> epoch date
# (cookies use '-' as date separator, HTTP uses ' ')
my($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($expires);
$year += 1900;
return sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",
$WDAY[$wday], $mday, $MON[$mon], $year, $hour, $min, $sec);
}
return $expires;
}
1;
__END__
=head1 NAME
Plack::Response - Portable HTTP Response object for PSGI response
=head1 SYNOPSIS
use Plack::Response;
sub psgi_handler {
my $env = shift;
my $res = Plack::Response->new(200);
$res->content_type('text/html');
$res->body("Hello World");
return $res->finalize;
}
=head1 DESCRIPTION
Plack::Response allows you a way to create PSGI response array ref through a simple API.
=head1 METHODS
=over 4
=item new
$res = Plack::Response->new;
$res = Plack::Response->new($status);
$res = Plack::Response->new($status, $headers);
$res = Plack::Response->new($status, $headers, $body);
Creates a new Plack::Response object.
=item status
$res->status(200);
$status = $res->status;
Sets and gets HTTP status code. C<code> is an alias.
=item headers
$headers = $res->headers;
$res->headers([ 'Content-Type' => 'text/html' ]);
$res->headers({ 'Content-Type' => 'text/html' });
$res->headers( HTTP::Headers->new );
Sets and gets HTTP headers of the response. Setter can take either an
array ref, a hash ref or L<HTTP::Headers> object containing a list of
headers.
=item body
$res->body($body_str);
$res->body([ "Hello", "World" ]);
$res->body($io);
Gets and sets HTTP response body. Setter can take either a string, an
array ref, or an IO::Handle-like object. C<content> is an alias.
Note that this method doesn't automatically set I<Content-Length> for
the response. You have to set it manually if you want, with the
C<content_length> method (see below).
=item header
$res->header('X-Foo' => 'bar');
my $val = $res->header('X-Foo');
Shortcut for C<< $res->headers->header >>.
=item content_type, content_length, content_encoding
$res->content_type('text/plain');
$res->content_length(123);
$res->content_encoding('gzip');
Shortcut for the equivalent get/set methods in C<< $res->headers >>.
=item redirect
$res->redirect($url);
$res->redirect($url, 301);
Sets redirect URL with an optional status code, which defaults to 302.
Note that this method doesn't normalize the given URI string. Users of
this module have to be responsible about properly encoding URI paths
and parameters.
=item location
Gets and sets C<Location> header.
Note that this method doesn't normalize the given URI string in the
setter. See above in C<redirect> for details.
=item cookies
$res->cookies->{foo} = 123;
$res->cookies->{foo} = { value => '123' };
Returns a hash reference containing cookies to be set in the
response. The keys of the hash are the cookies' names, and their
corresponding values are a plain string (for C<value> with everything
else defaults) or a hash reference that can contain keys such as
C<value>, C<domain>, C<expires>, C<path>, C<httponly>, C<secure>,
C<max-age>.
C<expires> can take a string or an integer (as an epoch time) and
B<does not> convert string formats such as C<+3M>.
$res->cookies->{foo} = {
value => 'test',
path => "/",
domain => '.example.com',
expires => time + 24 * 60 * 60,
};
=item finalize
$res->finalize;
Returns the status code, headers, and body of this response as a PSGI
response array reference.
=item to_app
$app = $res->to_app;
A helper shortcut for C<< sub { $res->finalize } >>.
=back
=head1 AUTHOR
Tokuhiro Matsuno
Tatsuhiko Miyagawa
=head1 SEE ALSO
L<Plack::Request>
=cut
|