| 1 | package Slovo::Plugin::CGI; | ||
| 2 | use Mojo::Base 'Mojolicious::Plugin', -signatures; | ||
| 3 | |||
| 4 | use Mojo::Util qw(punycode_decode); | ||
| 5 | |||
| 6 | my $mod_rewrite; | ||
| 7 | |||
| 8 | sub register ($self, $app, $config) { | ||
| 9 | |||
| 10 | # Check again if we need this hook at all | ||
| 11 | return $self unless $ENV{GATEWAY_INTERFACE}; | ||
| 12 | $mod_rewrite = $config->{mod_rewrite} //= 1; | ||
| 13 | $app->hook(before_dispatch => \&_handle_cgi); | ||
| 14 | return $self; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub _handle_cgi ($c) { | ||
| 18 | my $url = $c->req->url; | ||
| 19 | my $path = $url->path->to_string || '/'; | ||
| 20 | $path =~ s|^.+cached/?||; | ||
| 21 | if ($path =~ m'%') { | ||
| 22 | $path = Mojo::Util::url_unescape $path; | ||
| 23 | $path = Mojo::Util::decode 'UTF-8', $path; | ||
| 24 | } | ||
| 25 | |||
| 26 | # no path merging | ||
| 27 | $url->path($path =~ m'^/' ? $path : "/$path"); | ||
| 28 | |||
| 29 | # remove /slovo/slovo.cgi from generated links in the page | ||
| 30 | if ($mod_rewrite) { | ||
| 31 | my $base = $url->base =~ s|$ENV{SCRIPT_NAME}||r; | ||
| 32 | $url->base(Mojo::URL->new($base)); | ||
| 33 | } | ||
| 34 | |||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 38 | 1; | ||
| 39 | |||
| 40 | =encoding utf8 | ||
| 41 | |||
| 42 | =head1 NAME | ||
| 43 | |||
| 44 | Slovo::Plugin::CGI - before_dispatch hook under Apache 2/CGI | ||
| 45 | |||
| 46 | |||
| 47 | =head1 DESCRIPTION | ||
| 48 | |||
| 49 | L<Slovo::Plugin::CGI> extends L<Mojolicious::Plugin>. It provides a | ||
| 50 | L</before_dispatch> hook to handle Apache double encoding of the current url | ||
| 51 | and to strip the C<$ENV{SCRIPT_NAME}> from produced by Slovo urls. This plugin | ||
| 52 | is enabled by default and will detect if the app is run under CGI. If app is | ||
| 53 | run as a daemon this plugin will do nothing. | ||
| 54 | |||
| 55 | =head1 HOOKS | ||
| 56 | |||
| 57 | |||
| 58 | =head2 before_dispatch | ||
| 59 | |||
| 60 | Handles Apache double encoding of the UTf-8 url and strips | ||
| 61 | the C<$ENV{SCRIPT_NAME}> from produced by Slovo urls. | ||
| 62 | |||
| 63 | =head1 CONFIGURATION | ||
| 64 | |||
| 65 | The folowing option is currently supported. | ||
| 66 | |||
| 67 | =head2 mod_rewrite = 1 | ||
| 68 | |||
| 69 | Boolean. Defaluts to true. Set to 0 to disable removing of C<$ENV{SCRIPT_NAME}> | ||
| 70 | from C<$c-E<gt>req-E<gt>url-E<gt>base>. This will practically stop removing | ||
| 71 | C<$ENV{SCRIPT_NAME}> from all generated links. Having C<$ENV{SCRIPT_NAME}> in | ||
| 72 | URLs also tells mod rewrite to not look for static files in folder domove to | ||
| 73 | not apply any rules when requestingC<$ENV{SCRIPT_NAME}> | ||
| 74 | |||
| 75 | =head1 AUTHOR | ||
| 76 | |||
| 77 | Красимир Беров | ||
| 78 | CPAN ID: BEROV | ||
| 79 | berov на cpan точка org | ||
| 80 | http://i-can.eu | ||
| 81 | |||
| 82 | =head1 COPYRIGHT | ||
| 83 | |||
| 84 | This program is free software licensed under the Artistic License 2.0. | ||
| 85 | |||
| 86 | The full text of the license can be found in the | ||
| 87 | LICENSE file included with this module. | ||
| 88 | |||
| 89 | =cut | ||
| 90 |