| 1 | package Router::Simple::Route; | ||||||||||||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Class::Accessor::Lite 0.05 ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 13 | rw => [qw(name dest on_match method host pattern)], | |||||||||||||||||||||||||||
| 8 | 13 | ); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 44 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=16 SCAR=27.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 44 | my $class = shift; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # connect([$name, ]$pattern[, \%dest[, \%opt]]) | ||||||||||||||||||||||||||||
| 14 | 44 | ▶ | if (@_ == 1 || ref $_[1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 or ref $_[1]
MC/DC100%2/2
@_ == 1 or ref $_[1]
@_ == 1
ref $_[1]
Truth table100%3/3
A or B
A= @_ == 1B= ref $_[1]
| |||||||||||||||||||||||||||||
| 15 | 35 | unshift(@_, undef); | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 44 | my ($name, $pattern, $dest, $opt) = @_; | |||||||||||||||||||||||||||
| 19 | 44 | ▶ | Carp::croak("missing pattern") unless $pattern; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | my $row = +{ | ||||||||||||||||||||||||||||
| 21 | name => $name, | ||||||||||||||||||||||||||||
| 22 | dest => $dest, | ||||||||||||||||||||||||||||
| 23 | on_match => $opt->{on_match}, | ||||||||||||||||||||||||||||
| 24 | 44 | }; | |||||||||||||||||||||||||||
| 25 | 44 | ▶ | if (my $method = $opt->{method}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 23 | ▶ | $method = [$method] unless ref $method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 23 | $row->{method} = $method; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 23 | my $method_re = join '|', @{$method}; | |||||||||||||||||||||||||||
| 30 | 23 | $row->{method_re} = qr{^(?:$method_re)\z}; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 44 | ▶ | if (my $host = $opt->{host}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 6 | $row->{host} = $host; | |||||||||||||||||||||||||||
| 34 | 6 | ▶ | $row->{host_re} = ref $host ? $host : qr(^\Q$host\E\z); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 44 | $row->{pattern} = $pattern; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # compile pattern | ||||||||||||||||||||||||||||
| 40 | 44 | my @capture; | |||||||||||||||||||||||||||
| 41 | 44 | $row->{pattern_re} = do { | |||||||||||||||||||||||||||
| 42 | 44 | ▶ | if (ref $pattern) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 2 | $row->{_regexp_capture} = 1; | |||||||||||||||||||||||||||
| 44 | 2 | $pattern; | |||||||||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||||||||||
| 46 | 42 | $pattern =~ s! | |||||||||||||||||||||||||||
| 47 | \{((?:\{[0-9,]+\}|[^{}]+)+)\} | # /blog/{year:\d{4}} | ||||||||||||||||||||||||||||
| 48 | :([A-Za-z0-9_]+) | # /blog/:year | ||||||||||||||||||||||||||||
| 49 | (\*) | # /blog/*/* | ||||||||||||||||||||||||||||
| 50 | ([^{:*]+) # normal string | ||||||||||||||||||||||||||||
| 51 | ! | ||||||||||||||||||||||||||||
| 52 | 108 | ▶ | if ($1) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 53 | 30 | my ($name, $pattern) = split /:/, $1, 2; | |||||||||||||||||||||||||||
| 54 | 30 | push @capture, $name; | |||||||||||||||||||||||||||
| 55 | 30 | ▶ | $pattern ? "($pattern)" : "([^/]+)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | } elsif ($2) { | ||||||||||||||||||||||||||||
| 57 | 7 | push @capture, $2; | |||||||||||||||||||||||||||
| 58 | 7 | "([^/]+)"; | |||||||||||||||||||||||||||
| 59 | } elsif ($3) { | ||||||||||||||||||||||||||||
| 60 | 4 | push @capture, '__splat__'; | |||||||||||||||||||||||||||
| 61 | 4 | "(.+)"; | |||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 67 | quotemeta($4); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | !gex; | ||||||||||||||||||||||||||||
| 66 | # for example, pattern '/comment/' will both match '/comment/' and '/comment' | ||||||||||||||||||||||||||||
| 67 | 42 | ▶ | $pattern .= '?' if $opt->{directory_slash} and $pattern =~ m{\/$}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opt->{"directory_slash"} and $pattern =~ m[\/$]
MC/DC50%1/2
$opt->{"directory_slash"} and $pattern =~ m[\/$]
$opt->{"directory_slash"}
$pattern =~ m[\/$]
Truth table66%2/3
A and B
A= $opt->{"directory_slash"}B= $pattern =~ m[\/$]
| |||||||||||||||||||||||||||||
| 68 | 42 | qr{^$pattern\z}; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | }; | ||||||||||||||||||||||||||||
| 71 | 44 | $row->{capture} = \@capture; | |||||||||||||||||||||||||||
| 72 | 44 | ▶ | $row->{dest} ||= +{}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$row->{"dest"} ||= {}
MC/DC100%1/1
$row->{"dest"} ||= {}
$row->{"dest"}
Truth table100%2/2
A
A= $row->{"dest"}
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 44 | return bless $row, $class; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 141 | ▶ | sub match { | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=15 SCAR=27.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 141 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 141 | ▶ | if ($self->{host_re}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 20 | ▶ | unless ($env->{HTTP_HOST} =~ $self->{host_re}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 5 | return undef; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 136 | ▶ | if (my @captured = ($env->{PATH_INFO} =~ $self->{pattern_re})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 48 | my %args; | |||||||||||||||||||||||||||
| 87 | my @splat; | ||||||||||||||||||||||||||||
| 88 | 48 | ▶ | if ($self->{_regexp_capture}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 1 | push @splat, @captured; | |||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||
| 91 | 47 | ▶ | if (@{$self->{capture}} > 0 && scalar(@{$self->{capture}}) != scalar(@captured)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$self->{"capture"};} > 0 and scalar @{$self->{"capture"};} != scalar @captured
MC/DC100%2/2
@{$self->{"capture"};} > 0 and scalar @{$self->{"capture"};} != scalar @captured
@{$self->{"capture"};} > 0
scalar @{$self->{"capture"};} != scalar @captured
Truth table100%3/3
A and B
A= @{$self->{"capture"};} > 0B= scalar @{$self->{"capture"};} != scalar @captured
| |||||||||||||||||||||||||||||
| 92 | # Should not contain parenthesis in regexp pattern | ||||||||||||||||||||||||||||
| 93 | # | ||||||||||||||||||||||||||||
| 94 | # Good: "/{date:(?:\d+)}" | ||||||||||||||||||||||||||||
| 95 | # Bad: "/{date:(\d+)}" | ||||||||||||||||||||||||||||
| 96 | 1 | Carp::carp("Path pattern should not contain paren. This code may not works in future version of Router::Simple. : " . $self->{pattern}); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 47 | for my $i (0..@{$self->{capture}}-1) { | |||||||||||||||||||||||||||
| 100 | 50 | ▶ | if ($self->{capture}->[$i] eq '__splat__') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 4 | push @splat, $captured[$i]; | |||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||
| 103 | 46 | $args{$self->{capture}->[$i]} = $captured[$i]; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 48 | ▶ | if ($self->{method_re}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 30 | ▶ | unless (($env->{REQUEST_METHOD} || '') =~ $self->{method_re}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$env->{'REQUEST_METHOD'} || ''
MC/DC0%0/1
$env->{'REQUEST_METHOD'} || ''
$env->{'REQUEST_METHOD'}
Truth table50%1/2
A
A= $env->{'REQUEST_METHOD'}
| |||||||||||||||||||||||||||||
| 109 | 9 | $Router::Simple::_METHOD_NOT_ALLOWED = 1; | |||||||||||||||||||||||||||
| 110 | 9 | return undef; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | my $match = +{ | ||||||||||||||||||||||||||||
| 114 | 39 | ▶ | %{$self->{dest}}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | %args, | ||||||||||||||||||||||||||||
| 116 | ( @splat ? ( splat => \@splat ) : () ), | ||||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||
| 118 | 39 | ▶ | if ($self->{on_match}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 3 | my $ret = $self->{on_match}->($env, $match); | |||||||||||||||||||||||||||
| 120 | 3 | ▶ | return undef unless $ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 38 | return $match; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 88 | return undef; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 13 | 1; | |||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||