factor dispatch parser out
Matt S Trout [Fri, 19 Nov 2010 14:28:58 +0000 (14:28 +0000)]
Makefile.PL
lib/Web/Dispatch/Parser.pm [moved from lib/Web/Simple/DispatchParser.pm with 82% similarity]
lib/Web/Dispatch/Predicates.pm [new file with mode: 0644]
lib/Web/Simple/Application.pm
t/dispatch_parser.t

index 7d03492..5159ab5 100644 (file)
@@ -5,5 +5,6 @@ use inc::Module::Install 0.91;
 all_from 'lib/Web/Simple.pm';
 requires 'Syntax::Keyword::Gather';
 requires 'Plack';
+requires 'Moo';
 
 WriteAll;
similarity index 82%
rename from lib/Web/Simple/DispatchParser.pm
rename to lib/Web/Dispatch/Parser.pm
index b580a91..ae9aa23 100644 (file)
@@ -1,20 +1,23 @@
-package Web::Simple::DispatchParser;
-
-use strict;
-use warnings FATAL => 'all';
+package Web::Dispatch::Parser;
 
 sub DEBUG () { 0 }
 
 BEGIN {
-  if ($ENV{WEB_SIMPLE_DISPATCHPARSER_DEBUG}) {
+  if ($ENV{WEB_DISPATCH_PARSER_DEBUG}) {
     no warnings 'redefine';
     *DEBUG = sub () { 1 }
   }
 }
 
-sub diag { if (DEBUG) { warn $_[0] } }
+use Sub::Quote;
+use Web::Dispatch::Predicates;
+use Moo;
 
-sub new { bless({}, ref($_[0])||$_[0]) }
+has _cache => (
+  is => 'lazy', default => quote_sub q{ {} }
+);
+
+sub diag { if (DEBUG) { warn $_[0] } }
 
 sub _blam {
   my ($self, $error) = @_;
@@ -24,9 +27,9 @@ ${_}
 ${hat} here\n";
 }
 
-sub parse_dispatch_specification {
+sub parse {
   my ($self, $spec) = @_;
-  return $self->_parse_spec($spec);
+  return $self->_cache->{$spec} ||= $self->_parse_spec($spec);
 }
 
 sub _parse_spec {
@@ -50,22 +53,7 @@ sub _parse_spec {
       $self->_blam("No closing ) found for opening (");
     }
     return $match[0] if (@match == 1);
-    return sub {
-      my $env = { %{$_[0]} };
-      my $new_env;
-      my @got;
-      foreach my $match (@match) {
-        if (my @this_got = $match->($env)) {
-          my %change_env = %{shift(@this_got)};
-          @{$env}{keys %change_env} = values %change_env;
-          @{$new_env}{keys %change_env} = values %change_env;
-          push @got, @this_got;
-        } else {
-          return;
-        }
-      }
-      return ($new_env, @got);
-    };
+    return match_and(@match);
   }
 }
 
@@ -85,14 +73,7 @@ sub _parse_spec_combinator {
           last PARSE if (pos == length);
           last PARSE unless /\G\|/gc; # give up when next thing isn't |
         } until (pos == length) }; # accept trailing whitespace
-        return sub {
-          foreach my $try (@match) {
-            if (my @ret = $try->(@_)) {
-              return @ret;
-            }
-          }
-          return;
-        };
+        return match_or(@match);
       };
   }
   return;
@@ -146,7 +127,7 @@ sub _parse_spec_section {
 
 sub _http_method_match {
   my ($self, $str, $method) = @_;
-  sub { shift->{REQUEST_METHOD} eq $method ? {} : () };
+  match_method($method);
 }
 
 sub _url_path_match {
@@ -163,22 +144,12 @@ sub _url_path_match {
       push @path, $self->_url_path_segment_match($_)
         or $self->_blam("Couldn't parse path match segment");
     }
-    my $re = '^()'.join('/','',@path).($full_path ? '$' : '(/.*)$');
+    my $re = '^('.join('/','',@path).')'.($full_path ? '$' : '(/.*)$');
     $re = qr/$re/;
     if ($full_path) {
-      return sub {
-        if (my @cap = (shift->{PATH_INFO} =~ /$re/)) {
-          $cap[0] = {}; return @cap;
-        }
-        return ();
-      };
+      return match_path($re);
     }
-    return sub {
-      if (my @cap = (shift->{PATH_INFO} =~ /$re/)) {
-        $cap[0] = { PATH_INFO => pop(@cap) }; return @cap;
-      }
-      return ();
-    };
+    return match_path_strip($re);
   }
   return;
 }
