TIMTOWTDI
This commit is contained in:
Xavier 2019-08-28 00:36:18 +02:00
parent 810d2c7f94
commit 1f2f0240f0
8 changed files with 41 additions and 48 deletions

View File

@ -121,7 +121,7 @@ sub userError {
sub sendJSONresponse { sub sendJSONresponse {
my ( $self, $req, $j, %args ) = @_; my ( $self, $req, $j, %args ) = @_;
$args{code} ||= 200; $args{code} ||= 200;
$args{headers} ||= $req->respHeaders || []; $args{headers} ||= [ $req->spliceHdrs ];
my $type = 'application/json; charset=utf-8'; my $type = 'application/json; charset=utf-8';
if ( ref $j ) { if ( ref $j ) {
eval { $j = $_json->encode($j); }; eval { $j = $_json->encode($j); };
@ -151,9 +151,9 @@ sub sendError {
return [ return [
$code, $code,
[ [
'Content-Type' => 'application/xml; charset=utf-8', 'Content-Type' => 'application/xml; charset=utf-8',
@{ $req->respHeaders || [] }, 'Content-Length' => length($s),
'Content-Length' => length($s) $req->spliceHdrs,
], ],
[$s] [$s]
]; ];
@ -188,8 +188,9 @@ body{background:#000;color:#fff;padding:10px 50px;font-family:sans-serif;}a{text
return [ return [
$code, $code,
[ [
'Content-Type' => 'text/html; charset=utf-8', 'Content-Type' => 'text/html; charset=utf-8',
@{ $req->respHeaders || [] }, 'Content-Length' => length($s) 'Content-Length' => length($s),
$req->spliceHdrs,
], ],
[$s] [$s]
]; ];
@ -247,7 +248,7 @@ sub sendHtml {
$sc = '.' unless ($sc); $sc = '.' unless ($sc);
$sc =~ s#/*$#/#; $sc =~ s#/*$#/#;
$args{code} ||= 200; $args{code} ||= 200;
$args{headers} ||= $req->respHeaders || []; $args{headers} ||= [ $req->spliceHdrs ];
my $htpl; my $htpl;
$template = ( $args{templateDir} // $self->templateDir ) . "/$template.tpl"; $template = ( $args{templateDir} // $self->templateDir ) . "/$template.tpl";
return $self->sendError( $req, "Unable to read $template", 500 ) return $self->sendError( $req, "Unable to read $template", 500 )

View File

@ -55,6 +55,11 @@ sub respHeaders {
return $self->{respHeaders}; return $self->{respHeaders};
} }
sub spliceHdrs {
my ($self) = @_;
return splice @{ $self->{respHeaders} };
}
sub accept { $_[0]->env->{HTTP_ACCEPT} } sub accept { $_[0]->env->{HTTP_ACCEPT} }
sub encodings { $_[0]->env->{HTTP_ACCEPT_ENCODING} } sub encodings { $_[0]->env->{HTTP_ACCEPT_ENCODING} }
sub languages { $_[0]->env->{HTTP_ACCEPT_LANGUAGE} } sub languages { $_[0]->env->{HTTP_ACCEPT_LANGUAGE} }
@ -189,6 +194,10 @@ Example:
# Add header # Add header
$req->respHeaders->{"X-Key"} = "Value"; $req->respHeaders->{"X-Key"} = "Value";
=head2 spliceHdrs
Returns headers array and flush it.
=head2 set_param( $key, $value ) =head2 set_param( $key, $value )
L<Plack::Request> param() method is read-only. This method can be used to L<Plack::Request> param() method is read-only. This method can be used to

View File

@ -69,7 +69,7 @@ sub _run {
return sub { return sub {
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] ); my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
my $res = $self->handler($req); my $res = $self->handler($req);
push @{ $res->[1] }, @{ $req->{respHeaders} }; push @{ $res->[1] }, $req->spliceHdrs;
return $res; return $res;
}; };
} }
@ -89,7 +89,7 @@ sub status {
return sub { return sub {
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] ); my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
$self->api->status($req); $self->api->status($req);
return [ 200, [ @{ $req->{respHeaders} } ], [ $req->{respBody} ] ]; return [ 200, [ $req->spliceHdrs ], [ $req->{respBody} ] ];
}; };
} }
@ -107,7 +107,7 @@ sub reload {
return sub { return sub {
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] ); my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
$self->api->reload($req); $self->api->reload($req);
return [ 200, [ @{ $req->{respHeaders} } ], [ $req->{respBody} ] ]; return [ 200, [ $req->spliceHdrs ], [ $req->{respBody} ] ];
}; };
} }
@ -133,31 +133,19 @@ sub _authAndTrace {
if ( $res < 300 ) { if ( $res < 300 ) {
if ($noCall) { if ($noCall) {
return [ $res, $req->{respHeaders}, [] ]; return [ $res, [ $req->spliceHdrs ], [] ];
} }
else { else {
$self->logger->debug('User authenticated, calling handler()'); $self->logger->debug('User authenticated, calling handler()');
$res = $self->handler($req); $res = $self->handler($req);
push @{ $res->[1] }, $req->spliceHdrs;
# Insert respHeaders in response only if not already set return $res;
my %hdr1 = @{ $res->[1] };
my %hdr2 = @{ $req->{respHeaders} };
foreach ( keys %hdr2 ) {
unless ( $hdr1{$_} and $hdr2{$_} eq $hdr1{$_} ) {
push @{ $res->[1] }, ( $_ => $hdr2{$_} );
}
}
# Headers have been consumed -> remove them (#1863)
$req->{respHeaders} = [];
} }
return $res;
} }
elsif ( $res < 400 ) { elsif ( $res < 400 ) {
return [ $res, $req->{respHeaders}, [] ]; return [ $res, [ $req->spliceHdrs ], [] ];
} }
else { else {
my %h = $req->{respHeaders} ? @{ $req->{respHeaders} } : ();
my $s = $type->tsv->{portal}->() . "/lmerror/$res"; my $s = $type->tsv->{portal}->() . "/lmerror/$res";
$s = $s =
'<html><head><title>Redirection</title></head><body>' '<html><head><title>Redirection</title></head><body>'
@ -165,9 +153,15 @@ sub _authAndTrace {
. '<h1>Please wait</h1>' . '<h1>Please wait</h1>'
. qq{<p>An error occurs, you're going to be redirected to <a href="$s">$s</a>.</p>} . qq{<p>An error occurs, you're going to be redirected to <a href="$s">$s</a>.</p>}
. '</body></html>'; . '</body></html>';
$h{'Content-Type'} = 'text/html'; return [
$h{'Content-Length'} = length $s; $res,
return [ $res, [%h], [$s] ]; [
$req->spliceHdrs,
'Content-Type' => 'text/html',
'Content-Length' => length $s
],
[$s]
];
} }
} }

