| 1 |
package Lemonldap::NG::Common::PSGI::Request; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Mouse; |
| 5 |
use JSON; |
| 6 |
use Plack::Request; |
| 7 |
use URI::Escape; |
| 8 |
|
| 9 |
our $VERSION = '2.19.0'; |
| 10 |
|
| 11 |
our @ISA = ('Plack::Request'); |
| 12 |
|
| 13 |
# http :// server / path ? query # fragment |
| 14 |
# m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|; |
| 15 |
|
| 16 |
sub BUILD { |
| 17 |
my ( $self, $env ) = @_; |
| 18 |
foreach ( keys %$env ) { |
| 19 |
$self->{$_} ||= $env->{$_} if (/^(?:HTTP|SSL)_/); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
sub new { |
| 24 |
my $self = Plack::Request::new(@_); |
| 25 |
$self->env->{REQUEST_URI} = $self->env->{X_ORIGINAL_URI} |
| 26 |
if ( $self->env->{X_ORIGINAL_URI} ); |
| 27 |
$self->env->{PATH_INFO} =~ s|//+|/|g; |
| 28 |
$self->env->{PATH_INFO} ||= '/'; |
| 29 |
$self->env->{REQUEST_URI} =~ s|^//+|/|g; |
| 30 |
$self->{uri} = uri_unescape( $self->env->{REQUEST_URI} ); |
| 31 |
$self->{data} = {}; |
| 32 |
$self->{request_id} = ( $self->env->{UNIQUE_ID} |
| 33 |
|| join( '', map { [ 0 .. 9, "a" .. "f" ]->[ rand 16 ] } 1 .. 16 ) ); |
| 34 |
$self->{error} = 0; |
| 35 |
$self->{respHeaders} = []; |
| 36 |
return bless( $self, $_[0] ); |
| 37 |
} |
| 38 |
|
| 39 |
sub data { return $_[0]->{data} } |
| 40 |
|
| 41 |
sub uri { return $_[0]->{uri} } |
| 42 |
|
| 43 |
sub request_id { return $_[0]->{request_id} } |
| 44 |
|
| 45 |
sub userData { |
| 46 |
my ( $self, $v ) = @_; |
| 47 |
return $self->{userData} = $v if ($v); |
| 48 |
return $self->{userData} |
| 49 |
|| { |
| 50 |
( $Lemonldap::NG::Handler::Main::tsv->{whatToTrace} |
| 51 |
|| '_whatToTrace' ) => $self->{user}, }; |
| 52 |
} |
| 53 |
|
| 54 |
sub respHeaders { |
| 55 |
my ( $self, $respHeaders ) = @_; |
| 56 |
$self->{respHeaders} = $respHeaders if ($respHeaders); |
| 57 |
return $self->{respHeaders}; |
| 58 |
} |
| 59 |
|
| 60 |
sub spliceHdrs { |
| 61 |
my ($self) = @_; |
| 62 |
return splice @{ $self->{respHeaders} }; |
| 63 |
} |
| 64 |
|
| 65 |
sub accept { $_[0]->env->{HTTP_ACCEPT} } |
| 66 |
sub encodings { $_[0]->env->{HTTP_ACCEPT_ENCODING} } |
| 67 |
sub languages { $_[0]->env->{HTTP_ACCEPT_LANGUAGE} } |
| 68 |
sub authorization { $_[0]->env->{HTTP_AUTHORIZATION} } |
| 69 |
sub hostname { $_[0]->env->{HTTP_HOST} } |
| 70 |
sub origin { $_[0]->env->{HTTP_ORIGIN} } |
| 71 |
sub referer { $_[0]->env->{REFERER} } |
| 72 |
sub query_string { $_[0]->env->{QUERY_STRING} } |
| 73 |
|
| 74 |
sub error { |
| 75 |
my ( $self, $err ) = @_; |
| 76 |
$self->{error} = $err if ($err); |
| 77 |
return $self->{error}; |
| 78 |
} |
| 79 |
|
| 80 |
*params = \&Plack::Request::param; |
| 81 |
|
| 82 |
sub set_param { |
| 83 |
my ( $self, $k, $v ) = @_; |
| 84 |
$self->param; |
| 85 |
$self->env->{'plack.request.merged'}->{$k} = |
| 86 |
$self->env->{'plack.request.query'}->{$k} = $v; |
| 87 |
} |
| 88 |
|
| 89 |
sub wantJSON { |
| 90 |
return 1 |
| 91 |
if ( defined $_[0]->accept |
| 92 |
and $_[0]->accept =~ m#(?:application|text)/json# ); |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
# JSON parser |
| 97 |
sub jsonBodyToObj { |
| 98 |
my $self = shift; |
| 99 |
return $self->{json_body} if ( $self->{json_body} ); |
| 100 |
unless ( $self->content_type =~ /application\/json/ ) { |
| 101 |
$self->error('Data is not JSON'); |
| 102 |
return undef; |
| 103 |
} |
| 104 |
unless ( $self->body ) { |
| 105 |
$self->error('No data'); |
| 106 |
return undef; |
| 107 |
} |
| 108 |
my $j = eval { from_json( $self->content, { allow_nonref => 1 } ) }; |
| 109 |
if ($@) { |
| 110 |
$self->error("$@$!"); |
| 111 |
return undef; |
| 112 |
} |
| 113 |
return $self->{json_body} = $j; |
| 114 |
} |
| 115 |
|
| 116 |
1; |
| 117 |
__END__ |
| 118 |
|
| 119 |
=head1 NAME |
| 120 |
|
| 121 |
=encoding utf8 |
| 122 |
|
| 123 |
Lemonldap::NG::Common::PSGI::Request - HTTP request object for Lemonldap::NG |
| 124 |
PSGIs |
| 125 |
|
| 126 |
=head1 SYNOPSIS |
| 127 |
|
| 128 |
package My::PSGI; |
| 129 |
|
| 130 |
use base Lemonldap::NG::Common::PSGI; |
| 131 |
|
| 132 |
# See Lemonldap::NG::Common::PSGI |
| 133 |
... |
| 134 |
|
| 135 |
sub handler { |
| 136 |
my ( $self, $req ) = @_; |
| 137 |
# Do something and return a PSGI response |
| 138 |
# NB: $req is a Lemonldap::NG::Common::PSGI::Request object |
| 139 |
if ( $req->accept eq 'text/plain' ) { ... } |
| 140 |
|
| 141 |
return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Body lines' ] ]; |
| 142 |
} |
| 143 |
|
| 144 |
=head1 DESCRIPTION |
| 145 |
|
| 146 |
This package provides HTTP request objects used by Lemonldap::NG PSGIs. It |
| 147 |
contains common accessors to work with request. Note that it inherits from |
| 148 |
L<Plack::Request>. |
| 149 |
|
| 150 |
=head1 METHODS |
| 151 |
|
| 152 |
All methods of L<Plack::Request> are available. |
| 153 |
Lemonldap::NG::Common::PSGI::Request adds the following methods: |
| 154 |
|
| 155 |
=head2 accept |
| 156 |
|
| 157 |
'Accept' header content. |
| 158 |
|
| 159 |
=head2 encodings |
| 160 |
|
| 161 |
'Accept-Encoding' header content. |
| 162 |
|
| 163 |
=head2 error |
| 164 |
|
| 165 |
Used to store error value (usually a L<Lemonldap::NG::Portal::Main::Constants> |
| 166 |
constant). |
| 167 |
|
| 168 |
=head2 jsonBodyToObj |
| 169 |
|
| 170 |
Get the content of a JSON POST request as Perl object. |
| 171 |
|
| 172 |
=head2 languages |
| 173 |
|
| 174 |
'Accept-Language header content. |
| 175 |
|
| 176 |
=head2 hostname |
| 177 |
|
| 178 |
'Host' header content. |
| 179 |
|
| 180 |
=head2 read-body |
| 181 |
|
| 182 |
Since body() methods returns an L<IO::Handle> object, this method reads and |
| 183 |
return the request content as string. |
| 184 |
|
| 185 |
=head2 respHeaders |
| 186 |
|
| 187 |
Accessor to 'respHeaders' property. It is used to store headers that have to |
| 188 |
be pushed in response (see L<Lemonldap::NG::Common::PSGI>). |
| 189 |
|
| 190 |
Be careful, it contains an array reference, not a hash one because headers |
| 191 |
can be multi-valued. |
| 192 |
|
| 193 |
Example: |
| 194 |
|
| 195 |
# Set headers |
| 196 |
$req->respHeaders( "Location" => "http://x.y.z/", Etag => "XYZ", ); |
| 197 |
# Add header |
| 198 |
$req->respHeaders->{"X-Key"} = "Value"; |
| 199 |
|
| 200 |
=head2 spliceHdrs |
| 201 |
|
| 202 |
Returns headers array and flush it. |
| 203 |
|
| 204 |
=head2 set_param( $key, $value ) |
| 205 |
|
| 206 |
L<Plack::Request> param() method is read-only. This method can be used to |
| 207 |
modify a GET parameter value |
| 208 |
|
| 209 |
=head2 uri |
| 210 |
|
| 211 |
REQUEST_URI environment variable decoded. |
| 212 |
|
| 213 |
=head2 user |
| 214 |
|
| 215 |
REMOTE_USER environment variable. It contains username when a server |
| 216 |
authentication is done. |
| 217 |
|
| 218 |
=head2 userData |
| 219 |
|
| 220 |
Hash reference to the session information (if app inherits from |
| 221 |
L<Lemonldap::NG::Handler::PSGI> or any other handler PSGI package). If no |
| 222 |
session information is available, it contains: |
| 223 |
|
| 224 |
{ _whatToTrace => <REMOTE-USER value> } |
| 225 |
|
| 226 |
=head2 wantJSON |
| 227 |
|
| 228 |
Return true if current request ask JSON content (verify that "Accept" header |
| 229 |
contains "application/json" or "text/json"). |
| 230 |
|
| 231 |
=head1 SEE ALSO |
| 232 |
|
| 233 |
L<http://lemonldap-ng.org/>, L<Lemonldap::NG::Common::PSGI>, |
| 234 |
L<Lemonldap::NG::Hander::PSGI>, L<Plack::Request>, |
| 235 |
L<Lemonldap::NG::Portal::Main::Constants>, |
| 236 |
|
| 237 |
=head1 AUTHORS |
| 238 |
|
| 239 |
=over |
| 240 |
|
| 241 |
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team> |
| 242 |
|
| 243 |
=back |
| 244 |
|
| 245 |
=head1 BUG REPORT |
| 246 |
|
| 247 |
Use OW2 system to report bug or ask for features: |
| 248 |
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues> |
| 249 |
|
| 250 |
=head1 DOWNLOAD |
| 251 |
|
| 252 |
Lemonldap::NG is available at |
| 253 |
L<https://lemonldap-ng.org/download> |
| 254 |
|
| 255 |
=head1 COPYRIGHT AND LICENSE |
| 256 |
|
| 257 |
See COPYING file for details. |
| 258 |
|
| 259 |
This library is free software; you can redistribute it and/or modify |
| 260 |
it under the terms of the GNU General Public License as published by |
| 261 |
the Free Software Foundation; either version 2, or (at your option) |
| 262 |
any later version. |
| 263 |
|
| 264 |
This program is distributed in the hope that it will be useful, |
| 265 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 266 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 267 |
GNU General Public License for more details. |
| 268 |
|
| 269 |
You should have received a copy of the GNU General Public License |
| 270 |
along with this program. If not, see L<http://www.gnu.org/licenses/>. |
| 271 |
|
| 272 |
=cut |