HTTP/Daemon/OpenInteract2.pm

filter: Statementstmt 30.5% 74 / 242 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 56.4% 22 / 39 Podpod 25.0% 1 / 4 totaltotal 24.8% 97 / 391 CC 42 SCAR 36.1
CC
42
Cov
23%
interact
132.0
_open_pid_file
90.0
new
30.0
CRAP 37.1
« OpenInteract2/Repository.pm ↑ Summary OpenInteract2/Error.pm »
1 # $Id: OpenInteract2.pm,v 1.13 2005/03/18 04:09:48 lachoy Exp $
2  
3 # This is just the daemon with a product token...
4  
5 package HTTP::Daemon::OpenInteract2Daemon;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base qw( HTTP::Daemon );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 $HTTP::Daemon::OpenInteract2Daemon::VERSION = sprintf("%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/);
13  
14 1 my ( $log );
15  
16 0 ▶sub product_tokens {
Subroutine product_tokens: not called CC=1 SCAR=6.9
17 0 my ( $self ) = @_;
18 0 return join( ' ', 'OpenInteract/ ', CTX->version,
19 $self->SUPER::product_tokens );
20 }
21  
22 # Now the interesting stuff...
23  
24 package HTTP::Daemon::OpenInteract2;
25  
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use base qw( Class::Accessor::Fast Class::Observable );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use File::Spec::Functions qw( catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use OpenInteract2::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use OpenInteract2::Config::IniFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use OpenInteract2::Controller;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use OpenInteract2::DatasourceManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use OpenInteract2::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use OpenInteract2::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use OpenInteract2::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use OpenInteract2::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use POSIX qw( WNOHANG setsid );
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 my $PID_FILE = 'oi2.pid';
47  
48 1 $SIG{CHLD} = \&_reaper;
49 0 ▶$SIG{TERM} = $SIG{INT} = sub { exit(0); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
50  
51 1 my @FIELDS = qw(
52 website_dir daemon daemon_config daemon_config_file
53 deploy_url pid pid_file server_name static_path url
54 );
55 1 __PACKAGE__->mk_accessors( @FIELDS );
56  
57 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
58 0 my ( $class, $params ) = @_;
59 0 my $website_dir = $params->{website_dir};
60 0 ▶ unless ( $website_dir and -d $website_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($website_dir and -d $website_dir) 0 0 0
Condition0%0/3
$website_dir and -d $website_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$website_dir and -d $website_dir
$website_dir -d $website_dir
Truth table0%0/3
A and B
A= $website_dirB= -d $website_dir
ABresult
0X0
100
111
61 0 die "Parameter 'website_dir' must refer to an existing directory\n";
62 }
63 0 my $self = bless( {}, $class );
64 0 foreach my $field ( @FIELDS ) {
65 0 ▶ next unless ( $params->{ $field } );
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{$field} 0 0 0
66 0 $self->$field( $params->{ $field } );
67 }
68 0 $self->_init_oi2_base;
69 0 $self->_init_read_daemon_options;
70 0 $self->_init_create_daemon;
71 0 $self->_init_environment;
72 0 $self->_start_daemon;
73 0 return $self;
74 }
75  
76 0 ▶sub accept {
Subroutine accept: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my ( $self ) = @_;
78 0 return $self->daemon->accept;
79 }
80  
81 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my ( $self ) = @_;
83 0 return $self->daemon->close;
84 }
85  
86 0 ▶sub interact {
Subroutine interact: not called CC=11 SCAR=48.8
Pod undocumented
87 0 my ( $self, $client ) = @_;
88 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
89  
90 0 my $deploy_url = $self->deploy_url;
91  
92 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("New client attached from: ", $client->peerhost) 0 0 0
93 $log->info( "New client attached from: ", $client->peerhost );
94  
95 REQUEST:
96 0 ▶ while ( my $lwp_request = $client->get_request ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lwp_request = $client->get_request) 0 0 0
97 0 my $path = $lwp_request->uri->path;
98 0 $log->info( "Client request: '$path'" );
99  
100 # HEAD requests (who cares?)
101 0 ▶ if ( $lwp_request->method eq 'HEAD' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($lwp_request->method eq 'HEAD') { } 0 0 0
elsif ($self->_is_static_path($path)) { } 0 0 0
elsif (not $deploy_url or $deploy_url and $path =~ /^$deploy_url/) { } 0 0 0
102 0 my $lwp_response = HTTP::Response->new( RC_OK );
103 0 $client->send_response( $lwp_response );
104 0 $log->info( "Sent HEAD response ok" );
105 }
106  
107 # Static requests
108 elsif ( $self->_is_static_path( $path ) ) {
109 0 my $lwp_response = $self->_get_static_response( $path );
110 0 $client->send_response( $lwp_response );
111 0 $log->info( "Sent static file corresponding to '$path' ok" );
112 }
113  
114 # OI2 requests
115 ▶ elsif ( ! $deploy_url or ( $deploy_url and $path =~ /^$deploy_url/ ) ) {
Condition0%0/6
not $deploy_url or $deploy_url and $path =~ /^$deploy_url/
l!l&&r!l&&!r
000
$deploy_url and $path =~ /^$deploy_url/
!ll&&!rl&&r
000
MC/DC0%0/3
not $deploy_url or $deploy_url and $path =~ /^$deploy_url/
not $deploy_url $deploy_url $path =~ /^$deploy_url/
Truth table0%0/4
not(A) or B and C
A= not $deploy_urlB= $deploy_urlC= $path =~ /^$deploy_url/
ABCresult
1XX1
0111
00X0
0100
116 0 my $response = OpenInteract2::Response->new(
117 { client => $client });
118 0 my $request = OpenInteract2::Request->new(
119 { client => $client,
120 request => $lwp_request,
121 server_name => $self->server_name } );
122 0 OpenInteract2::Auth->new()->login();
123 0 my $controller = eval {
124 0 OpenInteract2::Controller->new( $request, $response )
125 };
126 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
127 0 $response->content( $@ );
128 }
129 else {
130 0 $controller->execute;
131 }
132 0 eval {
133 0 $response->send;
134 };
135 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
136 0 $log->logcroak( "Caught error from response: $@" );
137 }
138 else {
139 0 $log->info( "Sent OI request for '$path' ok" );
140 }
141 }
142  
143 # Non-deployment context requests
144 else {
145 0 my $lwp_response = $self->_get_non_context_response( $path );
146 0 $client->send_response( $lwp_response );
147 0 warn "daemon: Sent non context response to '$path' ",
148 "'Deploy: $deploy_url' ok\n";
149 }
150 }
151 0 $log->info( "Client finished." );
152 }
153  
154 ########################################
155 # INITIALIZATION
156  
157 0 ▶sub _init_oi2_base {
Subroutine _init_oi2_base: not called CC=1 SCAR=6.9
158 0 my ( $self ) = @_;
159 0 OpenInteract2::Log->init_from_website( $self->website_dir );
160 0 my $ctx = OpenInteract2::Context->create(
161 { website_dir => $self->website_dir });
162 0 $ctx->assign_request_type( 'lwp' );
163 0 $ctx->assign_response_type( 'lwp' );
164 0 $self->deploy_url( $ctx->DEPLOY_URL );
165 }
166  
167 0 ▶sub _init_read_daemon_options {
Subroutine _init_read_daemon_options: not called CC=5 SCAR=34.0
168 0 my ( $self ) = @_;
169  
170 # Already done?
171 0 my $config = $self->daemon_config;
172 0 ▶ return if ( ref $config eq 'HASH' and scalar keys %{ $config } );
Branch: 1 branch
BranchTrueFalseTotal
if ref $config eq "HASH" and scalar keys %{$config;} 0 0 0
Condition0%0/2
ref $config eq "HASH" and scalar keys %{$config;}
!ll
00
MC/DC0%0/1
ref $config eq "HASH" and scalar keys %{$config;}
ref $config eq "HASH"
Truth table0%0/2
A
A= ref $config eq "HASH"
Aresult
00
11
173  
174 0 my $daemon_config_file = $self->daemon_config_file;
175 0 ▶ unless ( $daemon_config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($daemon_config_file) 0 0 0
176 0 $daemon_config_file = catfile( $self->website_dir,
177 'conf', 'oi2_daemon.ini' );
178 0 $self->notify_observers(
179 'log', "Using daemon configuration from website directory" );
180 0 $self->daemon_config_file( $daemon_config_file );
181 }
182 0 $config = OpenInteract2::Config::IniFile->read_config({
183 filename => $self->daemon_config_file });
184 0 ▶ unless ( ref $config->{socket} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $config->{"socket"} eq "HASH") 0 0 0
185 0 die "No options specified under 'socket' section of ",
186 "configuration file '", $self->daemon_conf_file, "'\n";
187 }
188 0 return $self->daemon_config( $config );
189 }
190  
191 0 ▶sub _init_create_daemon {
Subroutine _init_create_daemon: not called CC=3 SCAR=24.8
192 0 my ( $self ) = @_;
193 0 ▶ unless ( ref $self->daemon_config->{socket} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->daemon_config->{"socket"} eq "HASH") 0 0 0
194 0 die "Daemon configuration is not setup properly: no ",
195 "entries under 'socket'\n";
196 }
197 0 my %socket_config = %{ $self->daemon_config->{socket} };
198 0 ▶ my $daemon = HTTP::Daemon::OpenInteract2Daemon->new( %socket_config )
Condition0%0/2
'HTTP::Daemon::OpenInteract2Daemon'->new(%socket_config) || die("Cannot create daemon! $!\n")
l!l
00
MC/DC0%0/1
'HTTP::Daemon::OpenInteract2Daemon'->new(%socket_config) || die("Cannot create daemon! $!\n")
'HTTP::Daemon::OpenInteract2Daemon'->new(%socket_config)
Truth table0%0/2
A
A= 'HTTP::Daemon::OpenInteract2Daemon'->new(%socket_config)
Aresult
11
00
199 || die "Cannot create daemon! $!\n";
200 0 $self->url( $daemon->url );
201 0 $self->notify_observers(
202 'log', "OpenInteract2 now running at URL '", $daemon->url, "'" );
203 0 return $self->daemon( $daemon );
204 }
205  
206 0 ▶sub _init_environment {
Subroutine _init_environment: not called CC=3 SCAR=24.8
207 0 my ( $self ) = @_;
208  
209 # We need to close all database handle created in the
210 # initialization process so the child doesn't try to use it.
211  
212 0 OpenInteract2::DatasourceManager->shutdown;
213  
214 # Entries in 'static_path' are not handled by OI2 (no security,
215 # templating, etc.), we just give the file to the client.
216  
217 0 my $config = $self->daemon_config;
218 0 ▶ if ( $config->{content}{static_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"content"}{"static_path"}) 0 0 0
219 my @paths = ( ref $config->{content}{static_path} eq 'ARRAY' )
220 0 ? @{ $config->{content}{static_path} }
221 0 ▶ : ( $config->{content}{static_path} );
Branch: 1 branch
BranchTrueFalseTotal
ref $config->{'content'}{'static_path'} eq 'ARRAY' ? : 0 0 0
222 0 $self->static_path( \@paths );
223 }
224  
225 # The 'server.name' entry is used by OI as a replacement for the
226 # server name reported by Apache/mod_perl in the 'ServerName'
227 # directive
228  
229 0 $self->server_name( $config->{server}{name} );
230 }
231  
232 0 ▶sub _open_pid_file {
Subroutine _open_pid_file: not called CC=9 SCAR=45.0
233 0 my ( $self ) = @_;
234 0 my $dir = dirname( $self->daemon_config_file );
235 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
236  
237 0 my $full_pid_file = catfile( $dir, $PID_FILE );
238 0 ▶ if ( -e $full_pid_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $full_pid_file) 0 0 0
239 0 ▶ my $fh = IO::File->new( $full_pid_file ) || return;
Condition0%0/2
'IO::File'->new($full_pid_file) || (return)
l!l
00
MC/DC0%0/1
'IO::File'->new($full_pid_file) || (return)
'IO::File'->new($full_pid_file)
Truth table0%0/2
A
A= 'IO::File'->new($full_pid_file)
Aresult
11
00
240 0 my $pid = <$fh>;
241 0 ▶ if ( $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
242 0 ▶ if ( kill 0 => $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid) 0 0 0
243 0 die "Server already running with PID '$pid'";
244 }
245 0 $log->info( "daemon: Removing PID file for defunct server process '$pid'" );
246 }
247 else {
248 0 $log->info( "daemon: Removing empty stale PID file" );
249 }
250 0 ▶ unless ( -w $full_pid_file && unlink $full_pid_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $full_pid_file and unlink $full_pid_file) 0 0 0
Condition0%0/3
-w $full_pid_file and unlink $full_pid_file
!ll&&!rl&&r
000
MC/DC0%0/2
-w $full_pid_file and unlink $full_pid_file
-w $full_pid_file unlink $full_pid_file
Truth table0%0/3
A and B
A= -w $full_pid_fileB= unlink $full_pid_file
ABresult
0X0
100
111
251 0 die "Cannot remove PID file '$full_pid_file'\n";
252 }
253 }
254 0 $self->pid_file( $full_pid_file );
255 0 ▶ return IO::File->new( $full_pid_file, O_WRONLY|O_CREAT|O_EXCL, 0644 )
Condition0%0/2
'IO::File'->new($full_pid_file, 193, 420) || die("Cannot create PID file '${full_pid_file}': $!")
l!l
00
MC/DC0%0/1
'IO::File'->new($full_pid_file, 193, 420) || die("Cannot create PID file '${full_pid_file}': $!")
'IO::File'->new($full_pid_file, 193, 420)
Truth table0%0/2
A
A= 'IO::File'->new($full_pid_file, 193, 420)
Aresult
11
00
256 || die "Cannot create PID file '$full_pid_file': $!";
257 }
258  
259 0 ▶sub _start_daemon {
Subroutine _start_daemon: not called CC=1 SCAR=6.9
260 0 my ( $self ) = @_;
261 0 my $fh = $self->_open_pid_file;
262 0 my $pid = $self->_become_daemon;
263 0 $self->pid( $pid );
264 0 $fh->print( $pid );
265 0 $fh->close();
266  
267 }
268  
269 0 ▶sub _become_daemon {
Subroutine _become_daemon: not called CC=3 SCAR=24.8
270 0 my ( $self ) = @_;
271 0 my $child = fork();
272 0 ▶ die "Cannot fork\n" unless ( defined $child );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child 0 0 0
273 0 ▶ exit(0) if ( $child );
Branch: 1 branch
BranchTrueFalseTotal
if $child 0 0 0
274 0 setsid();
275 0 open( STDIN, "</dev/null" );
276 0 open( STDOUT, ">daemon.log" );
277 0 open( STDERR, ">&STDOUT" );
278 0 chdir( '/' );
279 0 umask(0);
280 0 $ENV{PATH} = '/bin:/usr/bin:/sbin:/usr/sbin';
281 0 return $$;
282 }
283  
284 0 ▶sub _reaper {
Subroutine _reaper: not called CC=3 SCAR=24.8
285 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
286 0 ▶ while ( my $kid = waitpid( -1, WNOHANG ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $kid = waitpid(-1, 1) > 0) 0 0 0
287 0 $log->info( "Reaped child with PID '$kid'" );
288 }
289 }
290  
291 0 ▶sub _is_static_path {
Subroutine _is_static_path: not called CC=4 SCAR=30.0
292 0 my ( $self, $path ) = @_;
293 0 ▶ my $static_paths = $self->static_path || [];
Condition0%0/2
$self->static_path || []
l!l
00
MC/DC0%0/1
$self->static_path || []
$self->static_path
Truth table0%0/2
A
A= $self->static_path
Aresult
11
00
294 0 foreach my $check_path ( @{ $static_paths } ) {
295 0 ▶ if ( $path =~ /$check_path/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$check_path/) 0 0 0
296 0 return 1;
297 }
298 }
299 0 return 0;
300 }
301  
302 0 ▶sub _get_static_response {
Subroutine _get_static_response: not called CC=5 SCAR=34.0
303 0 my ( $self, $path ) = @_;
304 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
305  
306 0 my @parts = split /\/+/, $path;
307 0 my $file_path = catfile( CTX->lookup_directory( 'html' ), @parts );
308 0 $log->debug( "Trying to map '$path' -> '$file_path'" );
309 0 my ( $lwp_response );
310 0 ▶ if ( -f $file_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file_path) { } 0 0 0
311 0 ▶ eval { open( STATIC, '<', $file_path ) || die $! };
Branch: 1 branch
BranchTrueFalseTotal
open STATIC, "<", $file_path or die $! 0 0 0
312 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
313 0 $log->error( "Cannot open file '$file_path': $@" );
314 0 $lwp_response = HTTP::Response->new( RC_INTERNAL_SERVER_ERROR );
315 0 $lwp_response->content( "Failed to open file for request '$path'" );
316 }
317 else {
318 0 $log->info( "Opened file '$file_path' ok" );
319 0 $lwp_response = HTTP::Response->new( RC_OK );
320 0 my $mime_type = OpenInteract2::File->get_mime_type(
321 { filename => $file_path } );
322 0 $log->debug( "Got MIME type '$mime_type' for file" );
323 0 $lwp_response->content_type( $mime_type );
324 0 my $file_length = (stat $file_path)[7];
325 0 $log->debug( "Got file length '$file_length' for file" );
326 0 $lwp_response->content_length( $file_length );
327  
328 # TODO: It would be nice to stream this instead...
329 0 local $/ = undef;
330 0 my $data = <STATIC>;
331 0 CORE::close( STATIC );
332 0 $lwp_response->content( $data );
333 }
334 }
335 else {
336 0 $log->error( "Cannot map static file to request '$path'" );
337 0 $lwp_response = HTTP::Response->new( RC_NOT_FOUND );
338 0 $lwp_response->content( "File not found for request '$path'" );
339 }
340 0 return $lwp_response;
341 }
342  
343 0 ▶sub _get_non_context_response {
Subroutine _get_non_context_response: not called CC=1 SCAR=6.9
344 0 my ( $self, $path ) = @_;
345 0 my $lwp_response = HTTP::Response->new( RC_OK );
346 0 my $deploy_url = $self->deploy_url;
347 0 my $invalid_page = <<INVALID;
348 <h1>Invalid Request</h1>
349 <p>This web server cannot fill your request for <b><tt>$path</tt></b>.
350 It can only serve requests under the URL space
351 <b><tt>$deploy_url</tt></b>. Good luck!</p>
352 INVALID
353 0 $lwp_response->content( $invalid_page );
354 0 $lwp_response->content_type( 'text/html' );
355 0 return $lwp_response;
356 }
357  
358 1 1;
359  
360 __END__
« OpenInteract2/Repository.pm ↑ Summary OpenInteract2/Error.pm »