|
NAMEPlack::Middleware::Rewrite - mod_rewrite for PlackVERSIONversion 2.101SYNOPSIS# in app.psgi use Plack::Builder; builder { enable 'Rewrite', request => sub { s{^/here(?=/|$)}{/there}; return [303] if s{^/foo/?$}{/bar/} or s{^/baz/?$}{/quux/}; return [301, [ Location => 'http://example.org/' ], []] if m{^/example/?$}; return [201] if $_ eq '/favicon.ico'; return [503] if -e '/path/to/app/maintenance.lock'; return [200, [qw(Content-Type text/plain)], ['You found it!']] if $_ eq '/easter-egg'; }, response => sub { $_->status( 303 ) if $_->status eq 201 and $_->get( 'Location' ); $_->set( 'Content-Type', 'application/xhtml+xml' ) if ( $_[0]{'HTTP_ACCEPT'} || '' ) =~ m{application/xhtml\+xml(?!\s*;\s*q=0)}; }; $app; }; DESCRIPTIONThis middleware provides a convenient way to modify requests in flight in Plack apps. Rewrite rules are simply written in Perl, which means everything that can be done with mod_rewrite can be done with this middleware much more intuitively (if in syntactically wordier ways). Its primary purpose is rewriting paths, but almost anything is possible very easily.CONFIGURATION OPTIONS"request"Takes a reference to a function that will be called in scalar context for each request. On call, $_ will be aliased to "PATH_INFO", so that you can easily use regexp matches and subtitutions to examine and modify it. The PSGI environment will be passed to the function as its first and only argument.The function may return three kinds of valid value:
"response"Takes a reference to a function that will be called after the request has been processed and the response is ready to be returned.On call, $_ will be aliased to a specially extended a "Plack::Util::headers" object for the response, for convenient alteration of headers. The extension is a "status" method, which allows you to inspect and modify the response status code. Just as in ""request"", the PSGI environment is passed as first and only argument. Any return value from this function will be ignored unless it is a code reference. In that case it will be used to filter the response body, as documented in "RESPONSE CALLBACK" in Plack::Middleware: return sub { my $chunk = shift; return unless defined $chunk; $chunk =~ s/Foo/Bar/g; return $chunk; }; The callback takes one argument
$chunk and your callback is expected to return the
updated chunk. If the given $chunk is undef, it means
the stream has reached the end, so your callback should also return undef, or
return the final chunk and return undef when called next time.
LEGACY INTERFACEThe old interface uses a single attribute, "rules", instead of the "request" and "response" pair, with a more complex set of return values, containing an ambiguity. It is also less expressive than the new interface.The old interface is documented here for the purposes of maintaining old code; its use in new code is discouraged. In the far future it may get removed entirely, and in the meantime it will not gain new features. The return value of the "rules" callback is interpreted as follows:
Porting from the old to the new interfaceThere are two major incompatibilities between the interfaces:
AUTHORAristotle Pagaltzis <pagaltzis@gmx.de>COPYRIGHT AND LICENSEThis software is copyright (c) 2018 by Aristotle Pagaltzis.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |