Plack::Middleware::Rewrite::Query - Safely modify the QUERY_STRING of a PSGI request
version 0.1.1
builder { enable 'Rewrite::Query', # rename all 'foo' paramaters to 'bar' map => sub { my ($key, $value) = @_; (($key eq 'foo' ? 'bar' : $key), $value); }, # add a query parameter 'doz' with value '1' modify => sub { $_->add('doz', 1); }; $app; }; use Plack::Middleware::Rewrite::Query qw(rewrite_query); builder { # http://example.org/path/123 => http://example.org/path?id=123 enable 'Rewrite', rules => sub { if ( s{/([0-9]+)$}{} ) { my $id = $1; rewrite_query( $_[0], modify => sub { $_->set('id',$id) } ); } }; $app; };
This Plack::Middleware can be used to rewrite the QUERY_STRING of a PSGI request. Simpliy modifying QUERY_STRING won't alway work because Plack::Request stores query parameters at multiple places in a PSGI request. This middleware takes care for cleanup, except the PSGI variable REQUEST_URI, including the original query string, is not modified because it should not be used by applications, anyway.
Reference to a function that will be called with a Hash::MultiValue containing the query parameters. The query is also aliased to $_ for easy manipulation.
$_
Reference to a function that can be used to modify or remove key-value pairs. If both, modify and map are set then map is applied first.
modify
map
The following functions can be exportet on request:
Functional interface to the core of this middleware to be used in different context (see SYNOPSIS for an example).
Returns a QUERY_STRING, given a Hash::MultiValue. This includes URI escaping all keys and values.
Plack::Middleware::Rewrite
Jakob Voß
This software is copyright (c) 2014 by Jakob Voß.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Plack::Middleware::Rewrite::Query, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::Middleware::Rewrite::Query
CPAN shell
perl -MCPAN -e shell install Plack::Middleware::Rewrite::Query
For more information on module installation, please visit the detailed CPAN module installation guide.