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

filter: Statementstmt 0.0% 0 / 174 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 78 MC/DCmcdc 0.0% 0 / 49 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 388 CC 17 SCAR 25.0
CC
17
Cov
0%
new
56.0
content_length
20.0
body
12.0
CRAP 12.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::PAGI;
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 use Future::AsyncAwait;
34 use PAGI::Request;
35  
36  
37 # WebDyne modules
38 #
39 use WebDyne::Util;
40 use WebDyne::Constant;
41 use WebDyne::PAGI::Constant;
42 use WebDyne::Request::Common qw(handler_methods_init);
43 use WebDyne::Request::Fake;
44  
45  
46 # Version information
47 #
48 $VERSION='3.029';
49  
50  
51 # Debug load
52 #
53 0 && debug("Loading %s version $VERSION", __PACKAGE__);
54  
55  
56 # Init
57 #
58 &init() unless defined(&method);
59  
60  
61 # All done. Positive return
62 #
63 1;
64  
65  
66 #==================================================================================================
67  
68  
69 sub init {
70  
71 # Setup pass through methods
72 #
73 my %method=(
74 accept => undef,
75 accept_encoding => undef,
76 accept_language => undef,
77 args => 'query_string',
78 as_string => undef,
79 authority => undef,
80 authorization => undef,
81 auth_type => undef,
82 base => \&base,
83 base_url => \&base_url,
84 body_handle => \&body_handle,
85 body => \&body,
86 cache_control => undef,
87 charset => undef,
88 cleanup_register => undef,
89 client_address => sub { shift()->{'req'}->client()->[0] },
90 content => \&body,
91 content_encoding => undef,
92 content_length => \&content_length,
93 content_type => undef,,
94 cookies => 'cookies',
95 cookie => 'cookies',
96 custom_response => undef,
97 cwd => undef,
98 dir_config => undef,
99 document_root => undef,
100 env => \&env,
101 etag => undef,
102 filename => undef,
103 finalize => undef,
104 finfo => undef,
105 form_parameters => 'form_params',
106 forwarded_for => undef,
107 fragment => undef,
108 handler => undef,
109 header => \&headers_in,
110 headers => \&headers_in,
111 header_only => \&header_only,
112 headers_in => \&headers_in,
113 headers_out => \&headers_out,
114 hostname => 'host',
115 host => 'host',
116 https => \&https,
117 http_version => 'http_version',
118 id => \&id,
119 if_modified_since => undef,
120 if_none_match => undef,
121 input => \&body_handle,
122 is_ajax => undef,
123 is_main => undef,
124 location => undef,
125 log_error => undef,
126 lookup_file => undef,
127 lookup_uri => undef,
128 main => undef,
129 media_type => undef,
130 method => 'method',
131 mtime => undef,
132 multipart_parameters=> 'form_params',
133 next => undef,
134 notes => undef,
135 origin => undef,
136 output_filters => undef,
137 path_info => sub { scope_path(shift()->{'scope'}) },
138 path_parameters => undef,
139 path => 'path',
140 pool => undef,
141 preferred_charset => undef,
142 preferred_encoding => undef,
143 preferred_language => undef,
144 preferred_media_type=> undef,
145 prev => undef,
146 print => undef,
147 protocol => sub { 'HTTP/'. shift()->{'req'}->http_version() },
148 query_parameters => 'query_params',
149 query_string => 'query_string',
150 redirect => undef,
151 referer => undef,
152 register_cleanup => undef,
153 remote_address => 'client',
154 remote_host => 'client',
155 remote_port => undef,
156 remote_user => undef,
157 request_time => \&request_time,
158 route => undef,
159 run => undef,
160 scheme => 'scheme',
161 script_name => sub { shift()->{'scope'}{'root_path'} || '' },
162 secure => \&secure,
163 sendfile => undef,
164 send_http_header => undef,
165 server_name => \&server_name,
166 server_port => \&server_port,
167 session_id => undef,
168 session => undef,
169 set_handlers => undef,
170 status_line => undef,
171 status => undef,
172 unparsed_uri => \&uri,
173 uploads => 'uploads',
174 uri => undef,
175 url => undef,
176 user_agent => undef,
177 user => undef,
178 write => undef,
179 err_html => undef,
180 DESTROY => undef
181 );
182
183
184 # Implement and return
185 #
186 foreach my $handler (qw(req res sse ws)) {
187 *{$handler}=sub { return shift()->{$handler} };
188 }
189 return handler_methods_init(__PACKAGE__, 'PAGI::Request', \%method);
190  
191
192 }
193  
194  
195 sub new {
196  
197  
198 # New PAGI request
199 #
200 my ($class, %r)=@_;
201 0 && debug("$class, r: %s, calller:%s", Dumper(\%r, [caller(0)]));
202  
203  
204 # Require scope
205 #
206 #$r{'scope'} || return err('no PAGI scope object supplied');
207 #$r{'req'} || return err('no PAGI::Request object supplied');
208  
209  
210 # Try to figure out filename user wants
211 #
212 unless ($r{'filename'}) {
213  
214 # Not supplied - need to work out
215 #
216 0 && debug('filename not supplied, determining from request');
217  
218 my $fn;
219 my $invalid_path;
220 if (my $dn=($r{'document_root'} || $ENV{'DOCUMENT_ROOT'} || $DOCUMENT_ROOT)) {
221  
222 # Get from URI and location
223 #
224 my $uri=$r{'uri'} || scope_path($r{'scope'});
225 0 && debug("uri: $uri");
226 my @uri_part=grep { length($_) } split(m{/+}, $uri);
227 if (grep { $_ eq '..' } @uri_part) {
228 0 && debug("rejecting path traversal attempt in uri: $uri");
229 $fn=undef;
230 $invalid_path=1;
231 }
232 else {
233 $fn=File::Spec->rel2abs(File::Spec->catfile($dn, @uri_part));
234 }
235 0 && debug("fn: $fn from dn: $dn, uri: $uri");
236  
237 }
238  
239 # Need to add default psp file ?
240 #
241 unless ($invalid_path || ($fn=~WEBDYNE_PSP_EXT_RE)) { # fastest
242  
243 # Is it a directory that exists ? Only append default document if that is the case, else let the api code
244 # handle it
245 #
246 if (($fn=~/\/$/) || ((-d $fn) || !$fn)) {
247  
248 # Append default doc to path, which appears at moment to be a directory ?
249 #
250 my $document_default=$r{'document_default'} || $DOCUMENT_DEFAULT;
251 0 && debug("appending document default $document_default to fn:$fn");
252  
253 # If absolute path just use it
254 #
255 if (File::Spec->file_name_is_absolute($document_default)) {
256  
257 # Yep - absolute path
258 #
259 $fn=$document_default
260 }
261 else {
262  
263 # Otherwise append to existing path
264 #
265 $fn=File::Spec->catfile($fn, split m{/+}, $document_default); #/
266 $fn=File::Spec->rel2abs($fn);
267 }
268 }
269 else {
270  
271 # Not .psp file, do not want
272 #
273 0 && debug("fn: $fn does not end with /, leaving undef");
274 $fn=undef;
275 }
276 }
277  
278 # Final sanity check
279 #
280 0 && debug("final fn: $fn");
281 $r{'filename'}=$fn;
282  
283 }
284  
285  
286 # Finished, pass back
287 #
288 return bless \%r, $class;
289  
290 }
291  
292  
293 sub scope_path {
294  
295 my $scope_hr=shift();
296 my $path=$scope_hr->{'path'} || '';
297 my $root=$scope_hr->{'root_path'} || '';
298  
299 # Strip the mount prefix only at a path boundary, so /app does not
300 # match /apple. Preserve the original scope for downstream consumers.
301 #
302 $root=~s{/+$}{};
303 $path=~s{^\Q$root\E(?=/|$)}{} if length($root);
304 return $path;
305  
306 }
307  
308  
309 sub env {
310  
311 return shift()->{'scope'}
312
313 }
314  
315  
316 sub id {
317 return shift()->{'scope'}{'request_id'};
318 }
319  
320  
321 sub https {
322 return (shift()->scheme() || '') eq 'https';
323 }
324  
325  
326 sub secure {
327 return (shift()->scheme() || '') eq 'https';
328 }
329  
330  
331 sub server_name {
332 my $r=shift();
333 if (my $server_ar=$r->{'scope'}{'server'}) {
334 return $server_ar->[0];
335 }
336 return undef;
337 }
338  
339  
340 sub server_port {
341 my $r=shift();
342 if (my $server_ar=$r->{'scope'}{'server'}) {
343 return $server_ar->[1];
344 }
345 return undef;
346 }
347  
348  
349 sub content_length {
350  
351 my $self=shift();
352 my $length=$self->{'req'}->content_length();
353 return $length if defined($length);
354  
355 # Supply the buffered byte length for synchronous CGI parsing when
356 # the request did not declare Content-Length.
357 #
358 my $scope_hr=$self->{'scope'};
359 if (exists $scope_hr->{'webdyne.pagi.multipart_body'}) {
360 return length(${$scope_hr->{'webdyne.pagi.multipart_body'}});
361 }
362 if ($scope_hr->{'pagi.request.body.read'}) {
363 return length($scope_hr->{'pagi.request.body'});
364 }
365  
366 return undef;
367  
368 }
369  
370  
371 sub body {
372  
373 my $self=shift();
374 my $scope_hr=$self->{'scope'};
375  
376 if (exists $scope_hr->{'webdyne.pagi.multipart_body'}) {
377 return ${$scope_hr->{'webdyne.pagi.multipart_body'}};
378 }
379 if ($scope_hr->{'pagi.request.body.read'}) {
380 return $scope_hr->{'pagi.request.body'};
381 }
382  
383 return err('request body is not buffered; await req()->body() before synchronous access');
384  
385 }
386  
387  
388 sub body_handle {
389  
390 my $self=shift();
391 my $body=$self->body();
392 return unless defined($body);
393  
394 # Each handle reads the completed bytes from the beginning. No temporary
395 # file or private server event-loop access is needed for synchronous IO.
396 #
397 open(my $body_fh, '<', \$body) ||
398 return err('unable to open buffered PAGI body: %s', $!);
399 binmode($body_fh);
400 return $body_fh;
401  
402 }
403  
404  
405 sub headers_in {
406  
407 my $r=shift();
408 0 && debug("r: $r, param: %s", Dumper(\@_));
409 my $headers_in_or=$r->{'headers_in'} ||= do {
410 my $headers_pagi_or=$r->{'req'}->headers();
411 0 && debug('headers_pagi_or: %s', Dumper($headers_pagi_or));
412 my @header;
413 #foreach my $header ($headers_pagi_or->keys) {
414 foreach my $header ($headers_pagi_or->names) {
415 my @value=$headers_pagi_or->get_all($header);
416 $header=~s{(^|-)([a-z])}{$1\u$2}g;
417 map { push @header, ($header, $_) } @value;
418 }
419 0 && debug('header_fast: %s', Dumper(\@header));
420 HTTP::Headers::Fast->new(@header)
421 };
422 0 && debug('headers_in now: %s', Dumper($headers_in_or));
423
424 # Now run
425 #
426 #return $r->SUPER::headers_in(@_);
427 return $r->WebDyne::Request::Fake::headers_in(@_);
428
429 }
430  
431  
432 sub headers_out {
433 my $r=shift();
434 my $headers_or=$r->{'headers_out'}
435 ||= HTTP::Headers::Fast->new(map { @{$_} } @{$r->{'res'}->headers()});
436 if (@_) {
437 $r->{'res'}->header(@_);
438 return $headers_or->header(@_);
439 }
440 return $headers_or;
441 }
442  
443  
444 sub header_only {
445 return (shift()->method() eq 'HEAD');
446 }
447  
448  
449 sub request_time {
450 return shift()->{'scope'}{'start_time'};
451 }
452  
453  
454 sub base {
455 return URI->new(shift()->_uri_base())->canonical();
456 }
457  
458  
459 sub base_url {
460 return URI->new(shift()->_uri_base())->canonical();
461 }
462  
463  
464 1;
465  
466 __END__