forked from tokuhirom/p5-httpx-dispatcher
/
Rule.pm
158 lines (126 loc) · 3.72 KB
/
Rule.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package HTTPx::Dispatcher::Rule;
use strict;
use warnings;
use base qw/Class::Accessor::Fast/;
use Scalar::Util qw/blessed/;
use Carp;
__PACKAGE__->mk_accessors(qw/re pattern controller action capture requirements conditions name/);
sub new {
my ($class, $pattern, $args, $method) = @_;
$args ||= {};
$args->{conditions} ||= {};
$args->{method} = $method || 'get';
my $self = bless { %$args }, $class;
$self->compile($pattern);
$self;
}
# compile url pattern to regex.
# articles/:year/:month => qr{articles/(.+)/(.+)}
sub compile {
my ($self, $pattern) = @_;
# from URI Templates to url pattern
# articles/{year}/{month} => articles/:year/:month
$pattern =~ s/{(\w+)}/:$1/g;
# allow slash (eg. '/articles')
$pattern =~ s!^/+!!;
$self->pattern( $pattern );
# emulate named capture
my @capture;
$pattern =~ s{:([a-z0-9_]+)}{
push @capture, $1;
'(.+)'
}ge;
$self->re( qr{^$pattern$} );
$self->capture( \@capture );
}
sub match {
my ($self, $req) = @_;
croak "request required" unless blessed $req;
if( $self->{method} && $req->method ){
return if $req->method ne uc $self->{method};
}
my $uri = ref($req->uri) ? $req->uri->path : $req->uri;
$uri =~ s!^/+!!;
return unless $self->_condition_check( $req );
if ($uri =~ $self->re) {
my @last_match_start = @-; # backup perlre vars
my @last_match_end = @+;
my $response = {};
for my $key (qw/action controller/) {
$response->{$key} = $self->{$key} if $self->{$key};
}
my $requirements = $self->requirements;
my $cnt = 1;
for my $key (@{ $self->capture }) {
$response->{$key} = substr($uri, $last_match_start[$cnt], $last_match_end[$cnt] - $last_match_start[$cnt]);
# validate
# XXX this function needs test.
if ( exists( $requirements->{$key} )
&& !( $response->{$key} =~ $requirements->{$key} ) )
{
die "invalid args: $response->{$key} ( $key ) does not matched $requirements->{$key}";
}
$cnt++;
}
return $self->_filter_response( $response );
} else {
return;
}
}
sub _filter_response {
my ($self, $input) = @_;
my $output = {};
for my $key (qw/controller action/) {
$output->{$key} = delete $input->{$key} or croak "missing $key";
}
$output->{args} = $input;
return $output;
}
sub _condition_check {
my ($self, $req) = @_;
$self->_condition_check_method($req) && $self->_condition_check_function($req);
}
sub _condition_check_method {
my ($self, $req) = @_;
croak "request required" unless blessed $req;
my $method = $self->conditions->{method};
return 1 unless $method;
$method = [ $method ] unless ref $method;
if (grep { uc $req->method eq uc $_} @$method) {
return 1;
} else {
return 0;
}
}
sub _condition_check_function {
my ($self, $req) = @_;
croak "request required" unless blessed $req;
my $function = $self->conditions->{function};
return 1 unless $function;
local $_ = $req;
if ( $function->( $req ) ) {
return 1;
} else {
return 0;
}
}
sub uri_for {
my ($self, $args) = @_;
my $uri = $self->pattern;
my %args = %$args;
while (my ($key, $val) = each %args) {
$uri = $self->_uri_for_match($uri, $key, $val);
return unless defined $uri;
}
return "/$uri";
}
sub _uri_for_match {
my ($self, $uri, $key, $val) = @_;
if ($self->{$key} && $self->{$key} eq $val) { return $uri }
if ($uri =~ s{:$key}{$val}) {
return $uri;
} else {
return;
}
}
1;