| 1 | package Catalyst::ActionRole::Methods; | ||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose::Role; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '0.104'; | |||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||
| 7 | 0 | ▶ | around 'list_extra_info' => sub { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||
| 8 | 0 | my $orig = shift; | |||||||||||||||||||||||||
| 9 | 0 | my $self = shift; | |||||||||||||||||||||||||
| 10 | 0 | my $info = $self->$orig( @_ ); | |||||||||||||||||||||||||
| 11 | 0 | $info->{'HTTP_METHOD'} = [ $self->get_allowed_methods( $self->class, undef, $self->name ) ]; | |||||||||||||||||||||||||
| 12 | 0 | $info; | |||||||||||||||||||||||||
| 13 | 1 | }; | |||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||
| 15 | 6 | ▶ | around 'dispatch', sub { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||
| 16 | 6 | my $orig = shift; | |||||||||||||||||||||||||
| 17 | 6 | my $self = shift; | |||||||||||||||||||||||||
| 18 | 6 | my $c = shift; | |||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||
| 20 | 6 | my $return = $self->$orig($c, @_); | |||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||
| 22 | 6 | my $class = $self->class; | |||||||||||||||||||||||||
| 23 | 6 | my $controller = $c->component( $class ); | |||||||||||||||||||||||||
| 24 | 6 | my $method_name = $self->name; | |||||||||||||||||||||||||
| 25 | 6 | my $req_method = $c->request->method; | |||||||||||||||||||||||||
| 26 | 6 | my $suffix = uc $req_method; | |||||||||||||||||||||||||
| 27 | 6 | my ( $rest_method, $code ); | |||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||
| 30 | 6 | $rest_method = $method_name . '_' . $suffix; | |||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||
| 32 | 9 | ▶ | if ( $code = $controller->action_for( $rest_method ) ) { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 33 | 2 | my $sub_return = $c->forward( $code, $c->request->args ); | |||||||||||||||||||||||||
| 34 | 2 | ▶ | return defined $sub_return ? $sub_return : $return; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 35 | } elsif ( $code = $controller->can( $rest_method ) ) { | ||||||||||||||||||||||||||
| 36 | # nothing to do | ||||||||||||||||||||||||||
| 37 | } elsif ( 'OPTIONS' eq $suffix ) { | ||||||||||||||||||||||||||
| 38 | 1 | $c->response->status( 204 ); | |||||||||||||||||||||||||
| 39 | } elsif ( 'HEAD' eq $suffix ) { | ||||||||||||||||||||||||||
| 40 | 1 | $suffix = 'GET'; | |||||||||||||||||||||||||
| 41 | 1 | redo; | |||||||||||||||||||||||||
| 42 | } elsif ( 'not_implemented' eq $suffix ) { | ||||||||||||||||||||||||||
| 43 | 0 | ( my $enc_req_method = $req_method ) =~ s[(["'&<>])]{ '&#'.(ord $1).';' }ge; | |||||||||||||||||||||||||
| 44 | 1 | $c->response->status( 405 ); | |||||||||||||||||||||||||
| 45 | 1 | $c->response->content_type( 'text/html' ); | |||||||||||||||||||||||||
| 46 | 1 | $c->response->body( | |||||||||||||||||||||||||
| 47 | '<!DOCTYPE html><title>405 Method Not Allowed</title>' | ||||||||||||||||||||||||||
| 48 | . "<p>The requested method $enc_req_method is not allowed for this URL.</p>" | ||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||
| 51 | 2 | $suffix = 'not_implemented'; | |||||||||||||||||||||||||
| 52 | 2 | redo; | |||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||
| 56 | 4 | ▶ | if ( not $code ) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 57 | 2 | my @allowed = $self->get_allowed_methods( $class, $c, $method_name ); | |||||||||||||||||||||||||
| 58 | 2 | ▶ | $c->response->header( Allow => @allowed ? \@allowed : '' ); | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||
| 61 | # localise stuff so we can dispatch the action 'as normal, but get | ||||||||||||||||||||||||||
| 62 | # different stats shown, and different code run. | ||||||||||||||||||||||||||
| 63 | # Also get the full path for the action, and make it look like a forward | ||||||||||||||||||||||||||
| 64 | 4 | ▶ | local $self->{'code'} = $code || sub {}; | ||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Condition66%2/3
$code || sub {
}
MC/DC0%0/2
$code || sub {
}
$code
sub {
}
Truth table66%2/3
A || B
A= $codeB= sub {
}
| |||||||||||||||||||||||||||
| 65 | 4 | ( local $self->{'reverse'} = "-> $self->{'reverse'}" ) =~ s{[^/]+\z}{$rest_method}; | |||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||
| 67 | 4 | my $sub_return = $c->execute( $class, $self, @{ $c->request->args } ); | |||||||||||||||||||||||||
| 68 | 4 | ▶ | defined $sub_return ? $sub_return : $return; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 69 | 1 | }; | |||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub get_allowed_methods { | ||||||||||||||||||||||||
|
Subroutine
get_allowed_methods: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||
| 72 | 2 | my ( $self, $controller, $c, $name ) = @_; | |||||||||||||||||||||||||
| 73 | 2 | ▶ | my $class = ref $controller || $controller; # backcompat | ||||||||||||||||||||||||
|
Condition33%1/3
ref $controller || $controller
MC/DC0%0/2
ref $controller || $controller
ref $controller
$controller
Truth table33%1/3
A || B
A= ref $controllerB= $controller
| |||||||||||||||||||||||||||
| 74 | 2 | my %methods = map /^\Q$name\E\_(.+)()$/, $class->meta->get_all_method_names; | |||||||||||||||||||||||||
| 75 | 2 | ▶ | $methods{'HEAD'} = 1 if exists $methods{'GET'}; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 76 | 2 | delete $methods{'not_implemented'}; | |||||||||||||||||||||||||
| 77 | 2 | sort keys %methods; | |||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||
| 80 | 1 | 1; | |||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||