| 1 | package Thunderhorse; | ||||||||||||||||||||||||||||
| 2 | 24 | $Thunderhorse::VERSION = '0.107'; | |||||||||||||||||||||||||||
| 3 | ################################## | ||||||||||||||||||||||||||||
| 4 | # ~~~~~~~~~~~~ Ride ~~~~~~~~~~~~ # | ||||||||||||||||||||||||||||
| 5 | # ~~~~~~~~~~~~ Ride ~~~~~~~~~~~~ # | ||||||||||||||||||||||||||||
| 6 | # ~~~~~~~~~~~~ Ride ~~~~~~~~~~~~ # | ||||||||||||||||||||||||||||
| 7 | # ~~~~~~~~~~~~ Ride ~~~~~~~~~~~~ # | ||||||||||||||||||||||||||||
| 8 | # ~~~~~~~~ Thunderhorse ~~~~~~~~ # | ||||||||||||||||||||||||||||
| 9 | # ~~~~~~~~ Thunderhorse ~~~~~~~~ # | ||||||||||||||||||||||||||||
| 10 | # ~~~~~~~~ Thunderhorse ~~~~~~~~ # | ||||||||||||||||||||||||||||
| 11 | # ~~~~~~~~ Thunderhorse ~~~~~~~~ # | ||||||||||||||||||||||||||||
| 12 | # ~~~~~~~~~~~ Revenge ~~~~~~~~~~ # | ||||||||||||||||||||||||||||
| 13 | # ~~~~~~~~~~~ Revenge ~~~~~~~~~~ # | ||||||||||||||||||||||||||||
| 14 | # ~~~~~~~~~~~ Revenge ~~~~~~~~~~ # | ||||||||||||||||||||||||||||
| 15 | ################################## | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 24 | ▶ | use v5.40; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 24 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 24 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 24 | ▶ | use Gears::X::Thunderhorse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 24 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 25 | pagi_loop | ||||||||||||||||||||||||||||
| 26 | adapt_pagi | ||||||||||||||||||||||||||||
| 27 | build_handler | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 114 | ▶ | async sub pagi_loop ($ctx, @matches) | ||||||||||||||||||||||||||
|
Subroutine
pagi_loop: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 114 | { | |||||||||||||||||||||||||||
| 32 | 114 | my @pagi = $ctx->pagi->@*; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 114 | foreach my $match (@matches) { | |||||||||||||||||||||||||||
| 35 | # is this a bridge? If yes, take first element (the bridge location). | ||||||||||||||||||||||||||||
| 36 | # It is guaranteed to be a match, not an array | ||||||||||||||||||||||||||||
| 37 | 120 | ▶ | my $loc = (ref $match eq 'ARRAY' ? $match->[0] : $match)->location; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # $ctx->match may be an array if this is a bridge. Location handler | ||||||||||||||||||||||||||||
| 40 | # takes care of that | ||||||||||||||||||||||||||||
| 41 | 120 | $ctx->set_match($match); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # execute location handler (PAGI application) | ||||||||||||||||||||||||||||
| 44 | 120 | await $loc->pagi_app->(@pagi); | |||||||||||||||||||||||||||
| 45 | 113 | ▶ | last if $ctx->is_consumed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 10 | ▶ | sub adapt_pagi ($destination) | ||||||||||||||||||||||||||
|
Subroutine
adapt_pagi: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | # no need to async here because we don't await - destination must return a promise anyway | ||||||||||||||||||||||||||||
| 52 | # TODO: think of a proper way to enforce last placeholder being at the very end of the url | ||||||||||||||||||||||||||||
| 53 | 14 | ▶ | return sub ($scope, @args) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 54 | Gears::X::Thunderhorse->raise('bad PAGI execution chain, not a Thunderhorse app') | ||||||||||||||||||||||||||||
| 55 | 14 | ▶ | unless my $ctx = $scope->{thunderhorse}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # consume this context eagerly to keep further matches from firing | ||||||||||||||||||||||||||||
| 58 | 14 | $ctx->consume; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # take last matched element as the path | ||||||||||||||||||||||||||||
| 61 | # pagi apps can't be bridges, so don't check if $ctx->match is an array | ||||||||||||||||||||||||||||
| 62 | 14 | ▶ | my $path = $ctx->match->matched->[-1] // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$ctx->match->matched->[-1] // ""
MC/DC100%1/1
$ctx->match->matched->[-1] // ""
$ctx->match->matched->[-1]
Truth table100%2/2
A
A= $ctx->match->matched->[-1]
| |||||||||||||||||||||||||||||
| 63 | 14 | ▶ | my $trailing_slash = $scope->{path} =~ m{/$} ? '/' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 14 | $path =~ s{^/?}{/}; | |||||||||||||||||||||||||||
| 65 | 14 | $path =~ s{/?$}{$trailing_slash}; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # modify the scope for the app | ||||||||||||||||||||||||||||
| 68 | 14 | $scope = {$scope->%*}; | |||||||||||||||||||||||||||
| 69 | 14 | $scope->{root_path} = ($scope->{root_path} . $scope->{path}) =~ s{\Q$path\E$}{}r; | |||||||||||||||||||||||||||
| 70 | 14 | $scope->{path} = $path; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 14 | return $destination->($scope, @args); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 10 | } | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 87 | ▶ | sub build_handler ($controller, $destination) | ||||||||||||||||||||||||||
|
Subroutine
build_handler: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 106 | ▶ | return async sub ($scope, $receive, $send) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 79 | Gears::X::Thunderhorse->raise('bad PAGI execution chain, not a Thunderhorse app') | ||||||||||||||||||||||||||||
| 80 | 106 | ▶ | unless my $ctx = $scope->{thunderhorse}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 106 | $ctx->update($scope, $receive, $send); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 106 | my $match = $ctx->match; | |||||||||||||||||||||||||||
| 85 | 106 | my $bridge = ref $match eq 'ARRAY'; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # this location may be unimplemented when destination is undefined, but | ||||||||||||||||||||||||||||
| 88 | # a full handler should be built anyway. Unimplemented destinations | ||||||||||||||||||||||||||||
| 89 | # should still be wrappable in middleware. | ||||||||||||||||||||||||||||
| 90 | 106 | ▶ | if (defined $destination) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 104 | try { | |||||||||||||||||||||||||||
| 92 | 104 | my $facade = $controller->make_facade($ctx); | |||||||||||||||||||||||||||
| 93 | 104 | ▶ | my $result = $destination->($controller, $facade, ($bridge ? $match->[0] : $match)->matched->@*); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 91 | ▶ | $result = await $result | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | if $result isa 'Future'; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 89 | ▶ | if (!$ctx->is_consumed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 74 | ▶ | if (defined $result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 57 | await $controller->render_response($ctx, $result); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 17 | weaken $facade; | |||||||||||||||||||||||||||
| 103 | 17 | ▶ | Gears::X::Thunderhorse->raise("context hasn't been given up - forgot await?") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | if defined $facade; | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | catch ($ex) { | ||||||||||||||||||||||||||||
| 109 | 19 | await $controller->_on_error($ctx, $ex); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # NOTE: this method will not send the response if we already sent or if | ||||||||||||||||||||||||||||
| 114 | # the response is not ready. It does not check whether the context is | ||||||||||||||||||||||||||||
| 115 | # consumed altogether, so a manually consumed context with a non-ready | ||||||||||||||||||||||||||||
| 116 | # response will not send anything, likely rendering some kind of error | ||||||||||||||||||||||||||||
| 117 | # page (but not 404). Currently, a PAGI error is raised, informing | ||||||||||||||||||||||||||||
| 118 | # about app returning without sending respnose. | ||||||||||||||||||||||||||||
| 119 | # NOTE: this needs to be here, since we want to use $send from this context | ||||||||||||||||||||||||||||
| 120 | 104 | await $ctx->try_send_res; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # if this is a bridge and bridge did not render, it means we are | ||||||||||||||||||||||||||||
| 123 | # free to go deeper. Avoid first match, as it was handled already | ||||||||||||||||||||||||||||
| 124 | # above | ||||||||||||||||||||||||||||
| 125 | 99 | ▶ | if ($bridge && !$ctx->is_consumed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$bridge and not $ctx->is_consumed
MC/DC100%2/2
$bridge and not $ctx->is_consumed
$bridge
not $ctx->is_consumed
Truth table100%3/3
A and not(B)
A= $bridgeB= not $ctx->is_consumed
| |||||||||||||||||||||||||||||
| 126 | 8 | await pagi_loop($ctx, $match->@[1 .. $match->$#*]); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 87 | }; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||