lemonldap-ng/lemonldap-ng-common/lib/Lemonldap/NG/Common/PSGI/Request.pm

277 lines
6.5 KiB
Perl
Raw Normal View History

package Lemonldap::NG::Common::PSGI::Request;
use strict;
use Mouse;
2016-01-13 20:47:56 +01:00
use JSON;
use Plack::Request;
use URI::Escape;
2019-02-12 18:21:38 +01:00
our $VERSION = '2.1.0';
our @ISA = ('Plack::Request');
# http :// server / path ? query # fragment
# m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
2016-05-25 21:30:41 +02:00
sub BUILD {
my ( $self, $env ) = @_;
foreach ( keys %$env ) {
$self->{$_} ||= $env->{$_} if (/^(?:HTTP|SSL)_/);
2016-05-25 21:30:41 +02: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;
2019-10-08 22:26:23 +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} ||= '/';
2019-03-05 10:43:28 +01:00
$self->env->{REQUEST_URI} =~ s|^//+|/|g;
2019-03-07 18:22:16 +01:00
$self->{uri} = uri_unescape( $self->env->{REQUEST_URI} );
2018-07-05 23:00:40 +02:00
$self->{data} = {};
$self->{error} = 0;
$self->{respHeaders} = [];
2019-03-19 05:56:36 +01:00
$self->{tplParams} = {};
return bless( $self, $_[0] );
2016-04-02 22:17:39 +02:00
}
sub data { $_[0]->{data} }
2019-03-19 05:56:36 +01:00
sub tplParams { $_[0]->{tplParams} }
sub uri { $_[0]->{uri} }
2016-04-02 22:17:39 +02:00
sub userData {
2017-02-15 07:41:50 +01:00
my ( $self, $v ) = @_;
2018-12-17 18:43:54 +01:00
return $self->{userData} = $v if ($v);
return $self->{userData}
|| {
( $Lemonldap::NG::Handler::Main::tsv->{whatToTrace}
2020-02-20 23:34:02 +01:00
|| '_whatToTrace' ) => $self->{user},
};
}
sub respHeaders {
my ( $self, $respHeaders ) = @_;
$self->{respHeaders} = $respHeaders if ($respHeaders);
return $self->{respHeaders};
}
2019-08-28 00:36:18 +02:00
sub spliceHdrs {
my ($self) = @_;
return splice @{ $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 origin { $_[0]->env->{HTTP_ORIGIN} }
sub referer { $_[0]->env->{REFERER} }
2017-09-26 21:11:04 +02:00
sub query_string { $_[0]->env->{QUERY_STRING} }
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 {
return 1
if ( defined $_[0]->accept
and $_[0]->accept =~ m#(?:application|text)/json# );
return 0;
2016-05-02 12:30:23 +02:00
}
# JSON parser
sub jsonBodyToObj {
my $self = shift;
return $self->{json_body} if ( $self->{json_body} );
unless ( $self->content_type =~ /application\/json/ ) {
$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 } ) };
if ($@) {
$self->error("$@$!");
return undef;
}
return $self->{json_body} = $j;
2016-04-02 22:17:39 +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;
2015-12-25 11:46:04 +01:00
use base Lemonldap::NG::Common::PSGI;
2015-12-25 11:46:04 +01:00
# See Lemonldap::NG::Common::PSGI
...
sub handler {
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' ) { ... }
2015-12-25 11:46:04 +01:00
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
2019-04-03 14:15:16 +02:00
$req->respHeaders->{"X-Key"} = "Value";
2016-12-19 13:18:26 +01:00
2019-08-28 00:36:18 +02:00
=head2 spliceHdrs
Returns headers array and flush it.
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>,
2019-04-03 14:15:16 +02:00
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
2021-08-12 16:05:42 +02:00
L<https://lemonldap-ng.org/download>
2015-12-25 11:46:04 +01:00
=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