| 1 | package Perlbal::Plugin::AutoRemoveLeadingDir; | ||
| 2 | |||
| 3 | # | ||
| 4 | # this plugin auto-removes a leading directory path component | ||
| 5 | # in the URL, if it's the name of the directory the webserver | ||
| 6 | # is rooted at. | ||
| 7 | # | ||
| 8 | # if docroot = /home/lj/htdocs/stc/ | ||
| 9 | # | ||
| 10 | # and user requests: | ||
| 11 | # | ||
| 12 | # /stc/img/foo.jpg | ||
| 13 | # | ||
| 14 | # Then this plugin will treat that as if it's a request for /img/foo.jpg. | ||
| 15 | # | ||
| 16 | # This is useful for css/js/etc to have an "absolute" pathname for | ||
| 17 | # peer resources (think css having url(/stc/foo.jpg)) that can be served | ||
| 18 | # from either a separate hostname (stat.livejournal.com) and using a CDN, | ||
| 19 | # or from www. when cross-domain js restrictions require it. | ||
| 20 | |||
| 21 | use Perlbal; | ||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | |||
| 25 | sub load { 1 } | ||
| 26 | sub unload { 1 } | ||
| 27 | |||
| 28 | # called when we're being added to a service | ||
| 29 | sub register { | ||
| 30 | my ($class, $svc) = @_; | ||
| 31 | |||
| 32 | $svc->register_hook('AutoRemoveLeadingDir', 'start_serve_request', sub { | ||
| 33 | my Perlbal::ClientHTTPBase $client = shift; | ||
| 34 | my $uriref = shift; | ||
| 35 | |||
| 36 | my Perlbal::Service $svc = $client->{service}; | ||
| 37 | my ($tail) = ($svc->{docroot} =~ m!/([\w-]+)/?$!); | ||
| 38 | $$uriref =~ s!^/$tail!! if $tail; | ||
| 39 | return 0; | ||
| 40 | }); | ||
| 41 | |||
| 42 | return 1; | ||
| 43 | } | ||
| 44 | |||
| 45 | # called when we're no longer active on a service | ||
| 46 | sub unregister { | ||
| 47 | my ($class, $svc) = @_; | ||
| 48 | return 1; | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; |