| 1 | package PAGI::Nano::Context; | ||||||||||||||
| 2 | 12 | $PAGI::Nano::Context::VERSION = '0.001001'; | |||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 12 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 12 | ▶ | use Encode (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | # Shared behavior mixed into every Nano context (HTTP, WebSocket, SSE) alongside | ||||||||||||||
| 9 | # the stock PAGI context class for that scope type. It only needs $self->{scope}, | ||||||||||||||
| 10 | # which all of them carry. uri_for lives here (not in PAGI-Tools) because it | ||||||||||||||
| 11 | # resolves against the flat name registry PAGI::Nano injects on the scope — a | ||||||||||||||
| 12 | # Nano concept the base toolkit knows nothing about. | ||||||||||||||
| 13 | |||||||||||||||
| 14 | # Build a URL for a named route. Resolves against the flat name->path registry | ||||||||||||||
| 15 | # PAGI::Nano injects on the scope, so any name in the app (including across | ||||||||||||||
| 16 | # mounts, in either direction) is reachable. | ||||||||||||||
| 17 | 17 | ▶ | sub uri_for { | ||||||||||||
|
Subroutine
uri_for: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||
| 18 | 17 | my ($self, $name, $path_params, $query_params) = @_; | |||||||||||||
| 19 | |||||||||||||||
| 20 | 17 | my $routes = $self->{scope}{'pagi.nano.routes'}; | |||||||||||||
| 21 | 17 | ▶ | Carp::croak('uri_for: no named-route registry on the scope ' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 22 | . '(is this a PAGI::Nano app, and is the route named?)') | ||||||||||||||
| 23 | unless $routes; | ||||||||||||||
| 24 | 17 | my $path = $routes->{$name}; | |||||||||||||
| 25 | 17 | ▶ | Carp::croak("uri_for: no route named '$name'") unless defined $path; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | 17 | ▶ | $path_params ||= {}; | ||||||||||||
|
Condition100%2/2
$path_params ||= {}
MC/DC100%1/1
$path_params ||= {}
$path_params
Truth table100%2/2
A
A= $path_params
| |||||||||||||||
| 28 | 17 | ▶ | $query_params ||= {}; | ||||||||||||
|
Condition100%2/2
$query_params ||= {}
MC/DC100%1/1
$query_params ||= {}
$query_params
Truth table100%2/2
A
A= $query_params
| |||||||||||||||
| 29 | |||||||||||||||
| 30 | 17 | $path = _render_path($path, $path_params); | |||||||||||||
| 31 | |||||||||||||||
| 32 | 16 | ▶ | if (%$query_params) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | 3 | my @pairs; | |||||||||||||
| 34 | 3 | for my $k (sort keys %$query_params) { | |||||||||||||
| 35 | 4 | push @pairs, _uri_escape($k) . '=' . _uri_escape($query_params->{$k}); | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | 3 | $path .= '?' . join('&', @pairs); | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 16 | return $path; | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 17 | ▶ | sub _render_path { | ||||||||||||
|
Subroutine
_render_path: called CC=8 SCAR=20.9
| |||||||||||||||
| 44 | 17 | my ($template, $params) = @_; | |||||||||||||
| 45 | 17 | my $rendered = ''; | |||||||||||||
| 46 | 17 | my $offset = 0; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 17 | ▶ | while ($template =~ /(\{(\w+)(?::[^}]*)?\}|\*(\w+)|:(\w+))/g) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | # Capture all match state before an escaping helper runs another regex. | ||||||||||||||
| 50 | 13 | my ($start, $end) = ($-[0], $+[0]); | |||||||||||||
| 51 | 13 | my ($token, $braced, $splat, $colon) = ($1, $2, $3, $4); | |||||||||||||
| 52 | 13 | $rendered .= _escape_path_literal( | |||||||||||||
| 53 | substr($template, $offset, $start - $offset) | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 13 | ▶ | my $name = defined $braced ? $braced | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 57 | : defined $splat ? $splat | ||||||||||||||
| 58 | : $colon; | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 13 | ▶ | if (!exists $params->{$name}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 0 | $rendered .= $token; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | else { | ||||||||||||||
| 64 | 13 | ▶ | my $value = defined $params->{$name} ? "$params->{$name}" : ''; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | 13 | ▶ | if (defined $splat) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 1 | $rendered .= join '/', map { _uri_escape($_) } | |||||||||||||
| 67 | split '/', $value, -1; | ||||||||||||||
| 68 | } | ||||||||||||||
| 69 | else { | ||||||||||||||
| 70 | 12 | ▶ | Carp::croak( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | "uri_for: value for '$name' contains '/' -- use a *splat route for path-valued parameters" | ||||||||||||||
| 72 | ) if index($value, '/') >= 0; | ||||||||||||||
| 73 | 11 | $rendered .= _uri_escape($value); | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | } | ||||||||||||||
| 76 | 12 | $offset = $end; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 16 | $rendered .= _escape_path_literal(substr($template, $offset)); | |||||||||||||
| 80 | 16 | return $rendered; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | # Resolve a declared service by name. Delegates to the registry PAGI::Nano | ||||||||||||||
| 84 | # injects on the scope (the same mechanism uri_for uses for named routes), | ||||||||||||||
| 85 | # which applies the scope-discrimination rule: an app-scoped value is returned | ||||||||||||||
| 86 | # as-is, a per-request maker is invoked and memoized for this request/ | ||||||||||||||
| 87 | # connection, and a factory-marked maker is invoked fresh on every call. | ||||||||||||||
| 88 | 25 | ▶ | sub service { | ||||||||||||
|
Subroutine
service: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 89 | 25 | my ($self, $name) = @_; | |||||||||||||
| 90 | |||||||||||||||
| 91 | 25 | my $registry = $self->{scope}{'pagi.nano.services'}; | |||||||||||||
| 92 | 25 | ▶ | Carp::croak("service: no service registry on the scope " | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | . "(is this a PAGI::Nano app, and was '$name' declared?)") | ||||||||||||||
| 94 | unless $registry; | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 24 | return $registry->_resolve($name, $self); | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 50 | ▶ | sub _utf8_bytes { | ||||||||||||
|
Subroutine
_utf8_bytes: called CC=2 SCAR=7.1
| |||||||||||||||
| 100 | 50 | my ($s) = @_; | |||||||||||||
| 101 | 50 | ▶ | $s = '' unless defined $s; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | 50 | return Encode::encode('UTF-8', $s, Encode::FB_CROAK()); | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 29 | ▶ | sub _escape_path_literal { | ||||||||||||
|
Subroutine
_escape_path_literal: called CC=1 SCAR=0.0
| |||||||||||||||
| 106 | 29 | my $bytes = _utf8_bytes($_[0]); | |||||||||||||
| 107 | 29 | $bytes =~ s/([^A-Za-z0-9\-._~!\$&'()*+,;=:\@\/])/sprintf('%%%02X', ord($1))/ge; | |||||||||||||
| 108 | 29 | return $bytes; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 21 | ▶ | sub _uri_escape { | ||||||||||||
|
Subroutine
_uri_escape: called CC=1 SCAR=0.0
| |||||||||||||||
| 112 | 21 | my $bytes = _utf8_bytes($_[0]); | |||||||||||||
| 113 | 21 | $bytes =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge; | |||||||||||||
| 114 | 21 | return $bytes; | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 12 | 1; | |||||||||||||
| 118 | |||||||||||||||
| 119 | =encoding utf8 | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =head1 NAME | ||||||||||||||
| 122 | |||||||||||||||
| 123 | PAGI::Nano::Context - Shared behavior for the contexts PAGI::Nano vends | ||||||||||||||
| 124 | |||||||||||||||
| 125 | =head1 DESCRIPTION | ||||||||||||||
| 126 | |||||||||||||||
| 127 | A mixin inherited by L<PAGI::Nano::Context::HTTP>, | ||||||||||||||
| 128 | L<PAGI::Nano::Context::WebSocket>, and L<PAGI::Nano::Context::SSE> alongside the | ||||||||||||||
| 129 | stock PAGI context class for each scope type. It provides L</uri_for> and | ||||||||||||||
| 130 | L</service>, both available to handlers of every protocol. | ||||||||||||||
| 131 | |||||||||||||||
| 132 | =head1 METHODS | ||||||||||||||
| 133 | |||||||||||||||
| 134 | =head2 uri_for | ||||||||||||||
| 135 | |||||||||||||||
| 136 | my $url = $c->uri_for($name); | ||||||||||||||
| 137 | my $url = $c->uri_for($name, \%path_params); | ||||||||||||||
| 138 | my $url = $c->uri_for($name, \%path_params, \%query_params); | ||||||||||||||
| 139 | |||||||||||||||
| 140 | Builds the URL for a route named with L<PAGI::Nano/name>. Path placeholders | ||||||||||||||
| 141 | (C<:id>, C<{id}>, C<{id:regex}>, C<*splat>) are filled from C<%path_params>. | ||||||||||||||
| 142 | Pass decoded Perl strings, not pre-encoded URL components. Ordinary placeholders | ||||||||||||||
| 143 | represent one path segment and percent-encode inserted values as UTF-8 bytes; | ||||||||||||||
| 144 | they croak if a value contains C</>. Use C<*splat> for a path-valued parameter: | ||||||||||||||
| 145 | its slash separators are preserved while each segment is encoded separately. | ||||||||||||||
| 146 | This distinction is required because PAGI decodes percent-encoded paths, | ||||||||||||||
| 147 | including C<%2F>, before routing. Literal route text preserves RFC 3986 path | ||||||||||||||
| 148 | characters and slash separators. C<%query_params> (if any) is appended in sorted | ||||||||||||||
| 149 | key order with UTF-8 percent encoding and C<%20> for spaces. | ||||||||||||||
| 150 | Resolution is against the flat name registry PAGI::Nano injects on the scope, so | ||||||||||||||
| 151 | names defined anywhere in the app — including across a C<mount>, in either | ||||||||||||||
| 152 | direction — are reachable, with mount prefixes applied. Dies if the name is | ||||||||||||||
| 153 | unknown. | ||||||||||||||
| 154 | |||||||||||||||
| 155 | =head2 service | ||||||||||||||
| 156 | |||||||||||||||
| 157 | my $value = $c->service($name); | ||||||||||||||
| 158 | |||||||||||||||
| 159 | Resolves a service declared with L<PAGI::Nano/service>. Whether this returns | ||||||||||||||
| 160 | the same app-scoped value every time, a per-request object built and memoized | ||||||||||||||
| 161 | on first access, or a fresh object on every call, is decided by what the | ||||||||||||||
| 162 | service's builder returned — see L<PAGI::Nano/SERVICES> for the full scoping | ||||||||||||||
| 163 | rule. Dies if C<$name> was never declared. | ||||||||||||||
| 164 | |||||||||||||||
| 165 | =head1 AUTHOR | ||||||||||||||
| 166 | |||||||||||||||
| 167 | John Napiorkowski C<< <jjnapiork@cpan.org> >> | ||||||||||||||
| 168 | |||||||||||||||
| 169 | =head1 COPYRIGHT & LICENSE | ||||||||||||||
| 170 | |||||||||||||||
| 171 | Copyright 2026, John Napiorkowski. This library is free software; you can | ||||||||||||||
| 172 | redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||
| 173 | |||||||||||||||
| 174 | =cut | ||||||||||||||