lemonldap-ng/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/API.pm

86 lines
2.4 KiB
Perl
Raw Normal View History

package Lemonldap::NG::Handler::API;
use Exporter 'import';
our $VERSION = '1.4.0';
2014-06-17 21:22:36 +02:00
our ( %EXPORT_TAGS, @EXPORT_OK, @EXPORT );
2016-01-29 12:09:55 +01:00
our $mode;
2014-06-17 21:22:36 +02:00
BEGIN {
%EXPORT_TAGS = (
httpCodes => [
qw( MP OK REDIRECT HTTP_UNAUTHORIZED FORBIDDEN DONE DECLINED SERVER_ERROR AUTH_REQUIRED MAINTENANCE )
],
functions => [
qw( &hostname &remote_ip &uri &uri_with_args
&unparsed_uri &args &method &header_in )
]
);
push( @EXPORT_OK, @{ $EXPORT_TAGS{$_} } ) foreach ( keys %EXPORT_TAGS );
$EXPORT_TAGS{all} = \@EXPORT_OK;
if ( exists $ENV{MOD_PERL} ) {
if ( $ENV{MOD_PERL_API_VERSION} and $ENV{MOD_PERL_API_VERSION} >= 2 ) {
eval 'use constant MP => 2;';
}
else {
eval 'use constant MP => 1;';
}
2014-07-24 17:48:32 +02:00
}
else {
eval 'use constant MP => 0;';
2014-07-24 17:48:32 +02:00
}
2014-06-17 21:22:36 +02:00
}
sub AUTOLOAD {
my $func = $AUTOLOAD;
2015-06-15 13:41:44 +02:00
$func =~ s/^.*:://;
# Launch appropriate specific API function:
# - Apache (modperl 2),
# - Apache (modperl1),
# - Nginx
2016-01-29 12:09:55 +01:00
if ( !$mode or $func eq 'newRequest' ) {
2016-02-01 23:22:33 +01:00
#print STDERR "FONCTION $func\n";
#for ( my $i = 0 ; $i < 7 ; $i++ ) {
# print STDERR " $i: " . ( caller($i) )[0] . "\n";
#}
2016-01-29 12:09:55 +01:00
$mode =
2016-02-01 23:22:33 +01:00
(
( caller(1) )[0] =~
/^Lemonldap::NG::Handler::(?:Nginx|PSGI::Server)$/
2016-02-17 11:07:24 +01:00
or ( caller(6)
and ( caller(6) )[0] =~
/^Lemonldap::NG::Handler::(?:Nginx|PSGI::Server)$/ )
2016-02-01 23:22:33 +01:00
) ? 'PSGI/Server'
2016-01-29 12:09:55 +01:00
: (
( caller(0) )[0] =~ /^Lemonldap::NG::Handler::PSGI/
or (
(
( caller(6) )[0]
and ( ( caller(6) )[0] =~ /^Lemonldap::NG::Handler::PSGI/ )
)
)
) ? 'PSGI'
: ( $ENV{GATEWAY_INTERFACE}
and !( ref $_[1] eq 'Apache2::RequestRec' ) ) ? 'CGI'
: ( MP == 2 ) ? 'ApacheMP2'
: ( MP == 1 ) ? 'ApacheMP1'
: $main::{'nginx::'} ? 'ExperimentalNginx'
: 'CGI';
2016-01-29 12:09:55 +01:00
unless ( $INC{"Lemonldap/NG/Handler/API/$mode.pm"} ) {
$mode =~ s#/#::#g;
eval
"use Lemonldap::NG::Handler::API::$mode (':httpCodes', ':functions');";
die $@ if ($@);
2016-01-29 12:09:55 +01:00
}
$mode =~ s#/#::#g;
}
shift;
return "Lemonldap::NG::Handler::API::${mode}"->${func}(@_);
}
1;