1 package Web::Dispatch::Predicates;
7 match_and match_or match_not match_method match_path match_path_strip
8 match_extension match_query match_body match_uploads match_true match_false
11 sub _matcher { bless shift, 'Web::Dispatch::Matcher' }
25 my $my_env = { 'Web::Dispatch.original_env' => $env, %$env };
28 foreach my $match (@match) {
29 if (my @this_got = $match->($my_env)) {
30 my %change_env = %{shift(@this_got)};
31 @{$my_env}{keys %change_env} = values %change_env;
32 @{$new_env}{keys %change_env} = values %change_env;
38 return ($new_env, @got);
45 foreach my $try (@match) {
46 if (my @ret = $try->(@_)) {
57 if (my @discard = $match->($_[0])) {
69 $env->{REQUEST_METHOD} eq $method ? {} : ()
77 if (my @cap = ($env->{PATH_INFO} =~ /$re/)) {
78 $cap[0] = {}; return @cap;
84 sub match_path_strip {
88 if (my @cap = ($env->{PATH_INFO} =~ /$re/)) {
90 SCRIPT_NAME => ($env->{SCRIPT_NAME}||'').$cap[0],
91 PATH_INFO => pop(@cap),
100 my ($extension) = @_;
101 my $wild = (!$extension or $extension eq '*');
104 : qr/\.(\Q${extension}\E)$/;
106 if ($_[0]->{PATH_INFO} =~ $re) {
107 ($wild ? ({}, $1) : {});
115 _matcher(_param_matcher(query => $_[0]));
119 _matcher(_param_matcher(body => $_[0]));
123 _matcher(_param_matcher(uploads => $_[0]));
127 my ($type, $spec) = @_;
128 # We're probably parsing a match spec while building the parser, and
129 # on 5.8.8, loading ParamParser loads Encode which blows away $_ and pos.
130 # Furthermore, localizing $_ doesn't restore pos afterwards. So do this
131 # stupid thing instead to work on 5.8.8
135 require Web::Dispatch::ParamParser;
138 my $unpack = Web::Dispatch::ParamParser->can("get_unpacked_${type}_from");
140 _extract_params($unpack->($_[0]), $spec)
144 sub _extract_params {
145 my ($raw, $spec) = @_;
146 foreach my $name (@{$spec->{required}||[]}) {
147 return unless exists $raw->{$name};
152 $_->{multi} ? $raw->{$_->{name}}||[] : $raw->{$_->{name}}->[-1]
153 } @{$spec->{positional}||[]}
155 # separated since 'or' is short circuit
156 my ($named, $star) = ($spec->{named}, $spec->{star});
157 if ($named or $star) {
163 : map $_->[-1], values %$raw
166 foreach my $n (@{$named||[]}) {
167 next if !$n->{multi} and !exists $raw->{$n->{name}};
169 $n->{multi} ? $raw->{$n->{name}}||[] : $raw->{$n->{name}}->[-1];