1 package Web::Simple::DispatchParser;
4 use warnings FATAL => 'all';
9 if ($ENV{WEB_SIMPLE_DISPATCHPARSER_DEBUG}) {
10 no warnings 'redefine';
15 sub diag { if (DEBUG) { warn $_[0] } }
17 sub new { bless({}, ref($_[0])||$_[0]) }
20 my ($self, $error) = @_;
21 my $hat = (' ' x (pos||0)).'^';
22 die "Error parsing dispatch specification: ${error}\n
27 sub parse_dispatch_specification {
28 my ($self, $spec) = @_;
29 return $self->_parse_spec($spec);
33 my ($self, $spec, $nested) = @_;
36 /^\G\s*/; # eat leading whitespace
38 push @match, $self->_parse_spec_section($_)
39 or $self->_blam("Unable to work out what the next section is");
41 $self->_blam("Found closing ) with no opening (") unless $nested;
44 last PARSE if (pos == length);
45 $match[-1] = $self->_parse_spec_combinator($_, $match[-1])
46 or $self->_blam('No valid combinator - expected + or |');
47 } until (pos == length) }; # accept trailing whitespace
48 if ($nested and pos == length) {
50 $self->_blam("No closing ) found for opening (");
52 return $match[0] if (@match == 1);
54 my $env = { %{$_[0]} };
57 foreach my $match (@match) {
58 if (my @this_got = $match->($env)) {
59 my %change_env = %{shift(@this_got)};
60 @{$env}{keys %change_env} = values %change_env;
61 @{$new_env}{keys %change_env} = values %change_env;
67 return ($new_env, @got);
72 sub _parse_spec_combinator {
73 my ($self, $spec, $match) = @_;
83 push @match, $self->_parse_spec_section($_)
84 or $self->_blam("Unable to work out what the next section is");
85 last PARSE if (pos == length);
86 last PARSE unless /\G\|/gc; # give up when next thing isn't |
87 } until (pos == length) }; # accept trailing whitespace
89 foreach my $try (@match) {
90 if (my @ret = $try->(@_)) {
101 sub _parse_spec_section {
105 # GET POST PUT HEAD ...
108 return $self->_http_method_match($_, $1);
113 return $self->_url_path_match($_);
118 return $self->_url_extension_match($_, $1);
123 return $self->_parse_spec($_, pos);
129 my $match = $self->_parse_spec_section($_);
131 return {} unless $match->(@_);
138 return $self->_parse_param_handler($_, 'query');
140 return; # () will trigger the blam in our caller
143 sub _http_method_match {
144 my ($self, $str, $method) = @_;
145 sub { shift->{REQUEST_METHOD} eq $method ? {} : () };
148 sub _url_path_match {
153 PATH: while (/\G\//gc) {
159 push @path, $self->_url_path_segment_match($_)
160 or $self->_blam("Couldn't parse path match segment");
162 my $re = '^()'.join('/','',@path).($full_path ? '$' : '(/.*)$');
166 if (my @cap = (shift->{PATH_INFO} =~ /$re/)) {
167 $cap[0] = {}; return @cap;
173 if (my @cap = (shift->{PATH_INFO} =~ /$re/)) {
174 $cap[0] = { PATH_INFO => pop(@cap) }; return @cap;
182 sub _url_path_segment_match {
185 # trailing / -> require / on end of URL
186 /\G(?:(?=\s)|$)/gc and
188 # word chars only -> exact path part match
191 # ** -> capture unlimited path parts
194 # * -> capture path part
201 sub _url_extension_match {
202 my ($self, $str, $extension) = @_;
203 if ($extension eq '*') {
205 if ((my $tmp = shift->{PATH_INFO}) =~ s/\.(\w+)$//) {
206 ({ PATH_INFO => $tmp }, $1);
213 if ((my $tmp = shift->{PATH_INFO}) =~ s/\.\Q${extension}\E$//) {
214 ({ PATH_INFO => $tmp });
222 sub _parse_param_handler {
223 my ($self, $spec, $type) = @_;
225 require Web::Simple::ParamParser;
226 my $unpacker = Web::Simple::ParamParser->can("get_unpacked_${type}_from");
229 my (@required, @single, %multi, $star, $multistar);
238 /\G\@/gc and $multi = 1;
244 $multi ? ($multistar = 1) : ($star = 1);
246 if ($star && $multistar) {
247 $self->_blam("Can't use * and \@* in the same parameter match");
251 # @foo= or foo= or @foo~ or foo~
253 /\G(\w+)/gc or $self->_blam('Expected parameter name');
257 # check for = or ~ on the end
260 ? push(@required, $name)
261 : (/\G\~/gc or $self->_blam('Expected = or ~ after parameter name'));
263 # record the key in the right category depending on the multi (@) flag
265 $multi ? ($multi{$name} = 1) : (push @single, $name);
270 my $raw = $unpacker->($_[0]);
271 foreach my $name (@required) {
272 return unless exists $raw->{$name};
278 ? (grep { !exists $multi{$_} } keys %$raw)
282 $p{$name} = $raw->{$name}->[-1] if exists $raw->{$name};
287 ? (grep { !exists $p{$_} } keys %$raw)
291 $p{$name} = $raw->{$name}||[];