The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Plack::Middleware::Rewrite::Query - Safely modify the QUERY_STRING of a PSGI request

VERSION

version 0.1.1

SYNOPSIS

    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;
    };

DESCRIPTION

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.

CONFIGURATION

modify

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.

map

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.

FUNCTIONS

The following functions can be exportet on request:

rewrite_query( $env [, modify => $sub ] [, map => $sub ] )

Functional interface to the core of this middleware to be used in different context (see SYNOPSIS for an example).

query_string( $multihash )

Returns a QUERY_STRING, given a Hash::MultiValue. This includes URI escaping all keys and values.

SEE ALSO

Plack::Middleware::Rewrite

AUTHOR

Jakob Voß

COPYRIGHT AND LICENSE

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.