| 1 | package PAGI::FastAPI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 21 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use experimental qw/class try for_list/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 21 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 21 | our $VERSION = qv('v1.7.3'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 21 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 21 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 21 | ▶ | use JSON::PP qw(encode_json decode_json); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 21 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 21 | ▶ | use PAGI::App::URLMap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 21 | ▶ | use PAGI::Context; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 21 | ▶ | use PAGI::WebSocket; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 21 | ▶ | use PAGI::Middleware::CORS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 21 | ▶ | use PAGI::FastAPI::Context; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 21 | ▶ | use PAGI::FastAPI::Depends qw(Depends); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 21 | ▶ | use PAGI::FastAPI::Middleware::RateLimit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 21 | class PAGI::FastAPI { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 21 | field $title :param = 'PAGI::FastAPI Application'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 21 | field $version :param = "$VERSION"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 21 | field $secret :param = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 21 | field $routes = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 21 | field $middlewares = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 21 | field $pagi_middlewares = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 21 | field $mounts = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 21 | field $cors_options = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 21 | field $event_handlers = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 21 | field $openapi = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 21 | field $background_futures = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 21 | field $swagger_ui_css_url :param = 'https://unpkg.com/swagger-ui-dist@5/swagger-ui.css'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 21 | field $swagger_ui_bundle_url :param = 'https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 21 | field $swagger_ui_parameters :param = { operationsSorter => 'method', tagsSorter => 'alpha', }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 36 | ▶ | ADJUST { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 36 | $event_handlers = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | startup => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | shutdown => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 36 | $openapi = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | openapi => '3.1.0', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | info => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | title => $title, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | version => $version, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | paths => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | method mount ($path, $app) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mount: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $path = "/$path" unless $path =~ m{^/}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $path =~ s{/$}{} unless $path eq '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | push @$mounts, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | prefix => $path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | app => $app, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 35 | ▶ | method get ($path, %opts) { $self->_register_route('GET', $path, \%opts) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 11 | ▶ | method post ($path, %opts) { $self->_register_route('POST', $path, \%opts) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | method put ($path, %opts) { $self->_register_route('PUT', $path, \%opts) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | method patch ($path, %opts) { $self->_register_route('PATCH', $path, \%opts) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
patch: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | method delete ($path, %opts) { $self->_register_route('DELETE', $path, \%opts) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 6 | ▶ | method on_event ($event_type, $code) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
on_event: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | die "Event type must be 'startup' or 'shutdown'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | unless $event_type =~ /^(?:startup|shutdown)$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 6 | ▶ | die "Event handler must be a CODE reference" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | unless ref $code eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 6 | push @{$event_handlers->{$event_type}}, $code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | ▶ | method on_startup ($code) { $self->on_event('startup', $code) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
on_startup: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | method on_shutdown ($code) { $self->on_event('shutdown', $code) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
on_shutdown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 12 | ▶ | method add_middleware ($mw, %opts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_middleware: called CC=7 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 12 | ▶ | if (!ref $mw) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | eval "require $mw;" or die $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | $mw = $mw->new(%opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 12 | ▶ | if (blessed($mw) && ($mw->can('wrap') || $mw->can('to_app') || $mw->can('call'))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition44%4/9
builtin::blessed($mw) and $mw->can("wrap") || $mw->can("to_app") || $mw->can("call")
$mw->can("wrap") or $mw->can("to_app") or $mw->can("call")
$mw->can("wrap") or $mw->can("to_app")
MC/DC25%1/4
builtin::blessed($mw) and $mw->can("wrap") || $mw->can("to_app") || $mw->can("call")
builtin::blessed($mw)
$mw->can("wrap")
$mw->can("to_app")
$mw->can("call")
Truth table40%2/5
A and B or C or D
A= builtin::blessed($mw)B= $mw->can("wrap")C= $mw->can("to_app")D= $mw->can("call")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 7 | push @$pagi_middlewares, $mw; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 5 | push @$middlewares, $mw; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 12 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | method enable_csrf (%opts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
enable_csrf: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | require PAGI::Middleware::CSRF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | ▶ | my $csrf_secret = delete $opts{secret} // $secret | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%5/5
(delete $opts{"secret"} // $secret) // die("CSRF middleware requires 'secret' option")
delete $opts{"secret"} // $secret
MC/DC100%2/2
(delete $opts{"secret"} // $secret) // die("CSRF middleware requires 'secret' option")
delete $opts{"secret"}
$secret
Truth table100%3/3
A // B
A= delete $opts{"secret"}B= $secret
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | // die "CSRF middleware requires 'secret' option"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | my $mw = PAGI::Middleware::CSRF->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | secret => $csrf_secret, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | enforce => 'header', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | secure => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | %opts, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | return $self->add_middleware($mw); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | method add_cors (%opts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_cors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | $cors_options = \%opts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | ▶ | method websocket ($path, %args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
websocket: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | my $handler = delete $args{handler}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | ▶ | my $deps = delete $args{dependencies} // []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"dependencies"} // []
MC/DC0%0/1
delete $args{"dependencies"} // []
delete $args{"dependencies"}
Truth table50%1/2
A
A= delete $args{"dependencies"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | ▶ | die "Route 'WEBSOCKET $path' requires a 'handler' async coderef" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | unless ref $handler eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | my %opts = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | dependencies => $deps, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | handler => $handler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | %args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | $self->_register_route('WEBSOCKET', $path, \%opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | method add_rate_limit (%opts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_rate_limit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | my $limiter = PAGI::FastAPI::Middleware::RateLimit->new(%opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 5 | ▶ | $self->add_middleware(async sub ($c, $next) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 5 | return await $limiter->handle($c, $next); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | method add_bot_protection (%opts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_bot_protection: not called CC=1 SCAR=2.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 21 | ▶ | use PAGI::FastAPI::Middleware::BotProtection; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | my $guard = PAGI::FastAPI::Middleware::BotProtection->new(%opts); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | $self->add_middleware(async sub ($c, $next) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | return await $guard->handle($c, $next); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | method sse ($generator, %opts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sse: not called CC=3 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 21 | ▶ | use PAGI::FastAPI::Response::SSE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | return PAGI::FastAPI::Response::SSE->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | generator => $generator, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | headers => $opts{headers} // [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | status => $opts{status} // 200, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
$opts{"headers"} // []
$opts{"status"} // 200
MC/DC0%0/1
$opts{"headers"} // []
$opts{"headers"}
MC/DC0%0/1
$opts{"status"} // 200
$opts{"status"}
Truth table0%0/2
A
A= $opts{"headers"}
Truth table0%0/2
A
A= $opts{"status"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 5 | ▶ | method to_pagi () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_pagi: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # Delegate directly to to_app to ensure identical pipeline execution | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # and eliminate double-wrapping. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 5 | return $self->to_app(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 39 | ▶ | method to_app { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=10 SCAR=33.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 39 | my $fastapi_app = $self->_build_pagi_app; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 39 | my $final_app = $fastapi_app; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 39 | ▶ | if (@$mounts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my $urlmap = PAGI::App::URLMap->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | for my $m (@$mounts) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $urlmap->mount($m->{prefix} => $m->{app}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | $urlmap->mount('/' => $fastapi_app); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | $final_app = $urlmap->to_app; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 39 | ▶ | if ($cors_options) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 2 | $final_app = PAGI::Middleware::CORS->new(%$cors_options) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | ->wrap($final_app); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # Wrap PAGI middleware objects in reverse (LIFO order) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # Outermost layer wrapping around mounts and CORS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 39 | for my $mw (reverse @$pagi_middlewares) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 10 | ▶ | if (blessed($mw) && $mw->can('wrap')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
builtin::blessed($mw) and $mw->can("wrap")
MC/DC0%0/2
builtin::blessed($mw) and $mw->can("wrap")
builtin::blessed($mw)
$mw->can("wrap")
Truth table33%1/3
A and B
A= builtin::blessed($mw)B= $mw->can("wrap")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 10 | $final_app = $mw->wrap($final_app); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | ▶ | elsif (blessed($mw) && $mw->can('to_app')) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
builtin::blessed($mw) and $mw->can("to_app")
MC/DC0%0/2
builtin::blessed($mw) and $mw->can("to_app")
builtin::blessed($mw)
$mw->can("to_app")
Truth table0%0/3
A and B
A= builtin::blessed($mw)B= $mw->can("to_app")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | $final_app = $mw->to_app($final_app); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | elsif (ref $mw eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | $final_app = $mw->($final_app); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 39 | return $final_app; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 39 | ▶ | method _build_pagi_app { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_pagi_app: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 80 | ▶ | return async sub ($scope, $receive, $send) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=48 SCAR=41.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 80 | ▶ | if ($scope->{type} eq 'websocket') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 3 | return await $self->_handle_websocket($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | } elsif ($scope->{type} eq 'lifespan') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 5 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 9 | my $event = await $receive->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 9 | ▶ | if ($event->{type} eq 'lifespan.startup') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 5 | try { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 5 | for my $cb (@{$event_handlers->{startup}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 5 | await $cb->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 4 | await $send->({ type => 'lifespan.startup.complete' }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | catch ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 1 | await $send->({ type => 'lifespan.startup.failed', message => "$err" }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | elsif ($event->{type} eq 'lifespan.shutdown') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 4 | try { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 4 | ▶ | if (@$background_futures) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my @pending = @$background_futures; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | await Future->wait_all(@pending); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 4 | for my $cb (@{$event_handlers->{shutdown}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 3 | await $cb->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 4 | await $send->({ type => 'lifespan.shutdown.complete' }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | catch ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | await $send->({ type => 'lifespan.shutdown.failed', message => "$err" }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 72 | ▶ | die "Unsupported type: $scope->{type}" unless $scope->{type} =~ /^(?:http|sse|websocket)$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 72 | ▶ | my $path = $scope->{path} // '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"path"} // "/"
MC/DC0%0/1
$scope->{"path"} // "/"
$scope->{"path"}
Truth table50%1/2
A
A= $scope->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 72 | ▶ | my $method = uc($scope->{method} // 'GET'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"method"} // "GET"
MC/DC0%0/1
$scope->{"method"} // "GET"
$scope->{"method"}
Truth table50%1/2
A
A= $scope->{"method"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 72 | ▶ | if ($path eq '/docs') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 1 | await $send->({ type => 'http.response.start', status => 200, headers => [['content-type', 'text/html; charset=utf-8']] }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 1 | await $send->({ type => 'http.response.body', body => $self->_swagger_ui_html }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 71 | ▶ | if ($path eq '/openapi.json') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 6 | await $send->({ type => 'http.response.start', status => 200, headers => [['content-type', 'application/json']] }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 6 | await $send->({ type => 'http.response.body', body => encode_json($openapi) }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 6 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 65 | my %query_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 65 | ▶ | if (defined $scope->{query_string} && length $scope->{query_string}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $scope->{"query_string"} and length $scope->{"query_string"}
MC/DC100%2/2
defined $scope->{"query_string"} and length $scope->{"query_string"}
defined $scope->{"query_string"}
length $scope->{"query_string"}
Truth table100%3/3
A and B
A= defined $scope->{"query_string"}B= length $scope->{"query_string"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 3 | for my $pair (split '&', $scope->{query_string}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 3 | my ($k, $v) = split '=', $pair, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 3 | ▶ | $query_params{_uri_unescape($k)} = _uri_unescape($v // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$v // ""
MC/DC0%0/1
$v // ""
$v
Truth table50%1/2
A
A= $v
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 65 | my $body_data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | my $form_data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 65 | my $uploaded_files = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 65 | ▶ | if ($method eq 'POST' || $method eq 'PUT' || $method eq 'PATCH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$method eq "POST" or $method eq "PUT" or $method eq "PATCH"
$method eq "POST" or $method eq "PUT"
MC/DC33%1/3
$method eq "POST" or $method eq "PUT" or $method eq "PATCH"
$method eq "POST"
$method eq "PUT"
$method eq "PATCH"
Truth table50%2/4
A or B or C
A= $method eq "POST"B= $method eq "PUT"C= $method eq "PATCH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 20 | my $raw_body = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 20 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 20 | my $event = await $receive->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 20 | ▶ | if ($event->{type} eq 'http.request') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 19 | ▶ | $raw_body .= $event->{body} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$event->{"body"} // ""
MC/DC100%1/1
$event->{"body"} // ""
$event->{"body"}
Truth table100%2/2
A
A= $event->{"body"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 19 | ▶ | last unless $event->{more_body}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 20 | ▶ | if (length $raw_body) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 15 | my $content_type = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 15 | ▶ | for my $h (@{ $scope->{headers} // [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$scope->{"headers"} // []
MC/DC100%1/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table100%2/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 10 | ▶ | if (lc($h->[0]) eq 'content-type') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 10 | ▶ | $content_type = $h->[1] // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$h->[1] // ""
MC/DC0%0/1
$h->[1] // ""
$h->[1]
Truth table50%1/2
A
A= $h->[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 10 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 15 | my $content_type_lc = lc($content_type); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 15 | ▶ | if (index($content_type_lc, 'application/x-www-form-urlencoded') == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 3 | my %form; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 3 | for my $pair (split '&', $raw_body) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 5 | my ($k, $v) = split '=', $pair, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 5 | ▶ | next unless defined $k && length $k; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $k and length $k
MC/DC0%0/2
defined $k and length $k
defined $k
length $k
Truth table33%1/3
A and B
A= defined $kB= length $k
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 5 | ▶ | $form{_uri_unescape($k)} = _uri_unescape($v // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$v // ""
MC/DC0%0/1
$v // ""
$v
Truth table50%1/2
A
A= $v
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 3 | $body_data = \%form; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 3 | $form_data = \%form; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | elsif (index($content_type_lc, 'multipart/form-data') == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 4 | my ($b1, $b2) = $content_type =~ /boundary=(?:"([^"]+)"|([^;\s]+))/i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 4 | ▶ | my $boundary = $b1 // $b2 // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition60%3/5
($b1 // $b2) // ""
$b1 // $b2
MC/DC0%0/2
($b1 // $b2) // ""
$b1
$b2
Truth table66%2/3
A // B
A= $b1B= $b2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 4 | my ($fields, $files) = _parse_multipart_form_data($raw_body, $boundary); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 4 | $body_data = $fields; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 4 | $form_data = $fields; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 4 | $uploaded_files = $files; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 8 | try { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 8 | $body_data = decode_json($raw_body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | catch ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 2 | await $send->({ type => 'http.response.start', status => 422, headers => [['content-type', 'application/json']] }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 2 | await $send->({ type => 'http.response.body', body => encode_json({ detail => 'Invalid JSON body payload' }) }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 63 | my $pagi_context = PAGI::Context->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | my $ctx = PAGI::FastAPI::Context->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | query_params => \%query_params, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | body => $body_data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | form_data => $form_data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | uploaded_files => $uploaded_files, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | scope => $scope, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | pagi_context => $pagi_context, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 3 | ▶ | background_registrar => sub ($future) { $self->_retain_background($future) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 63 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 61 | ▶ | my $dispatcher = async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=25 SCAR=33.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 61 | for my $route (@$routes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 83 | ▶ | next unless $route->{method} eq $method; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 80 | ▶ | if (my @captures = ($path =~ $route->{regex})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 59 | my %path_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | my %query_params_validated; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 59 | for my $i (0 .. $#{$route->{path_params}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 6 | $path_params{$route->{path_params}[$i]} = _uri_unescape($captures[$i]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | # Store extracted path parameters via Context's constructor state | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 59 | my $p_params = $c->path_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 59 | %$p_params = %path_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 59 | for my ($param, $type) (%{$route->{query_types}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 3 | my $val = $query_params{$param}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 3 | ▶ | if (my $err = $type->validate($val)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 1 | $c->status(422); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 1 | return { detail => "Query param '$param' invalid: $err" }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 2 | ▶ | my $t_name = eval { $type->name } // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
$type->name
}
} // ""
MC/DC0%0/1
eval {
do {
$type->name
}
} // ""
eval {
do {
$type->name
}
}
Truth table50%1/2
A
A= eval {
do {
$type->name
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 2 | ▶ | $query_params_validated{$param} = $type->has_coercion ? $type->coerce($val) : (($t_name eq 'Int') ? $val + 0 : $val); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 58 | my $q_params = $c->query_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 58 | %$q_params = %query_params_validated; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 58 | ▶ | if (defined $route->{body_spec}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 8 | my $spec = $route->{body_spec}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 8 | ▶ | if (blessed($spec) && $spec->can('validate')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($spec) and $spec->can("validate")
MC/DC50%1/2
builtin::blessed($spec) and $spec->can("validate")
builtin::blessed($spec)
$spec->can("validate")
Truth table66%2/3
A and B
A= builtin::blessed($spec)B= $spec->can("validate")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 1 | ▶ | if (my $err = $spec->validate($c->body)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | $c->status(422); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | return { detail => "Body validation failed: $err" }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | elsif (ref $spec eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 7 | ▶ | unless (ref $c->body eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | $c->status(422); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | return { detail => 'Expected a JSON object in request body' }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 7 | for my ($field, $type) (%$spec) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 13 | my $val = $c->body($field); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 13 | ▶ | if (my $err = $type->validate($val)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 2 | $c->status(422); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 2 | return { detail => "Body field '$field' invalid: $err" }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 56 | for my $dep (@{$route->{dependencies}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 9 | my $dep_res = await $dep->{code}->($c); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 9 | ▶ | if ($c->status >= 400) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 2 | ▶ | return $dep_res // { detail => 'Dependency execution failed' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$dep_res // {"detail", "Dependency execution failed"}
MC/DC0%0/1
$dep_res // {"detail", "Dependency execution failed"}
$dep_res
Truth table50%1/2
A
A= $dep_res
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 7 | ▶ | if (defined $dep->{key} && defined $dep_res) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $dep->{"key"} and defined $dep_res
MC/DC100%2/2
defined $dep->{"key"} and defined $dep_res
defined $dep->{"key"}
defined $dep_res
Truth table100%3/3
A and B
A= defined $dep->{"key"}B= defined $dep_res
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 5 | $c->stash->{$dep->{key}} = $dep_res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # Handlers are documented as 'async sub ($c)', which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | # always returns a Future when called, so awaiting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | # unconditionally is correct for the common case. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | # However, several first-class Response objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | # (e.g. PAGI::FastAPI::Response::SSE, per its own | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | # SYNOPSIS) are documented as being returned from a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | # *plain*, non-async 'sub ($c) {...}' handler. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | # A plain sub's return value is not a Future, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | # 'await'ing a non-Future object requires it to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | # implement the Future::AsyncAwait awaitable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | # protocol, which Response objects don't, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | # awaiting unconditionally crashes those documented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | # examples with "Can't locate object method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | # AWAIT_IS_READY". Only await when the handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | # actually returned a Future. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 54 | my $handler_result = $route->{handler}->($c); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 54 | ▶ | return (blessed($handler_result) && $handler_result->isa('Future')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($handler_result) and $handler_result->isa("Future")
MC/DC0%0/2
builtin::blessed($handler_result) and $handler_result->isa("Future")
builtin::blessed($handler_result)
$handler_result->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($handler_result)B= $handler_result->isa("Future")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | ? await $handler_result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | : $handler_result; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 2 | $c->status(404); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 2 | return { detail => 'Not Found' }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 63 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 63 | my $pipeline = $dispatcher; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 63 | for my $mw (reverse @$middlewares) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 23 | my $next_stage = $pipeline; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 23 | ▶ | $pipeline = async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 23 | return await $mw->($c, $next_stage); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 23 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 63 | my $res = await $pipeline->($ctx); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | # 1. Check if response is an SSE / Streaming object that handles its own send lifecycle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 63 | ▶ | if (Scalar::Util::blessed($res) && $res->can('dispatch')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($res) and $res->can("dispatch")
MC/DC0%0/2
builtin::blessed($res) and $res->can("dispatch")
builtin::blessed($res)
$res->can("dispatch")
Truth table66%2/3
A and B
A= builtin::blessed($res)B= $res->can("dispatch")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | return await $res->dispatch($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | # 2. Response objects (HTML, Custom Response, etc.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 63 | ▶ | if (Scalar::Util::blessed($res) && $res->isa('PAGI::FastAPI::Response')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($res) and $res->isa("PAGI::FastAPI::Response")
MC/DC50%1/2
builtin::blessed($res) and $res->isa("PAGI::FastAPI::Response")
builtin::blessed($res)
$res->isa("PAGI::FastAPI::Response")
Truth table66%2/3
A and B
A= builtin::blessed($res)B= $res->isa("PAGI::FastAPI::Response")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 4 | $res->prepare_headers($ctx); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 4 | await $send->({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | type => 'http.response.start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | status => $ctx->status, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | headers => $ctx->res_headers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 4 | await $send->({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | type => 'http.response.body', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | body => $res->body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | # 3. Standard HASH/ARRAY data structures -> JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | # 4. Standard unblessed scalars -> Plain text / String output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 59 | my $response_body_str = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 59 | my $is_json = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 59 | ▶ | if (defined $res) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 59 | my $reftype = ref $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 59 | ▶ | if ($reftype eq 'HASH' || $reftype eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$reftype eq "HASH" or $reftype eq "ARRAY"
MC/DC0%0/2
$reftype eq "HASH" or $reftype eq "ARRAY"
$reftype eq "HASH"
$reftype eq "ARRAY"
Truth table33%1/3
A or B
A= $reftype eq "HASH"B= $reftype eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 59 | $response_body_str = encode_json($res); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 59 | $is_json = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | ▶ | elsif (Scalar::Util::blessed($res) && $res->can('TO_JSON')) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
builtin::blessed($res) and $res->can("TO_JSON")
MC/DC0%0/2
builtin::blessed($res) and $res->can("TO_JSON")
builtin::blessed($res)
$res->can("TO_JSON")
Truth table0%0/3
A and B
A= builtin::blessed($res)B= $res->can("TO_JSON")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | $response_body_str = encode_json($res); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | $is_json = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | $response_body_str = "$res"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 59 | my $has_content_type = grep { lc($_->[0]) eq 'content-type' } @{$ctx->res_headers}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | my @res_headers = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | ($is_json && !$has_content_type ? (['content-type', 'application/json']) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 59 | ▶ | @{$ctx->res_headers} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$is_json and not $has_content_type
MC/DC0%0/2
$is_json and not $has_content_type
$is_json
not $has_content_type
Truth table33%1/3
A and not(B)
A= $is_jsonB= not $has_content_type
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 59 | await $send->({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | type => 'http.response.start', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | status => $ctx->status, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | headers => \@res_headers, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 59 | await $send->({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | type => 'http.response.body', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | body => $response_body_str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 39 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 23 | ▶ | sub _uri_unescape ($str) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_uri_unescape: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 23 | ▶ | return $str unless defined $str; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 23 | $str =~ tr/+/ /; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 23 | $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 23 | return $str; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | # Parses a 'multipart/form-data' request body per RFC 7578. Returns two | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | # HashRefs: (\%form_data, \%uploaded_files). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | # %form_data mirrors the shape of application/x-www-form-urlencoded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | # parsing, one scalar string value per field name (last value wins on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | # a repeated name, matching the existing urlencoded behavior above). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | # %uploaded_files is keyed by field name; each value is always an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | # ArrayRef of { filename, content_type, content, size } HashRefs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | # even for a single file, so a field using <input type="file" multiple> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | # and one using a plain single-file input have the same shape to consume. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 4 | ▶ | sub _parse_multipart_form_data ($raw_body, $boundary) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_multipart_form_data: called CC=19 SCAR=34.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 4 | my %form_data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | my %uploaded_files; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 4 | ▶ | return (\%form_data, \%uploaded_files) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
defined $raw_body and length $raw_body and defined $boundary and length $boundary
defined $raw_body and length $raw_body and defined $boundary
defined $raw_body and length $raw_body
MC/DC0%0/4
defined $raw_body and length $raw_body and defined $boundary and length $boundary
defined $raw_body
length $raw_body
defined $boundary
length $boundary
Truth table20%1/5
A and B and C and D
A= defined $raw_bodyB= length $raw_bodyC= defined $boundaryD= length $boundary
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | unless defined $raw_body && length $raw_body | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | && defined $boundary && length $boundary; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | # Normalise newlines and trim final boundary trailing dashes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 4 | $raw_body =~ s/--\Q$boundary\E--\s*\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 4 | my @chunks = split /--\Q$boundary\E\r?\n?/, $raw_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 4 | ▶ | shift @chunks if @chunks && $chunks[0] !~ /Content-Disposition/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@chunks and not $chunks[0] =~ /Content-Disposition/iu
MC/DC0%0/2
@chunks and not $chunks[0] =~ /Content-Disposition/iu
@chunks
not $chunks[0] =~ /Content-Disposition/iu
Truth table33%1/3
A and not(B)
A= @chunksB= not $chunks[0] =~ /Content-Disposition/iu
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 4 | for my $chunk (@chunks) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 6 | ▶ | next unless length $chunk; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 6 | $chunk =~ s/\r?\n\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | 6 | my ($header_block, $part_body) = split /\r?\n\r?\n/, $chunk, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 6 | ▶ | next unless defined $part_body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | 6 | my %headers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 6 | for my $line (split /\r?\n/, $header_block) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 9 | ▶ | $headers{lc $1} = $2 if $line =~ /^([^:]+):\s*(.*)\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 6 | ▶ | my $disposition = $headers{'content-disposition'} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$headers{"content-disposition"} // ""
MC/DC0%0/1
$headers{"content-disposition"} // ""
$headers{"content-disposition"}
Truth table50%1/2
A
A= $headers{"content-disposition"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | 6 | my ($name) = $disposition =~ /\bname="([^"]*)"/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | 6 | ▶ | next unless defined $name && length $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $name and length $name
MC/DC0%0/2
defined $name and length $name
defined $name
length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | 6 | my ($filename_star) = $disposition =~ /\bfilename\*\s*=\s*UTF-8''([^;]+)/i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 6 | my ($filename) = $disposition =~ /\bfilename="([^"]*)"/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 6 | ▶ | $filename = _uri_unescape($filename_star) if defined $filename_star; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 6 | ▶ | if (defined $filename && length $filename) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $filename and length $filename
MC/DC50%1/2
defined $filename and length $filename
defined $filename
length $filename
Truth table66%2/3
A and B
A= defined $filenameB= length $filename
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 3 | ▶ | push @{ $uploaded_files{$name} }, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$headers{"content-type"} // "application/octet-stream"
MC/DC0%0/1
$headers{"content-type"} // "application/octet-stream"
$headers{"content-type"}
Truth table50%1/2
A
A= $headers{"content-type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | filename => $filename, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 3 | content_type => $headers{'content-type'} // 'application/octet-stream', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | content => $part_body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | size => length $part_body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 3 | $form_data{$name} = $part_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 4 | return (\%form_data, \%uploaded_files); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | # Derives a stable, unique-by-construction OpenAPI operationId from a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | # route's method and path, e.g. GET /widgets/{id} -> "get_widgets_id". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | # Unique because method+path is already unique per registered route | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | # (two routes can't share both). Callers can override this default via | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | # the 'operation_id' route option, e.g. when a path changes but external | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | # tooling (codegen, OpenAPI `links`, ...) depends on the ID being | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | # stable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | 41 | ▶ | sub _operation_id_for ($method, $path) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_operation_id_for: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 41 | my $slug = lc($path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 41 | $slug =~ s/[{}]//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | 41 | $slug =~ s{/}{_}g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 41 | $slug =~ s/[^a-z0-9_]/_/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 41 | $slug =~ s/^_+|_+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 41 | ▶ | $slug = 'root' unless length $slug; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 41 | return lc($method) . '_' . $slug; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | # Retains a fire-and-forget Future so it isn't garbage-collected or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | # cancelled before it completes, and removes it from the retention | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | # list once it settles. Owned at the application-instance level (not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | # per-request), since the whole point is the task keeps running after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | # its originating request/response has already finished. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 3 | ▶ | method _retain_background ($future) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_retain_background: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | 3 | push @$background_futures, $future; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 3 | ▶ | $future->on_ready(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | 3 | @$background_futures = grep { $_ != $future } @$background_futures; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 3 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | # A background task is deliberately fire-and-forget: nobody else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | # ever calls ->get/->failure on it. Without an on_fail handler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | # Future.pm would emit its "lost future" leak warning if the task | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | # dies. Log it instead of letting it surface as a leak. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 1 | ▶ | $future->on_fail(sub ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 1 | warn "[PAGI::FastAPI] background task failed: $err"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 3 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 3 | return $future; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 3 | ▶ | async method _handle_websocket ($scope, $receive, $send) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_websocket: called CC=9 SCAR=28.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 3 | ▶ | my $raw_path = $scope->{path} // '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"path"} // "/"
MC/DC0%0/1
$scope->{"path"} // "/"
$scope->{"path"}
Truth table50%1/2
A
A= $scope->{"path"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 3 | my ($route, $path_params) = $self->_match_route('WEBSOCKET', $raw_path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 3 | ▶ | unless ($route) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 1 | $scope->{path_params} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 1 | my $ws = PAGI::WebSocket->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 1 | await $ws->close(4004, "Not Found"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 2 | ▶ | $scope->{path_params} = $path_params // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$path_params // {}
MC/DC0%0/1
$path_params // {}
$path_params
Truth table50%1/2
A
A= $path_params
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 2 | my $ws = PAGI::WebSocket->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 2 | my $resolved_deps = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 2 | ▶ | if ($route->{dependencies} && @{$route->{dependencies}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$route->{"dependencies"} and @{$route->{"dependencies"};}
MC/DC0%0/2
$route->{"dependencies"} and @{$route->{"dependencies"};}
$route->{"dependencies"}
@{$route->{"dependencies"};}
Truth table33%1/3
A and B
A= $route->{"dependencies"}B= @{$route->{"dependencies"};}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 0 | try { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | for my $dep (@{$route->{dependencies}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | my $res = await $dep->{code}->($ws); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 0 | ▶ | if (defined $dep->{key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | $resolved_deps->{$dep->{key}} = $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | catch ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | await $ws->close(1008, "Unauthorized: $err"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 2 | my $handler = $route->{handler}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | 2 | try { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | 2 | await $handler->($ws, $resolved_deps); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | catch ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 0 | ▶ | if (!$ws->is_closed) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | 0 | await $ws->close(1011, "Internal Server Error"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 3 | ▶ | method _match_route ($method, $path) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_match_route: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 3 | for my $route (@$routes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | 5 | ▶ | next unless $route->{method} eq $method; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 5 | ▶ | if (my @captures = ($path =~ $route->{regex})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | 2 | my %path_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | 2 | for my $i (0 .. $#{$route->{path_params}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 1 | $path_params{$route->{path_params}[$i]} = _uri_unescape($captures[$i]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | 2 | return ($route, \%path_params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | 1 | return (undef, {}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | 48 | ▶ | method _register_route ($method, $path, $opts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_register_route: called CC=41 SCAR=37.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 48 | ▶ | my $query_types = $opts->{query} // {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"query"} // {}
MC/DC100%1/1
$opts->{"query"} // {}
$opts->{"query"}
Truth table100%2/2
A
A= $opts->{"query"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | 48 | my $body_spec = $opts->{body}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | 48 | ▶ | my $raw_deps = $opts->{dependencies} // []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"dependencies"} // []
MC/DC100%1/1
$opts->{"dependencies"} // []
$opts->{"dependencies"}
Truth table100%2/2
A
A= $opts->{"dependencies"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | 48 | my $handler = $opts->{handler}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 48 | my $rl_opts = $opts->{rate_limit}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | 48 | ▶ | die "Route '$method $path' requires a 'handler' async coderef" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | unless ref $handler eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | die "Route '$method $path' 'tags' must be an ArrayRef" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | 47 | ▶ | if exists $opts->{tags} && ref $opts->{tags} ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $opts->{"tags"} and ref $opts->{"tags"} ne "ARRAY"
MC/DC100%2/2
exists $opts->{"tags"} and ref $opts->{"tags"} ne "ARRAY"
exists $opts->{"tags"}
ref $opts->{"tags"} ne "ARRAY"
Truth table100%3/3
A and B
A= exists $opts->{"tags"}B= ref $opts->{"tags"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | die "Route '$method $path' 'responses' must be a HashRef" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | 46 | ▶ | if exists $opts->{responses} && ref $opts->{responses} ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $opts->{"responses"} and ref $opts->{"responses"} ne "HASH"
MC/DC100%2/2
exists $opts->{"responses"} and ref $opts->{"responses"} ne "HASH"
exists $opts->{"responses"}
ref $opts->{"responses"} ne "HASH"
Truth table100%3/3
A and B
A= exists $opts->{"responses"}B= ref $opts->{"responses"} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | die "Route '$method $path' 'operation_id' must be a non-empty string" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 45 | ▶ | if exists $opts->{operation_id} && !length($opts->{operation_id} // ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
exists $opts->{"operation_id"} and not length($opts->{"operation_id"} // "")
$opts->{"operation_id"} // ""
MC/DC0%0/2
exists $opts->{"operation_id"} and not length($opts->{"operation_id"} // "")
exists $opts->{"operation_id"}
$opts->{"operation_id"}
Truth table33%1/3
A and not(B)
A= exists $opts->{"operation_id"}B= $opts->{"operation_id"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | 45 | ▶ | if ($rl_opts) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | 1 | ▶ | my $limiter = PAGI::FastAPI::Middleware::RateLimit->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | ref $rl_opts eq 'HASH' ? %$rl_opts : () | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | 1 | my $inner_handler = $handler; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | 9 | ▶ | $handler = async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | 6 | ▶ | return await $limiter->handle($c, async sub ($ctx) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | 6 | return await $inner_handler->($ctx); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | 9 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | 45 | ▶ | die "Route '$method $path' 'dependencies' must be a HashRef or ArrayRef" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $raw_deps eq "HASH" or ref $raw_deps eq "ARRAY"
MC/DC100%2/2
ref $raw_deps eq "HASH" or ref $raw_deps eq "ARRAY"
ref $raw_deps eq "HASH"
ref $raw_deps eq "ARRAY"
Truth table100%3/3
A or B
A= ref $raw_deps eq "HASH"B= ref $raw_deps eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | unless ref $raw_deps eq 'HASH' || ref $raw_deps eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 44 | my @dependencies; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | 44 | ▶ | if (ref $raw_deps eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 2 | for my ($key, $code) (%$raw_deps) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | 3 | ▶ | die "Route '$method $path' dependency '$key' must be a CODE reference" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | unless ref $code eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | 3 | push @dependencies, { key => $key, code => $code }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | } elsif (ref $raw_deps eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | 42 | for my $dep (@$raw_deps) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | 5 | ▶ | if (blessed($dep) && $dep->isa('PAGI::FastAPI::Depends')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($dep) and $dep->isa("PAGI::FastAPI::Depends")
MC/DC50%1/2
builtin::blessed($dep) and $dep->isa("PAGI::FastAPI::Depends")
builtin::blessed($dep)
$dep->isa("PAGI::FastAPI::Depends")
Truth table66%2/3
A and B
A= builtin::blessed($dep)B= $dep->isa("PAGI::FastAPI::Depends")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 3 | push @dependencies, { key => $dep->key, code => $dep->code }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | } elsif (ref $dep eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 1 | push @dependencies, { key => undef, code => $dep }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | 1 | die "Route '$method $path' has an unrecognized dependency entry " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | . "(expected CODE ref or PAGI::FastAPI::Depends instance)"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | 43 | my @path_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 43 | my $regex_path = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | 43 | my $last_pos = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | 43 | ▶ | while ($path =~ /\{([a-zA-Z_]\w*)\}/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | 8 | push @path_params, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | 8 | $regex_path .= quotemeta(substr($path, $last_pos, $-[0] - $last_pos)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 8 | $regex_path .= '([^/]+)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | 8 | $last_pos = $+[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | 43 | $regex_path .= quotemeta(substr($path, $last_pos)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | 43 | $regex_path = "^$regex_path\$"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | 43 | ▶ | if ($method ne 'WEBSOCKET') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | 41 | my @parameters; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | 41 | for my $param (@path_params) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | 7 | push @parameters, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | name => $param, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | in => 'path', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | required => \1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | schema => { type => 'string' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | 41 | for my ($param, $type) (%$query_types) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | 4 | ▶ | my $t_name = eval { $type->name } // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
$type->name
}
} // ""
MC/DC0%0/1
eval {
do {
$type->name
}
} // ""
eval {
do {
$type->name
}
}
Truth table50%1/2
A
A= eval {
do {
$type->name
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 4 | ▶ | push @parameters, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | name => $param, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | in => 'query', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | required => \1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | schema => { type => ($t_name eq 'Int' ? 'integer' : 'string') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | my $route_doc = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | operationId => $opts->{operation_id} // _operation_id_for($method, $path), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | summary => $opts->{summary} // "$method $path", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | parameters => \@parameters, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | responses => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | 200 => { description => "Successful Response" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | 422 => { description => "Validation Error" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | 41 | ▶ | %{ $opts->{responses} // {} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition71%5/7
$opts->{"operation_id"} // _operation_id_for($method, $path)
$opts->{"summary"} // "$method $path"
$opts->{"responses"} // {}
MC/DC0%0/2
$opts->{"operation_id"} // _operation_id_for($method, $path)
$opts->{"operation_id"}
_operation_id_for($method, $path)
MC/DC100%1/1
$opts->{"summary"} // "$method $path"
$opts->{"summary"}
MC/DC100%1/1
$opts->{"responses"} // {}
$opts->{"responses"}
Truth table33%1/3
A // B
A= $opts->{"operation_id"}B= _operation_id_for($method, $path)
Truth table100%2/2
A
A= $opts->{"summary"}
Truth table100%2/2
A
A= $opts->{"responses"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 41 | ▶ | $route_doc->{tags} = $opts->{tags} if exists $opts->{tags}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | 41 | ▶ | $route_doc->{description} = $opts->{description} if exists $opts->{description}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 41 | ▶ | $route_doc->{deprecated} = $opts->{deprecated} ? \1 : \0 if exists $opts->{deprecated}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | 41 | ▶ | if ($body_spec) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | 3 | my $properties = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | 3 | ▶ | if (ref $body_spec eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | 2 | for my ($k, $t) (%$body_spec) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | 4 | ▶ | my $t_name = eval { $t->name } // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
$t->name
}
} // ""
MC/DC0%0/1
eval {
do {
$t->name
}
} // ""
eval {
do {
$t->name
}
}
Truth table50%1/2
A
A= eval {
do {
$t->name
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | 4 | ▶ | $properties->{$k} = { type => ($t_name eq 'Int' ? 'integer' : 'string') }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | $route_doc->{requestBody} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | 3 | required => \1, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | content => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | 'application/json' => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | schema => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | properties => $properties, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | 41 | $openapi->{paths}{$path}{lc($method)} = $route_doc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | 43 | push @$routes, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | path => $path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | regex => qr/$regex_path/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | path_params => \@path_params, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | query_types => $query_types, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | body_spec => $body_spec, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | dependencies => \@dependencies, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | handler => $handler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | 1 | ▶ | method _swagger_ui_html { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_swagger_ui_html: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | 1 | my $json = encode_json($swagger_ui_parameters); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | # Strip opening '{' and closing '}' to inject properties directly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | 1 | $json =~ s/^\s*\{|\}\s*$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | # Add a comma prefix if options are present | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | 1 | ▶ | my $extra_opts = length($json) ? ",\n $json" : ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | 1 | return <<"HTML"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | <!DOCTYPE html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | <html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | <head> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | <title>$title - Swagger UI</title> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | <link rel="stylesheet" href="$swagger_ui_css_url" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | </head> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | <body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | <div id="swagger-ui"></div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | <script src="$swagger_ui_bundle_url"></script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | <script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | SwaggerUIBundle({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | url: '/openapi.json', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | dom_id: '#swagger-ui'$extra_opts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | </script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | </body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | </html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | HTML | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | 21 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | =encoding utf-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | PAGI::FastAPI - Asynchronous, Type-Safe Micro-Framework with Dependency Injection and OpenAPI & Swagger UI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | Version v1.7.3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | use v5.38; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | use PAGI::FastAPI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | use PAGI::FastAPI::Depends qw(Depends); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | use Types::Standard qw(Int Str); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | my $app = PAGI::FastAPI->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | title => 'Store Microservice', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | version => '1.0.0', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | # 1. Add CORS Support (delegates to PAGI::Middleware::CORS from PAGI::Tools) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | $app->add_cors( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | origins => ['https://example.com'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | methods => ['GET', 'POST'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | # 2. Add Authentication Middleware Hook | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | # (hand-rolled here for illustration only, for ready-made schemes, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | # including proper 401 challenges, see PAGI::FastAPI::Security) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | $app->add_middleware(async sub ($c, $next) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | my $auth = $c->header('Authorization') // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | if ($auth ne 'Bearer secret_token') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | $c->status(401); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | return { detail => 'Unauthorized' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | $c->stash->{user_id} = 42; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | return await $next->($c); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | # 3. Register Lifespan Handlers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | $app->on_startup(async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | warn "Connecting to database connection pool...\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | $app->on_shutdown(async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | warn "Closing database connections...\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | # 4. Declare Async Dependencies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | my $get_db = async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | return { db_name => 'production_db' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | my $get_current_user = async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | my $token = $c->header('Authorization') // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | unless ($token eq 'Bearer secret_token') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | $c->status(401); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | return { detail => 'Invalid credentials' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | return { user_id => 42, role => 'admin' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | # 5. Route using HashRef Dependency Map | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | $app->get('/profile', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | dependencies => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | db => $get_db, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | user => $get_current_user, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | handler => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | my $db = $c->stash->{db}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | my $user = $c->stash->{user}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | return { user => $user, db => $db->{db_name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | # 6. Route using Depends() Array Spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | $app->get('/admin', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | dependencies => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | Depends($get_current_user, key => 'user'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | if ($c->stash->{user}{role} ne 'admin') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | $c->status(403); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | return { detail => 'Admin privileges required' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | handler => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | return { message => 'Welcome to admin panel' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | # 7. Non-blocking GET route with path parameter & query validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | $app->get('/items/{id}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | query => { limit => Int }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | handler => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | item_id => $c->param('id'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | limit => $c->param('limit'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | status => 'active', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | # 8. Non-blocking POST route with JSON payload validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | $app->post('/items', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | body => { name => Str, price => Int }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | handler => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | created => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | name => $c->body('name'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | price => $c->body('price'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | # 9. Non-blocking WebSocket Endpoint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | # $ws is a PAGI::WebSocket (from PAGI::Tools), so on_close/each_json/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | # try_send_json/keepalive and more are all built in. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | $app->websocket('/ws', handler => async sub ($ws, $deps) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | await $ws->accept; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | $ws->on_close(async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | my ($code, $reason) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | # runs on every disconnect path, not just a clean loop exit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | await $ws->each_json(async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | my ($data) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | await $ws->send_json({ echo => $data }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | # 10. Authentication via the companion PAGI::FastAPI::Security distribution | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | # (extraction only, you supply the verification logic) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | # use PAGI::FastAPI::Security::HTTPBearer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | # my $bearer = PAGI::FastAPI::Security::HTTPBearer->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | # $app->get('/secure', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | # dependencies => [ $bearer->depends(key => 'token') ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | # handler => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | # return { token => $c->stash->{token} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | # ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | # See L<PAGI::FastAPI::Security> for HTTP Basic, API Key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | # (header/query/cookie), and OAuth2 password-bearer schemes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | my $pagi_app = $app->to_app; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | C<PAGI::FastAPI> is an asynchronous micro-framework for modern Perl (5.36+) inspired by Python's FastAPI. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | It combines non-blocking async execution via L<Future::AsyncAwait> on the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | L<PAGI> (Perl Asynchronous Gateway Interface) specification with request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | parameter validation via L<Type::Tiny> and automatic interactive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | documentation generation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | =head2 Key Features | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | =item * B<PAGI Protocol Engine:> Asynchronous and non-blocking natively, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | built for scalable web applications. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | =item * B<Sub-App & Static Mounting:> Mount external PAGI applications, file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | drivers, or sub-routers using L<mount()|/"C<mount($path_prefix, $pagi_app)>">. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | =item * B<WebSocket Support:> Full non-blocking WebSocket handshake and frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | streaming via L<PAGI::WebSocket>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | =item * B<CORS:> C<add_cors> delegates to L<PAGI::Middleware::CORS> (from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | L<PAGI::Tools>) rather than a separate implementation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | =item * B<Automatic Type Validation:> Request query parameters and JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | payloads are checked against L<Type::Tiny> constraints before reaching route | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | handlers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | =item * B<Automatic Interactive Docs:> Serves an interactive Swagger UI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | interface at C</docs> and machine-readable OpenAPI 3.1 JSON at C</openapi.json>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | =item * B<HTTP 422 Interception:> Automatically intercepts invalid or missing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | parameters and returns formatted JSON errors with an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | C<HTTP 422 Unprocessable Entity> status code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | =item * B<Pluggable Authentication:> Authentication is implemented as ordinary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | dependencies and middleware, with no framework lock-in. For ready-made schemes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | (HTTP Bearer, HTTP Basic, API Key, OAuth2 password bearer), see the companion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | distribution L<PAGI::FastAPI::Security>, see L</AUTHENTICATION AND SECURITY> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | below. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | =item * B<Rate Limiting:> C<add_rate_limit> and the per-route C<rate_limit> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | option provide fixed-window request throttling with pluggable storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | drivers, see L<PAGI::FastAPI::Middleware::RateLimit>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | =item * B<Bot Protection:> C<add_bot_protection> enforces a stateless, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | cryptographic proof-of-work challenge/response flow on unauthenticated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | requests, see L<PAGI::FastAPI::Middleware::BotProtection>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | =item * B<File Uploads:> C<multipart/form-data> request bodies are parsed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | automatically, see L</FILE UPLOADS>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | =item * B<Background Tasks:> C<< $c->background(async sub {...}) >> for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | fire-and-forget work that outlives the request, see L</BACKGROUND TASKS>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | =item * B<Server-Sent Events:> C<< $c->sse >> streams production-grade SSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | responses with keepalives and auto-JSON serialisation, see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | L<PAGI::FastAPI::Response::SSE>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | =item * B<CSRF Protection:> C<enable_csrf>, plus C<< $c->csrf_token >> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | C<< $c->csrf_verify >>, for form and session-based CSRF defence. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1053 | =item * B<Async Message Queue Facade:> L<PAGI::FastAPI::Queue> offers a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | pluggable, topic-based C<push>/C<pop>/C<size> queue for use as an ordinary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | dependency via C<< $queue->dep >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | =item * B<Typed Path Parameters:> C<TypedPath()> from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | L<PAGI::FastAPI::TypedPath> validates and (optionally) coerces path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | parameters via L<Type::Tiny>, the same way C<query>/C<body> already are, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | through the existing C<Depends()> mechanism, automatically returning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | C<HTTP 422> for a non-matching path segment instead of handing your handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | an unchecked string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1064 | =item * B<Response Shape Filtering:> C<with_response_model()> from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1065 | L<PAGI::FastAPI::ResponseModel> validates a handler's return value against | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | a declared L<Type::Tiny> schema and, for a HashRef-of-fields schema, filters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | the output to just the declared fields, so an accidental extra column | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | from a database row (e.g. a password hash) doesn't leak to the client. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1069 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | =item * B<Typed Exception Dispatch:> L<PAGI::FastAPI::Middleware::ExceptionHandler> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | routes a C<die>-thrown exception to a handler registered for its class, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | with a configurable fallback, for the case where an exception escapes a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | handler rather than following the C<< $c->status(...) >>-and-return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | convention described in L</ERROR HANDLING>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1076 | =item * B<Redirect & File Responses:> L<PAGI::FastAPI::Response::Redirect> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1077 | (C<redirect_to()>) and L<PAGI::FastAPI::Response::File> (C<file_response()>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | extend the base L<PAGI::FastAPI::Response> contract for HTTP redirects and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | file-download responses, with automatic content-type guessing and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | C<Content-Disposition> handling for the latter. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | =item * B<Cookie Parsing:> L<PAGI::FastAPI::Cookies> parses the request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | C<Cookie> header into a plain HashRef (C<parse_cookies()>, C<cookie()>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | Setting response cookies needs no extra module, C<< $c->add_header('set-cookie' => ...) >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1085 | already works. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1089 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1090 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | =head2 C<new(%options)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | my $app = PAGI::FastAPI->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | title => 'My API', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | version => '1.2.3', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | Instantiates a new C<PAGI::FastAPI> instance. Acceptable named arguments: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1100 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | =item * C<title> - (Optional) Title string for the application and OpenAPI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | specification. Default: C<'PAGI::FastAPI Application'>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | =item * C<version> - (Optional) Version string for the OpenAPI specification. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1106 | Default: C<'$VERSION'>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | =item * C<secret> - (Optional) Application-level secret scalar. Currently used | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1109 | as the default C<secret> for L</enable_csrf> when no C<secret> is passed to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | that call directly. Storing an app-wide secret here lets you avoid repeating | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1111 | it at every call site that needs it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1115 | =head2 C<get($path, %options)>, C<post($path, %options)>, C<put($path, %options)>, C<patch($path, %options)>, C<delete($path, %options)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | Registers a route for the specified HTTP verb. Options include: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1121 | =item * C<query> - (Optional) HashRef mapping query string keys to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | L<Type::Tiny> type constraints. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | =item * C<body> - (Optional) HashRef mapping request body keys to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | L<Type::Tiny> type constraints. The request body is parsed as JSON by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | default; if the C<Content-Type> header is C<application/x-www-form-urlencoded> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1127 | or C<multipart/form-data>, it is parsed as form fields instead (see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | L</FILE UPLOADS> for the C<multipart/form-data> case specifically). Either | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | way, the same type constraints and validation apply to the parsed field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | values. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1132 | =item * C<dependencies> - (Optional) HashRef or ArrayRef of dependency code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | blocks or L<PAGI::FastAPI::Depends> specs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | =item * C<handler> - (Required) An C<async sub ($c)> code reference executing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | business logic. Receives a L<PAGI::FastAPI::Context> instance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1138 | =item * C<tags> - (Optional) ArrayRef of strings, included verbatim in this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | route's generated OpenAPI document (C<< $openapi->{paths}{$path}{$method}{tags} >>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1140 | Swagger UI (served at C</docs>) groups routes by tag. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | =item * C<operation_id> - (Optional) String, used as this route's OpenAPI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1143 | C<operationId> instead of the auto-generated default (see below). Useful | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1144 | when you want a stable ID that survives the route's path changing, since | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | external tooling (client codegen, OpenAPI C<responses/*/links>, etc.) that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1146 | references an C<operationId> breaks if that ID changes later. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1148 | Every route gets an C<operationId> in its generated OpenAPI document, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | whether or not this option is given, when omitted, one is derived | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | deterministically from the method and path (e.g. C<GET /widgets/{id}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | becomes C<get_widgets_id>), which is guaranteed unique since no two | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1152 | routes share the same method and path. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1154 | =item * C<summary> - (Optional) String, used as this route's OpenAPI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | C<summary>. Defaults to C<"$method $path"> (e.g. C<"GET /widgets/{id}">) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | when omitted, exactly as in earlier versions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | =item * C<description> - (Optional) String, included as this route's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1159 | OpenAPI C<description> when supplied. Omitted from the generated document | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | entirely (not even an empty string) when not given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1162 | =item * C<deprecated> - (Optional) Boolean. When true, marks this route | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | C<deprecated> in the generated OpenAPI document, which Swagger UI renders | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | with a strikethrough. Omitted from the document when not given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | =item * C<responses> - (Optional) HashRef mapping HTTP status codes to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | OpenAPI Response Object HashRefs, merged I<on top of> the default C<200>/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | C<422> entries every route already documents. Add an entry for a status | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | code your handler can actually return (C<201>, C<404>, etc.), or supply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1170 | your own C<200>/C<422> key to override the default wording for that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | route. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1173 | Each response HashRef is passed through to the generated document | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | untouched, so any valid OpenAPI Response Object key is accepted, not just | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | C<description>, for example C<links>, to point Swagger UI/tooling at a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1176 | I<different> operation worth calling next (referencing that operation's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | C<operationId>, per above): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1179 | $app->get('/activity-log', handler => async sub ($c) { ... }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | $app->post('/widgets', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1182 | tags => ['Widgets'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | summary => 'Create a widget', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1184 | description => 'Creates a new widget and returns its ID.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | responses => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | 201 => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | description => 'Widget created', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | links => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1189 | CheckActivityLog => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | operationId => 'get_activity_log', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | description => 'See recent activity for this widget', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1194 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1195 | 409 => { description => 'A widget with that name already exists' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | handler => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | $c->status(201); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1199 | return { id => 42 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1200 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | =head2 C<mount($path_prefix, $pagi_app)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1207 | $app->mount('/css', PAGI::App::File->new(root => './public/css')->to_app); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1208 | $app->mount('/api/v2', $v2_sub_app); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | Mounts a standalone PAGI application closure or sub-application under the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | given path prefix. Under the hood, C<to_app()> composes mounted applications | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | using L<PAGI::App::URLMap>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | =head2 C<on_startup($code_ref)>, C<on_shutdown($code_ref)>, C<on_event($event_type, $code_ref)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | Registers async callbacks for PAGI Lifespan Protocol events (C<'startup'> or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1217 | C<'shutdown'>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | =head2 C<add_middleware> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | $app->add_middleware($middleware, %opts); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | Appends a middleware component to the application's middleware execution stack. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | The C<$middleware> parameter can be passed as a class name or an instantiated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | object: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1228 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1230 | =item * B<Class Name (String):> Automatically loads the class via C<require> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | (dying on load failure) and instantiates it by calling C<< $middleware->new(%opts) >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | =item * B<Object Instance:> Attached directly. If the object implements PAGI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | wrapper interfaces (e.g., C<wrap>, C<to_app>, or C<call>), it is placed into | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | the PAGI middleware stack; otherwise, it is routed to the legacy/internal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1236 | middleware stack. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1238 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | Returns C<$self> to allow method chaining. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | B<Example Usage:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | # Pass class name with constructor options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | $app->add_middleware('PAGI::Middleware::Session', secret => 'my-secret'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | # Pass an instantiated middleware object: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | my $mw = PAGI::Middleware::Logger->new(level => 'debug'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1249 | $app->add_middleware($mw); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | =head2 C<enable_csrf> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1253 | $app->enable_csrf(%options); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1255 | Enables Cross-Site Request Forgery (CSRF) protection on the application by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1256 | instantiating and attaching L<PAGI::Middleware::CSRF>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1258 | By default, the middleware is configured with C<enforce =E<gt> 'header'> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1259 | C<secure =E<gt> 0>. Any passed C<%options> override these defaults. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1261 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1263 | =item * C<secret> (Scalar, optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1265 | The cryptographic secret key used to sign and verify CSRF tokens. If omitted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1266 | from C<%options>, it falls back to the application-level C<secret> attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1267 | set during L<PAGI::FastAPI> instantiation. Dies if no secret can be resolved | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1268 | from either location. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1270 | =item * C<%options> (Hash, optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1272 | Additional configuration arguments passed directly to L<PAGI::Middleware::CSRF/new> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1273 | (such as C<cookie_name>, C<token_length>, or C<secure>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1275 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1277 | Returns C<$self> to allow method chaining. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1279 | B<Example Usage:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1281 | # Uses application-level default secret: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1282 | my $app = PAGI::FastAPI->new(secret => 'master-app-secret'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1283 | $app->enable_csrf(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1285 | # Custom secret and production settings: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1286 | $app->enable_csrf( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1287 | secret => 'csrf-specific-secret', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1288 | secure => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1289 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1291 | =head2 C<add_cors(%options)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1293 | Enables Cross-Origin Resource Sharing (CORS), including automatic handling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1294 | of C<OPTIONS> preflight requests, by delegating to L<PAGI::Middleware::CORS> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1295 | (from L<PAGI::Tools>) rather than a hand-rolled implementation, see that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1296 | module's own documentation for the authoritative behaviour. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1297 | Options (matching L<PAGI::Middleware::CORS>'s own names exactly, so they | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1298 | mean the same thing here as anywhere else in the PAGI ecosystem): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1300 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1302 | =item * C<origins> - ArrayRef of allowed origins (default: C<['*']>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1303 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1304 | =item * C<methods> - ArrayRef of allowed HTTP methods | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1305 | (default: C<['GET','POST','PUT','DELETE','PATCH','OPTIONS']>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1307 | =item * C<headers> - ArrayRef of allowed request headers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1308 | (default: C<['Content-Type','Authorization','X-Requested-With']>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1310 | =item * C<expose_headers> - ArrayRef of headers to expose to the client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1311 | (default: C<[]>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1313 | =item * C<credentials> - Boolean enabling credentials support (default: C<0>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1315 | =item * C<max_age> - Preflight cache max age in seconds (default: C<86400>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1317 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1319 | B<Changed in v0.1.0>: previously this took C<allow_origins>/C<allow_methods>/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1320 | C<allow_headers>/C<allow_credentials> and implemented CORS handling directly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1321 | in C<PAGI::FastAPI>. It's now a thin wrapper that hands your options straight | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1322 | to L<PAGI::Middleware::CORS>, so the option names above match that module's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1323 | exactly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1325 | =head2 C<websocket($path, %options)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1326 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1327 | $app->websocket('/ws/{room}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1328 | handler => async sub ($ws, $deps) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1329 | await $ws->accept; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1330 | while (defined(my $msg = await $ws->receive_text)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1331 | await $ws->send_text("Room $ws->path_params->{room}: $msg"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1332 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1333 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1334 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1336 | Registers a WebSocket endpoint at C<$path>. The C<handler> receives a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1337 | L<PAGI::WebSocket> instance and an optional HashRef of resolved dependencies. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1338 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1339 | =head2 C<add_rate_limit(%options)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1341 | $app->add_rate_limit( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1342 | requests => 100, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1343 | window => 60, # 100 requests per 60s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1344 | key_cb => sub ($c) { $c->header('X-API-Key') // '127.0.0.1' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1345 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1347 | Registers application-wide rate limiting middleware. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1349 | =head2 C<add_bot_protection(%options)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1351 | $app->add_bot_protection( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1352 | difficulty => 3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1353 | secret => $ENV{BOT_PROTECTION_SECRET}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1354 | ttl => 300, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1355 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1357 | Enables Proof-of-Work (PoW) bot protection middleware globally across the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1358 | application. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1360 | When configured, incoming requests lacking valid C<x-bot-challenge> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1361 | C<x-bot-nonce> headers are intercepted and rejected with an HTTP | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1362 | C<401 Unauthorized> response containing a signed challenge token. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1363 | Legitimate client browsers solve the cryptographic puzzle in JavaScript and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1364 | retry the request automatically. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1366 | Accepts the following named options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1368 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1369 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1370 | =item * C<difficulty> (Optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1372 | Integer specifying the number of leading zeros required in the calculated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1373 | SHA-256 hash collision. Higher values exponentially increase CPU effort for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1374 | client devices while keeping server verification costs near-instant. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1375 | Defaults to C<3>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1377 | =item * C<secret> (Optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1378 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1379 | A secret seed scalar used to generate HMAC signatures for challenges. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1380 | B<Must be customised in production environments> to prevent challenge | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1381 | tampering or forgery. Defaults to C<'change_me_in_production'>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1382 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1383 | =item * C<ttl> (Optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1385 | Integer specifying the validity duration of generated challenges in seconds. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1386 | Defaults to C<300> (5 minutes). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1388 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1390 | Returns C<$self> to support method chaining. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1392 | =head2 C<sse($generator, %options)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1394 | $app->get('/api/v1/llm-stream', sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1395 | return $c->sse(async sub ($sse) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1396 | await $sse->keepalive(15); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1397 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1398 | for my $token ("Hello", " world!", " SSE!") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1399 | await $sse->send_json({ token => $token }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1400 | await $c->sleep(0.1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1401 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1402 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1403 | await $sse->close; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1404 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1405 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1407 | Returns a C<PAGI::FastAPI::Response::SSE> response object for real-time | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1408 | Server-Sent Events (SSE) streaming. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1410 | Accepts an asynchronous code reference C<$generator> receiving a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1411 | L<PAGI::SSE> instance, followed by optional named arguments: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1413 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1415 | =item * C<headers> (Optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1417 | ArrayRef of additional HTTP headers to include in the handshake response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1418 | (e.g., C<< headers => [ ['X-Stream-ID' => '123'] ] >>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1419 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1420 | =item * C<status> (Optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1422 | Integer status code for the initial HTTP handshake response. Defaults to C<200>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1424 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1426 | Inside the generator callback, use C<$sse> methods such as C<send_event()>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1427 | C<send_json()>, C<send()>, C<keepalive()>, and C<close()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1428 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1429 | Returns an instance of L<PAGI::FastAPI::Response::SSE>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1430 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1431 | =head2 C<to_pagi> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1433 | my $pagi_app = $app->to_pagi(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1434 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1435 | Compiles the application into a single, executable PAGI-compliant async code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1436 | reference ready to be served by an ASGI/PAGI application server (such as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1437 | L<PAGI::Server>) or wrapped by L<PAGI::Test::Client>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1438 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1439 | This method: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1440 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1441 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1442 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1443 | =item 1. Builds the core route handling application via C<< $self->to_app() >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1444 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1445 | =item 2. Wraps the application in reverse order (LIFO - Last-In, First-Out) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1446 | with all registered PAGI middleware components so that the first added | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1447 | middleware is executed first on incoming HTTP requests. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1449 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1450 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1451 | Middleware objects are wrapped based on their supported interface: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1452 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1453 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1455 | =item * B<Objects with C<wrap>:> Invokes C<< $mw->wrap($app) >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1456 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1457 | =item * B<Objects with C<to_app>:> Invokes C<< $mw->to_app($app) >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1458 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1459 | =item * B<Code references:> Invokes C<< $mw->($app) >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1461 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1463 | Returns an async C<CODEREF> matching the PAGI interface C<< async sub ($scope, $receive, $send) >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1464 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1465 | B<Example Usage:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1466 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1467 | my $app = PAGI::FastAPI->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1468 | $app->enable_csrf(secret => 'my-secret'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1469 | $app->get('/health', handler => async sub ($c) { { status => 'ok' } }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1470 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1471 | # Compile for server deployment or test runner | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1472 | my $pagi_app = $app->to_pagi(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1473 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1474 | =head2 C<to_app()> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1475 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1476 | my $pagi_closure = $app->to_app; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1477 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1478 | Generates and returns an asynchronous code reference conforming to the PAGI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1479 | protocol specification. If sub-applications were registered via | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1480 | L<mount()|/"C<mount($path_prefix, $pagi_app)>">, C<to_app()> automatically | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1481 | wraps the routes using L<PAGI::App::URLMap>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1482 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1483 | =head1 AUTO-GENERATED ENDPOINTS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1484 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1485 | C<PAGI::FastAPI> automatically registers the following system endpoints: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1486 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1487 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1488 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1489 | =item * C<GET /docs> - Serves an interactive Swagger UI web viewer in the browser. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1490 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1491 | =item * C<GET /openapi.json> - Serves the generated OpenAPI 3.1 schema. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1492 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1493 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1495 | =head1 FILE UPLOADS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1497 | A request body with a C<Content-Type> of C<multipart/form-data> is parsed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1498 | per RFC 7578 and made available through L<PAGI::FastAPI::Context>, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1499 | same way an C<application/x-www-form-urlencoded> body already is: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1500 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1501 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1502 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1503 | =item * L<PAGI::FastAPI::Context/form_data> - the plain text fields. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1504 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1505 | =item * L<PAGI::FastAPI::Context/uploaded_files>, L<PAGI::FastAPI::Context/uploaded_file> - the uploaded file parts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1506 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1507 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1508 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1509 | $app->post('/profile/avatar', handler => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1510 | my $caption = $c->form_data('caption'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1511 | my $avatar = $c->uploaded_file('avatar'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1513 | unless ($avatar) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1514 | $c->status(422); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1515 | return { detail => "Missing 'avatar' file" }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1516 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1517 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1518 | open my $fh, '>:raw', "/var/uploads/$avatar->{filename}" or die $!; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1519 | print {$fh} $avatar->{content}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1520 | close $fh; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1521 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1522 | return { filename => $avatar->{filename}, size => $avatar->{size} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1523 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1524 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1525 | As with C<application/x-www-form-urlencoded>, C<$c-E<gt>body> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1526 | C<$c-E<gt>param> also see the plain text fields from a multipart body (not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1527 | the file parts), C<form_data> exists as a clearer-named alias for that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1528 | same data, specifically for form-sourced bodies. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1529 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1530 | File contents are currently read fully into memory for the duration of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1531 | the request; there is no streaming or on-disk spooling for large uploads, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1532 | so plan request body size limits (at your reverse proxy or C<PAGI::Server> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1533 | configuration, whichever applies) accordingly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1534 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1535 | =head1 ERROR HANDLING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1536 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1537 | When a request fails parameter validation (either query string or JSON/form-urlencoded body), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1538 | C<PAGI::FastAPI> short-circuits handler execution and returns C<HTTP 422 Unprocessable Entity> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1539 | with a JSON body: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1540 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1541 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1542 | "detail": "Query param 'priority' invalid: Undef did not pass type constraint" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1543 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1544 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1545 | Unmatched routes return C<HTTP 404 Not Found> with C<{"detail": "Not Found"}>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1546 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1547 | The two cases above, and a dependency's own C<< $c->status(...) >>-and-return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1548 | convention (see L</AUTHENTICATION AND SECURITY>), cover I<expected> failure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1549 | paths. For the separate case of an actual Perl exception (C<die>) escaping | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1550 | a handler or dependency, e.g. from a database layer or a third-party | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1551 | module you're calling into, see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1552 | L<PAGI::FastAPI::Middleware::ExceptionHandler>, which lets you register a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1553 | handler per exception class via C<add_middleware>, with a configurable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1554 | fallback for anything unregistered. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1555 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1556 | =head1 BACKGROUND TASKS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1557 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1558 | For fire-and-forget work that shouldn't make the caller wait, sending a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1559 | notification email, writing an audit log entry, warming a cache, use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1560 | L<PAGI::FastAPI::Context/background>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1561 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1562 | $app->post('/signup', handler => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1563 | my $email = $c->body('email'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1564 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1565 | $c->background(async sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1566 | await send_welcome_email($email); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1567 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1568 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1569 | return { status => 'account created' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1570 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1571 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1572 | The response is sent as soon as the handler returns; the scheduled task | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1573 | keeps running afterward. Retention is owned by the application, not by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1574 | C<$c> or the request: a background task won't be garbage-collected or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1575 | dropped before it completes just because the request that scheduled it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1576 | has finished, and any still-pending tasks are awaited during PAGI Lifespan | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1577 | C<shutdown> (before your own C<on_shutdown> callbacks run), so normal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1578 | process shutdown doesn't kill one mid-flight. A task that dies is logged | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1579 | via C<warn> rather than crashing the request that scheduled it or any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1580 | other in-flight request. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1581 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1582 | See L<PAGI::FastAPI::Context/background> for full details, including how | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1583 | argument validation works and what a misuse error looks like. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1585 | =head1 EVENT LOOPS: FUTURE::IO IS THE GOAL, IO::ASYNC IS AN IMPLEMENTATION DETAIL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1586 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1587 | The PAGI protocol is deliberately silent on which event loop drives it, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1588 | that's meant to be an implementation detail of whichever server runs your | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1589 | app, not part of the application-level contract. L<PAGI::Server>, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1590 | reference server, happens to use L<IO::Async> today, but don't write your | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1591 | own application code as if that's guaranteed or load-bearing. Prefer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1592 | L<Future::IO> for anything loop-driven you write yourself (timers, delays, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1593 | periodic tasks) and it keeps working regardless of which backend a given | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1594 | PAGI server chooses, now or in the future. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1595 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1596 | For a periodic task (a heartbeat or a cache sweep, anything you'd otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1597 | reach for a timer object for), prefer a self-rescheduling coroutine over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1598 | constructing an C<IO::Async::Timer::Periodic> (or any other loop-specific | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1599 | timer class) directly: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1600 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1601 | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1602 | use Future::IO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1603 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1604 | async sub heartbeat { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1605 | while (1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1606 | await Future::IO->sleep(30); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1607 | ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1608 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1609 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1610 | heartbeat()->retain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1611 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1612 | No loop object of your own to construct or own, C<< Future::IO->sleep >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1613 | delegates to whatever backend is already configured in the process, whatever | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1614 | that turns out to be. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1615 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1616 | B<Why "prefer" and not "always," then?> Pragmatically, the L<Future::IO> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1617 | ecosystem is still growing, and not every library has caught up to it yet. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1618 | You'll sometimes still need a loop-specific integration for a particular | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1619 | dependency, most commonly L<Mojo::Pg> or anything else built directly on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1620 | L<Mojo::IOLoop>, which predates L<Future::IO> and doesn't use it. Treat the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1621 | rest of this section as a workaround for that specific, narrower situation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1622 | not as a description of how C<PAGI::FastAPI> itself works, which it doesn't | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1623 | depend on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1624 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1625 | =head2 Fallback: bridging a Mojo::IOLoop-based dependency | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1626 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1627 | If your handlers depend on a library on a I<different> event loop from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1628 | whatever the PAGI server ends up using, concretely, L<Mojo::Pg> or anything | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1629 | else on L<Mojo::IOLoop>, calling that library's non-blocking/callback API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1630 | will do nothing: separate reactors by default don't service each other. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1631 | symptom is a request or C<on_startup>/C<on_shutdown> handler that just hangs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1632 | (and, under L<PAGI::Server>, eventually fails with a lifespan-timeout error) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1633 | even though the call you're C<await>-ing looks correct. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1634 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1635 | Conversely, calling that library in blocking mode instead (e.g. plain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1636 | C<< $pg->db->query(...) >> with no callback) does fire promptly, but freezes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1637 | the I<entire> process, every other in-flight request and WebSocket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1638 | connection, for the duration of that call, since it's a real synchronous | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1639 | call with nothing cooperative about it. This is easy to end up with by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1640 | accident: the blocking form is the default/simplest way to call most of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1641 | these libraries, and it will work correctly in every manual test with one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1642 | client before degrading badly under concurrent load. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1643 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1644 | The fix, when C<PAGI::Server> happens to be running on L<IO::Async> (which, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1645 | again, is an implementation detail you shouldn't assume, but is true of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1646 | reference server today): get L<IO::Async> and the other library's loop onto | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1647 | the I<same> underlying reactor, so a single running loop drives both. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1648 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1649 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1650 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1651 | =item 1. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1652 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1653 | To make L<IO::Async> work with the reactor, one must install the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1654 | L<IO::Async::Loop::EV> because it is the one used when one installs L<EV>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1655 | which is done by L<Mojo::IOLoop> automatically. Note that installing plain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1656 | L<EV> will not work: the constructor used for L<IO::Async::Loop> will not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1657 | automatically prefer C<EV> even if it has been installed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1658 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1659 | =item 2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1660 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1661 | Establish the C<IO_ASYNC_LOOP=EV> variable for the entire process prior to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1662 | starting the loop. This is necessary even when the L<IO::Async::Loop> is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1663 | created by someone else’s code, for instance, C<pagi-server>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1664 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1665 | IO_ASYNC_LOOP=EV pagi-server your_app.pl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1666 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1667 | =item 3. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1668 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1669 | Throughout the whole task, utilise the callback/non-blocking variant of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1670 | API from another library (for example, the command C<< $pg->db->query($sql, @binds, $cb) >>), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1671 | using a C<Future> to wrap up every call so that it can be executed using | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1672 | C<await>-ed like any other command. Consult the information available in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1673 | L<Future::AsyncAwait> regarding the routine C<< Future->new >>/C<< $f->done >>/C<< $f->fail >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1674 | for more information. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1675 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1676 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1677 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1678 | Without both (1) and (2), the two reactors remain separate no matter how | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1679 | correctly you use C<await> on your side. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1680 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1681 | =head1 AUTHENTICATION AND SECURITY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1682 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1683 | C<PAGI::FastAPI> has no authentication built in by design, auth needs vary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1684 | too much between applications to standardise, so the framework gives you two | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1685 | general-purpose building blocks instead: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1686 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1687 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1688 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1689 | =item * B<Middleware> (C<add_middleware>), runs for every request, good | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1690 | for a single global auth check. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1691 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1692 | =item * B<Dependencies> (the C<dependencies> route option, or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1693 | L<PAGI::FastAPI::Depends>), runs per-route, good for auth that varies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1694 | by endpoint (e.g. some routes public, some requiring a token, some | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1695 | requiring a specific role). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1696 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1697 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1698 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1699 | A dependency signals an auth failure the same way any other dependency | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1700 | signals failure: by calling C<< $c->status($code) >> with a code E<gt>= | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1701 | 400 and returning a body HashRef, which short-circuits the route handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1702 | before it runs: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1703 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1704 | my $get_current_user = async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1705 | my $token = $c->header('Authorization') // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1706 | unless ($token eq 'Bearer secret_token') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1707 | $c->status(401); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1708 | return { detail => 'Invalid credentials' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1709 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1710 | return { user_id => 42, role => 'admin' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1711 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1712 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1713 | Note that a dependency must signal failure this way, not by C<die>ing, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1714 | dependency execution is not wrapped in an C<eval>, so a dying dependency | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1715 | propagates as an uncaught exception instead of a clean HTTP response. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1716 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1717 | =head2 Ready-made schemes: PAGI::FastAPI::Security | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1718 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1719 | Writing the token-extraction and 401/403-response boilerplate above by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1720 | hand for every scheme gets repetitive. The companion distribution | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1721 | L<PAGI::FastAPI::Security> provides ready-made, C<Depends()>-compatible | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1722 | classes for the common HTTP authentication schemes, modelled on Python | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1723 | FastAPI's C<fastapi.security> module: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1724 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1725 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1726 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1727 | =item * L<PAGI::FastAPI::Security::HTTPBearer> - C<Authorization: Bearer E<lt>tokenE<gt>>, with a proper C<401> + C<WWW-Authenticate: Bearer> challenge on failure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1728 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1729 | =item * L<PAGI::FastAPI::Security::HTTPBasic> - C<Authorization: Basic E<lt>base64E<gt>>, with a C<401> + C<WWW-Authenticate: Basic realm="...">> challenge. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1730 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1731 | =item * L<PAGI::FastAPI::Security::APIKey> - an API key read from a header, query string parameter, or cookie, with a C<403> on failure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1732 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1733 | =item * L<PAGI::FastAPI::Security::OAuth2::PasswordBearer> - OAuth2 bearer-token extraction plus C<token_url>/C<scopes> metadata for future OpenAPI C<securitySchemes> generation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1734 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1735 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1736 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1737 | Each scheme only I<extracts> the credential, it deliberately does not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1738 | verify it, so you aren't locked into one JWT library, password-hashing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1739 | scheme, or identity provider. Pair it with your own verification as a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1740 | second dependency: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1741 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1742 | use PAGI::FastAPI::Security::HTTPBearer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1743 | use PAGI::FastAPI::Depends qw(Depends); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1744 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1745 | my $bearer = PAGI::FastAPI::Security::HTTPBearer->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1746 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1747 | $app->get('/items', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1748 | dependencies => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1749 | $bearer->depends(key => 'token'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1750 | Depends(async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1751 | my $claims = eval { verify_jwt($c->stash->{token}) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1752 | unless ($claims) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1753 | $c->status(401); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1754 | return { detail => 'Invalid or expired token' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1755 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1756 | return $claims; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1757 | }, key => 'claims'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1758 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1759 | handler => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1760 | return { user_id => $c->stash->{claims}{sub} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1761 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1762 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1763 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1764 | Every scheme also accepts C<auto_error =E<gt> 0>, resolving to C<undef> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1765 | instead of short-circuiting, for routes that behave differently for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1766 | authenticated vs. anonymous requests. See L<PAGI::FastAPI::Security> for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1767 | the full documentation and an end-to-end JWT-verification example. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1768 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1769 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1770 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1771 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1772 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1773 | =item * L<PAGI> - Perl Asynchronous Gateway Interface specification. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1774 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1775 | =item * L<PAGI::App::URLMap> - Routing middleware for prefix-matching PAGI applications. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1776 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1777 | =item * L<PAGI::WebSocket> - Asynchronous WebSocket connection object (from PAGI::Tools) used by C<websocket()> handlers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1778 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1779 | =item * L<PAGI::Middleware::CORS> - CORS middleware (from PAGI::Tools) used by C<add_cors>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1780 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1781 | =item * L<Future::IO> - Loop-agnostic async I/O primitives; see L</EVENT LOOPS: FUTURE::IO IS THE GOAL, IO::ASYNC IS AN IMPLEMENTATION DETAIL>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1782 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1783 | =item * L<PAGI::FastAPI::Context> - Context object passed to route handlers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1784 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1785 | =item * L<PAGI::FastAPI::Depends> - Dependency injection helper. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1786 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1787 | =item * L<PAGI::FastAPI::Security> - Ready-made authentication schemes (HTTP Bearer, HTTP Basic, API Key, OAuth2 password bearer) for C<dependencies>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1788 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1789 | =item * L<DBIx::Class::Async> - Async DBIx::Class integration; see F<eg/dbic_async_integration.pl> for a worked example with this framework. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1790 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1791 | =item * L<Type::Tiny> - Efficient Perl type constraint system. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1792 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1793 | =item * L<Future::AsyncAwait> - Async/Await syntax for Perl. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1794 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1795 | =item * L<PAGI::FastAPI::Middleware::BotProtection> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1796 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1797 | =item * L<PAGI::FastAPI::BotProtection::ProofOfWork> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1798 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1799 | =item * L<PAGI::FastAPI::Response::SSE> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1800 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1801 | =item * L<PAGI::SSE> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1802 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1803 | =item * L<PAGI::FastAPI::Middleware::RateLimit> - App-level and per-route rate limiting. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1804 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1805 | =item * L<PAGI::FastAPI::RateLimit::Driver>, L<PAGI::FastAPI::RateLimit::Driver::Memory> - Pluggable rate-limit storage drivers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1806 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1807 | =item * L<PAGI::FastAPI::Queue> - Pluggable async message queue facade. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1808 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1809 | =item * L<PAGI::FastAPI::Queue::Driver>, L<PAGI::FastAPI::Queue::Driver::Memory> - Pluggable queue storage drivers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1810 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1811 | =item * L<PAGI::FastAPI::TypedPath> - Path parameter validation/coercion via C<Depends()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1812 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1813 | =item * L<PAGI::FastAPI::ResponseModel> - Response shape validation and field filtering. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1814 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1815 | =item * L<PAGI::FastAPI::Middleware::ExceptionHandler> - Typed exception-to-handler dispatch. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1816 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1817 | =item * L<PAGI::FastAPI::Response::Redirect> - HTTP redirect responses. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1818 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1819 | =item * L<PAGI::FastAPI::Response::File> - File-download responses. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1820 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1821 | =item * L<PAGI::FastAPI::Cookies> - Request cookie parsing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1822 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1823 | =item * L</FILE UPLOADS> - Parsing C<multipart/form-data> request bodies. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1824 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1825 | =item * L</BACKGROUND TASKS> - Fire-and-forget work via C<< $c->background >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1826 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1827 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1828 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1829 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1830 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1831 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1832 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1833 | =head1 REPOSITORY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1834 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1835 | L<https://github.com/manwar/PAGI-FastAPI> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1836 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1837 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1838 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1839 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1840 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1841 | bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1842 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1843 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1844 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1845 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1846 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1847 | perldoc PAGI::FastAPI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1848 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1849 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1850 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1851 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1852 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1853 | =item * BUG Report | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1854 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1855 | L<https://github.com/manwar/PAGI-FastAPI/issues> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1856 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1857 | =item * Search MetaCPAN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1858 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1859 | L<https://metacpan.org/dist/PAGI-FastAPI/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1860 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1861 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1862 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1863 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1864 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1865 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1866 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1867 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1868 | the terms of the Artistic License (2.0). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1869 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1870 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1871 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1872 | 21 | 1; # End of PAGI::FastAPI | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||