Bug 29744: Harmonize psgi/plack detection methods

This patch updates and moves the existing psgi_env method out of Auth
and into Context and then replaces any manual references of the same
code to use the new method.

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: David Cook <dcook@prosentient.com.au>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Martin Renvoize 2022-03-28 10:23:58 +01:00 committed by Tomas Cohen Arazi
parent b7fa8136b4
commit 0b80172a49
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
5 changed files with 16 additions and 24 deletions

View file

@ -55,14 +55,14 @@ use Koha::CookieManager;
use vars qw($ldap $cas $caslogout);
our (@ISA, @EXPORT_OK);
sub safe_exit {
if (C4::Context::psgi_env) { die 'psgi:exit' }
else { exit }
}
BEGIN {
sub psgi_env { any { /^psgi\./ } keys %ENV }
sub safe_exit {
if (psgi_env) { die 'psgi:exit' }
else { exit }
}
C4::Context->set_remote_address;
require Exporter;

View file

@ -81,7 +81,7 @@ sub get_login_shib {
my $matchAttribute = $config->{mapping}->{ $config->{matchpoint} }->{is};
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
if ( C4::Context::psgi_env ) {
return $ENV{"HTTP_".uc($matchAttribute)} || '';
} else {
return $ENV{$matchAttribute} || '';
@ -126,7 +126,7 @@ sub _autocreate {
my %borrower = ( $config->{matchpoint} => $match );
while ( my ( $key, $entry ) = each %{$config->{'mapping'}} ) {
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
if ( C4::Context::psgi_env ) {
$borrower{$key} = ( $entry->{'is'} && $ENV{"HTTP_" . uc($entry->{'is'}) } ) || $entry->{'content'} || '';
} else {
$borrower{$key} = ( $entry->{'is'} && $ENV{ $entry->{'is'} } ) || $entry->{'content'} || '';
@ -144,7 +144,7 @@ sub _sync {
my %borrower;
$borrower{'borrowernumber'} = $borrowernumber;
while ( my ( $key, $entry ) = each %{$config->{'mapping'}} ) {
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
if ( C4::Context::psgi_env ) {
$borrower{$key} = ( $entry->{'is'} && $ENV{"HTTP_" . uc($entry->{'is'}) } ) || $entry->{'content'} || '';
} else {
$borrower{$key} = ( $entry->{'is'} && $ENV{ $entry->{'is'} } ) || $entry->{'content'} || '';

View file

@ -999,21 +999,15 @@ sub needs_install {
return ($self->preference('Version')) ? 0 : 1;
}
=head3 is_psgi_or_plack
=head3 psgi_env
is_psgi_or_plack returns true if there is an environmental variable
psgi_env returns true if there is an environmental variable
prefixed with "psgi" or "plack". This is useful for detecting whether
this is a PSGI app or a CGI app, and implementing code as appropriate.
=cut
sub is_psgi_or_plack {
my $is_psgi_or_plack = 0;
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
$is_psgi_or_plack = 1;
}
return $is_psgi_or_plack;
}
sub psgi_env { any { /(^psgi\.|^plack\.)/i } keys %ENV };
=head3 is_internal_PSGI_request
@ -1026,7 +1020,7 @@ app
#NOTE: This is not a very robust method but it's the best we have so far
sub is_internal_PSGI_request {
my $is_internal = 0;
if ( (__PACKAGE__->is_psgi_or_plack) && ( $ENV{REQUEST_URI} !~ /^(\/intranet|\/opac)/ ) ){
if ( (__PACKAGE__->psgi_env) && ( $ENV{REQUEST_URI} !~ /^(\/intranet|\/opac)/ ) ){
$is_internal = 1;
}
return $is_internal;

View file

@ -208,9 +208,7 @@ outside this context.
=cut
sub get_script_name {
# This is the method about.pl uses to detect Plack; now that two places use it, it MUST be
# right.
if ( ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) && $ENV{SCRIPT_NAME} =~ m,^/(intranet|opac)(.*), ) {
if ( ( C4::Context::psgi_env ) && $ENV{SCRIPT_NAME} =~ m,^/(intranet|opac)(.*), ) {
return '/cgi-bin/koha' . $2;
} else {
return $ENV{SCRIPT_NAME};

View file

@ -125,7 +125,7 @@ if ($^O ne 'VMS') {
my $zebraVersion = `zebraidx -V`;
# Check running PSGI env
if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
if ( C4::Context::psgi_env ) {
$template->param(
is_psgi => 1,
psgi_server => ($ENV{ PLACK_ENV }) ? "Plack ($ENV{PLACK_ENV})" :