From: Matt S Trout Date: Thu, 22 Oct 2009 20:02:00 +0000 (-0400) Subject: redispatch_to X-Git-Tag: v0.003~67 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FWeb-Simple.git;a=commitdiff_plain;h=391190822264aefefa6755d410141688a7155dee redispatch_to --- diff --git a/examples/bloggery/bloggery.cgi b/examples/bloggery/bloggery.cgi index b23e6d0..047b3ed 100755 --- a/examples/bloggery/bloggery.cgi +++ b/examples/bloggery/bloggery.cgi @@ -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 diff --git a/lib/Web/Simple.pm b/lib/Web/Simple.pm index 33b23b3..df3f461 100644 --- a/lib/Web/Simple.pm +++ b/lib/Web/Simple.pm @@ -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 }; diff --git a/lib/Web/Simple/Application.pm b/lib/Web/Simple/Application.pm index cf2e54a..e3f8f7b 100644 --- a/lib/Web/Simple/Application.pm +++ b/lib/Web/Simple/Application.pm @@ -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; } diff --git a/t/bloggery.t b/t/bloggery.t index 3752074..9ed2442 100644 --- a/t/bloggery.t +++ b/t/bloggery.t @@ -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;