diff --git a/lib/Web/Dispatch/Predicates.pm b/lib/Web/Dispatch/Predicates.pm
new file mode 100644 (file)
index 0000000..fff7542
--- /dev/null
@@ -0,0 +1,75 @@
+package Web::Dispatch::Predicates;
+
+use strictures 1;
+use base qw(Exporter);
+
+our @EXPORT = qw(match_and match_or match_method match_path match_path_strip);
+
+sub match_and {
+  my @match = @_;
+  sub {
+    my ($env) = @_;
+    my $my_env = { %$env };
+    my $new_env;
+    my @got;
+    foreach my $match (@match) {
+      if (my @this_got = $match->($my_env)) {
+       my %change_env = %{shift(@this_got)};
+       @{$my_env}{keys %change_env} = values %change_env;
+       @{$new_env}{keys %change_env} = values %change_env;
+       push @got, @this_got;
+      } else {
+       return;
+      }
+    }
+    return ($new_env, @got);
+  }
+}
+
+sub match_or {
+  my @match = @_;
+  sub {
+    foreach my $try (@match) {
+      if (my @ret = $try->(@_)) {
+        return @ret;
+      }
+    }
+    return;
+  }
+}
+
+sub match_method {
+  my ($method) = @_;
+  sub {
+    my ($env) = @_;
+    $env->{REQUEST_METHOD} eq $method ? {} : ()
+  }
+}
+
+sub match_path {
+  my ($re) = @_;
+  sub {
+    my ($env) = @_;
+    if (my @cap = ($env->{PATH_INFO} =~ /$re/)) {
+      $cap[0] = {}; return @cap;
+    }
+    return;
+  }
+}
+
+sub match_path_strip {
+  my ($re) = @_;
+  sub {
+    my ($env) = @_;
+    if (my @cap = ($env->{PATH_INFO} =~ /$re/)) {
+      $cap[0] = {
+       SCRIPT_NAME => ($env->{SCRIPT_NAME}||'').$cap[0],
+       PATH_INFO => pop(@cap),
+      };
+      return @cap;
+    }
+    return;
+  }
+}
+
+1;
index a11972c..4021170 100644 (file)
@@ -130,8 +130,8 @@ sub _construct_redispatch {
 }
 
 sub _build_dispatch_parser {
-  require Web::Simple::DispatchParser;
-  return Web::Simple::DispatchParser->new;
+  require Web::Dispatch::Parser;
+  return Web::Dispatch::Parser->new;
 }
 
 sub _cannot_call_twice {
@@ -193,7 +193,7 @@ sub _build_dispatcher_from_spec {
   my $parser = $class->_build_dispatch_parser;
   my $matcher = (
     defined($proto) && length($proto)
-      ? $parser->parse_dispatch_specification($proto)
+      ? $parser->parse($proto)
       : sub { ({}, $_[1]) }
   );
   return $class->_build_dispatcher({
index f147468..dae8ad4 100644 (file)
@@ -3,12 +3,12 @@ use warnings FATAL => 'all';
 
 use Test::More qw(no_plan);
 
-use Web::Simple::DispatchParser;
+use Web::Dispatch::Parser;
 
-my $dp = Web::Simple::DispatchParser->new;
+my $dp = Web::Dispatch::Parser->new;
 
 {
-   my $get = $dp->parse_dispatch_specification('GET');
+   my $get = $dp->parse('GET');
 
    is_deeply(
      [ $get->({ REQUEST_METHOD => 'GET' }) ],
@@ -24,12 +24,12 @@ my $dp = Web::Simple::DispatchParser->new;
 }
 
 ok(
-  !eval { $dp->parse_dispatch_specification('GET POST'); 1; },
+  !eval { $dp->parse('GET POST'); 1; },
   "Don't yet allow two methods"
 );
 
 {
-   my $html = $dp->parse_dispatch_specification('.html');
+   my $html = $dp->parse('.html');
 
    is_deeply(
      [ $html->({ PATH_INFO => '/foo/bar.html' }) ],
@@ -45,7 +45,7 @@ ok(
 }
 
 {
-   my $any_ext = $dp->parse_dispatch_specification('.*');
+   my $any_ext = $dp->parse('.*');
 
    is_deeply(
      [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ],
@@ -61,7 +61,7 @@ ok(
 }
 
 {
-   my $slash = $dp->parse_dispatch_specification('/');
+   my $slash = $dp->parse('/');
 
    is_deeply(
      [ $slash->({ PATH_INFO => '/' }) ],
@@ -77,7 +77,7 @@ ok(
 }
 
 {
-   my $post = $dp->parse_dispatch_specification('/post/*');
+   my $post = $dp->parse('/post/*');
 
    is_deeply(
      [ $post->({ PATH_INFO => '/post/one' }) ],
@@ -93,7 +93,7 @@ ok(
 }
 
 {
-   my $combi = $dp->parse_dispatch_specification('GET+/post/*');
+   my $combi = $dp->parse('GET+/post/*');
 
    is_deeply(
      [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ],
@@ -115,7 +115,7 @@ ok(
 }
 
 {
-   my $or = $dp->parse_dispatch_specification('GET|POST');
+   my $or = $dp->parse('GET|POST');
 
    foreach my $meth (qw(GET POST)) {
 
@@ -134,7 +134,7 @@ ok(
 }
 
 {
-   my $or = $dp->parse_dispatch_specification('GET|POST|DELETE');
+   my $or = $dp->parse('GET|POST|DELETE');
 
    foreach my $meth (qw(GET POST DELETE)) {
 
@@ -153,7 +153,7 @@ ok(
 }
 
 {
-   my $nest = $dp->parse_dispatch_specification('(GET+/foo)|POST');
+   my $nest = $dp->parse('(GET+/foo)|POST');
 
    is_deeply(
      [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ],
@@ -183,7 +183,7 @@ ok(
 {
   local $@;
   ok(
-    !eval { $dp->parse_dispatch_specification('/foo+(GET'); 1 },
+    !eval { $dp->parse('/foo+(GET'); 1 },
     'Death with missing closing )'
   );
   my $err = q{
@@ -199,7 +199,7 @@ ok(
 }
 
 {
-   my $not = $dp->parse_dispatch_specification('!.html+.*');
+   my $not = $dp->parse('!.html+.*');
 
    is_deeply(
      [ $not->({ PATH_INFO => '/foo.xml' }) ],
@@ -221,17 +221,17 @@ ok(
 }
 
 {
-   my $sub = $dp->parse_dispatch_specification('/foo/*/...');
+   my $sub = $dp->parse('/foo/*/...');
 
    is_deeply(
      [ $sub->({ PATH_INFO => '/foo/1/bar' }) ],
-     [ { PATH_INFO => '/bar' }, 1 ],
+     [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ],
      '/foo/*/... matches /foo/1/bar and strips to /bar'
    );
 
    is_deeply(
      [ $sub->({ PATH_INFO => '/foo/1/' }) ],
-     [ { PATH_INFO => '/' }, 1 ],
+     [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ],
      '/foo/*/... matches /foo/1/bar and strips to /'
    );
 
@@ -266,7 +266,7 @@ my %all_multi = (
 );
 
 foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') {
-    my $foo = $dp->parse_dispatch_specification($lose);
+    my $foo = $dp->parse($lose);
 
     is_deeply(
         [ $foo->({ QUERY_STRING => '' }) ],
@@ -303,7 +303,7 @@ foreach my $win (
     [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
 ) {
     my ($spec, @res) = @$win;
-    my $match = $dp->parse_dispatch_specification($spec);
+    my $match = $dp->parse($spec);
     #use Data::Dump::Streamer; warn Dump($match);
     is_deeply(
         [ $match->({ QUERY_STRING => $q }) ],
@@ -317,7 +317,7 @@ foreach my $win (
 #
 
 foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') {
-    my $foo = $dp->parse_dispatch_specification($lose2);
+    my $foo = $dp->parse($lose2);
 
     is_deeply(
         [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ],
@@ -354,7 +354,7 @@ foreach my $win2 (
     [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
 ) {
     my ($spec, @res) = @$win2;
-    my $match = $dp->parse_dispatch_specification($spec);
+    my $match = $dp->parse($spec);
     # use Data::Dump::Streamer; warn Dump($match);
     is_deeply(
         [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ],
@@ -368,7 +368,7 @@ foreach my $win2 (
 #
 
 foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') {
-    my $foo = $dp->parse_dispatch_specification($lose3);
+    my $foo = $dp->parse($lose3);
 
     is_deeply(
         [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ],
@@ -405,7 +405,7 @@ foreach my $win3 (
     [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ],
 ) {
     my ($spec, @res) = @$win3;
-    my $match = $dp->parse_dispatch_specification($spec);
+    my $match = $dp->parse($spec);
     # use Data::Dump::Streamer; warn Dump($match);
     is_deeply(
         [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ],