| 1 | package Eve::HttpRequest::Psgi; | ||||||||||
| 2 | |||||||||||
| 3 | 9 | ▶ | use parent qw(Eve::HttpRequest); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 9 | ▶ | use Hash::MultiValue; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 9 | ▶ | use JSON::XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 9 | ▶ | use Plack::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | B<Eve::HttpRequest::Psgi> - an HTTP request adapter for the PSGI interface. | ||||||||||
| 15 | |||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||
| 17 | |||||||||||
| 18 | use Eve::HttpRequest::Psgi; | ||||||||||
| 19 | |||||||||||
| 20 | my $request = Eve::HttpRequest->new( | ||||||||||
| 21 | uri_constructor => sub { return Eve::Uri->new(@_); }, | ||||||||||
| 22 | env_hash => $env_hash); | ||||||||||
| 23 | |||||||||||
| 24 | my $uri = $request->get_uri(); | ||||||||||
| 25 | my $method = $request->get_method(); | ||||||||||
| 26 | my $param = $request->get_parameter(name => 'some_parameter'); | ||||||||||
| 27 | my @param_list = $request->get_parameter(name => 'some_list'); | ||||||||||
| 28 | my $cookie = $request->get_cookie(name => 'some_cookie'); | ||||||||||
| 29 | |||||||||||
| 30 | my $upload_hash = $request->get_upload(name => 'huge_file'); | ||||||||||
| 31 | |||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||
| 33 | |||||||||||
| 34 | The class adapts some functionality of the C<Plack::Request> module. | ||||||||||
| 35 | |||||||||||
| 36 | =head3 Constructor arguments | ||||||||||
| 37 | |||||||||||
| 38 | =over 4 | ||||||||||
| 39 | |||||||||||
| 40 | =item C<uri_constructor> | ||||||||||
| 41 | |||||||||||
| 42 | a code reference returning a newly constructed URI | ||||||||||
| 43 | |||||||||||
| 44 | =item C<env_hash> | ||||||||||
| 45 | |||||||||||
| 46 | an environment hash that is supplied to an application by a PSGI handler. | ||||||||||
| 47 | |||||||||||
| 48 | =back | ||||||||||
| 49 | |||||||||||
| 50 | =head1 METHODS | ||||||||||
| 51 | |||||||||||
| 52 | =head2 B<init()> | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub init { | ||||||||
|
Subroutine
init: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 57 | 0 | my ($self, %arg_hash) = @_; | |||||||||
| 58 | 0 | Eve::Support::arguments( | |||||||||
| 59 | \%arg_hash, my ($uri_constructor, $env_hash)); | ||||||||||
| 60 | |||||||||||
| 61 | 0 | $self->{'cgi'} = Plack::Request->new($env_hash); | |||||||||
| 62 | 0 | $self->{'_uri_constructor'} = $uri_constructor; | |||||||||
| 63 | |||||||||||
| 64 | $self->{'_body_parameters'} = Hash::MultiValue->from_mixed({ | ||||||||||
| 65 | 0 | %{$self->cgi->query_parameters()->as_hashref_mixed()}, | |||||||||
| 66 | 0 | %{$self->cgi->body_parameters()->as_hashref_mixed()}}); | |||||||||
| 67 | 0 | $self->{'_cookies'} = $self->cgi->cookies(); | |||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | if ($self->cgi->headers->content_type() eq 'application/json') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | $self->_body_parameters = Hash::MultiValue->new( | ||||||||||
| 71 | 0 | %{$self->cgi->query_parameters()->as_hashref_mixed()}, | |||||||||
| 72 | 0 | %{JSON::XS->new()->utf8()->decode($self->cgi->content())}); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 0 | return; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | =head2 B<get_uri> | ||||||||||
| 79 | |||||||||||
| 80 | Returns an URI instance built from an HTTP request URI. | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | sub get_uri { | ||||||||
|
Subroutine
get_uri: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 85 | 0 | my $self = shift; | |||||||||
| 86 | |||||||||||
| 87 | 0 | my $uri = $self->_uri_constructor->( | |||||||||
| 88 | string => $self->cgi->uri()->canonical()); | ||||||||||
| 89 | |||||||||||
| 90 | 0 | $uri->set_query_hash(hash => $self->cgi->query_parameters()->as_hashref()); | |||||||||
| 91 | |||||||||||
| 92 | 0 | return $uri; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | =head2 B<get_method> | ||||||||||
| 96 | |||||||||||
| 97 | Returns an HTTP method name. | ||||||||||
| 98 | |||||||||||
| 99 | =cut | ||||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | sub get_method { | ||||||||
|
Subroutine
get_method: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 102 | 0 | my $self = shift; | |||||||||
| 103 | |||||||||||
| 104 | 0 | return $self->cgi->method(); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | =head2 B<get_parameter()> | ||||||||||
| 108 | |||||||||||
| 109 | Returns a request parameter value or a list of values for a specified | ||||||||||
| 110 | parameter name. When called in a scalar context, will return a single | ||||||||||
| 111 | value, which for a multivalue parameter will result in a first value | ||||||||||
| 112 | of the list: | ||||||||||
| 113 | |||||||||||
| 114 | my $parameter_value = $request->get_parameter(name => 'some'); | ||||||||||
| 115 | |||||||||||
| 116 | To receive a list of all values for a multivalue parameter, call the | ||||||||||
| 117 | method in a list context: | ||||||||||
| 118 | |||||||||||
| 119 | my @parameter_value_list = $request->get_parameter(name => 'some'); | ||||||||||
| 120 | |||||||||||
| 121 | =head3 Arguments | ||||||||||
| 122 | |||||||||||
| 123 | =over 4 | ||||||||||
| 124 | |||||||||||
| 125 | =item C<name> | ||||||||||
| 126 | |||||||||||
| 127 | =back | ||||||||||
| 128 | |||||||||||
| 129 | =cut | ||||||||||
| 130 | |||||||||||
| 131 | 0 | ▶ | sub get_parameter { | ||||||||
|
Subroutine
get_parameter: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 132 | 0 | my ($self, %arg_hash) = @_; | |||||||||
| 133 | 0 | Eve::Support::arguments(\%arg_hash, my ($name)); | |||||||||
| 134 | |||||||||||
| 135 | 0 | my @result = $self->_body_parameters->get_all($name); | |||||||||
| 136 | 0 | my $result = $self->_body_parameters->get($name); | |||||||||
| 137 | |||||||||||
| 138 | 0 | ▶ | if (scalar @result > 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 0 | return @result; | |||||||||
| 140 | } else { | ||||||||||
| 141 | 0 | return $result; | |||||||||
| 142 | } | ||||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | =head2 B<get_upload()> | ||||||||||
| 146 | |||||||||||
| 147 | Returns a hash containing information about an uploaded file. This | ||||||||||
| 148 | hash will have C<tempname>, C<size> and C<filename> keys, values for | ||||||||||
| 149 | which represent the temporary file path, its size in bytes and the | ||||||||||
| 150 | original name of the file. | ||||||||||
| 151 | |||||||||||
| 152 | =head3 Arguments | ||||||||||
| 153 | |||||||||||
| 154 | =over 4 | ||||||||||
| 155 | |||||||||||
| 156 | =item C<name> | ||||||||||
| 157 | |||||||||||
| 158 | =back | ||||||||||
| 159 | |||||||||||
| 160 | =cut | ||||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | sub get_upload { | ||||||||
|
Subroutine
get_upload: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 163 | 0 | my ($self, %arg_hash) = @_; | |||||||||
| 164 | 0 | Eve::Support::arguments(\%arg_hash, my ($name)); | |||||||||
| 165 | |||||||||||
| 166 | 0 | my $upload = $self->cgi->uploads->{$name}; | |||||||||
| 167 | |||||||||||
| 168 | 0 | ▶ | my $result = defined $upload ? { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 'tempname' => $upload->tempname, | ||||||||||
| 170 | 'size' => $upload->size, | ||||||||||
| 171 | 'filename' => $upload->filename, | ||||||||||
| 172 | 'content_type' => $upload->content_type} : undef; | ||||||||||
| 173 | |||||||||||
| 174 | 0 | return $result; | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | =head2 B<get_parameter_hash()> | ||||||||||
| 178 | |||||||||||
| 179 | Returns a hash reference with the requested parameter values. | ||||||||||
| 180 | |||||||||||
| 181 | =cut | ||||||||||
| 182 | |||||||||||
| 183 | 0 | ▶ | sub get_parameter_hash { | ||||||||
|
Subroutine
get_parameter_hash: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 184 | 0 | my $self = shift; | |||||||||
| 185 | |||||||||||
| 186 | 0 | return $self->_body_parameters; | |||||||||
| 187 | } | ||||||||||
| 188 | |||||||||||
| 189 | =head2 B<get_cookie()> | ||||||||||
| 190 | |||||||||||
| 191 | Returns a request cookie value for a specified name. | ||||||||||
| 192 | |||||||||||
| 193 | =head3 Arguments | ||||||||||
| 194 | |||||||||||
| 195 | =over 4 | ||||||||||
| 196 | |||||||||||
| 197 | =item C<name> | ||||||||||
| 198 | |||||||||||
| 199 | =back | ||||||||||
| 200 | |||||||||||
| 201 | =cut | ||||||||||
| 202 | |||||||||||
| 203 | 0 | ▶ | sub get_cookie { | ||||||||
|
Subroutine
get_cookie: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 204 | 0 | my ($self, %arg_hash) = @_; | |||||||||
| 205 | 0 | Eve::Support::arguments(\%arg_hash, my ($name)); | |||||||||
| 206 | |||||||||||
| 207 | my $result = defined $self->_cookies->{$name} ? | ||||||||||
| 208 | 0 | ▶ | $self->_cookies->{$name} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 209 | |||||||||||
| 210 | 0 | return $result; | |||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | =head1 SEE ALSO | ||||||||||
| 214 | |||||||||||
| 215 | =over 4 | ||||||||||
| 216 | |||||||||||
| 217 | =item C<CGI> | ||||||||||
| 218 | |||||||||||
| 219 | =item C<Eve::Class> | ||||||||||
| 220 | |||||||||||
| 221 | =item C<Eve::Uri> | ||||||||||
| 222 | |||||||||||
| 223 | =back | ||||||||||
| 224 | |||||||||||
| 225 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 226 | |||||||||||
| 227 | Copyright 2012 Igor Zinovyev. | ||||||||||
| 228 | |||||||||||
| 229 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 230 | under the terms of either: the GNU General Public License as published | ||||||||||
| 231 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 232 | |||||||||||
| 233 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 234 | |||||||||||
| 235 | |||||||||||
| 236 | =head1 AUTHORS | ||||||||||
| 237 | |||||||||||
| 238 | =over 4 | ||||||||||
| 239 | |||||||||||
| 240 | =item L<Sergey Konoplev|mailto:gray.ru@gmail.com> | ||||||||||
| 241 | |||||||||||
| 242 | =item L<Igor Zinovyev|mailto:zinigor@gmail.com> | ||||||||||
| 243 | |||||||||||
| 244 | =cut | ||||||||||
| 245 | |||||||||||
| 246 | 9 | 1; | |||||||||