blib/lib/PAGI/Endpoint/HTTP.pm

filter: Statementstmt 100% 54 / 54 Branchbran 81.2% 13 / 16 Conditioncond 57.8% 11 / 19 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 82.7% 96 / 116 CC 17 SCAR 17.7
CC
17
Cov
88%
dispatch
10.3
allowed_methods
6.5
__ANON__
3.1
CRAP 5.9
« blib/lib/PAGI/Middleware/Rewrite.pm ↑ Summary blib/lib/PAGI/Response.pm »
1 package PAGI::Endpoint::HTTP;
2 7 $PAGI::Endpoint::HTTP::VERSION = '0.002000';
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  
10 # Factory class method - override in subclass for customization
11 10 ▶sub context_class { 'PAGI::Context' }
Subroutine context_class: called CC=1 SCAR=0.0
Pod documented
12  
13 sub new {
14 16 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15 16 return bless \%args, $class;
16 }
17  
18 # HTTP methods we support
19 7 our @HTTP_METHODS = qw(get post put patch delete head options);
20  
21 sub allowed_methods {
22 6 ▶ my ($self) = @_;
Subroutine allowed_methods: called CC=6 SCAR=18.7
Pod documented
23 6 my @allowed;
24 6 for my $method (@HTTP_METHODS) {
25 42 ▶ push @allowed, uc($method) if $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) 13 29 42
26 }
27 # HEAD is allowed if GET is defined
28 6 ▶ push @allowed, 'HEAD' if $self->can('get') && !$self->can('head');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("get") and not $self->can("head") 6 0 6
Condition33%1/3
$self->can("get") and not $self->can("head")
!ll&&!rl&&r
006
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")
ABresult
0X0
100
111
29 # OPTIONS is always allowed
30 6 ▶ push @allowed, 'OPTIONS' unless grep { $_ eq 'OPTIONS' } @allowed;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "OPTIONS";} @allowed 6 0 6
31 6 return sort @allowed;
32 }
33  
34 13 ▶async sub dispatch {
Subroutine dispatch: called CC=10 SCAR=23.3
Pod documented
35 13 my ($self, $ctx) = @_;
36 13 ▶ my $http_method = lc($ctx->method // 'GET');
Condition50%1/2
$ctx->method // "GET"
l!l
130
MC/DC0%0/1
$ctx->method // "GET"
$ctx->method
Truth table50%1/2
A
A= $ctx->method
Aresult
11
00
37  
38 13 my $res;
39  
40 # OPTIONS - return allowed methods (auto-respond unless overridden)
41 13 ▶ if ($http_method eq 'options' && !$self->can('options')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($http_method eq 'options' and not $self->can('options')) { } 2 11 13
elsif ($http_method eq 'head' and not $self->can('head') and $self->can('get')) { } 1 10 11
elsif ($self->can($http_method)) { } 7 3 10
Condition66%2/3
$http_method eq "options" and not $self->can("options")
!ll&&!rl&&r
1102
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")
ABresult
0X0
100
111
42 2 my $allow = join(', ', $self->allowed_methods);
43 2 $res = $ctx->response->header('Allow', $allow)->empty;
44 }
45 # HEAD falls back to GET if not explicitly defined
46 ▶ 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")
!ll&&!rl&&r
1001
$http_method eq "head" and not $self->can("head")
!ll&&!rl&&r
1001
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")
ABCresult
0XX0
10X0
1100
1111
47 1 $res = await $self->get($ctx);
48 }
49 # Dispatch to the appropriate method handler
50 elsif ($self->can($http_method)) {
51 7 $res = await $self->$http_method($ctx);
52 }
53 # 405 Method Not Allowed
54 else {
55 3 my $allow = join(', ', $self->allowed_methods);
56 3 $res = $ctx->response->header('Allow', $allow)->status(405)->text("405 Method Not Allowed");
57 }
58  
59 13 ▶ croak ref($self) . "->$http_method did not return a response"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($res) and $res->can("respond") 1 12 13
Condition66%2/3
builtin::blessed($res) and $res->can("respond")
!ll&&!rl&&r
1012
MC/DC50%1/2
builtin::blessed($res) and $res->can("respond")
builtin::blessed($res) $res->can("respond")
Truth table66%2/3
A and B
A= builtin::blessed($res)B= $res->can("respond")
ABresult
0X0
100
111
60 unless blessed($res) && $res->can('respond');
61 12 await $ctx->respond($res);
62 7 }
63  
64 sub to_app {
65 8 ▶ my ($class) = @_;
Subroutine to_app: called CC=1 SCAR=0.0
Pod documented
66 8 my $context_class = $class->context_class;
67 8 my $endpoint = $class->new; # ONE instance for the app lifetime (singleton)
68  
69 7 ▶ return async sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
70 7 my ($scope, $receive, $send) = @_;
71  
72 7 ▶ my $type = $scope->{type} // 'http';
Condition50%1/2
$scope->{"type"} // "http"
l!l
70
MC/DC0%0/1
$scope->{"type"} // "http"
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
Aresult
11
00
73 7 ▶ croak "Expected http scope, got '$type'" unless $type eq 'http';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "http" 0 7 7
74  
75 7 require PAGI::Context;
76 7 my $ctx = $context_class->new($scope, $receive, $send);
77  
78 7 await $endpoint->dispatch($ctx);
79 8 };
80 }
81  
82 7 1;
83  
84 __END__
« blib/lib/PAGI/Middleware/Rewrite.pm ↑ Summary blib/lib/PAGI/Response.pm »