blib/lib/PAGI/Endpoint/HTTP.pm

filter: Statementstmt 100% 57 / 57 Branchbran 81.2% 13 / 16 Conditioncond 56.2% 9 / 16 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 12 / 12 Podpod 80.0% 4 / 5 totaltotal 84.3% 97 / 115 CC 16 SCAR 16.4
CC
16
Cov
89%
dispatch
9.2
allowed_methods
6.5
__ANON__
3.1
CRAP 5.2
« lib/PAGI/App/WebSocket/Echo.pm ↑ Summary lib/PAGI/Middleware/CORS.pm »
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
BranchTrueFalseTotal
if $self->can($method) 13 29 42
27 }
28 # HEAD is allowed if GET is defined
29 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
30 # OPTIONS is always allowed
31 6 ▶ push @allowed, 'OPTIONS' unless grep { $_ eq 'OPTIONS' } @allowed;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "OPTIONS";} @allowed 6 0 6
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"
l!l
130
MC/DC0%0/1
$ctx->method // "GET"
$ctx->method
Truth table50%1/2
A
A= $ctx->method
Aresult
11
00
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
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
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")
!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
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
BranchTrueFalseTotal
unless is_response($res) 1 12 13
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"
l!l
70
MC/DC0%0/1
$scope->{"type"} // "http"
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
Aresult
11
00
74 7 ▶ croak "Expected http scope, got '$type'" unless $type eq 'http';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "http" 0 7 7
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__
« lib/PAGI/App/WebSocket/Echo.pm ↑ Summary lib/PAGI/Middleware/CORS.pm »