| 1 | package Web::Dispatch::HTTPMethods; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strictures 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Web::Dispatch::Predicates qw(match_method); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our @EXPORT = qw(GET HEAD POST PUT DELETE OPTIONS); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | sub HEAD(&;@) { method_helper(HEAD => @_) } | ||||||||||||||||||||||||||
|
Subroutine
HEAD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 13 | ▶ | sub GET(&;@) { method_helper(GET => @_) } | ||||||||||||||||||||||||||
|
Subroutine
GET: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | sub POST(&;@) { method_helper(POST => @_) } | ||||||||||||||||||||||||||
|
Subroutine
POST: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | sub PUT(&;@) { method_helper(PUT => @_) } | ||||||||||||||||||||||||||
|
Subroutine
PUT: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub DELETE(&;@) { method_helper(DELETE => @_) } | ||||||||||||||||||||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | sub OPTIONS(&;@) { method_helper(OPTIONS => @_) } | ||||||||||||||||||||||||||
|
Subroutine
OPTIONS: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | { | ||||||||||||||||||||||||||||
| 18 | package Web::Dispatch::HTTPMethods::Endpoint; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 13 | ▶ | sub new { bless { map { $_=>0 } @EXPORT }, shift } | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | sub hdrs { 'Content-Type' => 'text/plain' } | ||||||||||||||||||||||||||
|
Subroutine
hdrs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | sub create_implicit_HEAD { | ||||||||||||||||||||||||||
|
Subroutine
create_implicit_HEAD: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 24 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 25 | 6 | ▶ | if($self->{GET} && not $self->{HEAD}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"GET"} and not $self->{"HEAD"}
MC/DC50%1/2
$self->{"GET"} and not $self->{"HEAD"}
$self->{"GET"}
not $self->{"HEAD"}
Truth table66%2/3
A and not(B)
A= $self->{"GET"}B= not $self->{"HEAD"}
| |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | $self->{HEAD} = sub { [ @{$self->{GET}->(@_)}[0,1], []] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 6 | ▶ | sub create_implicit_OPTIONS { | ||||||||||||||||||||||||||
|
Subroutine
create_implicit_OPTIONS: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 32 | 1 | ▶ | $self->{OPTIONS} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | [200, [$self->hdrs, Allow=>$self->allowed] , [] ]; | |||||||||||||||||||||||||||
| 34 | 6 | }; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | sub allowed { join ',', grep { $_[0]->{$_} } @EXPORT } | ||||||||||||||||||||||||||
|
Subroutine
allowed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 6 | ▶ | sub to_app { | ||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 41 | 6 | my $implicit_HEAD = $self->create_implicit_HEAD; | |||||||||||||||||||||||||||
| 42 | 6 | my $implicit_OPTIONS = $self->create_implicit_OPTIONS; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 6 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 45 | 6 | my $env = shift; | |||||||||||||||||||||||||||
| 46 | 6 | ▶ | if($env->{REQUEST_METHOD} eq 'HEAD') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 47 | 2 | $implicit_HEAD->($env); | |||||||||||||||||||||||||||
| 48 | } elsif($env->{REQUEST_METHOD} eq 'OPTIONS') { | ||||||||||||||||||||||||||||
| 49 | 1 | $implicit_OPTIONS->($env); | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 3 | [405, [$self->hdrs, Allow=>$self->allowed] , ['Method Not Allowed'] ]; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 6 | }; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | sub isa_endpoint { | ||||||||||||||||||||||||||
|
Subroutine
isa_endpoint: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 33 | ▶ | blessed($_[0]) && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | $_[0]->isa('Web::Dispatch::HTTPMethods::Endpoint') | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 33 | ▶ | sub endpoint_from { return $_[-1] } | ||||||||||||||||||||||||||
|
Subroutine
endpoint_from: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 13 | ▶ | sub new_endpoint { Web::Dispatch::HTTPMethods::Endpoint->new(@_) } | ||||||||||||||||||||||||||
|
Subroutine
new_endpoint: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 33 | ▶ | sub method_helper { | ||||||||||||||||||||||||||
|
Subroutine
method_helper: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 33 | my $predicate = match_method(my $method = shift); | |||||||||||||||||||||||||||
| 67 | 33 | my ($code, @following ) = @_; | |||||||||||||||||||||||||||
| 68 | endpoint_from( my @dispatchers = | ||||||||||||||||||||||||||||
| 69 | scalar(@following) ? ($predicate, @_) : ($predicate, @_, new_endpoint) | ||||||||||||||||||||||||||||
| 70 | 33 | ▶ | )->{$method} = $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 33 | ▶ | die "Non HTTP Method dispatcher detected in HTTP Method scope" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | unless(isa_endpoint($dispatchers[-1])); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 33 | return @dispatchers; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | 1; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 NAME | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Web::Dispatch::HTTPMethods - Helpers to make RESTFul Dispatchers Easier | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | package MyApp:WithHTTPMethods; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | use Web::Simple; | ||||||||||||||||||||||||||||
| 90 | use Web::Dispatch::HTTPMethods; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub as_text { | ||||||||||||||||||||||||||||
| 93 | [200, ['Content-Type' => 'text/plain'], | ||||||||||||||||||||||||||||
| 94 | [$_[0]->{REQUEST_METHOD}, $_[0]->{REQUEST_URI}] ] | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub dispatch_request { | ||||||||||||||||||||||||||||
| 98 | sub (/get) { | ||||||||||||||||||||||||||||
| 99 | GET { as_text(pop) } | ||||||||||||||||||||||||||||
| 100 | }, | ||||||||||||||||||||||||||||
| 101 | sub (/get-head) { | ||||||||||||||||||||||||||||
| 102 | GET { as_text(pop) } | ||||||||||||||||||||||||||||
| 103 | HEAD { [204,[],[]] }, | ||||||||||||||||||||||||||||
| 104 | }, | ||||||||||||||||||||||||||||
| 105 | sub (/get-post-put) { | ||||||||||||||||||||||||||||
| 106 | GET { as_text(pop) } ## NOTE: no commas separating http methods | ||||||||||||||||||||||||||||
| 107 | POST { as_text(pop) } | ||||||||||||||||||||||||||||
| 108 | PUT { as_text(pop) } | ||||||||||||||||||||||||||||
| 109 | }, | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Exports the most commonly used HTTP methods as subroutine helpers into your | ||||||||||||||||||||||||||||
| 115 | L<Web::Simple> based application. | ||||||||||||||||||||||||||||
| 116 | Use of these methods additionally adds an automatic HTTP code 405 | ||||||||||||||||||||||||||||
| 117 | C<Method Not Allowed> response if none of the HTTP methods match for a given dispatch and | ||||||||||||||||||||||||||||
| 118 | also adds a dispatch rule for C<HEAD> if no C<HEAD> exists but a C<GET> does | ||||||||||||||||||||||||||||
| 119 | (in which case the C<HEAD> returns the C<GET> dispatch with an empty body.) | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | We also add support at the end of the chain for the OPTIONS method. | ||||||||||||||||||||||||||||
| 122 | This defaults to HTTP 200 OK + Allows http headers. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | We also try to set correct HTTP headers such as C<Allows> as makes sense based | ||||||||||||||||||||||||||||
| 125 | on your dispatch chain. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | The following dispatch chains are basically the same: | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub dispatch_request { | ||||||||||||||||||||||||||||
| 130 | sub (/get-http-methods) { | ||||||||||||||||||||||||||||
| 131 | GET { [200, ['Content-Type' => 'text/plain'], ['Hello World']] } | ||||||||||||||||||||||||||||
| 132 | }, | ||||||||||||||||||||||||||||
| 133 | sub(/get-classic) { | ||||||||||||||||||||||||||||
| 134 | sub (GET) { [200, ['Content-Type' => 'text/plain'], ['Hello World']] }, | ||||||||||||||||||||||||||||
| 135 | sub (HEAD) { [200, ['Content-Type' => 'text/plain'], []] }, | ||||||||||||||||||||||||||||
| 136 | sub (OPTIONS) { | ||||||||||||||||||||||||||||
| 137 | [200, ['Content-Type' => 'text/plain', Allows=>'GET,HEAD,OPTIONS'], []]; | ||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | sub () { | ||||||||||||||||||||||||||||
| 140 | [405, ['Content-Type' => 'text/plain', Allows=>'GET,HEAD,OPTIONS'], | ||||||||||||||||||||||||||||
| 141 | ['Method Not Allowed']] | ||||||||||||||||||||||||||||
| 142 | }, | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | The idea here is less boilerplate to distract the reader from the main point of | ||||||||||||||||||||||||||||
| 147 | the code and also to encapsulate some best practices. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | B<NOTE> You currently cannot mix http method style and prototype sub style in | ||||||||||||||||||||||||||||
| 150 | the same scope, as in the following example: | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub dispatch_request { | ||||||||||||||||||||||||||||
| 153 | sub (/get-head) { | ||||||||||||||||||||||||||||
| 154 | GET { ... } | ||||||||||||||||||||||||||||
| 155 | sub (HEAD) { ... } | ||||||||||||||||||||||||||||
| 156 | }, | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | If you try this our code will notice and issue a C<die>. If you have a good use | ||||||||||||||||||||||||||||
| 160 | case please bring it to the authors. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 EXPORTS | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | This automatically exports the following subroutines: | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | GET | ||||||||||||||||||||||||||||
| 167 | PUT | ||||||||||||||||||||||||||||
| 168 | POST | ||||||||||||||||||||||||||||
| 169 | HEAD | ||||||||||||||||||||||||||||
| 170 | DELETE | ||||||||||||||||||||||||||||
| 171 | OPTIONS | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | See L<Web::Simple> for AUTHOR | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | See L<Web::Simple> for CONTRIBUTORS | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | See L<Web::Simple> for COPYRIGHT | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | See L<Web::Simple> for LICENSE | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||