blib/lib/Plack/Middleware/Method_Allow.pm

filter: Statementstmt 64.2% 18 / 28 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 4 / 6 Podpod 100% 3 / 3 totaltotal 68.8% 31 / 45 CC 5 SCAR 11.9
CC
5
Cov
67%
call
6.0
allow
4.0
prepare_app
2.0
CRAP 3.3
↑ Summary
1 package Plack::Middleware::Method_Allow;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use parent qw{Plack::Middleware};
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.01';
7 1 our $PACKAGE = __PACKAGE__;
8 1 our %ALLOW = ();
9  
10 =head1 NAME
11  
12 Plack::Middleware::Method_Allow - perl Plack Middleware to filter HTTP Methods
13  
14 =head1 SYNOPSIS
15  
16 builder {
17 enable "Plack::Middleware::Method_Allow", allow=>['GET', 'POST'];
18 $app;
19 };
20  
21 =head1 DESCRIPTION
22  
23 Explicitly allow HTTP methods and return 405 METHOD NOT ALLOWED for all others
24  
25 =cut
26  
27 =head1 PROPERTIES
28  
29 =head2 allow
30  
31 Method that sets the allowed HTTP methods. Must be an array reference of strings.
32  
33 =cut
34  
35 11 ▶sub allow {
Subroutine allow: called CC=4 SCAR=13.9
Pod documented
36 11 my $self = shift;
37 11 ▶ $self->{'allow'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 8 3 11
38 11 ▶ $self->{'allow'} = [] unless defined $self->{'allow'}; #default is to deny all
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"allow"} 1 10 11
39 11 ▶ die("Error: Syntax `enable '$PACKAGE', allow=>['METHOD', ...]`") unless ref($self->{'allow'}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"allow"} eq "ARRAY" 7 4 11
40 4 return $self->{'allow'};
41 }
42  
43 =head1 METHODS
44  
45 =head2 prepare_app
46  
47 Method is called once at load to read the allow list.
48  
49 =cut
50  
51 0 ▶sub prepare_app {
Subroutine prepare_app: not called CC=1 SCAR=6.9
Pod documented
52 0 my $self = shift;
53 0 %ALLOW = map {$_ => 1} @{$self->allow};
54 0 return $self;
55 }
56  
57 =head2 call
58  
59 Method is called for each request which return 405 Method Not Allowed for any HTTP method that is not in list.
60  
61 =cut
62  
63 0 ▶sub call {
Subroutine call: not called CC=2 SCAR=17.9
Pod documented
64 0 my $self = shift;
65 0 my $env = shift;
66 0 ▶ if (exists $ALLOW{$env->{'REQUEST_METHOD'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Plack::Middleware::Method_Allow::ALLOW{$env->{'REQUEST_METHOD'}}) { } 0 0 0
67 0 return $self->app->($env);
68 } else {
69 0 return [405 => ['Content-Type' => 'text/plain'] => ['Method Not Allowed']];
70 }
71 }
72  
73 =head1 SEE ALSO
74  
75 L<Plack::Middleware>
76  
77 =head1 AUTHOR
78  
79 Michael R. Davis
80  
81 =head1 COPYRIGHT AND LICENSE
82  
83 MIT License
84  
85 Copyright (c) 2022 Michael R. Davis
86  
87 =cut
88  
89 1 1;
↑ Summary