line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hub::Apache2::Handler; |
2
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
41
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# mod_perl.so |
5
|
1
|
|
|
1
|
|
626
|
use APR::OS (); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Apache2::Access (); |
7
|
|
|
|
|
|
|
use Apache2::RequestRec (); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# mod_apreq2.so |
10
|
|
|
|
|
|
|
#eval<<__end_eval; |
11
|
|
|
|
|
|
|
#use Apache2::Request (); |
12
|
|
|
|
|
|
|
#use Apache2::Cookie (); |
13
|
|
|
|
|
|
|
#use Apache2::Upload (); |
14
|
|
|
|
|
|
|
#__end_eval |
15
|
|
|
|
|
|
|
#$ENV{'USE_MOD_APREQ2'} = !$@; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use CGI qw(:standard); |
18
|
|
|
|
|
|
|
use Hub qw(:lib :webapp); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '4.00043'; |
21
|
|
|
|
|
|
|
our @EXPORT = qw//; |
22
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
23
|
|
|
|
|
|
|
handle_apache_request |
24
|
|
|
|
|
|
|
response_handler_callback |
25
|
|
|
|
|
|
|
/; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Format apache log messages with timestamp |
28
|
|
|
|
|
|
|
$SIG{__WARN__} = \&_sigwarn; |
29
|
|
|
|
|
|
|
$SIG{__DIE__} = \&_sigdie; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
32
|
|
|
|
|
|
|
# handle_apache_request - Apache2 mod_perl invokation wrapper |
33
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub handle_apache_request { |
36
|
|
|
|
|
|
|
my $handler = shift; |
37
|
|
|
|
|
|
|
my $r = shift; |
38
|
|
|
|
|
|
|
# Change to working directory |
39
|
|
|
|
|
|
|
die "Environment variable 'WORKING_DIR' not set or invalid" unless |
40
|
|
|
|
|
|
|
defined $ENV{'WORKING_DIR'} && -d $ENV{'WORKING_DIR'}; |
41
|
|
|
|
|
|
|
$ENV{'WORKING_DIR'} =~ s/\/$//; |
42
|
|
|
|
|
|
|
chdir $ENV{'WORKING_DIR'}; |
43
|
|
|
|
|
|
|
# Process request |
44
|
|
|
|
|
|
|
return Hub::callback(\&_handle_request, $handler, $r); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
48
|
|
|
|
|
|
|
# _handle_request - Worker method |
49
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _handle_request { |
52
|
|
|
|
|
|
|
my $handler = shift; |
53
|
|
|
|
|
|
|
my $r = shift; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Rescan disk for files changes |
56
|
|
|
|
|
|
|
Hub::frefresh(); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Set the request data |
59
|
|
|
|
|
|
|
if (defined $r) { |
60
|
|
|
|
|
|
|
$r->no_cache(1); |
61
|
|
|
|
|
|
|
my $table = $r->headers_in; |
62
|
|
|
|
|
|
|
if (can($table, 'FIRSTKEY')) { |
63
|
|
|
|
|
|
|
foreach my $k (keys %$table) { |
64
|
|
|
|
|
|
|
$$Hub{"/sys/request/headers/$k"} = $table->get($k); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# Get SID from Cookie |
70
|
|
|
|
|
|
|
my $session_id = (); |
71
|
|
|
|
|
|
|
if ($$Hub{'/sys/request/headers/Cookie'}) { |
72
|
|
|
|
|
|
|
my $pattern = Hub::COOKIE_SID . '=([0-9]+)'; |
73
|
|
|
|
|
|
|
($session_id) = $$Hub{'/sys/request/headers/Cookie'} =~ /$pattern/; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Apache Session |
77
|
|
|
|
|
|
|
if ($$Hub{'/conf/session/enable'}) { |
78
|
|
|
|
|
|
|
$$Hub{'/session'} = mkinst('Session', $session_id); |
79
|
|
|
|
|
|
|
} else { |
80
|
|
|
|
|
|
|
$$Hub{'/session'} = {}; # Not a persistent session |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Process request |
84
|
|
|
|
|
|
|
my $result = &$handler($r); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Save session |
87
|
|
|
|
|
|
|
$$Hub{'/session'}->save() if can($$Hub{'/session'}, 'save'); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
return $result; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
}#_handle_request |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
94
|
|
|
|
|
|
|
# response_handler_callback - Environment wrapper for response handlers |
95
|
|
|
|
|
|
|
# response_handler_callback \&subroutine, $r |
96
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub response_handler_callback { |
99
|
|
|
|
|
|
|
my $handler = shift; |
100
|
|
|
|
|
|
|
my $r = shift; |
101
|
|
|
|
|
|
|
my $time1 = [gettimeofday]; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
my $page_path = Hub::getaddr($ENV{'SCRIPT_FILENAME'}); |
104
|
|
|
|
|
|
|
$$Hub{'/sys/request/page/url'} = $ENV{'SCRIPT_NAME'}; |
105
|
|
|
|
|
|
|
$$Hub{'/sys/request/page/name'} = Hub::getname($page_path); |
106
|
|
|
|
|
|
|
$$Hub{'/sys/request/page/path'} = Hub::getpath($page_path); |
107
|
|
|
|
|
|
|
$$Hub{'/sys/request/page/ext'} = Hub::getext($page_path); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Set this beore action URL's execute (so they may change it) |
110
|
|
|
|
|
|
|
$$Hub{'/sys/response/template'} = $page_path; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Parse CGI parameters into '/cgi' namespace |
113
|
|
|
|
|
|
|
my %req_opts = ( |
114
|
|
|
|
|
|
|
POST_MAX => 50000000, |
115
|
|
|
|
|
|
|
TEMP_DIR => '/tmp', |
116
|
|
|
|
|
|
|
); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Hub::merge(\%req_opts, $$Hub{'/conf/request'}, -overwrite) |
119
|
|
|
|
|
|
|
if isa($$Hub{'/conf/request'}, 'HASH'); |
120
|
|
|
|
|
|
|
$$Hub{'/sys/CGI'} = $$Hub{'/sys/ENV/USE_MOD_APREQ2'} |
121
|
|
|
|
|
|
|
? Apache2::Request->new($r, %req_opts) |
122
|
|
|
|
|
|
|
: CGI->new(); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
$$Hub{'/cgi'} = {}; |
125
|
|
|
|
|
|
|
foreach my $key (Hub::keydepth_sort($$Hub{'/sys/CGI'}->param())) { |
126
|
|
|
|
|
|
|
next unless $key; |
127
|
|
|
|
|
|
|
my @value = $$Hub{'/sys/CGI'}->param($key); |
128
|
|
|
|
|
|
|
warn "param ", $key, "=", join("\n", @value), "\n" |
129
|
|
|
|
|
|
|
if ($$Hub{'/sys/ENV/DEBUG'}); |
130
|
|
|
|
|
|
|
$$Hub{Hub::fixpath("/cgi/$key")} = @value > 1 ? \@value : pop @value; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# Execute action URL's |
134
|
|
|
|
|
|
|
if (defined $$Hub{'/cgi/action'}) { |
135
|
|
|
|
|
|
|
my $actions = $$Hub{'/cgi/action'}; |
136
|
|
|
|
|
|
|
$actions = isa($actions, 'ARRAY') ? $actions : [$actions]; |
137
|
|
|
|
|
|
|
foreach my $filename (@$actions) { |
138
|
|
|
|
|
|
|
unless ($$Hub{$filename}) { |
139
|
|
|
|
|
|
|
warn "Cannot find action module: $filename"; |
140
|
|
|
|
|
|
|
next; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
my $retval = Hub::modexec(-filename => $filename); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# Create /user if authenticated |
147
|
|
|
|
|
|
|
Hub::Apache2::AuthenSHA::authenticate(); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# Callback to the response handler |
150
|
|
|
|
|
|
|
my $time2 = [gettimeofday]; |
151
|
|
|
|
|
|
|
my $result = &$handler($r); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
# Dump performance statistics |
154
|
|
|
|
|
|
|
my $time3 = [gettimeofday]; |
155
|
|
|
|
|
|
|
if ($$Hub{'/sys/ENV/DEBUG'}) { |
156
|
|
|
|
|
|
|
warn Hub::fw(20, Hub::getname($$Hub{'/sys/ENV/SCRIPT_NAME'})) . " " |
157
|
|
|
|
|
|
|
. " total=" . Hub::fw(8,tv_interval($time1, $time3)) |
158
|
|
|
|
|
|
|
. " parse=" . Hub::fw(8,tv_interval($time2, $time3)) |
159
|
|
|
|
|
|
|
. " setup=" . Hub::fw(8,tv_interval($time1, $time2)) |
160
|
|
|
|
|
|
|
. " path=" . $$Hub{'/sys/ENV/SCRIPT_NAME'} |
161
|
|
|
|
|
|
|
. "\n"; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
return $result; |
165
|
|
|
|
|
|
|
}#response_handler_callback |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
168
|
|
|
|
|
|
|
# _sigwarn - Warning handler |
169
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub _sigwarn { |
172
|
|
|
|
|
|
|
my @caller = caller(0); |
173
|
|
|
|
|
|
|
my $tid = APR::OS::current_thread_id(); |
174
|
|
|
|
|
|
|
print STDERR '[', Hub::datetime(-apache), "] [warning] [$$:$tid] ", @_ |
175
|
|
|
|
|
|
|
if @caller && $caller[2] > 0; |
176
|
|
|
|
|
|
|
if ($ENV{'DEBUG'} && $ENV{'DEBUG'} > 1) { |
177
|
|
|
|
|
|
|
for my $i (0 .. 8) { |
178
|
|
|
|
|
|
|
my @caller = caller($i); |
179
|
|
|
|
|
|
|
last unless @caller; |
180
|
|
|
|
|
|
|
last if $caller[2] == 0; |
181
|
|
|
|
|
|
|
print STDERR Hub::fw(27), "[stack-$i] $caller[0] line $caller[2]\n"; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
}#_sigwarn |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
187
|
|
|
|
|
|
|
# _sigdie - Die handler (fatals to browser) |
188
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub _sigdie { |
191
|
|
|
|
|
|
|
# if ($ENV{'DEBUG'}) { |
192
|
|
|
|
|
|
|
# print STDOUT @_; |
193
|
|
|
|
|
|
|
# } else { |
194
|
|
|
|
|
|
|
# print "An error occured\n"; |
195
|
|
|
|
|
|
|
# } |
196
|
|
|
|
|
|
|
die '[', Hub::datetime(-apache), "] [fatal] ", @_; |
197
|
|
|
|
|
|
|
}#_sigdie |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
1; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
__END__ |