Lemonldap/NG/SSOaaS/Apache/Client.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 160 CC 13 SCAR 44.2
CC
13
Cov
0%
handler
182.0
BEGIN
2.0
BEGIN
2.0
CRAP 82.7
1 # Apache2 FastCGI client to query remote LL::NG FastCGI server
2 #
3 package Lemonldap::NG::SSOaaS::Apache::Client;
4  
5 use strict;
6 use Apache2::Connection;
7 use Apache2::RequestUtil;
8 use Apache2::RequestRec;
9 use Apache2::Const -compile =>
10 qw(FORBIDDEN HTTP_UNAUTHORIZED REDIRECT OK DECLINED DONE SERVER_ERROR AUTH_REQUIRED HTTP_SERVICE_UNAVAILABLE);
11 use Apache2::Log;
12 use APR::Table;
13 use IO::Socket::INET;
14 use FCGI::Client;
15 use URI;
16 use URI::Escape qw(uri_unescape);
17  
18 use constant FORBIDDEN => Apache2::Const::FORBIDDEN;
19 use constant HTTP_UNAUTHORIZED => Apache2::Const::HTTP_UNAUTHORIZED;
20 use constant REDIRECT => Apache2::Const::REDIRECT;
21 use constant DECLINED => Apache2::Const::DECLINED;
22 use constant SERVER_ERROR => Apache2::Const::SERVER_ERROR;
23  
24 our $VERSION = '2.18.0';
25  
26 sub handler {
27 my ( $class, $r ) = @_;
28 $r ||= $class;
29 my ( $uri, $args ) = ( $r->uri, $r->args );
30 my $uri_full = $uri . ( $args ? "?$args" : '' );
31 my $env = {
32  
33 #%ENV,
34 HTTP_HOST => $r->hostname,
35 REMOTE_ADDR => (
36 $r->connection->can('remote_ip')
37 ? $r->connection->remote_ip
38 : $r->connection->client_ip
39 ),
40 QUERY_STRING => $args,
41 REQUEST_URI => $uri_full,
42 PATH_INFO => '',
43 SERVER_PORT => $r->get_server_port,
44 REQUEST_METHOD => $r->method,
45 };
46  
47 foreach (qw(VHOSTTYPE RULES_URL HTTPS_REDIRECT PORT_REDIRECT)) {
48 if ( my $t = $r->dir_config($_) ) {
49 $env->{$_} = $t;
50 }
51 }
52  
53 $r->headers_in->do(
54 sub {
55 my $h = shift;
56 my $k = uc($h);
57 if ( $k ne 'HOST' ) {
58 $k =~ s/-/_/g;
59 $env->{"HTTP_$k"} = $r->headers_in->{$h};
60 }
61 return 1;
62 }
63 );
64 $uri = URI->new( "http://" . $r->hostname . $r->unparsed_uri );
65 $env->{PATH_INFO} = uri_unescape( $uri->path );
66 my ( $host, $port ) = ( $r->dir_config('LLNG_SERVER') =~ /^(.*):(\d+)$/ );
67 unless ( $host and $port ) {
68 print STDERR 'Missing or bad LLNG_SERVER';
69 return SERVER_ERROR;
70 }
71 my $sock = IO::Socket::INET->new(
72 PeerAddr => $host,
73 PeerPort => $port,
74 ) or die $!;
75 foreach ( keys %$env ) {
76 delete $env->{$_} unless ( length $env->{$_} );
77 }
78 my ( $stdout, $stderr, $status ) =
79 FCGI::Client::Connection->new( sock => $sock )->request($env);
80 my %hdrs =
81 map { m/(.*?):\s*(.*)/; } split /(?:\r?\n)+/, $stdout;
82 if ( $hdrs{Status} =~ /^(\d+)\s+(.*?)$/ ) {
83 $status = $1;
84 }
85 else {
86 print STDERR "Bad status line $hdrs{Status}\n";
87 return SERVER_ERROR;
88 }
89  
90 if ( ( $status == 302 or $status == 401 ) and $hdrs{Location} ) {
91 $r->err_headers_out->set( Location => $hdrs{Location} );
92 return REDIRECT;
93 }
94  
95 $r->user( $hdrs{'Lm-Remote-User'} ) if $hdrs{'Lm-Remote-User'};
96 $r->subprocess_env( REMOTE_CUSTOM => $hdrs{'Lm-Remote-Custom'} )
97 if $hdrs{'Lm-Remote-Custom'};
98  
99 my $i = 1;
100 while ( $hdrs{"Headername$i"} ) {
101 $r->headers_in->set( $hdrs{"Headername$i"} => $hdrs{"Headervalue$i"} )
102 if $hdrs{"Headervalue$i"};
103 $i++;
104 }
105 $status = DECLINED if ( $status < 300 );
106  
107 return $status;
108 }
109  
110 1;
111 __END__