| 1 | package Eve::HttpResponse::Psgi; | ||||||||||
| 2 | |||||||||||
| 3 | 10 | ▶ | use parent qw(Eve::HttpResponse); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 10 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 10 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 10 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 10 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 10 | ▶ | use open qw(:std :utf8); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 10 | ▶ | use charnames qw(:full); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 10 | ▶ | use HTTP::Status; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 10 | ▶ | use Encode (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 10 | ▶ | use Plack::Response (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | =head1 NAME | ||||||||||
| 17 | |||||||||||
| 18 | B<Eve::HttpResponse> - an HTTP response adapter. | ||||||||||
| 19 | |||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||
| 21 | |||||||||||
| 22 | use Eve::HttpResponse; | ||||||||||
| 23 | |||||||||||
| 24 | my $response = Eve::HttpResponse->new(nph_mode => 0); | ||||||||||
| 25 | |||||||||||
| 26 | $response->set_status(code => 302); | ||||||||||
| 27 | $response->set_header(name => 'Location', value => '/other'); | ||||||||||
| 28 | $response->set_cookie( | ||||||||||
| 29 | name => 'cookie1', | ||||||||||
| 30 | value => 'value', | ||||||||||
| 31 | domain => '.example.com', | ||||||||||
| 32 | path => '/some/', | ||||||||||
| 33 | expires => '+1d', | ||||||||||
| 34 | secure = >1); | ||||||||||
| 35 | $response->set_body(text => 'Hello world!'); | ||||||||||
| 36 | |||||||||||
| 37 | print $response->get_text(); | ||||||||||
| 38 | |||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||
| 40 | |||||||||||
| 41 | The class is an adapter for the Plack::Request module. It is used to | ||||||||||
| 42 | store the response data before it is being sent to the client. | ||||||||||
| 43 | |||||||||||
| 44 | =head1 METHODS | ||||||||||
| 45 | |||||||||||
| 46 | =head2 B<init()> | ||||||||||
| 47 | |||||||||||
| 48 | =cut | ||||||||||
| 49 | |||||||||||
| 50 | 11 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 51 | 11 | my $self = shift; | |||||||||
| 52 | |||||||||||
| 53 | 11 | $self->{'_psgi'} = Plack::Response->new(200); | |||||||||
| 54 | 11 | $self->SUPER::init(); | |||||||||
| 55 | |||||||||||
| 56 | 11 | return; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | =head2 B<set_header()> | ||||||||||
| 60 | |||||||||||
| 61 | Sets or overwrites an HTTP header of the response. | ||||||||||
| 62 | |||||||||||
| 63 | =head3 Arguments | ||||||||||
| 64 | |||||||||||
| 65 | =over 4 | ||||||||||
| 66 | |||||||||||
| 67 | =item C<name> | ||||||||||
| 68 | |||||||||||
| 69 | =item C<value> | ||||||||||
| 70 | |||||||||||
| 71 | =back | ||||||||||
| 72 | |||||||||||
| 73 | =cut | ||||||||||
| 74 | |||||||||||
| 75 | 8 | ▶ | sub set_header { | ||||||||
|
Subroutine
set_header: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 76 | 8 | my ($self, %arg_hash) = @_; | |||||||||
| 77 | 8 | Eve::Support::arguments(\%arg_hash, my ($name, $value)); | |||||||||
| 78 | |||||||||||
| 79 | 8 | ▶ | if ($name =~ /encoding|charset/i) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 4 | my $enc = Encode::find_encoding($value); | |||||||||
| 81 | 4 | ▶ | if (not defined $enc) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 1 | Eve::Error::Value->throw(message => 'Unknown charset: '.$value); | |||||||||
| 83 | } | ||||||||||
| 84 | 3 | $value = $enc->mime_name(); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 7 | $self->_psgi->header($name => $value); | |||||||||
| 88 | |||||||||||
| 89 | 7 | return; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | =head2 B<set_status()> | ||||||||||
| 93 | |||||||||||
| 94 | Sets or overwrites the HTTP response status. | ||||||||||
| 95 | |||||||||||
| 96 | =head3 Arguments | ||||||||||
| 97 | |||||||||||
| 98 | =over 4 | ||||||||||
| 99 | |||||||||||
| 100 | =item C<code> | ||||||||||
| 101 | |||||||||||
| 102 | =back | ||||||||||
| 103 | |||||||||||
| 104 | =cut | ||||||||||
| 105 | |||||||||||
| 106 | 4 | ▶ | sub set_status { | ||||||||
|
Subroutine
set_status: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 107 | 4 | my ($self, %arg_hash) = @_; | |||||||||
| 108 | 4 | Eve::Support::arguments(\%arg_hash, my $code); | |||||||||
| 109 | |||||||||||
| 110 | 4 | $self->_psgi->status($code); | |||||||||
| 111 | |||||||||||
| 112 | 4 | return; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | =head2 B<set_cookie()> | ||||||||||
| 116 | |||||||||||
| 117 | Sets an HTTP response cookie. | ||||||||||
| 118 | |||||||||||
| 119 | =head3 Arguments | ||||||||||
| 120 | |||||||||||
| 121 | =over 4 | ||||||||||
| 122 | |||||||||||
| 123 | =item C<name> | ||||||||||
| 124 | |||||||||||
| 125 | =item C<value> | ||||||||||
| 126 | |||||||||||
| 127 | =item C<domain> | ||||||||||
| 128 | |||||||||||
| 129 | =item C<path> | ||||||||||
| 130 | |||||||||||
| 131 | =item C<expires> | ||||||||||
| 132 | |||||||||||
| 133 | (optional) a cookie expiration time in the epoch format | ||||||||||
| 134 | |||||||||||
| 135 | =item C<secure> | ||||||||||
| 136 | |||||||||||
| 137 | (optional) defaults to false | ||||||||||
| 138 | |||||||||||
| 139 | =back | ||||||||||
| 140 | |||||||||||
| 141 | =cut | ||||||||||
| 142 | |||||||||||
| 143 | 5 | ▶ | sub set_cookie { | ||||||||
|
Subroutine
set_cookie: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 144 | 5 | my ($self, %arg_hash) = @_; | |||||||||
| 145 | 5 | Eve::Support::arguments(\%arg_hash, | |||||||||
| 146 | my ($name, $value), my $path = '/', | ||||||||||
| 147 | my ($domain, $expires, $secure) = ((\undef) x 3)); | ||||||||||
| 148 | |||||||||||
| 149 | 5 | $self->_psgi->cookies->{$name} = { | |||||||||
| 150 | value => $value, | ||||||||||
| 151 | path => $path, | ||||||||||
| 152 | domain => $domain, | ||||||||||
| 153 | expires => $expires, | ||||||||||
| 154 | secure => $secure | ||||||||||
| 155 | }; | ||||||||||
| 156 | |||||||||||
| 157 | 5 | return; | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | =head2 B<set_body()> | ||||||||||
| 161 | |||||||||||
| 162 | Sets or overwrites the HTTP response body. | ||||||||||
| 163 | |||||||||||
| 164 | =head3 Arguments | ||||||||||
| 165 | |||||||||||
| 166 | =over 4 | ||||||||||
| 167 | |||||||||||
| 168 | =item C<text> | ||||||||||
| 169 | |||||||||||
| 170 | =back | ||||||||||
| 171 | |||||||||||
| 172 | =cut | ||||||||||
| 173 | |||||||||||
| 174 | 3 | ▶ | sub set_body { | ||||||||
|
Subroutine
set_body: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 175 | 3 | my ($self, %arg_hash) = @_; | |||||||||
| 176 | 3 | Eve::Support::arguments(\%arg_hash, my $text); | |||||||||
| 177 | |||||||||||
| 178 | 10 | ▶ | use bytes; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 179 | 3 | $self->_psgi->body($text); | |||||||||
| 180 | 3 | $self->_psgi->content_length(length $text); | |||||||||
| 181 | 10 | ▶ | no bytes; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 182 | |||||||||||
| 183 | 3 | return; | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | =head2 B<get_text()> | ||||||||||
| 187 | |||||||||||
| 188 | =head3 Returns | ||||||||||
| 189 | |||||||||||
| 190 | The HTTP response as text. | ||||||||||
| 191 | |||||||||||
| 192 | =cut | ||||||||||
| 193 | |||||||||||
| 194 | 12 | ▶ | sub get_text { | ||||||||
|
Subroutine
get_text: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 195 | 12 | my $self = shift; | |||||||||
| 196 | |||||||||||
| 197 | 12 | my $result = $self->_psgi->finalize(); | |||||||||
| 198 | 12 | my $headers = ''; | |||||||||
| 199 | |||||||||||
| 200 | 12 | ▶ | while(@{$result->[1]}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 201 | 12 | my $name = shift(@{$result->[1]}); | |||||||||
| 202 | 12 | my $value = shift(@{$result->[1]}); | |||||||||
| 203 | |||||||||||
| 204 | 12 | $headers .= $name . ": " . $value . "\r\n"; | |||||||||
| 205 | } | ||||||||||
| 206 | |||||||||||
| 207 | return | ||||||||||
| 208 | "Status: " . $result->[0] . " " | ||||||||||
| 209 | . HTTP::Status::status_message($result->[0]) . "\r\n" | ||||||||||
| 210 | . $headers . "\r\n" | ||||||||||
| 211 | 12 | . join("\r\n", @{$result->[2]}); | |||||||||
| 212 | } | ||||||||||
| 213 | |||||||||||
| 214 | =head2 B<get_raw_list()> | ||||||||||
| 215 | |||||||||||
| 216 | =head3 Returns | ||||||||||
| 217 | |||||||||||
| 218 | The passthrough method for the Plack::Request C<finalize> method. | ||||||||||
| 219 | |||||||||||
| 220 | =cut | ||||||||||
| 221 | |||||||||||
| 222 | 1 | ▶ | sub get_raw_list { | ||||||||
|
Subroutine
get_raw_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 223 | 1 | my $self = shift; | |||||||||
| 224 | |||||||||||
| 225 | 1 | return $self->_psgi->finalize(); | |||||||||
| 226 | } | ||||||||||
| 227 | |||||||||||
| 228 | =head1 SEE ALSO | ||||||||||
| 229 | |||||||||||
| 230 | =over 4 | ||||||||||
| 231 | |||||||||||
| 232 | =item C<Encode> | ||||||||||
| 233 | |||||||||||
| 234 | =item C<HTTP::Status> | ||||||||||
| 235 | |||||||||||
| 236 | =item C<Plack::Request> | ||||||||||
| 237 | |||||||||||
| 238 | =item C<Eve::Class> | ||||||||||
| 239 | |||||||||||
| 240 | =back | ||||||||||
| 241 | |||||||||||
| 242 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 243 | |||||||||||
| 244 | Copyright 2012 Igor Zinovyev. | ||||||||||
| 245 | |||||||||||
| 246 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 247 | under the terms of either: the GNU General Public License as published | ||||||||||
| 248 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 249 | |||||||||||
| 250 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 251 | |||||||||||
| 252 | |||||||||||
| 253 | =head1 AUTHORS | ||||||||||
| 254 | |||||||||||
| 255 | =over 4 | ||||||||||
| 256 | |||||||||||
| 257 | =item L<Igor Zinovyev|mailto:zinigor@gmail.com> | ||||||||||
| 258 | |||||||||||
| 259 | =back | ||||||||||
| 260 | |||||||||||
| 261 | =cut | ||||||||||
| 262 | |||||||||||
| 263 | 10 | 1; | |||||||||