| 1 | package PAGI::Middleware::MethodOverride; | ||||||||||||||||||||
| 2 | 1 | $PAGI::Middleware::MethodOverride::VERSION = '0.002002'; | |||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use parent 'PAGI::Middleware'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | PAGI::Middleware::MethodOverride - Override HTTP method from request data | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | use PAGI::Middleware::Builder; | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | my $app = builder { | ||||||||||||||||||||
| 17 | enable 'MethodOverride', | ||||||||||||||||||||
| 18 | param => '_method', | ||||||||||||||||||||
| 19 | allowed_methods => [qw(PUT PATCH DELETE)]; | ||||||||||||||||||||
| 20 | $my_app; | ||||||||||||||||||||
| 21 | }; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | PAGI::Middleware::MethodOverride allows overriding the HTTP method | ||||||||||||||||||||
| 26 | using a form field, query parameter, or header. This enables HTML | ||||||||||||||||||||
| 27 | forms (which only support GET and POST) to submit PUT, PATCH, and | ||||||||||||||||||||
| 28 | DELETE requests. | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head1 CONFIGURATION | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =over 4 | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =item * param (default: '_method') | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | Form field or query parameter name for method override. | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =item * header (default: 'X-HTTP-Method-Override') | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | HTTP header name for method override. | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =item * allowed_methods (default: [PUT, PATCH, DELETE]) | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | Methods that can be overridden to. GET and POST are not allowed | ||||||||||||||||||||
| 45 | for security reasons. | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =item * check_header (default: 1) | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | Check the X-HTTP-Method-Override header. | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =item * check_param (default: 1) | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | Check the _method query/form parameter. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =back | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 4 | ▶ | sub _init { | ||||||||||||||||||
|
Subroutine
_init: called CC=6 SCAR=18.5
| |||||||||||||||||||||
| 60 | 4 | my ($self, $config) = @_; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 4 | ▶ | $self->{param} = $config->{param} // '_method'; | ||||||||||||||||||
|
Condition50%1/2
$config->{"param"} // "_method"
MC/DC0%0/1
$config->{"param"} // "_method"
$config->{"param"}
Truth table50%1/2
A
A= $config->{"param"}
| |||||||||||||||||||||
| 63 | 4 | ▶ | $self->{header} = $config->{header} // 'x-http-method-override'; | ||||||||||||||||||
|
Condition50%1/2
$config->{"header"} // "x-http-method-override"
MC/DC0%0/1
$config->{"header"} // "x-http-method-override"
$config->{"header"}
Truth table50%1/2
A
A= $config->{"header"}
| |||||||||||||||||||||
| 64 | 4 | ▶ | $self->{allowed_methods} = $config->{allowed_methods} // [qw(PUT PATCH DELETE)]; | ||||||||||||||||||
|
Condition100%2/2
$config->{"allowed_methods"} // ["PUT", "PATCH", "DELETE"]
MC/DC100%1/1
$config->{"allowed_methods"} // ["PUT", "PATCH", "DELETE"]
$config->{"allowed_methods"}
Truth table100%2/2
A
A= $config->{"allowed_methods"}
| |||||||||||||||||||||
| 65 | 4 | ▶ | $self->{check_header} = $config->{check_header} // 1; | ||||||||||||||||||
|
Condition50%1/2
$config->{"check_header"} // 1
MC/DC0%0/1
$config->{"check_header"} // 1
$config->{"check_header"}
Truth table50%1/2
A
A= $config->{"check_header"}
| |||||||||||||||||||||
| 66 | 4 | ▶ | $self->{check_param} = $config->{check_param} // 1; | ||||||||||||||||||
|
Condition50%1/2
$config->{"check_param"} // 1
MC/DC0%0/1
$config->{"check_param"} // 1
$config->{"check_param"}
Truth table50%1/2
A
A= $config->{"check_param"}
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # Build allowed method lookup | ||||||||||||||||||||
| 69 | 4 | $self->{allowed_lookup} = { map { uc($_) => 1 } @{$self->{allowed_methods}} }; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 4 | ▶ | sub wrap { | ||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||
| 73 | 4 | my ($self, $app) = @_; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 4 | ▶ | return async sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.3
| |||||||||||||||||||||
| 76 | 4 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||
| 77 | 4 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||
| 79 | 0 | return; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # Only apply to POST requests | ||||||||||||||||||||
| 83 | 4 | ▶ | if (uc($scope->{method} // '') ne 'POST') { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$scope->{"method"} // ""
MC/DC0%0/1
$scope->{"method"} // ""
$scope->{"method"}
Truth table50%1/2
A
A= $scope->{"method"}
| |||||||||||||||||||||
| 84 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||
| 85 | 1 | return; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 3 | my $override_method = $self->_get_override_method($scope); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 3 | ▶ | if ($override_method) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | # Validate method is allowed | ||||||||||||||||||||
| 92 | 3 | my $upper_method = uc($override_method); | |||||||||||||||||||
| 93 | 3 | ▶ | if ($self->{allowed_lookup}{$upper_method}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | # Create new scope with overridden method | ||||||||||||||||||||
| 95 | my $new_scope = $self->modify_scope($scope, { | ||||||||||||||||||||
| 96 | method => $upper_method, | ||||||||||||||||||||
| 97 | original_method => $scope->{method}, | ||||||||||||||||||||
| 98 | 2 | }); | |||||||||||||||||||
| 99 | 2 | await $app->($new_scope, $receive, $send); | |||||||||||||||||||
| 100 | 2 | return; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||
| 105 | 4 | }; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 3 | ▶ | sub _get_override_method { | ||||||||||||||||||
|
Subroutine
_get_override_method: called CC=11 SCAR=26.2
| |||||||||||||||||||||
| 109 | 3 | my ($self, $scope) = @_; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | # Check header first (most secure) | ||||||||||||||||||||
| 112 | 3 | ▶ | if ($self->{check_header}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 3 | my $header_name = lc($self->{header}); | |||||||||||||||||||
| 114 | 3 | ▶ | for my $h (@{$scope->{headers} // []}) { | ||||||||||||||||||
|
Condition50%1/2
$scope->{"headers"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||
| 115 | 2 | ▶ | if (lc($h->[0]) eq $header_name) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 2 | return $h->[1]; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # Check query parameter | ||||||||||||||||||||
| 122 | 1 | ▶ | if ($self->{check_param}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 1 | ▶ | my $query = $scope->{query_string} // ''; | ||||||||||||||||||
|
Condition50%1/2
$scope->{"query_string"} // ""
MC/DC0%0/1
$scope->{"query_string"} // ""
$scope->{"query_string"}
Truth table50%1/2
A
A= $scope->{"query_string"}
| |||||||||||||||||||||
| 124 | 1 | my $param_name = $self->{param}; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | # Simple query string parsing | ||||||||||||||||||||
| 127 | 1 | for my $pair (split /&/, $query) { | |||||||||||||||||||
| 128 | 1 | my ($key, $value) = split /=/, $pair, 2; | |||||||||||||||||||
| 129 | 1 | ▶ | $key //= ''; | ||||||||||||||||||
|
Condition50%1/2
$key //= ""
MC/DC0%0/1
$key //= ""
$key
Truth table50%1/2
A
A= $key
| |||||||||||||||||||||
| 130 | 1 | ▶ | $value //= ''; | ||||||||||||||||||
|
Condition50%1/2
$value //= ""
MC/DC0%0/1
$value //= ""
$value
Truth table50%1/2
A
A= $value
| |||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | # URL decode | ||||||||||||||||||||
| 133 | 1 | $key =~ s/\+/ /g; | |||||||||||||||||||
| 134 | 0 | $key =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge; | |||||||||||||||||||
| 135 | 1 | $value =~ s/\+/ /g; | |||||||||||||||||||
| 136 | 0 | $value =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 1 | ▶ | if ($key eq $param_name) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 1 | return $value; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 0 | return; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 1 | 1; | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||