| 1 | package Perlbal::Plugin::Redirect; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | sub handle_request { | ||||||||||||||||||||||||||
|
Subroutine
handle_request: called CC=6 SCAR=22.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 6 | 1 | my ($svc, $pb) = @_; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | my $mappings = $svc->{extra_config}{_redirect_host}; | |||||||||||||||||||||||||||
| 9 | 1 | my $req_header = $pb->{req_headers}; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # returns 1 if done with client, 0 if no action taken | ||||||||||||||||||||||||||||
| 12 | 1 | ▶ | my $map_using = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 13 | 1 | my ($match_on) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | my $target_host = $mappings->{$match_on}; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | return 0 unless $target_host; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | my $path = $req_header->request_uri; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | $pb->send_full_response(301, [ | |||||||||||||||||||||||||||
| 22 | 'Location' => "http://$target_host$path", | ||||||||||||||||||||||||||||
| 23 | 'Content-Length' => 0 | ||||||||||||||||||||||||||||
| 24 | ], ""); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | return 1; | |||||||||||||||||||||||||||
| 27 | 1 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # The following is lifted wholesale from the vhosts plugin. | ||||||||||||||||||||||||||||
| 30 | # FIXME: Factor it out to a utility function, I guess? | ||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||
| 32 | # foo.site.com should match: | ||||||||||||||||||||||||||||
| 33 | # foo.site.com | ||||||||||||||||||||||||||||
| 34 | # *.foo.site.com | ||||||||||||||||||||||||||||
| 35 | # *.site.com | ||||||||||||||||||||||||||||
| 36 | # *.com | ||||||||||||||||||||||||||||
| 37 | # * | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | my $vhost = lc($req_header->header("Host")); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # if no vhost, just try the * mapping | ||||||||||||||||||||||||||||
| 42 | 1 | ▶ | return $map_using->("*") unless $vhost; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Strip off the :portnumber, if any | ||||||||||||||||||||||||||||
| 45 | 1 | $vhost =~ s/:\d+$//; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # try the literal mapping | ||||||||||||||||||||||||||||
| 48 | 1 | ▶ | return 1 if $map_using->($vhost); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # and now try wildcard mappings, removing one part of the domain | ||||||||||||||||||||||||||||
| 51 | # at a time until we find something, or end up at "*" | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # first wildcard, prepending the "*." | ||||||||||||||||||||||||||||
| 54 | 0 | my $wild = "*.$vhost"; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | return 1 if $map_using->($wild); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # now peel away subdomains | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | while ($wild =~ s/^\*\.[\w\-\_]+/*/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | return 1 if $map_using->($wild); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # last option: use the "*" wildcard | ||||||||||||||||||||||||||||
| 63 | 0 | return $map_using->("*"); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 1 | my ($class, $svc) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | $svc->register_hook('Redirect', 'start_http_request', sub { handle_request($svc, $_[0]); }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub unregister { | ||||||||||||||||||||||||||
|
Subroutine
unregister: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 0 | my ($class, $svc) = @_; | |||||||||||||||||||||||||||
| 74 | 0 | $svc->unregister_hooks('Redirect'); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | sub handle_redirect_command { | ||||||||||||||||||||||||||
|
Subroutine
handle_redirect_command: called CC=5 SCAR=17.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 1 | my $mc = shift->parse(qr/^redirect\s+host\s+(\S+)\s+(\S+)$/, "usage: REDIRECT HOST <match_host> <target_host>"); | |||||||||||||||||||||||||||
| 79 | 1 | my ($match_host, $target_host) = $mc->args; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | my $svcname; | |||||||||||||||||||||||||||
| 82 | 1 | ▶ | unless ($svcname ||= $mc->{ctx}{last_created}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$svcname ||= $mc->{"ctx"}{"last_created"}
MC/DC0%0/2
$svcname ||= $mc->{"ctx"}{"last_created"}
$svcname
$mc->{"ctx"}{"last_created"}
Truth table33%1/3
A ||= B
A= $svcnameB= $mc->{"ctx"}{"last_created"}
| |||||||||||||||||||||||||||||
| 83 | 0 | return $mc->err("No service name in context from CREATE SERVICE <name> or USE <service_name>"); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | my $svc = Perlbal->service($svcname); | |||||||||||||||||||||||||||
| 87 | 1 | ▶ | return $mc->err("Non-existent service '$svcname'") unless $svc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | $svc->{extra_config}{_redirect_host} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$svc->{"extra_config"}{"_redirect_host"} ||= {}
MC/DC0%0/1
$svc->{"extra_config"}{"_redirect_host"} ||= {}
$svc->{"extra_config"}{"_redirect_host"}
Truth table50%1/2
A
A= $svc->{"extra_config"}{"_redirect_host"}
| |||||||||||||||||||||||||||||
| 90 | 1 | $svc->{extra_config}{_redirect_host}{lc($match_host)} = lc($target_host); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | return 1; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # called when we are loaded | ||||||||||||||||||||||||||||
| 96 | 1 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 1 | Perlbal::register_global_hook('manage_command.redirect', \&handle_redirect_command); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | return 1; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # called for a global unload | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub unload { | ||||||||||||||||||||||||||
|
Subroutine
unload: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | 0 | return 1; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 NAME | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Perlbal::Plugin::Redirect - Plugin to do redirecting in Perlbal land | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | LOAD redirect | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | CREATE SERVICE redirector | ||||||||||||||||||||||||||||
| 118 | SET role = web_server | ||||||||||||||||||||||||||||
| 119 | SET plugins = redirect | ||||||||||||||||||||||||||||
| 120 | REDIRECT HOST example.com www.example.net | ||||||||||||||||||||||||||||
| 121 | ENABLE redirector | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 LIMITATIONS | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Right now this can only redirect at the hostname level. Also, it just | ||||||||||||||||||||||||||||
| 126 | assumes you want an http: URL. | ||||||||||||||||||||||||||||