| 1 | package PAGI::Endpoint::HTTP; | ||||||||||||||||||||||||||||||||||||
| 2 | 7 | $PAGI::Endpoint::HTTP::VERSION = '0.002002'; | |||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use PAGI::Utils qw(is_response); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||
| 11 | # Factory class method - override in subclass for customization | ||||||||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | sub context_class { 'PAGI::Context' } | ||||||||||||||||||||||||||||||||||
|
Subroutine
context_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 16 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 15 | 16 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||
| 16 | 16 | return bless \%args, $class; | |||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||
| 19 | # HTTP methods we support | ||||||||||||||||||||||||||||||||||||
| 20 | 7 | our @HTTP_METHODS = qw(get post put patch delete head options); | |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | 6 | ▶ | sub allowed_methods { | ||||||||||||||||||||||||||||||||||
|
Subroutine
allowed_methods: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 23 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||
| 24 | 6 | my @allowed; | |||||||||||||||||||||||||||||||||||
| 25 | 6 | for my $method (@HTTP_METHODS) { | |||||||||||||||||||||||||||||||||||
| 26 | 42 | ▶ | push @allowed, uc($method) if $self->can($method); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||
| 28 | # HEAD is allowed if GET is defined | ||||||||||||||||||||||||||||||||||||
| 29 | 6 | ▶ | push @allowed, 'HEAD' if $self->can('get') && !$self->can('head'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->can("get") and not $self->can("head")
MC/DC0%0/2
$self->can("get") and not $self->can("head")
$self->can("get")
not $self->can("head")
Truth table33%1/3
A and not(B)
A= $self->can("get")B= not $self->can("head")
| |||||||||||||||||||||||||||||||||||||
| 30 | # OPTIONS is always allowed | ||||||||||||||||||||||||||||||||||||
| 31 | 6 | ▶ | push @allowed, 'OPTIONS' unless grep { $_ eq 'OPTIONS' } @allowed; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 32 | 6 | return sort @allowed; | |||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||
| 35 | 13 | ▶ | async sub dispatch { | ||||||||||||||||||||||||||||||||||
|
Subroutine
dispatch: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 36 | 13 | my ($self, $ctx) = @_; | |||||||||||||||||||||||||||||||||||
| 37 | 13 | ▶ | my $http_method = lc($ctx->method // 'GET'); | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ctx->method // "GET"
MC/DC0%0/1
$ctx->method // "GET"
$ctx->method
Truth table50%1/2
A
A= $ctx->method
| |||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | 13 | my $res; | |||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||
| 41 | # OPTIONS - return allowed methods (auto-respond unless overridden) | ||||||||||||||||||||||||||||||||||||
| 42 | 13 | ▶ | if ($http_method eq 'options' && !$self->can('options')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
$http_method eq "options" and not $self->can("options")
MC/DC50%1/2
$http_method eq "options" and not $self->can("options")
$http_method eq "options"
not $self->can("options")
Truth table66%2/3
A and not(B)
A= $http_method eq "options"B= not $self->can("options")
| |||||||||||||||||||||||||||||||||||||
| 43 | 2 | my $allow = join(', ', $self->allowed_methods); | |||||||||||||||||||||||||||||||||||
| 44 | 2 | $res = $ctx->response->header('Allow', $allow)->empty; | |||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||
| 46 | # HEAD falls back to GET if not explicitly defined | ||||||||||||||||||||||||||||||||||||
| 47 | ▶ | elsif ($http_method eq 'head' && !$self->can('head') && $self->can('get')) { | |||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$http_method eq "head" and not $self->can("head") and $self->can("get")
$http_method eq "head" and not $self->can("head")
MC/DC33%1/3
$http_method eq "head" and not $self->can("head") and $self->can("get")
$http_method eq "head"
not $self->can("head")
$self->can("get")
Truth table50%2/4
A and not(B) and C
A= $http_method eq "head"B= not $self->can("head")C= $self->can("get")
| |||||||||||||||||||||||||||||||||||||
| 48 | 1 | $res = await $self->get($ctx); | |||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||
| 50 | # Dispatch to the appropriate method handler | ||||||||||||||||||||||||||||||||||||
| 51 | elsif ($self->can($http_method)) { | ||||||||||||||||||||||||||||||||||||
| 52 | 7 | $res = await $self->$http_method($ctx); | |||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||
| 54 | # 405 Method Not Allowed | ||||||||||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||||||||||
| 56 | 3 | my $allow = join(', ', $self->allowed_methods); | |||||||||||||||||||||||||||||||||||
| 57 | 3 | $res = $ctx->response->header('Allow', $allow)->status(405)->text("405 Method Not Allowed"); | |||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||
| 60 | 13 | ▶ | croak ref($self) . "->$http_method did not return a response" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 61 | unless is_response($res); | ||||||||||||||||||||||||||||||||||||
| 62 | 12 | await $ctx->respond($res); | |||||||||||||||||||||||||||||||||||
| 63 | 7 | } | |||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||
| 65 | 8 | ▶ | sub to_app { | ||||||||||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 66 | 8 | my ($class) = @_; | |||||||||||||||||||||||||||||||||||
| 67 | 8 | my $context_class = $class->context_class; | |||||||||||||||||||||||||||||||||||
| 68 | 8 | my $endpoint = $class->new; # ONE instance for the app lifetime (singleton) | |||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | return async sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||
| 71 | 7 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||
| 73 | 7 | ▶ | my $type = $scope->{type} // 'http'; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"type"} // "http"
MC/DC0%0/1
$scope->{"type"} // "http"
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
| |||||||||||||||||||||||||||||||||||||
| 74 | 7 | ▶ | croak "Expected http scope, got '$type'" unless $type eq 'http'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | 7 | require PAGI::Context; | |||||||||||||||||||||||||||||||||||
| 77 | 7 | my $ctx = $context_class->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||
| 79 | 7 | await $endpoint->dispatch($ctx); | |||||||||||||||||||||||||||||||||||
| 80 | 8 | }; | |||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | 7 | 1; | |||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||||||||||