Lemonldap/NG/Handler/ApacheMP2/Main.pm untested

filter: Statementstmt 0.0% 0 / 158 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 234 CC 6 SCAR 10.8
CC
6
Cov
0%
set_custom
6.0
unset_header_in
6.0
redirectFilter
6.0
CRAP 2.9
1 # Main ApacheMP2 adapter for LLNG handler
2 #
3 # See https://lemonldap-ng.org/documentation/latest/handlerarch
4 package Lemonldap::NG::Handler::ApacheMP2::Main;
5  
6 use strict;
7 use AutoLoader 'AUTOLOAD';
8 use Apache2::RequestUtil;
9 use Apache2::RequestRec;
10 use Apache2::Log;
11 use Apache2::ServerUtil;
12 use Apache2::Connection;
13 use Apache2::RequestIO;
14 use Apache2::Const;
15 use Apache2::Filter;
16 use APR::Table;
17 use Apache2::Const -compile =>
18 qw(FORBIDDEN HTTP_UNAUTHORIZED REDIRECT OK DECLINED DONE SERVER_ERROR AUTH_REQUIRED HTTP_SERVICE_UNAVAILABLE);
19 use URI;
20 use base 'Lemonldap::NG::Handler::Main';
21  
22 use constant FORBIDDEN => Apache2::Const::FORBIDDEN;
23 use constant HTTP_UNAUTHORIZED => Apache2::Const::HTTP_UNAUTHORIZED;
24 use constant REDIRECT => Apache2::Const::REDIRECT;
25 use constant OK => Apache2::Const::OK;
26 use constant DECLINED => Apache2::Const::DECLINED;
27 use constant DONE => Apache2::Const::DONE;
28 use constant SERVER_ERROR => Apache2::Const::SERVER_ERROR;
29 use constant AUTH_REQUIRED => Apache2::Const::AUTH_REQUIRED;
30 use constant MAINTENANCE => Apache2::Const::HTTP_SERVICE_UNAVAILABLE;
31 use constant BUFF_LEN => 8192;
32  
33 our $VERSION = '2.19.0';
34  
35 # Set default logger
36 use constant defaultLogger => 'Lemonldap::NG::Common::Logger::Apache2';
37  
38 # Set also default logger for PSGI launched in the same Perl process
39 $ENV{LLNG_DEFAULTLOGGER} ||= 'Lemonldap::NG::Common::Logger::Apache2';
40  
41 eval { require threads::shared; };
42  
43 our $request; # Apache2::RequestRec object for current request
44  
45 #*run = \&Lemonldap::NG::Handler::Main::run;
46  
47 $ENV{LLNGSTATUSLISTEN} ||= 'localhost:64321';
48 __PACKAGE__->init();
49  
50 # INTERNAL METHODS
51  
52 ## @method void thread_share(string $variable)
53 # try to share $variable between threads
54 # note: eval is needed,
55 # else it fails to compile if threads::shared is not loaded
56 # @param $variable the name of the variable to share
57 sub thread_share {
58 my ( $class, $variable ) = @_;
59 eval "threads::shared::share(\$variable);";
60 }
61  
62 ## @method void setServerSignature(string sign)
63 # modifies web server signature
64 # @param $sign String to add to server signature
65 sub setServerSignature {
66 my ( $class, $sign ) = @_;
67 eval {
68 Apache2::ServerUtil->server->push_handlers(
69 PerlPostConfigHandler => sub {
70 my ( $c, $l, $t, $s ) = @_;
71 $s->add_version_component($sign);
72 }
73 );
74 };
75 }
76  
77 ## @method void set_user(string user)
78 # sets remote_user
79 # @param user string username
80 sub set_user {
81 my ( $class, $request, $user ) = @_;
82 $request->env->{'psgi.r'}->user($user);
83 }
84  
85 ## @method void set_custom(string custom)
86 # sets remote_custom
87 # @param custom string custom_header
88 sub set_custom {
89 my ( $class, $request, $custom ) = @_;
90 $request->env->{'psgi.r'}->subprocess_env( REMOTE_CUSTOM => $custom )
91 if defined $custom;
92 }
93  
94 ## @method void set_header_in(hash headers)
95 # sets or modifies request headers
96 # @param headers hash containing header names => header value
97 sub set_header_in {
98 my ( $class, $request, %headers ) = @_;
99 while ( my ( $h, $v ) = each %headers ) {
100 utf8::downgrade($v);
101 $request->env->{'psgi.r'}->headers_in->set( $h => $v );
102 }
103 }
104  
105 ## @method void unset_header_in(array headers)
106 # removes request headers
107 # This function looks a bit heavy: it is to ensure that if a request
108 # header 'Auth-User' is removed, 'Auth_User' be removed also
109 # @param headers array with header names to remove
110 sub unset_header_in {
111 my ( $class, $request, @headers ) = @_;
112 foreach my $h1 (@headers) {
113 $h1 = lc $h1;
114 $h1 =~ s/-/_/g;
115 $request->env->{'psgi.r'}->headers_in->do(
116 sub {
117 my $h = shift;
118 my $h2 = lc $h;
119 $h2 =~ s/-/_/g;
120 $request->env->{'psgi.r'}->headers_in->unset($h)
121 if ( $h1 eq $h2 );
122 return 1;
123 }
124 );
125 }
126 }
127  
128 ## @method void set_header_out(hash headers)
129 # sets response headers
130 # @param headers hash containing header names => header value
131 sub set_header_out {
132 my ( $class, $request, %headers ) = @_;
133 while ( my ( $h, $v ) = each %headers ) {
134 $request->env->{'psgi.r'}->err_headers_out->set( $h => $v );
135 }
136 }
137  
138 ## @method boolean is_initial_req
139 # returns true unless the current request is a subrequest
140 # @return is_initial_req boolean
141 sub is_initial_req {
142 return $_[1]->env->{'psgi.r'}->is_initial_req;
143 }
144  
145 ## @method void print(string data)
146 # write data in HTTP response body
147 # @param data Text to add in response body
148 sub print {
149 my ( $class, $request, $data ) = @_;
150 $request->env->{'psgi.r'}->print($data);
151 }
152  
153 ## @rmethod protected int redirectFilter(string url, Apache2::Filter f)
154 # Launch the current HTTP request then redirects the user to $url.
155 # Used by logout_app and logout_app_sso targets
156 # @param $url URL to redirect the user
157 # @param $f Current Apache2::Filter object
158 # @return Constant $class->OK
159 sub redirectFilter {
160 my $class = shift;
161 my $url = shift;
162 my $f = shift;
163 unless ( $f->ctx ) {
164  
165 # Here, we can use Apache2 functions instead of set_header_out
166 # since this function is used only with Apache2.
167 $f->r->status( $class->REDIRECT );
168 $f->r->status_line("303 See Other");
169 $f->r->headers_out->unset('Location');
170 $f->r->err_headers_out->set( 'Location' => URI->new($url)->as_string );
171 $f->ctx(1);
172 }
173 while ( $f->read( my $buffer, 1024 ) ) {
174 }
175 $class->updateStatus( $f->r, '$class->REDIRECT',
176 $class->data->{ $class->tsv->{whatToTrace} }, 'filter' );
177 return $class->OK;
178 }
179  
180 sub logout_app {
181 my $class = shift;
182 my $u = shift;
183 $class->logger->debug("logout_app redirect to $u");
184 eval 'use Apache2::Filter' unless ( $INC{"Apache2/Filter.pm"} );
185 return (
186 sub {
187 my ($req) = @_;
188 my $portal = $class->tsv->{portal}->($req);
189 my $dest = $u || $portal;
190 $req->{env}->{'psgi.r'}->add_output_filter(
191 sub {
192 return $class->redirectFilter( $dest, @_ );
193 }
194 );
195 1;
196 },
197 0
198 );
199 }
200  
201 sub logout_app_sso {
202 my $class = shift;
203 my $u = shift;
204 $class->logger->debug("logout_app_sso redirect to $u");
205 eval 'use Apache2::Filter' unless ( $INC{"Apache2/Filter.pm"} );
206 return (
207 sub {
208 my ($req) = @_;
209 my $portal = $class->tsv->{portal}->($req);
210 my $dest = $u || $portal;
211 $class->localUnlog( $req, @_ );
212 $req->{env}->{'psgi.r'}->add_output_filter(
213 sub {
214 my $r = $_[0]->r;
215 return $class->redirectFilter(
216 $portal . "?url="
217 . $class->encodeUrl( $req, $dest )
218 . "&logout=1",
219 @_
220 );
221 }
222 );
223 1;
224 },
225 0
226 );
227 }
228  
229 1;
230  
231 __END__