2015-05-14 08:44:38 +02:00
|
|
|
package Lemonldap::NG::Common::PSGI::Request;
|
|
|
|
|
|
|
|
use strict;
|
2017-07-11 13:50:09 +02:00
|
|
|
use 5.14.0;
|
2015-05-14 08:44:38 +02:00
|
|
|
use Mouse;
|
2016-01-13 20:47:56 +01:00
|
|
|
use JSON;
|
2017-01-04 17:36:54 +01:00
|
|
|
use Plack::Request;
|
2015-05-14 08:44:38 +02:00
|
|
|
use URI::Escape;
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
our $VERSION = '2.0.0';
|
2015-05-14 08:44:38 +02:00
|
|
|
|
2017-01-04 17:36:54 +01:00
|
|
|
our @ISA = ('Plack::Request');
|
|
|
|
|
2015-05-14 08:44:38 +02:00
|
|
|
# http :// server / path ? query # fragment
|
|
|
|
# m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
|
|
|
|
|
2016-05-25 21:30:41 +02:00
|
|
|
sub BUILD {
|
|
|
|
my ( $self, $env ) = @_;
|
|
|
|
foreach ( keys %$env ) {
|
2016-05-27 07:00:47 +02:00
|
|
|
$self->{$_} ||= $env->{$_} if (/^(?:HTTP|SSL)_/);
|
2016-05-25 21:30:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 17:36:54 +01:00
|
|
|
sub new {
|
|
|
|
my $self = Plack::Request::new(@_);
|
|
|
|
$self->env->{REQUEST_URI} = $self->env->{X_ORIGINAL_URI}
|
|
|
|
if ( $self->env->{X_ORIGINAL_URI} );
|
|
|
|
$self->env->{PATH_INFO} =~ s|//+|/|g;
|
2018-03-27 22:45:06 +02:00
|
|
|
|
|
|
|
if ( my $tmp = $self->script_name ) {
|
|
|
|
$self->env->{PATH_INFO} =~ s|^$tmp|/|;
|
|
|
|
}
|
2018-06-19 19:58:12 +02:00
|
|
|
$self->env->{PATH_INFO} ||= '/';
|
2017-01-04 17:36:54 +01:00
|
|
|
$self->{uri} = uri_unescape( $self->env->{REQUEST_URI} );
|
2018-06-26 21:45:55 +02:00
|
|
|
$self->{uri} =~ s|^//+|/|g;
|
2017-03-28 23:07:49 +02:00
|
|
|
$self->{datas} = {};
|
2017-01-04 17:36:54 +01:00
|
|
|
$self->{error} = 0;
|
|
|
|
$self->{respHeaders} = [];
|
2017-03-28 23:07:49 +02:00
|
|
|
return bless( $self, $_[0] );
|
2016-04-02 22:17:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-28 23:07:49 +02:00
|
|
|
sub datas { $_[0]->{datas} }
|
|
|
|
|
2017-01-04 17:36:54 +01:00
|
|
|
sub uri { $_[0]->{uri} }
|
2016-04-02 22:17:39 +02:00
|
|
|
|
2017-01-04 17:36:54 +01:00
|
|
|
sub userData {
|
2017-02-15 07:41:50 +01:00
|
|
|
my ( $self, $v ) = @_;
|
|
|
|
return $_[0]->{userData} = $v if ($v);
|
2017-01-04 17:36:54 +01:00
|
|
|
return $_[0]->{userData} || { _whatToTrace => $_[0]->user, };
|
2015-05-14 08:44:38 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 17:36:54 +01:00
|
|
|
sub respHeaders {
|
|
|
|
my ( $self, $respHeaders ) = @_;
|
|
|
|
$self->{respHeaders} = $respHeaders if ($respHeaders);
|
|
|
|
return $self->{respHeaders};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub accept { $_[0]->env->{HTTP_ACCEPT} }
|
|
|
|
sub encodings { $_[0]->env->{HTTP_ACCEPT_ENCODING} }
|
|
|
|
sub languages { $_[0]->env->{HTTP_ACCEPT_LANGUAGE} }
|
|
|
|
sub authorization { $_[0]->env->{HTTP_AUTHORIZATION} }
|
|
|
|
sub hostname { $_[0]->env->{HTTP_HOST} }
|
|
|
|
sub referer { $_[0]->env->{REFERER} }
|
2017-09-26 21:11:04 +02:00
|
|
|
sub query_string { $_[0]->env->{QUERY_STRING} }
|
2017-01-04 17:36:54 +01:00
|
|
|
|
|
|
|
sub error {
|
|
|
|
my ( $self, $err ) = @_;
|
|
|
|
$self->{error} = $err if ($err);
|
|
|
|
return $self->{error};
|
|
|
|
}
|
|
|
|
|
|
|
|
*params = \&Plack::Request::param;
|
|
|
|
|
|
|
|
sub set_param {
|
|
|
|
my ( $self, $k, $v ) = @_;
|
|
|
|
$self->param;
|
|
|
|
$self->env->{'plack.request.merged'}->{$k} =
|
|
|
|
$self->env->{'plack.request.query'}->{$k} = $v;
|
|
|
|
}
|
2016-01-30 13:26:14 +01:00
|
|
|
|
2016-05-02 12:30:23 +02:00
|
|
|
sub wantJSON {
|
2016-11-14 11:27:18 +01:00
|
|
|
return 1
|
|
|
|
if ( defined $_[0]->accept
|
|
|
|
and $_[0]->accept =~ m#(?:application|text)/json# );
|
|
|
|
return 0;
|
2016-05-02 12:30:23 +02:00
|
|
|
}
|
|
|
|
|
2015-05-14 08:44:38 +02:00
|
|
|
# JSON parser
|
|
|
|
sub jsonBodyToObj {
|
|
|
|
my $self = shift;
|
2017-01-04 17:36:54 +01:00
|
|
|
return $self->{json_body} if ( $self->{json_body} );
|
|
|
|
unless ( $self->content_type =~ /application\/json/ ) {
|
2015-05-14 08:44:38 +02:00
|
|
|
$self->error('Data is not JSON');
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
unless ( $self->body ) {
|
|
|
|
$self->error('No data');
|
|
|
|
return undef;
|
|
|
|
}
|
2017-09-28 14:52:14 +02:00
|
|
|
my $j = eval { from_json( $self->content, { allow_nonref => 1 } ) };
|
2016-01-21 21:02:11 +01:00
|
|
|
if ($@) {
|
2015-05-14 08:44:38 +02:00
|
|
|
$self->error("$@$!");
|
|
|
|
return undef;
|
|
|
|
}
|
2017-01-04 17:36:54 +01:00
|
|
|
return $self->{json_body} = $j;
|
2016-04-02 22:17:39 +02:00
|
|
|
}
|
|
|
|
|
2015-05-14 08:44:38 +02:00
|
|
|
1;
|
2015-12-25 11:46:04 +01:00
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
=encoding utf8
|
|
|
|
|
|
|
|
Lemonldap::NG::Common::PSGI::Request - HTTP request object for Lemonldap::NG
|
|
|
|
PSGIs
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
package My::PSGI;
|
|
|
|
|
|
|
|
use base Lemonldap::NG::Common::PSGI;
|
|
|
|
|
|
|
|
# See Lemonldap::NG::Common::PSGI
|
|
|
|
...
|
|
|
|
|
2016-02-11 07:00:35 +01:00
|
|
|
sub handler {
|
2016-01-02 10:29:05 +01:00
|
|
|
my ( $self, $req ) = @_;
|
2015-12-25 11:46:04 +01:00
|
|
|
# Do something and return a PSGI response
|
|
|
|
# NB: $req is a Lemonldap::NG::Common::PSGI::Request object
|
|
|
|
if ( $req->accept eq 'text/plain' ) { ... }
|
|
|
|
|
|
|
|
return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Body lines' ] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This package provides HTTP request objects used by Lemonldap::NG PSGIs. It
|
2017-01-04 21:22:04 +01:00
|
|
|
contains common accessors to work with request. Note that it inherits from
|
|
|
|
L<Plack::Request>.
|
2015-12-25 11:46:04 +01:00
|
|
|
|
|
|
|
=head1 METHODS
|
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
All methods of L<Plack::Request> are available.
|
|
|
|
Lemonldap::NG::Common::PSGI::Request adds the following methods:
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 accept
|
2015-12-25 11:46:04 +01:00
|
|
|
|
|
|
|
'Accept' header content.
|
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 encodings
|
2015-12-25 11:46:04 +01:00
|
|
|
|
|
|
|
'Accept-Encoding' header content.
|
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 error
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
Used to store error value (usually a L<Lemonldap::NG::Portal::Main::Constants>
|
|
|
|
constant).
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 jsonBodyToObj
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
Get the content of a JSON POST request as Perl object.
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 languages
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
'Accept-Language header content.
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 hostname
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
'Host' header content.
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 read-body
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
Since body() methods returns an L<IO::Handle> object, this method reads and
|
|
|
|
return the request content as string.
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 respHeaders
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
Accessor to 'respHeaders' property. It is used to store headers that have to
|
|
|
|
be pushed in response (see L<Lemonldap::NG::Common::PSGI>).
|
2016-12-19 13:18:26 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
Be careful, it contains an array reference, not a hash one because headers
|
|
|
|
can be multi-valued.
|
2016-12-19 13:18:26 +01:00
|
|
|
|
2018-01-23 23:08:12 +01:00
|
|
|
Example:
|
2016-12-19 13:18:26 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
# Set headers
|
2016-12-19 13:18:26 +01:00
|
|
|
$req->respHeaders( "Location" => "http://x.y.z/", Etag => "XYZ", );
|
2017-01-04 21:22:04 +01:00
|
|
|
# Add header
|
|
|
|
$req->respHeaders->{"X-Key"} = "Value";
|
2016-12-19 13:18:26 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 set_param( $key, $value )
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
L<Plack::Request> param() method is read-only. This method can be used to
|
|
|
|
modify a GET parameter value
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 uri
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
REQUEST_URI environment variable decoded.
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 user
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
REMOTE_USER environment variable. It contains username when a server
|
|
|
|
authentication is done.
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 userData
|
2016-12-19 13:18:26 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
Hash reference to the session information (if app inherits from
|
|
|
|
L<Lemonldap::NG::Handler::PSGI> or any other handler PSGI package). If no
|
|
|
|
session information is available, it contains:
|
2016-12-19 13:18:26 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
{ _whatToTrace => <REMOTE-USER value> }
|
2015-12-25 11:46:04 +01:00
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=head2 wantJSON
|
2016-12-19 13:18:26 +01:00
|
|
|
|
|
|
|
Return true if current request ask JSON content (verify that "Accept" header
|
|
|
|
contains "application/json" or "text/json").
|
|
|
|
|
2015-12-25 11:46:04 +01:00
|
|
|
=head1 SEE ALSO
|
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
L<http://lemonldap-ng.org/>, L<Lemonldap::NG::Common::PSGI>,
|
|
|
|
L<Lemonldap::NG::Hander::PSGI>, L<Plack::Request>,
|
|
|
|
L<Lemonldap::NG::Portal::Main::Constants>,
|
2015-12-25 11:46:04 +01:00
|
|
|
|
|
|
|
=head1 AUTHORS
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
|
2015-12-25 11:46:04 +01:00
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
=head1 BUG REPORT
|
|
|
|
|
|
|
|
Use OW2 system to report bug or ask for features:
|
2017-11-11 14:06:23 +01:00
|
|
|
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
|
2015-12-25 11:46:04 +01:00
|
|
|
|
|
|
|
=head1 DOWNLOAD
|
|
|
|
|
|
|
|
Lemonldap::NG is available at
|
|
|
|
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>
|
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE
|
|
|
|
|
2017-01-04 21:22:04 +01:00
|
|
|
See COPYING file for details.
|
2015-12-25 11:46:04 +01:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see L<http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
=cut
|