blib/lib/WebDyne/PAGI.pm

filter: Statementstmt 92.5% 25 / 27 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.4% 34 / 36 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/WebDyne/Err/Constant.pm ↑ Summary blib/lib/WebDyne/PSGI.pm »
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 package WebDyne::PAGI;
14  
15  
16 # Compiler Pragma
17 #
18 1 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 # External Modules
25 #
26 1 ▶use HTTP::Status qw(:constants is_success is_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Cwd qw(fastcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=2.6
31 use Sub::Util qw(set_subname);
32 use File::Basename;
33 use File::Spec;
34  
35  
36 # PAGI modules
37 #
38 use PAGI::Request;
39 use PAGI::Response;
40  
41  
42 # WebDyne Modules
43 #
44 use WebDyne;
45 use WebDyne::Constant;
46 use WebDyne::Util;
47 use WebDyne::PAGI::Constant;
48 use WebDyne::Request::PAGI;
49  
50  
51 # Environment
52 #
53 my %ENV_BASE=(
54 %{$WEBDYNE_PAGI_ENV_SET},
55 (map { $_=>$ENV{$_} } (
56 grep { defined($ENV{$_}) }
57 qw(DOCUMENT_DEFAULT DOCUMENT_ROOT),
58 @{$WEBDYNE_PAGI_ENV_KEEP},
59 grep {/WEBDYNE/i} keys %ENV
60 ))
61 );
62  
63  
64 # Version information
65 #
66 $VERSION='3.027';
67  
68  
69 #==================================================================================================
70  
71 sub new {
72  
73  
74 # Get options
75 #
76 my ($class, %opt)=@_;
77
78
79 # Test ?
80 #
81 if ($opt{'test'}) {
82 $opt{'root'}=$WEBDYNE_DEFAULT_TEST_FN;
83 }
84
85
86 # Indexing. 1 for enable with internal, string for some other indexing file
87 #
88 if ($opt{'index'} eq '1') {
89 $opt{'index'}=$WEBDYNE_DEFAULT_INDEX_FN;
90 }
91
92  
93 # Fix document root
94 #
95 $opt{'root'}=File::Spec->rel2abs($opt{'root'});
96  
97  
98 # Load local config if requested. Keep the default off so direct
99 # WebDyne::PAGI->new()->to_app callers preserve existing behaviour.
100 #
101 if ($opt{'conf'}) {
102 $class->local_constant_load($opt{'root'}, $opt{'conf'});
103 }
104  
105  
106 # API file name cache
107 #
108 $opt{'API_fn'}={};
109
110
111 # Done
112 #
113 return bless(\%opt, $class);
114
115 }
116  
117
118 sub to_app {
119  
120  
121 # Self ref
122 #
123 my $self=shift();
124  
125  
126 # Dispatch table
127 #
128 my %handler=(
129 http => sub { shift()->handler_http(@_) },
130 sse => sub { shift()->handler_sse(@_) },
131 ws => sub { shift()->handler_ws(@_) },
132 websocket => sub { shift()->handler_ws(@_) },
133 lifespan => sub { shift()->handler_lifespan(@_) }
134 );
135
136  
137 # Main application
138 #
139 my $app_cr = async sub {
140  
141 my ($scope, $receive, $send) = @_;
142 if (my $handler_cr=$handler{my $type=$scope->{type}}) {
143 # Supported type, dispatch
144 #
145 return await $handler_cr->($self, $scope, $receive, $send)->($scope, $receive, $send);
146 }
147 else {
148 # Unsupported type
149 #
150 die "Unsupported scope type: $type";
151 }
152  
153 };
154  
155  
156 # Wrap with configured PAGI middleware.
157 #
158 $app_cr=$self->build($app_cr);
159
160
161 # Done
162 #
163 return $app_cr;
164
165 }
166  
167  
168 sub build {
169  
170  
171 # Wrap a PAGI app code ref in configured middleware.
172 #
173 my ($self, $app_cr)=@_;
174  
175  
176 # Static service can be overridden per instance without changing the
177 # package default for later apps in the same interpreter.
178 #
179 my $static_fg=$WEBDYNE_PAGI_STATIC;
180 $static_fg=$self->{'static'} if exists($self->{'static'});
181  
182  
183 # Build list of active middleware.
184 #
185 my @middleware;
186 foreach my $middleware_ar (@{$WEBDYNE_PAGI_MIDDLEWARE}) {
187 my ($middleware, $middleware_opt_hr)=@{$middleware_ar};
188  
189 # Skip static if not wanted
190 #
191 if ($middleware eq 'Static') {
192 next unless $static_fg;
193 }
194  
195  
196 # And code refs are run and given self as first param
197 #
198 if (ref($middleware_opt_hr) eq 'CODE') {
199 $middleware_opt_hr=$middleware_opt_hr->($self);
200 }
201  
202  
203 # Save it for wrapping below
204 #
205 push @middleware, [$middleware, $middleware_opt_hr];
206 }
207  
208  
209 # No active middleware, preserve bare app behaviour and avoid requiring
210 # PAGI::Middleware::Builder for direct WebDyne::PAGI use.
211 #
212 return $app_cr unless @middleware;
213  
214  
215 # Build middleware stack
216 #
217 require PAGI::Middleware::Builder;
218 my $builder_or=PAGI::Middleware::Builder->new();
219 foreach my $middleware_ar (@middleware) {
220 my ($middleware, $middleware_opt_hr)=@{$middleware_ar};
221 $builder_or->add_middleware($middleware, %{$middleware_opt_hr});
222 }
223  
224  
225 # Done
226 #
227 return $builder_or->to_app($app_cr);
228  
229 }
230  
231  
232 sub local_constant_load {
233  
234  
235 # Read in local webdyne.conf.pl
236 #
237 my ($class, $root_dn, $conf)=@_;
238  
239  
240 # If root_dn is a file get dir name
241 #
242 if (-f $root_dn) {
243 $root_dn=(File::Spec->splitpath($root_dn))[1];
244 }
245  
246  
247 # Resolve conf option. 1 means root/.webdyne.conf.pl, otherwise use
248 # explicit path relative to root unless already absolute.
249 #
250 my $conf_fn;
251 if ($conf eq '1') {
252 $conf_fn=File::Spec->catfile($root_dn, sprintf('.%s', $WEBDYNE_CONF_FN));
253 }
254 else {
255 $conf_fn=File::Spec->file_name_is_absolute($conf) ?
256 $conf :
257 File::Spec->catfile($root_dn, $conf);
258 }
259  
260  
261 # Load via existing constant import path.
262 #
263 WebDyne::Constant->import($conf_fn);
264 return $conf_fn;
265  
266 }
267  
268  
269 sub handler_sse {
270  
271 my $self=shift();
272 return async sub {
273 my ($scope, $receive, $send)=@_;
274 my ($size, $disconnected)=(0, 0);
275 my $req_or=PAGI::Request->new($scope, async sub {
276 my $event_hr=await $receive->();
277 if ($event_hr->{'type'} eq 'sse.disconnect') {
278 $disconnected=1;
279 return {%{$event_hr}, type => 'http.disconnect'};
280 }
281 die "unexpected event while reading SSE form" unless $event_hr->{'type'} eq 'sse.request';
282 $size += length(defined($event_hr->{'body'}) ? $event_hr->{'body'} : '');
283 die "SSE form exceeds upload limit" if $size > $WEBDYNE_CGI_POST_MAX;
284 return $event_hr;
285 });
286  
287 # Only URL-encoded forms need staging here. EventSource GETs keep
288 # their existing path; multipart SSE submissions are not supported.
289 # Use PAGI's buffered helper so CGI can read synchronously below.
290 #
291 if ($req_or->content_type() eq 'application/x-www-form-urlencoded') {
292 my $length=$req_or->content_length();
293 $size=$length if defined($length) && $length =~ /\A[0-9]+\z/;
294 unless ($size > $WEBDYNE_CGI_POST_MAX) {
295 $size=0;
296 my $read=eval { await $req_or->body(); 1 };
297 die $@ unless $read || $size > $WEBDYNE_CGI_POST_MAX;
298 }
299 if ($size > $WEBDYNE_CGI_POST_MAX) {
300 await $send->({type => 'sse.http.response.start', status => HTTP_REQUEST_ENTITY_TOO_LARGE,
301 headers => [['content-type', 'text/plain']]});
302 await $send->({type => 'sse.http.response.body', body => "Request body exceeds upload limit\n", more => 0});
303 return;
304 }
305 return if $disconnected;
306 }
307  
308 my ($sse_cr, $status);
309 {
310 # Confine localized process state to synchronous page setup.
311 # POST fields are buffered before CGI builds the parameter hash.
312 #
313 local *ENV=\%ENV_BASE;
314 my $res_or=PAGI::Response->new($scope);
315 require PAGI::SSE;
316 my $sse_or=PAGI::SSE->new($scope, $receive, $send);
317 my $r=WebDyne::Request::PAGI->new(
318 document_root => $self->{'root'}, document_default => $self->{'index'},
319 scope => $scope, req => $req_or, res => $res_or, sse => $sse_or,
320 receive => $receive, send => $send,
321 ) || return err('unable to create SSE request');
322 $status=WebDyne->handler($r);
323 $sse_cr=$r->custom_response($status) if $status eq HTTP_CONTINUE;
324 }
325  
326 # Decline before starting a stream. Preserve HTTP error statuses;
327 # other results without an SSE callback indicate a setup failure.
328 # Send outside the localized environment and await both events.
329 #
330 unless (ref($sse_cr) eq 'CODE') {
331 $status=HTTP_INTERNAL_SERVER_ERROR
332 unless defined($status) && $status =~ /\A[45][0-9]{2}\z/;
333 my $message=HTTP::Status::status_message($status) || 'Request failed';
334 await $send->({
335 type => 'sse.http.response.start', status => $status,
336 headers => [['content-type', 'text/plain']],
337 });
338 await $send->({
339 type => 'sse.http.response.body',
340 body => "$status $message\n", more => 0,
341 });
342 return;
343 }
344 await $sse_cr->($scope, $receive, $send);
345 };
346  
347 }
348  
349  
350 sub handler_sse_error {
351  
352 return async sub {
353
354  
355 # Get request
356 #
357 my ($scope, $receive, $send)=@_;
358 0 && debug('in handler_sse_error, scope:%s receive:%s, send:%s', Dumper($scope, $receive, $send));
359  
360  
361 # Create helper objects
362 #
363 require PAGI::SSE;
364 my $sse_or=PAGI::SSE->new($scope, $receive, $send) ||
365 return err('unable to get PAGI::SSE object');
366 0 && debug("sse_or: $sse_or");
367
368
369 # Send error
370 #
371 await $sse_or->send('SSE error - see logs');
372
373 }
374
375 }
376  
377  
378 sub handler_ws {
379  
380  
381 # Get request
382 #
383 my ($self, $scope, $receive, $send)=@_;
384 0 && debug('in handler_ws, scope:%s receive:%s, send:%s', Dumper($scope, $receive, $send));
385  
386  
387 # Setup %ENV
388 #
389 local *ENV=\%ENV_BASE;
390  
391  
392 # Create helper objects
393 #
394 my $req_or=PAGI::Request->new($scope, $receive) ||
395 return err('unable to get PAGI::Request object');
396 my $res_or=PAGI::Response->new($scope) ||
397 return err('unable to get PAGI::Response object');
398 require PAGI::WebSocket;
399 my $ws_or=PAGI::WebSocket->new($scope, $receive, $send) ||
400 return err('unable to get PAGI::WebSocket object');
401 0 && debug("req_or: $req_or, res_or: $res_or, ws_or: $ws_or");
402  
403  
404 # Get main WebDyne handler request object
405 #
406 my $r=WebDyne::Request::PAGI->new( document_root => $self->{'root'}, document_default => $self->{'index'}, scope=>$scope, req=>$req_or, res=>$res_or, ws=>$ws_or,
407 receive => $receive, send=> $send) ||
408 return err('unable to create new WebDyne::Request::PAGI object: %s',
409 $@ || errclr() || 'unknown error');
410 0 && debug("r: $r");
411
412
413 # Call handler. No point error checking but log errors
414 #
415 0 && debug('calling WebDyne handler');
416 my $status=WebDyne->handler($r);
417 0 && debug("status: $status");
418 if ($status eq HTTP_CONTINUE) {
419 my $ws_cr=$r->custom_response($status);
420 return $ws_cr if ref($ws_cr) eq 'CODE';
421 }
422  
423 # Reject before accepting the socket. The server converts this into
424 # an HTTP 403 handshake response; no optional extension is required.
425 #
426 return async sub {
427 my ($scope, $receive, $send)=@_;
428 await $send->({type => 'websocket.close'});
429 };
430  
431 }
432  
433 sub handler_http {
434  
435
436 # Self ref contains things like document_root, dcoument_default
437 #
438 my $self=shift();
439  
440  
441 # Return async sub for handling WebDyne requests
442 #
443 return set_subname('handler_http_anon', async sub {
444  
445  
446 # Get request
447 #
448 my ($scope, $receive, $send)=@_;
449 0 && debug('in handler, scope:%s receive:%s, send:%s', Dumper($scope, $receive, $send));
450
451  
452 # Restrict local env and expose the PAGI request path to WebDyne's
453 # shared Router::Simple based API implementation.
454 #
455 my ($r, $html, $html_fh, $status, $req_or, $res_or);
456  
457 # WebDyne page code reads synchronously. Buffer HTTP bodies before
458 # dispatch, bounded by the existing 512 KiB default upload limit.
459 # Count actual bytes even when Content-Length is absent or inaccurate.
460 # Wrapping receive preserves PAGI's buffered body/json helpers; using
461 # body_stream for every request would disable those helpers.
462 #
463 my ($body_bytes, $body_oversize, $body_disconnected)=(0, 0, 0);
464 my $bounded_receive_cr=async sub {
465 my $event_hr=await $receive->();
466 if ($event_hr->{'type'} eq 'http.disconnect') {
467 $body_disconnected=1;
468 return $event_hr;
469 }
470 die "unexpected event while reading HTTP body" unless $event_hr->{'type'} eq 'http.request';
471 $body_bytes += length(defined($event_hr->{'body'}) ? $event_hr->{'body'} : '');
472 if ($body_bytes > $WEBDYNE_CGI_POST_MAX) {
473 $body_oversize=1;
474 die "request body exceeds upload limit";
475 }
476 return $event_hr;
477 };
478 $req_or=PAGI::Request->new($scope, $bounded_receive_cr) ||
479 return err('unable to get PAGI::Request object');
480 $res_or=PAGI::Response->new($scope) ||
481 return err('unable to get PAGI::Response object');
482  
483 # Reject a declared oversize body before consuming any input. The
484 # receive counter remains authoritative for bodies we do accept.
485 #
486 my $content_length=$req_or->content_length();
487 $body_oversize=1 if defined($content_length) && $content_length =~ /\A[0-9]+\z/
488 && $content_length > $WEBDYNE_CGI_POST_MAX;
489 unless ($body_oversize) {
490 my $staged=eval {
491 if (
492 ($req_or->content_type() || '') =~ m{\Aapplication/x-www-form-urlencoded\b}i
493 # PAGI form data need not declare Content-Length.
494 # && $req_or->content_length()
495 ) {
496 await $req_or->body();
497 }
498 elsif (
499 ($req_or->content_type() || '') =~ m{\Amultipart/form-data(?:\s*;|\z)}i
500 # Stage chunked uploads too, with the independent limit.
501 # && $req_or->content_length()
502 ) {
503 my $multipart_body='';
504 my $stream_or=$req_or->body_stream(max_bytes => $WEBDYNE_CGI_POST_MAX);
505 await $stream_or->stream_to(sub { $multipart_body .= shift() });
506 $scope->{'webdyne.pagi.multipart_body'}=\$multipart_body;
507 }
508 else {
509 await $req_or->body();
510 }
511 1;
512 };
513 die $@ unless $staged || $body_oversize;
514 }
515 if ($body_oversize) {
516 return await $res_or
517 ->status(HTTP_REQUEST_ENTITY_TOO_LARGE)
518 ->send("Request body exceeds upload limit\n")
519 ->respond($send);
520 }
521  
522 # PAGI's helpers may return partial bytes on disconnect. Never run
523 # page code with an incomplete upload or synthesize a response for it.
524 #
525 return if $body_disconnected;
526  
527 {
528 # Keep the request environment localized only while WebDyne is
529 # constructing and executing the request. Do not retain a
530 # localized global %ENV across an asynchronous response await.
531 #
532 local *ENV=\%ENV_BASE;
533 @ENV{qw(PATH_INFO QUERY_STRING REQUEST_METHOD SCRIPT_NAME)}=(
534 WebDyne::Request::PAGI::scope_path($scope),
535 $scope->{'query_string'} || '',
536 $scope->{'method'} || '',
537 $scope->{'root_path'} || '',
538 );
539  
540 # If the requested path is not a file, an API PSP may own a path
541 # prefix such as /api.psp or /example/api.psp. Resolve that prefix
542 # before constructing the request so the normal WebDyne handler can
543 # process the PSP. The discovered API file path becomes the mount
544 # point, so /example/api.psp owning /example/api/foo passes /foo to
545 # Router::Simple.
546 #
547 my $api_fn=api_filename($self, $scope);
548 if ($api_fn) {
549 my $api_path=File::Spec->abs2rel($api_fn, File::Spec->rel2abs($self->{'root'}));
550 $api_path=~s{\Q@{[WEBDYNE_PSP_EXT]}\E$}{};
551 $ENV{'PATH_INFO'}=~s{^/\Q$api_path\E(?=/|$)}{}i;
552 }
553  
554 # Create new WebDyne Request object, optionally using the API
555 # prefix resolved above as its PSP filename.
556 #
557 $html_fh=IO::String->new($html);
558 my %request_opt=(select => $html_fh, document_root => $self->{'root'}, document_default => $self->{'index'}, scope=>$scope, req=>$req_or, res=>$res_or,
559 receive => $receive, send=> $send, no_head_insert=>$self->{'no_head_insert'}, filename=>$self->{'filename'});
560 $request_opt{'filename'} ||= do {$api_fn if $api_fn};
561 $r=WebDyne::Request::PAGI->new(%request_opt) ||
562 return err('unable to create new WebDyne::Request::PAGI object: %s',
563 $@ || errclr() || 'unknown error');
564 0 && debug("r: $r");
565  
566 # Call handler and evaluate results
567 #
568 $status=WebDyne->handler($r);
569 0 && debug("handler returned status: $status");
570 $r->status($status);
571 }
572  
573  
574 # Can close html file handle now
575 #
576 $html_fh->close();
577 0 && debug("html returned:\n$html");
578  
579  
580 # Present error if non 200 (success) status returned. Yes - there are other status codes but this is most
581 # common and quickest test, other 200 codes will fall through the if/else statements and still work
582 #
583 unless ($status == HTTP_OK) {
584
585
586 # OK. Most common match didn't happen. Is it an error ?
587 #
588 0 && debug('status: %s is not HTTP_OK, branching', $status);
589 if (!defined($status) || ($status < 0) || is_error($status) || !$html) {
590
591
592 # Something went wrong. Let's start working through it
593 #
594 if (($status eq HTTP_NOT_FOUND) && !(-f (my $fn=$r->filename()))) {
595
596
597 # If get here nothing found, send 404 error
598 #
599 0 && debug("status: $status, fn:$fn, setting HTTP_NOT_FOUND");
600 $r->status(HTTP_NOT_FOUND);
601 my $error=errdump() || "File not found, status ($status)"; errclr();
602 $html=$r->err_html($status, $error)
603 }
604 elsif (is_error($status) ) {
605
606 # Some other error besides 404
607 #
608 0 && debug("returning custom error: $status");
609 $r->status($status);
610 $html=$r->custom_response($status) || errstr() || do {
611 $r->content_type($WEBDYNE_CONTENT_TYPE_TEXT);
612 "Error: $status with no content - try server error logs ?";
613 };
614  
615 }
616 else {
617
618 # Weird non HTTP status code, something has gone wrong along way
619 #
620 0 && debug('undefined status returned, looking for error handler');
621 my $error=errdump() || $@; errclr();
622 $error ||= "Unexpected return status ($status) from handler";
623 0 && debug("request handler status:$status, detected error: $error, calling err_html");
624 $r->status(HTTP_INTERNAL_SERVER_ERROR);
625 $html=$r->err_html($status, $error)
626  
627 }
628
629 }
630 else {
631
632 # Not an error, but not HTTP_OK
633 #
634 0 && debug("status: $status is not an error, proceeding");
635
636 }
637  
638 }
639 my $final_status=$r->status() || $status || HTTP_OK;
640 0 && debug("final handler status: %s, content_type: %s, html:%s", $final_status, $r->content_type(), $html);
641
642
643 # Send headers unless already sent
644 #
645 $r->res->status($final_status);
646 my $headers_ar=$r->headers_out->psgi_flatten_without_sort();
647 0 && debug('sending headers: %s', Dumper($headers_ar));
648 my $cookie_seen;
649 for (my $i=0; $i<@{$headers_ar}; $i+=2) {
650 my ($header, $value)=@{$headers_ar}[$i, $i+1];
651 if (lc($header) eq 'set-cookie') {
652 $r->res->remove_header('set-cookie') unless $cookie_seen++;
653 $r->res->header('set-cookie' => $value);
654 }
655 else {
656 $r->res->header_try($header => $value);
657 }
658 }
659
660
661 # If html is defined set header content type unless already set during
662 # handler execution, then always send the response. An API page with
663 # no matching route legitimately returns an empty 200 response; PAGI
664 # still requires response.start to be emitted in that case.
665 #
666 my $body=$html || '';
667 if ($body) {
668 0 && debug('sending html to client via await()');
669 $r->res->content_type($r->content_type() || $WEBDYNE_CONTENT_TYPE_HTML);
670 }
671  
672 # PAGI requires lowercase names in response events. Normalize only
673 # the outgoing header pairs, preserving values, order and duplicates.
674 #
675 my $respond_or=$r->res->send($body)->respond(sub {
676 my $event_hr=shift();
677 if ($event_hr->{'type'} eq 'http.response.start') {
678 $event_hr={
679 %{$event_hr},
680 headers => [
681 map { [lc($_->[0]), $_->[1]] }
682 @{$event_hr->{'headers'} || []}
683 ],
684 };
685 }
686 return $send->($event_hr);
687 });
688  
689 # Retain the response Future across await. Awaiting the temporary can
690 # crash Devel::Confess stack tracing on send failure with Perl 5.38.
691 #
692 my $respond_status=await $respond_or;
693 $r->DESTROY();
694 return $respond_status;
695  
696  
697
698 })
699
700 }
701  
702  
703 sub api_filename {
704  
705 my ($self, $scope)=@_;
706 return unless WEBDYNE_API_ENABLE;
707  
708 my $path=WebDyne::Request::PAGI::scope_path($scope);
709 return unless length($path);
710  
711 my @part=grep { length($_) } split(m{/+}, $path);
712 return if grep { $_ eq '.' || $_ eq '..' } @part;
713  
714 my $root=File::Spec->rel2abs($self->{'root'});
715 my $API_fn=$self->{'API_fn'};
716 for my $ix (0 .. $#part) {
717 my $candidate=File::Spec->catfile($root, @part[0 .. $ix]);
718 $candidate .= WEBDYNE_PSP_EXT unless $candidate =~ WEBDYNE_PSP_EXT_RE;
719  
720 my $relative=File::Spec->abs2rel($candidate, $root);
721 next if $relative eq '..' || $relative =~ /^\.\.(?:[\\\/]|$)/;
722 if ($API_fn->{$candidate} || (-f $candidate)) {
723 0 && debug("found api file name: $candidate, %s, dispatching", Dumper($API_fn));
724 $API_fn->{$candidate}++; # Cache so not stat()ing on file system
725 return $candidate;
726 }
727 }
728  
729 return;
730 }
731  
732  
733 sub handler_lifespan {
734  
735 my $self=shift();
736
737 return set_subname('handler_lifespan_anon', async sub {
738  
739 my ($scope, $receive, $send) = @_;
740 while (1) {
741 my $event_hr = await $receive->();
742 if ($event_hr->{'type'} eq 'lifespan.startup') {
743 printf STDERR "[lifespan] WebDyne PAGI handler startup. DOCUMENT_ROOT: %s, DOCUMENT_DEFAULT: %s\n", $self->{'root'}, basename($self->{'index'} || $DOCUMENT_DEFAULT);
744 await $send->({ type => 'lifespan.startup.complete' });
745
746 }
747 elsif ($event_hr->{'type'} eq 'lifespan.shutdown') {
748 print STDERR "[lifespan] WebDyne PAGI handler shutdown.\n";
749 await $send->({ type => 'lifespan.shutdown.complete' });
750 last;
751 }
752 }
753 })
754 }
755  
756  
757 sub normalize_dn {
758  
759 # Normal dir, normally document_root
760 #
761 my $rel_dn=shift();
762 my $abs_dn=File::Spec->rel2abs($rel_dn);
763 $abs_dn =~ s{/$}{} unless $abs_dn eq '/';
764 return $abs_dn;
765
766 }
767  
768 1;
769 __END__
« blib/lib/WebDyne/Err/Constant.pm ↑ Summary blib/lib/WebDyne/PSGI.pm »