redispatch_to
Matt S Trout [Thu, 22 Oct 2009 20:02:00 +0000 (16:02 -0400)]
examples/bloggery/bloggery.cgi
lib/Web/Simple.pm
lib/Web/Simple/Application.pm
t/bloggery.t

index b23e6d0..047b3ed 100755 (executable)
@@ -87,7 +87,7 @@ dispatch [
     filter_response { $self->render_html($_[1]) },
   },
   sub (GET + /) {
-    $self->redispatch('/index.html')
+    redispatch_to '/index.html';
   },
   sub (GET + /index) {
     $self->post_list
index 33b23b3..df3f461 100644 (file)
@@ -25,6 +25,9 @@ sub _export_into {
     *{"${app_package}::filter_response"} = sub (&) {
       $app_package->_construct_response_filter($_[0]);
     };
+    *{"${app_package}::redispatch_to"} = sub {
+      $app_package->_construct_redispatch($_[0]);
+    };
     *{"${app_package}::default_config"} = sub {
       my @defaults = @_;
       *{"${app_package}::_default_config"} = sub { @defaults };
index cf2e54a..e3f8f7b 100644 (file)
@@ -23,6 +23,17 @@ sub _is_response_filter {
     and $_[1]->isa('Web::Simple::ResponseFilter');
 }
 
+sub _construct_redispatch {
+  bless(\$_[1], 'Web::Simple::Redispatch');
+}
+
+sub _is_redispatch {
+  return unless
+    "$_[1]" =~ /\w+=[A-Z]/
+      and $_[1]->isa('Web::Simple::Redispatch');
+  return ${$_[1]};
+}
+
 sub _dispatch_parser {
   require Web::Simple::DispatchParser;
   return Web::Simple::DispatchParser->new;
@@ -65,6 +76,9 @@ sub _run_dispatch_for {
             $result,
             $self->_run_dispatch_for($new_env, \@disp)
           );
+        } elsif (my $path = $self->_is_redispatch($result)) {
+          $new_env->{PATH_INFO} = $path;
+          return $self->_run_dispatch_for($new_env, $dispatchables);
         }
         return $result;
       }
index 3752074..9ed2442 100644 (file)
@@ -25,9 +25,9 @@ sub run_request {
 
 my $res;
 
-$res = run_request(GET 'http://localhost/index.html');
+warn run_request(GET 'http://localhost/index.html')->as_string;
 
-warn $res->as_string;
+warn run_request(GET 'http://localhost/')->as_string;
 
 warn run_request(GET 'http://localhost/One-Post.html')->as_string;