From 6aa06d2d40b1cc0aa36ad9f5041f8b0d5c79cfe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20Deltombe?= Date: Fri, 27 Jun 2014 16:20:51 +0000 Subject: [PATCH] Handler: remove useless call to defaultValuesInit at server startup, move remaining methods of LocalInit into SharedConf and remove LocalInit.pm (#630) --- lemonldap-ng-handler/MANIFEST | 2 - .../NG/Handler/Initialization/GlobalInit.pm | 2 +- .../NG/Handler/Initialization/LocalInit.pm | 60 --------- .../lib/Lemonldap/NG/Handler/Main.pm | 27 +--- .../lib/Lemonldap/NG/Handler/SharedConf.pm | 115 +++++++++--------- ...ldap-NG-Handler-Initialization-LocalInit.t | 89 -------------- 6 files changed, 59 insertions(+), 236 deletions(-) delete mode 100755 lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Initialization/LocalInit.pm delete mode 100644 lemonldap-ng-handler/t/04-Lemonldap-NG-Handler-Initialization-LocalInit.t diff --git a/lemonldap-ng-handler/MANIFEST b/lemonldap-ng-handler/MANIFEST index e0eda22dd..476ea7143 100644 --- a/lemonldap-ng-handler/MANIFEST +++ b/lemonldap-ng-handler/MANIFEST @@ -12,7 +12,6 @@ lib/Lemonldap/NG/Handler/API/CGI.pm lib/Lemonldap/NG/Handler/API/Nginx.pm lib/Lemonldap/NG/Handler/CGI.pm lib/Lemonldap/NG/Handler/Initialization/GlobalInit.pm -lib/Lemonldap/NG/Handler/Initialization/LocalInit.pm lib/Lemonldap/NG/Handler/Main.pm lib/Lemonldap/NG/Handler/Main/Headers.pm lib/Lemonldap/NG/Handler/Main/Jail.pm @@ -33,7 +32,6 @@ META.yml README t/01-Lemonldap-NG-Handler-Main.t t/02-Lemonldap-NG-Handler-Main-Portal.t -t/04-Lemonldap-NG-Handler-Initialization-LocalInit.t t/05-Lemonldap-NG-Handler-Initialization-GlobalInit.t t/10-Lemonldap-NG-Handler-SharedConf.t t/12-Lemonldap-NG-Handler-Jail.t diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Initialization/GlobalInit.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Initialization/GlobalInit.pm index d828485aa..95224da8d 100755 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Initialization/GlobalInit.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Initialization/GlobalInit.pm @@ -222,7 +222,7 @@ sub localSessionStorageInit { my $params = ""; if ($localSessionStorage) { require Data::Dumper; - $params = " $self->{localStorage}," + $params = " $self->{localSessionStorage}," . Data::Dumper->new( [ $localSessionStorageOptions ] )->Terse(1)->Indent(0)->Dump; # To send cache params on one line } #print { $tsv->{statusPipe} } "RELOADCACHE$params"; diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Initialization/LocalInit.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Initialization/LocalInit.pm deleted file mode 100755 index e190a44b3..000000000 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Initialization/LocalInit.pm +++ /dev/null @@ -1,60 +0,0 @@ -# Methods run at server startup / reload - -package Lemonldap::NG::Handler::Initialization::LocalInit; - -use Lemonldap::NG::Handler::SharedConf; # Needed to get VERSION -use Lemonldap::NG::Handler::Main::Logger; -use Lemonldap::NG::Handler::API qw(:httpCodes); - -our $VERSION = '1.4.0'; -my ( $statusPipe, $statusOut ); - -## @imethod void localInit(hashRef args) -# Launch the status process (statusProcess()) if wanted -# @param $args reference to the initialization hash -sub localInit($$) { - my ( $class, $args ) = splice @_; - - $class->statusInit if ($args->{status}); - $class->serverSignatureInit unless ($args->{hideSignature}); - - return ( $statusPipe, $statusOut ); -} - -sub serverSignatureInit { - my $class = shift; - Lemonldap::NG::Handler::API->setServerSignature( - "Lemonldap::NG::Handler/" . $Lemonldap::NG::Handler::VERSION - ) if ( $Lemonldap::NG::Handler::VERSION ); -} - - -## @ifn protected void statusInit() -# Launch the status process -sub statusInit { - my $class = shift; - require IO::Pipe; - $statusPipe = IO::Pipe->new; - $statusOut = IO::Pipe->new; - if ( my $pid = fork() ) { - $statusPipe->writer(); - $statusOut->reader(); - $statusPipe->autoflush(1); - } - else { - $statusPipe->reader(); - $statusOut->writer(); - my $fdin = $statusPipe->fileno; - my $fdout = $statusOut->fileno; - open STDIN, "<&$fdin"; - open STDOUT, ">&$fdout"; - my @tmp = (); - push @tmp, "-I$_" foreach (@INC); - exec 'perl', '-MLemonldap::NG::Handler::Status', - @tmp, - '-e', - "&Lemonldap::NG::Handler::Status::run()"; - } -} - -1; diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main.pm index a2afb338c..891a9b7a8 100755 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Main.pm @@ -1,24 +1,9 @@ -## @file -# Base file for Lemonldap::NG handlers - -## @class -# Base class for Lemonldap::NG handlers. -# All methods in handler are class methods: in ModPerl environment, handlers -# are always launched without object created. -# -# The main method is run() who is called by Apache for each requests (using -# handler() wrapper). -# -# The main initialization subroutine is init() who launch localInit() and -# globalInit(). +# Methods run at request serving package Lemonldap::NG::Handler::Main; -#use strict; - use MIME::Base64; use Exporter 'import'; -#use AutoLoader 'AUTOLOAD'; use Lemonldap::NG::Common::Crypto; use Lemonldap::NG::Common::Session; require POSIX; @@ -79,7 +64,6 @@ BEGIN { Lemonldap::NG::Handler::API->thread_share($tsv); } -use Lemonldap::NG::Handler::Initialization::LocalInit; use Lemonldap::NG::Handler::Initialization::GlobalInit; use Lemonldap::NG::Handler::Main::Jail; use Lemonldap::NG::Handler::Main::Headers; @@ -482,14 +466,6 @@ sub initLocalStorage { return 1; } -## @imethod void localInit(hashRef args) -# launch localInit method which launch the status process -# @param $args reference to the initialization hash -sub localInit($$) { - my ( $class, $args ) = splice @_; - ( $tsv->{statusPipe}, $tsv->{statusOut} ) = Lemonldap::NG::Handler::Initialization::LocalInit->localInit($args); -} - ## @imethod void globalInit(hashRef args) # instanciate a GlobalInit object with variables: # customFunctions, useSafeJail, and safe @@ -670,7 +646,6 @@ sub status($$) { 'debug', $r ); return $class->abort( $r, "$class: status page can not be displayed" ) unless ( $tsv->{statusPipe} and $tsv->{statusOut} ); - $r->handler("perl-script"); print { $tsv->{statusPipe} } "STATUS" . ( Lemonldap::NG::Handler::API->args($r) diff --git a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/SharedConf.pm b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/SharedConf.pm index c17df54cc..53bd73ef5 100644 --- a/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/SharedConf.pm +++ b/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/SharedConf.pm @@ -23,7 +23,6 @@ use Lemonldap::NG::Handler::API qw(:httpCodes); use Lemonldap::NG::Handler::Initialization::GlobalInit; use Lemonldap::NG::Common::Conf; #link protected lmConf use Lemonldap::NG::Common::Conf::Constants; #inherits -use Cache::Cache qw($EXPIRES_NEVER); use Lemonldap::NG::Handler::Main::Logger; use base qw(Lemonldap::NG::Handler::Main); @@ -55,74 +54,74 @@ BEGIN { # INIT PROCESS ## @imethod void init(hashRef args) -# Load parameters and build the Lemonldap::NG::Common::Conf object. +# Read parameters and build the Lemonldap::NG::Common::Conf object. # @param $args hash containing parameters sub init($$) { - my ( $class, $args ) = splice @_; + my ( $class, $args ) = @_; + + # According to doc, localStorage can be declared in $args root, + # but it must be in $args->{configStorage} + foreach (qw(localStorage localStorageOptions)) { + $args->{configStorage}->{$_} ||= $args->{$_}; + } + $lmConf = Lemonldap::NG::Common::Conf->new($args->{configStorage}); die( "$class : unable to build configuration: " . "$Lemonldap::NG::Common::Conf::msg" ) unless ($lmConf); - # Get local configuration parameters - my $localconf = $lmConf->getLocalConf(HANDLERSECTION); - if ($localconf) { - $args->{$_} ||= $localconf->{$_} foreach ( keys %$localconf ); - } + # Merge local configuration parameters so that params defined in + # startup parameters have precedence over lemonldap-ng.ini params + $localConfig = { + %{ $lmConf->getLocalConf(HANDLERSECTION) }, + %{ $args } + }; - # Store in localConfig global variable - $localConfig = $args; + $checkTime = $localConfig->{checkTime} || $checkTime; - # localStorage can be declared in configStorage or at the root or both - foreach (qw(localStorage localStorageOptions)) { - $args->{$_} ||= $args->{configStorage}->{$_} || $lmConf->{$_}; - $args->{configStorage}->{$_} ||= $args->{$_}; - } - - $checkTime = $args->{checkTime} || $checkTime; - - $class->defaultValuesInit($args); - $class->localInit($args); + # Few actions that must be done at server startup: + # * set server signature + $class->serverSignatureInit unless ($localConfig->{hideSignature}); + # * launch status process + ( $tsv->{statusPipe}, $tsv->{statusOut} ) = $class->statusInit + if ($localConfig->{status}); } -## @imethod protected void defaultValuesInit(hashRef args) -# Set default values for non-customized variables -# @param $args hash containing parameters -# @return boolean -sub defaultValuesInit { - my ( $class, $args ) = splice @_; - - # Local configuration overrides global configuration - my %h = ( %$args, %$localConfig ); - - #return $class->SUPER::defaultValuesInit( \%h ); - - my $globalinit = Lemonldap::NG::Handler::Initialization::GlobalInit->new( - customFunctions => $tsv->{customFunctions}, - useSafeJail => $tsv->{useSafeJail}, - ); - - ( - @$tsv{ - qw( cookieName securedCookie whatToTrace - https port customFunctions - timeoutActivity useRedirectOnError useRedirectOnForbidden - useSafeJail key maintenance - cda httpOnly cookieExpiration - cipher ) - } - ) - = $globalinit->defaultValuesInit( - @$tsv{ - qw( cookieName securedCookie whatToTrace - https port customFunctions - timeoutActivity useRedirectOnError useRedirectOnForbidden - useSafeJail key maintenance - cda httpOnly cookieExpiration - cipher ) - }, - \%h - ); +# @method void serverSignatureInit +# adapt server signature +sub serverSignatureInit { + my $class = shift; + Lemonldap::NG::Handler::API->setServerSignature( + "Lemonldap::NG::Handler/" . $Lemonldap::NG::Handler::VERSION + ) if ( $Lemonldap::NG::Handler::VERSION ); +} +## @ifn protected void statusInit() +# Launch the status process +sub statusInit { + my $class = shift; + require IO::Pipe; + $statusPipe = IO::Pipe->new; + $statusOut = IO::Pipe->new; + if ( my $pid = fork() ) { + $statusPipe->writer(); + $statusOut->reader(); + $statusPipe->autoflush(1); + return ( $statusPipe, $statusOut ); + } + else { + $statusPipe->reader(); + $statusOut->writer(); + my $fdin = $statusPipe->fileno; + my $fdout = $statusOut->fileno; + open STDIN, "<&$fdin"; + open STDOUT, ">&$fdout"; + my @tmp = (); + push @tmp, "-I$_" foreach (@INC); + exec 'perl', '-MLemonldap::NG::Handler::Status', + @tmp, + '-e', + "&Lemonldap::NG::Handler::Status::run()"; + } } # MAIN diff --git a/lemonldap-ng-handler/t/04-Lemonldap-NG-Handler-Initialization-LocalInit.t b/lemonldap-ng-handler/t/04-Lemonldap-NG-Handler-Initialization-LocalInit.t deleted file mode 100644 index c1b8ce89f..000000000 --- a/lemonldap-ng-handler/t/04-Lemonldap-NG-Handler-Initialization-LocalInit.t +++ /dev/null @@ -1,89 +0,0 @@ -# Before `make install' is performed this script should be runnable with -# `make test'. After `make install' it should work as `perl Lemonldap-NG-Handler-Vhost.t' - -######################### - -# change 'tests => 1' to 'tests => last_test_to_print'; - -package My::Package; - -use Test::More tests => 2; - -BEGIN { - use_ok('Lemonldap::NG::Handler::Initialization::LocalInit'); -} - -######################### - -# Insert your test code below, the Test::More module is use()ed here so read -# its man page ( perldoc Test::More ) for help writing this test script. - -ok( - Lemonldap::NG::Handler::Initialization::LocalInit->localInit({ - status => 1, - hideSignature => 0, - }), - 'localInit method' -); - -__END__ -ok( - my ( - $localStorage, $localStorageOptions, - $statusPipe, $statusOut - ) - = $localinit->localInit( - { - localStorage => 'Cache::FileCache', - localStorageOptions => { 'namespace' => 'lemonldap-ng-sessions', }, - status => 1, - } - ), - 'LocalInit methods: localInit' -); - -ok( - ( - $localStorage eq 'Cache::FileCache' - and $localStorageOptions->{'namespace'} eq 'lemonldap-ng-sessions' - ), - 'LocalInit methods: localInit values' -); - -ok( $localinit->statusProcess == 0, 'LocalInit methods: statusProcess' ); - -ok( $statusPipe->isa('IO::Pipe::End'), 'status pipe: In pipe' ); - -ok( $statusOut->isa('IO::Pipe::End'), 'status pipe: Out pipe' ); - -ok( print( $statusPipe "uid => / OK\nuid => / OK\nuid => /no REJECT\n" ), - 'status pipe: New requests' ); - -ok( print( $statusPipe "STATUS\n" ), 'status pipe: Status request' ); - -ok( &read, 'status pipe: Status result' ); - -ok( close($statusOut), 'status pipe: close out pipe' ); - -ok( close($statusPipe), 'status pipe: close in pipe' ); - -sub read { - my $ok = 0; - - #open LOG, '>/tmp/log'; - while (<$statusOut>) { - - #print LOG $_; - $ok++ if (/^OK\s+:\s*2\s*\(2\.00\s*\/\s*mn\)$/); - $ok++ if (/^REJECT\s+:\s*1\s*\(1\.00\s*\/\s*mn\)$/); - if (/^END$/) { - $ok++; - last; - } - } - - #print LOG "$ok\n"; - #close LOG; - return ( $ok == 3 ); -} -