View File

@ -47,7 +47,7 @@ sub addAuthRouteWithRedirect {
sub _auth_and_redirect { sub _auth_and_redirect {
my ( $self, $req ) = @_; my ( $self, $req ) = @_;
$self->api->goToPortal( $req, $req->{env}->{REQUEST_URI} ); $self->api->goToPortal( $req, $req->{env}->{REQUEST_URI} );
return [ 302, $req->respHeaders, [] ]; return [ 302, [$req->spliceHdrs], [] ];
} }
sub defaultAuthRoute { sub defaultAuthRoute {
@ -71,6 +71,7 @@ sub _run {
if ( $res->[0] < 300 ) { if ( $res->[0] < 300 ) {
$self->routes( $self->authRoutes ); $self->routes( $self->authRoutes );
$req->userData( $self->api->data ); $req->userData( $self->api->data );
$req->respHeaders($res->[1]);
} }
elsif ( $res->[0] != 403 and not $req->data->{noTry} ) { elsif ( $res->[0] != 403 and not $req->data->{noTry} ) {
@ -85,15 +86,7 @@ sub _run {
return $res; return $res;
} }
$res = $self->handler($req); $res = $self->handler($req);
push @{ $res->[1] }, $req->spliceHdrs;
# Insert respHeaders in response only if not already set
my %hdr1 = @{ $res->[1] };
my %hdr2 = @{ $req->{respHeaders} };
foreach ( keys %hdr2 ) {
unless ( $hdr1{$_} and $hdr2{$_} eq $hdr1{$_} ) {
push @{ $res->[1] }, ( $_ => $hdr2{$_} );
}
}
return $res; return $res;
}; };
} }

View File

@ -26,7 +26,7 @@ sub _run {
return sub { return sub {
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] ); my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
my $res = $self->_authAndTrace($req); my $res = $self->_authAndTrace($req);
push @{ $res->[1] }, @{ $req->respHeaders }, push @{ $res->[1] }, $req->spliceHdrs,
Cookie => ( $req->{Cookie} // '' ); Cookie => ( $req->{Cookie} // '' );
return $res; return $res;
}; };

View File

@ -65,13 +65,10 @@ sub _run {
#@param $req Lemonldap::NG::Common::PSGI::Request #@param $req Lemonldap::NG::Common::PSGI::Request
sub handler { sub handler {
my ( $self, $req ) = @_; my ( $self, $req ) = @_;
my $hdrs = $req->{respHeaders};
$req->{respHeaders} = [];
my @convertedHdrs = my @convertedHdrs =
( 'Content-Length' => 0, Cookie => ( $req->env->{HTTP_COOKIE} // '' ) ); ( 'Content-Length' => 0, Cookie => ( $req->env->{HTTP_COOKIE} // '' ) );
my $i = 0; my $i = 0;
while ( my $k = shift @$hdrs ) { while ( my ( $k, $v ) = splice( @{ $req->{respHeaders} }, 0, 2 ) ) {
my $v = shift @$hdrs;
if ( $k =~ /^(?:Lm-Remote-(?:User|Custom)|Cookie)$/ ) { if ( $k =~ /^(?:Lm-Remote-(?:User|Custom)|Cookie)$/ ) {
push @convertedHdrs, $k, $v; push @convertedHdrs, $k, $v;
} }

View File

@ -163,7 +163,7 @@ sub handler {
); );
# Redirect # Redirect
return [ 302, [ Location => $urldc, @{ $req->respHeaders } ], [] ]; return [ 302, [ Location => $urldc, $req->spliceHdrs ], [] ];
} }
@ -180,7 +180,7 @@ sub handler {
[ [
'Content-Type' => 'text/plain', 'Content-Type' => 'text/plain',
'Content-Length' => 2, 'Content-Length' => 2,
@{ $req->respHeaders } $req->spliceHdrs,
], ],
['OK'] ['OK']
]; ];

View File

@ -339,8 +339,7 @@ sub autoRedirect {
$req->data->{redirectFormMethod} = "get"; $req->data->{redirectFormMethod} = "get";
} }
else { else {
return [ 302, return [ 302, [ Location => $req->{urldc}, $req->spliceHdrs ], [] ];
[ Location => $req->{urldc}, @{ $req->respHeaders } ], [] ];
} }
} }
my ( $tpl, $prms ) = $self->display($req); my ( $tpl, $prms ) = $self->display($req);