blib/lib/WebDyne/Request/Apache.pm untested

filter: Statementstmt 0.0% 0 / 234 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 52 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 482 CC 47 SCAR 34.7
CC
47
Cov
0%
read_chunk
132.0
remote_port
90.0
remote_address
56.0
CRAP 32.2
1 #
2 # This file is part of WebDyne.
3 #
4 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 # Full license text is available at:
10 #
11 # <http://dev.perl.org/licenses/>
12 #
13  
14 package WebDyne::Request::Apache;
15  
16  
17 # Compiler Pragma
18 #
19 use strict qw(vars);
20 use vars qw($VERSION @ISA);
21 use warnings;
22 no warnings qw(uninitialized);
23  
24  
25 # External modules
26 #
27 use File::Spec;
28 use File::Spec::Unix;
29 use HTTP::Status qw(status_message HTTP_OK HTTP_NOT_FOUND HTTP_FOUND);
30 use URI;
31 use Data::Dumper;
32 use HTTP::Headers::Fast;
33  
34  
35 # mod_perl 2 modules
36 #
37 use Apache2::RequestRec ();
38 use Apache2::RequestIO ();
39 use Apache2::RequestUtil ();
40 use Apache2::Connection ();
41 use Apache2::Const ();
42 use Apache2::ServerUtil ();
43 use APR::Table ();
44  
45  
46 # WebDyne modules
47 #
48 use WebDyne::Util;
49 use WebDyne::Constant;
50 use WebDyne::Request::Common qw(handler_methods_init);
51 use WebDyne::Request::Fake;
52  
53  
54 # Version information
55 #
56 $VERSION='3.029';
57  
58  
59 # Debug load
60 #
61 0 && debug("Loading %s version $VERSION", __PACKAGE__);
62  
63  
64 # Init
65 #
66 &init() unless defined(&method);
67  
68  
69 # All done. Positive return
70 #
71 1;
72  
73  
74 #==================================================================================================
75  
76  
77 sub init {
78  
79 # Setup request handler abstraction methods
80 #
81 my %method=(
82 accept_encoding => undef,
83 accept_language => undef,
84 accept => undef,
85 args => 'args',
86 as_string => undef,
87 authority => undef,
88 authorization => undef,
89 auth_type => \&auth_type,
90 base => undef,
91 base_url => undef,
92 body_handle => \&body_handle,
93 body => \&body,
94 cache_control => undef,
95 charset => undef,
96 cleanup_register => undef,
97 client_address => \&remote_address,
98 content => \&body,
99 content_encoding => \&content_encoding,
100 content_length => \&content_length,
101 content_type => \&content_type,
102 cookies => undef,
103 cookie => undef,
104 custom_response => undef,
105 cwd => undef,
106 dir_config => undef,
107 document_root => 'document_root',
108 env => \&env,
109 etag => undef,
110 #filename => 'filename',
111 filename => \&filename,
112 finalize => undef,
113 finfo => undef,
114 form_parameters => undef,
115 forwarded_for => undef,
116 fragment => undef,
117 handler => 'handler',
118 header => \&headers_in,
119 headers => \&headers_in,
120 header_only => 'header_only',
121 headers_in => \&headers_in,
122 headers_out => \&headers_out,
123 hostname => 'hostname',
124 host => \&host,
125 https => \&https,
126 http_version => 'protocol',
127 id => \&id,
128 if_modified_since => undef,
129 if_none_match => undef,
130 input => \&body_handle,
131 is_ajax => undef,
132 is_main => undef,
133 location => undef,
134 log_error => undef,
135 lookup_file => 'lookup_file',
136 lookup_uri => 'lookup_uri',
137 main => undef,
138 media_type => undef,
139 method => 'method',
140 mtime => undef,
141 multipart_parameters=> undef,
142 next => undef,
143 notes => 'notes',
144 origin => undef,
145 output_filters => undef,
146 path_info => sub { shift()->uri->path },
147 #path_info => 'path_info',
148 path_parameters => undef,
149 path => \&path_info,
150 pool => 'pool',
151 preferred_charset => undef,
152 preferred_encoding => undef,
153 preferred_language => undef,
154 preferred_media_type=> undef,
155 prev => undef,
156 print => 'print',
157 protocol => 'protocol',
158 query_parameters => undef,
159 query_string => 'args',
160 redirect => undef,
161 referer => undef,
162 register_cleanup => undef,
163 remote_address => \&remote_address,
164 remote_host => \&remote_host,
165 remote_port => \&remote_port,
166 remote_user => 'user',
167 request_time => 'request_time',
168 route => undef,
169 run => undef,
170 scheme => \&scheme,
171 script_name => undef,
172 secure => \&https,
173 sendfile => undef,
174 send_http_header => undef,
175 server_name => \&server_name,
176 server_port => \&server_port,
177 session_id => undef,
178 session => undef,
179 set_handlers => 'set_handlers',
180 status_line => 'status_line',
181 status => 'status',
182 unparsed_uri => 'unparsed_uri',
183 uploads => undef,
184 uri => \&uri,
185 url => \&uri,
186 user_agent => undef,
187 user => 'user',
188 write => undef,
189 err_html => undef,
190 DESTROY => \&DESTROY
191 );
192  
193  
194 # Make handler methods
195 #
196 foreach my $handler (qw(req res)) {
197 *{$handler}=sub { return shift()->{$handler} };
198 }
199
200
201 # Now create everything and check
202 #
203 return handler_methods_init(__PACKAGE__, 'Apache2::RequestRec', \%method);
204
205 }
206  
207  
208 sub new {
209  
210  
211 # New Apache request
212 #
213 my ($class, $r, %opt)=@_;
214 0 && debug("$class, r: %s, opt:%s, calller:%s", Dumper($r, \%opt, [caller(0)]));
215  
216  
217 # Require Apache request record
218 #
219 $r || return err('no Apache request supplied');
220  
221  
222 # Finished, pass back
223 #
224 return bless { req => $r, %opt }, $class;
225  
226 }
227  
228  
229 sub filename {
230  
231 # Can be overridden by DOCUMENT_ROOT environment var
232 #
233 my $r=shift();
234 return $ENV{'DOCUMENT_ROOT'} || $r->{'filename'} || $r->{'req'}->filename();
235
236 }
237  
238  
239 sub _headers {
240  
241 my ($r, $direction, @param)=@_;
242 0 && debug("r: $r, direction: $direction: param: %s", Dumper(\@param));
243 my $table=$r->{'req'}->$direction();
244 if (@param == 1) {
245 #return $table->get($param[0]);
246 return $table->{$param[0]};
247 }
248 elsif (@param > 1) {
249 while (my ($k, $v) = splice(@param, 0, 2)) {
250 $table->set($k => $v);
251 }
252 return $r;
253 }
254 else {
255 my $headers_or=HTTP::Headers::Fast->new();
256 $table->do(sub { $headers_or->push_header($_[0] => $_[1]); return 1; });
257 return $headers_or;
258 }
259 }
260
261  
262 sub headers_in {
263 shift()->_headers('headers_in', @_);
264 }
265  
266  
267 sub headers_out {
268 shift()->_headers('headers_out', @_);
269 }
270  
271  
272 sub content_type {
273 my $r=shift();
274 return @_ ? $r->{'req'}->content_type(@_) : $r->{'req'}->content_type();
275 }
276  
277  
278 sub content_length {
279 my $r=shift();
280 return @_ ? $r->headers_out('Content-Length', shift()) : $r->headers_in('Content-Length');
281 }
282  
283  
284 sub content_encoding {
285 my $r=shift();
286 return @_ ? $r->headers_out('Content-Encoding', shift()) : $r->headers_in('Content-Encoding');
287 }
288  
289  
290 sub host {
291 my $r=shift();
292 return $r->headers_in('Host');
293 }
294  
295  
296 sub server_name {
297 my $r=shift();
298 return $r->{'req'}->can('get_server_name') ? $r->{'req'}->get_server_name() : $r->{'req'}->hostname();
299 }
300  
301  
302 sub server_port {
303 my $r=shift();
304 if ($r->{'req'}->can('get_server_port')) {
305 return $r->{'req'}->get_server_port();
306 }
307 if (my $conn=$r->{'req'}->connection()) {
308 return $conn->local_addr->port if $conn->can('local_addr');
309 }
310 return undef;
311 }
312  
313  
314 sub remote_address {
315 my $r=shift();
316 my $req=$r->{'req'};
317 if (my $conn=$req->connection()) {
318 return $conn->client_ip if $conn->can('client_ip');
319 return $conn->remote_ip if $conn->can('remote_ip');
320 if ($conn->can('remote_addr')) {
321 my $addr=$conn->remote_addr;
322 return $addr->ip_get if ($addr && $addr->can('ip_get'));
323 }
324 }
325 return $req->subprocess_env('REMOTE_ADDR') if $req->can('subprocess_env');
326 }
327  
328  
329 sub remote_host {
330 my $r=shift();
331 my $req=$r->{'req'};
332 if ($req->can('get_remote_host')) {
333 return $req->get_remote_host(Apache2::Const::REMOTE_NAME());
334 }
335 return $r->remote_address();
336 }
337  
338  
339 sub remote_port {
340 my $r=shift();
341 my $req=$r->{'req'};
342 if (my $conn=$req->connection()) {
343 return $conn->client_port if $conn->can('client_port');
344 if ($conn->can('client_addr')) {
345 my $addr=$conn->client_addr;
346 return $addr->port if ($addr && $addr->can('port'));
347 }
348 return $conn->remote_port if $conn->can('remote_port');
349 if ($conn->can('remote_addr')) {
350 my $addr=$conn->remote_addr;
351 return $addr->port if ($addr && $addr->can('port'));
352 }
353 }
354 return $req->subprocess_env('REMOTE_PORT') if $req->can('subprocess_env');
355 return undef;
356 }
357  
358  
359 sub auth_type {
360 my $r=shift();
361 return $r->{'req'}->can('ap_auth_type') ? $r->{'req'}->ap_auth_type() : $r->{'req'}->auth_type();
362 }
363  
364  
365 sub https {
366 my $r=shift();
367 return (($r->headers_in('HTTPS') || $r->{'req'}->subprocess_env('HTTPS') || '') eq 'on');
368 }
369  
370  
371 sub scheme {
372 my $r=shift();
373 return $r->{'req'}->subprocess_env('REQUEST_SCHEME') ||
374 ($r->https() ? 'https' : 'http');
375 }
376  
377  
378 sub id {
379 return shift()->{'req'}->subprocess_env('UNIQUE_ID')
380 }
381  
382  
383 sub env {
384 my $r=shift();
385 my $table=$r->{'req'}->subprocess_env();
386 my %env;
387 $table->do(sub { $env{$_[0]}=$_[1]; return 1; });
388 return \%env;
389 }
390  
391  
392 sub body {
393 my $r=shift();
394 return $r->{'body'} if exists $r->{'body'};
395 my $input_or=WebDyne::Request::Apache::Body->TIEHANDLE($r->{'req'});
396 my $buf='';
397 my $chunk;
398 while ($input_or->read_chunk($chunk, 8192)) {
399 $buf.=$chunk;
400 }
401 return $r->{'body'}=$buf;
402 }
403  
404  
405 sub body_handle {
406  
407 my $r=shift();
408 unless ($r->{'input'}) {
409 no warnings qw(once);
410 #$r->{'input'}=tie *BODY, WebDyne::Request::Apache::Body_Handle, $r->{'req'};
411 tie *BODY, WebDyne::Request::Apache::Body, $r->{'req'};
412 $r->{'input'}=*BODY;
413
414 }
415 return $r->{'input'};
416 }
417  
418  
419  
420 sub uri {
421 #return URI->new(shift()->{'req'}->uri);
422 my $r=shift();
423 my $uri_or=URI->new();
424 $uri_or->scheme($r->scheme);
425 $uri_or->host($r->server_name);
426 $uri_or->port($r->server_port);
427 $uri_or->path($r->{'req'}->uri);
428 $uri_or->query($r->{'req'}->args);
429 return $uri_or->canonical;
430 }
431  
432  
433 sub DESTROY {
434  
435 my $r=shift();
436 untie $r->{'input'} if $r->{'input'};
437
438 }
439  
440 1;
441  
442  
443 # TieHandle package to link mod_perl body to a handle
444 #
445 package WebDyne::Request::Apache::Body;
446  
447 sub TIEHANDLE {
448 my ($class, $request_or)=@_;
449 my $length=$request_or->headers_in()->{'Content-Length'};
450 die "invalid Content-Length\n" if defined($length) && $length !~ /\A[0-9]+\z/;
451  
452 # LimitRequestBody is the primary protection. Also bound adapter reads,
453 # including multipart input, if the server permits a larger request.
454 #
455 my $limit=$WebDyne::Request::Apache::WEBDYNE_CGI_POST_MAX;
456 die "request body exceeds upload limit\n" if defined($length) && $length > $limit;
457 return bless {
458 req => $request_or,
459 length => $length,
460 limit => $limit,
461 read => 0,
462 buffer => '',
463 eof => 0,
464 }, $class;
465 }
466  
467  
468 sub read_chunk {
469 my ($self, undef, $length)=@_;
470 return 0 unless $length;
471 return 0 if $self->{'eof'};
472  
473 # Without a declared length, read to Apache's EOF. One extra byte
474 # distinguishes an exact-limit body from an oversized one.
475 #
476 my $remaining=defined($self->{'length'})
477 ? $self->{'length'}-$self->{'read'} : $self->{'limit'}-$self->{'read'}+1;
478 unless ($remaining) {
479 $self->{'eof'}=1;
480 return 0;
481 }
482 $length=$remaining if $length > $remaining;
483 $length=8192 if $length > 8192;
484 my $chunk='';
485 my $read=$self->{'req'}->read($chunk, $length);
486 die "unable to read request body\n" unless defined($read);
487 unless ($read) {
488 die "incomplete request body\n" if defined($self->{'length'});
489 $self->{'eof'}=1;
490 }
491 $self->{'read'}+=$read;
492 die "request body exceeds upload limit\n" if $self->{'read'} > $self->{'limit'};
493 $_[1]=$chunk;
494 return $read;
495 }
496  
497  
498 sub READ {
499 my ($self, undef, $len, $offset) = @_;
500 my $buf = '';
501 my $read = $self->read_chunk($buf, $len);
502 return 0 unless $read;
503 substr($_[1], $offset || 0) = $buf;
504 return $read;
505 }
506  
507 sub READLINE {
508 my ($self) = @_;
509 return undef if $self->{eof};
510 my $line = '';
511 while (1) {
512  
513 # check existing buffer
514 if ($self->{buffer} =~ s/(.*?\n)//s) {
515 return $1;
516 }
517  
518 # read more data
519 my $chunk = '';
520 my $n = $self->read_chunk($chunk, 8192);
521  
522 unless ($n) {
523 $self->{eof} = 1;
524 return length($self->{buffer})
525 ? delete $self->{buffer}
526 : undef;
527 }
528  
529 $self->{buffer} .= $chunk;
530 }
531 }
532  
533 sub EOF {
534 my ($self) = @_;
535 return $self->{eof} && $self->{buffer} eq '';
536 }
537  
538 sub CLOSE {
539 return 1;
540 }
541  
542 1;
543  
544 __END__