Subject: | Feature: Optionally adjust PATH_INFO and SCRIPT_NAME, like URLMap |
Plack::App::URLMap modifies PATH_INFO and SCRIPT_NAME in the psgi
environment, making it easy to nest applications in nested paths and
other nifty dispatching goodness.
This feature could be added with an option, used thusly:
package MyApp;
my $router = Path::Router->new;
$router->add_route('/foo' =>
target => sub {
my ($env) = @_;
# this should have '/foo' stripped from path_info and stored in
# script_name.
warn "my path here is ", $env->{PATH_INFO};
warn "my script_name here is ", $ENV->{SCRIPT_NAME};
# return response...
}
);
my $app = Plack::App::Path::Router::Custom->new(
router => $router,
rewrite_script_name => 1,
);
rough implementation sketch (untested):
package Plack::App::Path::Router::Custom;
has rewrite_script_name => (
is => 'ro', isa => 'Bool',
default => 0,
);
sub call {
my ($self, $env) = @_;
my $path_info = $env->{PATH_INFO};
my $script_name = $env->{SCRIPT_NAME};
my $match = $self->router->match( $env->{PATH_INFO} );
my $orig_path_info = $env->{PATH_INFO};
my $orig_script_name = $env->{SCRIPT_NAME};
my $location = $match->path;
my $path = $env->{PATH_INFO};
$path =~ s!^\Q$location\E!!;
$env->{PATH_INFO} = $path;
$env->{SCRIPT_NAME} = $script_name . $location;
# note this is constructed *after* we alter $env
my $req = $self->new_request( $env );
if ($match) {
... unchanged to getting $res
# Note that the original ::Custom code doesn't call
# response_cb at all, which I'd consider a bug
return $self->response_cb($app->($env), sub {
$env->{PATH_INFO} = $orig_path_info;
$env->{SCRIPT_NAME} = $orig_script_name;
});
}
}