| 1 | ########################################################################### | ||||||||||||||||||||||||||||
| 2 | # plugin to do name-based virtual hosts | ||||||||||||||||||||||||||||
| 3 | ########################################################################### | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # things to test: | ||||||||||||||||||||||||||||
| 6 | # one persistent connection, first to a docs plugin, then to web proxy... see if it returns us to our base class after end of request | ||||||||||||||||||||||||||||
| 7 | # PUTing a large file to a selector, seeing if it is put correctly to the PUT-enabled web_server proxy | ||||||||||||||||||||||||||||
| 8 | # obvious cases: non-existent domains, default domains (*), proper matching (foo.brad.lj before *.brad.lj) | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package Perlbal::Plugin::Vhosts; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | no warnings qw(deprecated); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | our %Services; # service_name => $svc | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # when "LOAD" directive loads us up | ||||||||||||||||||||||||||||
| 20 | 3 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 12 | ▶ | Perlbal::register_global_hook('manage_command.vhost', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.0
| |||||||||||||||||||||||||||||
| 24 | 12 | my $mc = shift->parse(qr/^vhost\s+(?:(\w+)\s+)?(\S+)\s*=\s*(\w+)$/, | |||||||||||||||||||||||||||
| 25 | "usage: VHOST [<service>] <host_or_pattern> = <dest_service>"); | ||||||||||||||||||||||||||||
| 26 | 10 | my ($selname, $host, $target) = $mc->args; | |||||||||||||||||||||||||||
| 27 | 10 | ▶ | unless ($selname ||= $mc->{ctx}{last_created}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$selname ||= $mc->{"ctx"}{"last_created"}
MC/DC0%0/2
$selname ||= $mc->{"ctx"}{"last_created"}
$selname
$mc->{"ctx"}{"last_created"}
Truth table66%2/3
A ||= B
A= $selnameB= $mc->{"ctx"}{"last_created"}
| |||||||||||||||||||||||||||||
| 28 | 0 | return $mc->err("omitted service name not implied from context"); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 10 | my $ss = Perlbal->service($selname); | |||||||||||||||||||||||||||
| 32 | return $mc->err("Service '$selname' is not a selector service") | ||||||||||||||||||||||||||||
| 33 | 10 | ▶ | unless $ss && $ss->{role} eq "selector"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ss and $ss->{"role"} eq "selector"
MC/DC50%1/2
$ss and $ss->{"role"} eq "selector"
$ss
$ss->{"role"} eq "selector"
Truth table66%2/3
A and B
A= $ssB= $ss->{"role"} eq "selector"
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 9 | $host = lc $host; | |||||||||||||||||||||||||||
| 36 | 9 | ▶ | return $mc->err("invalid host pattern: '$host'") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | unless $host =~ /^[\w\-\_\.\*\;\:]+$/; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 8 | ▶ | $ss->{extra_config}->{_vhosts} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$ss->{"extra_config"}{"_vhosts"} ||= {}
MC/DC0%0/1
$ss->{"extra_config"}{"_vhosts"} ||= {}
$ss->{"extra_config"}{"_vhosts"}
Truth table50%1/2
A
A= $ss->{"extra_config"}{"_vhosts"}
| |||||||||||||||||||||||||||||
| 40 | 8 | $ss->{extra_config}->{_vhosts}{$host} = $target; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 8 | return $mc->ok; | |||||||||||||||||||||||||||
| 43 | 3 | }); | |||||||||||||||||||||||||||
| 44 | 3 | return 1; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # unload our global commands, clear our service object | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub unload { | ||||||||||||||||||||||||||
|
Subroutine
unload: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | Perlbal::unregister_global_hook('manage_command.vhost'); | |||||||||||||||||||||||||||
| 52 | 0 | unregister($class, $_) foreach (values %Services); | |||||||||||||||||||||||||||
| 53 | 0 | return 1; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # called when we're being added to a service | ||||||||||||||||||||||||||||
| 57 | 4 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 4 | my ($class, $svc) = @_; | |||||||||||||||||||||||||||
| 59 | 4 | ▶ | unless ($svc && $svc->{role} eq "selector") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$svc and $svc->{"role"} eq "selector"
MC/DC0%0/2
$svc and $svc->{"role"} eq "selector"
$svc
$svc->{"role"} eq "selector"
Truth table33%1/3
A and B
A= $svcB= $svc->{"role"} eq "selector"
| |||||||||||||||||||||||||||||
| 60 | 0 | die "You can't load the vhost plugin on a service not of role selector.\n"; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 4 | $svc->selector(\&vhost_selector); | |||||||||||||||||||||||||||
| 64 | 4 | $svc->{extra_config}->{_vhosts} = {}; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 4 | $Services{"$svc"} = $svc; | |||||||||||||||||||||||||||
| 67 | 4 | return 1; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # called when we're no longer active on a service | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub unregister { | ||||||||||||||||||||||||||
|
Subroutine
unregister: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 0 | my ($class, $svc) = @_; | |||||||||||||||||||||||||||
| 73 | 0 | $svc->selector(undef); | |||||||||||||||||||||||||||
| 74 | 0 | delete $Services{"$svc"}; | |||||||||||||||||||||||||||
| 75 | 0 | return 1; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub dumpconfig { | ||||||||||||||||||||||||||
|
Subroutine
dumpconfig: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 0 | my ($class, $svc) = @_; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my $vhosts = $svc->{extra_config}->{_vhosts}; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return unless $vhosts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | my @return; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | while (my ($vhost, $target) = each %$vhosts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | push @return, "VHOST $vhost = $target"; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | return @return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # call back from Service via ClientHTTPBase's event_read calling service->select_new_service(Perlbal::ClientHTTPBase) | ||||||||||||||||||||||||||||
| 95 | 89 | ▶ | sub vhost_selector { | ||||||||||||||||||||||||||
|
Subroutine
vhost_selector: called CC=13 SCAR=27.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 89 | my Perlbal::ClientHTTPBase $cb = shift; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 89 | my $req = $cb->{req_headers}; | |||||||||||||||||||||||||||
| 99 | 89 | ▶ | return $cb->_simple_response(404, "Not Found (no reqheaders)") unless $req; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 89 | my $vhost = $req->header("Host"); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Browsers and the Apache API considers 'www.example.com.' == 'www.example.com' | ||||||||||||||||||||||||||||
| 104 | 89 | ▶ | $vhost and $vhost =~ s/\.$//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 89 | my $uri = $req->request_uri; | |||||||||||||||||||||||||||
| 107 | 89 | ▶ | my $maps = $cb->{service}{extra_config}{_vhosts} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$cb->{"service"}{"extra_config"}{"_vhosts"} ||= {}
MC/DC0%0/1
$cb->{"service"}{"extra_config"}{"_vhosts"} ||= {}
$cb->{"service"}{"extra_config"}{"_vhosts"}
Truth table50%1/2
A
A= $cb->{"service"}{"extra_config"}{"_vhosts"}
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # ability to ask for one host, but actually use another. (for | ||||||||||||||||||||||||||||
| 110 | # circumventing javascript/java/browser host restrictions when you | ||||||||||||||||||||||||||||
| 111 | # actually control two domains). | ||||||||||||||||||||||||||||
| 112 | 89 | ▶ | if ($vhost && $uri =~ m!^/__using/([\w\.]+)(?:/\w+)(?:\?.*)?$!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$vhost and $uri =~ m[^/__using/([\w\.]+)(?:/\w+)(?:\?.*)?$]
MC/DC0%0/2
$vhost and $uri =~ m[^/__using/([\w\.]+)(?:/\w+)(?:\?.*)?$]
$vhost
$uri =~ m[^/__using/([\w\.]+)(?:/\w+)(?:\?.*)?$]
Truth table66%2/3
A and B
A= $vhostB= $uri =~ m[^/__using/([\w\.]+)(?:/\w+)(?:\?.*)?$]
| |||||||||||||||||||||||||||||
| 113 | 0 | my $alt_host = $1; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # update our request object's Host header, if we ended up switching them | ||||||||||||||||||||||||||||
| 116 | # around with /__using/... | ||||||||||||||||||||||||||||
| 117 | 0 | my $svc_name = $maps->{"$vhost;using:$alt_host"}; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $svc = $svc_name ? Perlbal->service($svc_name) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | unless ($svc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $cb->_simple_response(404, "Vhost twiddling not configured for requested pair."); | |||||||||||||||||||||||||||
| 121 | 0 | return 1; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | $req->header("Host", $alt_host); | |||||||||||||||||||||||||||
| 125 | 0 | $svc->adopt_base_client($cb); | |||||||||||||||||||||||||||
| 126 | 0 | return 1; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # returns 1 if done with client, 0 if no action taken | ||||||||||||||||||||||||||||
| 130 | 103 | ▶ | my $map_using = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 131 | 103 | my ($match_on, $force) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 103 | my $map_name = $maps->{$match_on}; | |||||||||||||||||||||||||||
| 134 | 103 | ▶ | my $svc = $map_name ? Perlbal->service($map_name) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 103 | ▶ | return 0 unless $svc || $force; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$svc or $force
MC/DC100%2/2
$svc or $force
$svc
$force
Truth table100%3/3
A or B
A= $svcB= $force
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 89 | ▶ | unless ($svc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 2 | $cb->_simple_response(404, "Not Found (no configured vhost)"); | |||||||||||||||||||||||||||
| 140 | 2 | return 1; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 87 | $svc->adopt_base_client($cb); | |||||||||||||||||||||||||||
| 144 | 87 | return 1; | |||||||||||||||||||||||||||
| 145 | 89 | }; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # foo.site.com should match: | ||||||||||||||||||||||||||||
| 148 | # foo.site.com | ||||||||||||||||||||||||||||
| 149 | # *.foo.site.com -- this one's questionable, but might as well? | ||||||||||||||||||||||||||||
| 150 | # *.site.com | ||||||||||||||||||||||||||||
| 151 | # *.com | ||||||||||||||||||||||||||||
| 152 | # * | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # if no vhost, just try the * mapping | ||||||||||||||||||||||||||||
| 155 | 89 | ▶ | return $map_using->("*", 1) unless $vhost; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Strip off the :portnumber, if any | ||||||||||||||||||||||||||||
| 158 | 17 | $vhost =~ s/:\d+$//; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # try the literal mapping | ||||||||||||||||||||||||||||
| 161 | 17 | ▶ | return if $map_using->($vhost); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # and now try wildcard mappings, removing one part of the domain | ||||||||||||||||||||||||||||
| 164 | # at a time until we find something, or end up at "*" | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # first wildcard, prepending the "*." | ||||||||||||||||||||||||||||
| 167 | 5 | my $wild = "*.$vhost"; | |||||||||||||||||||||||||||
| 168 | 5 | ▶ | return if $map_using->($wild); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # now peel away subdomains | ||||||||||||||||||||||||||||
| 171 | 5 | ▶ | while ($wild =~ s/^\*\.[\w\-\_]+/*/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 7 | ▶ | return if $map_using->($wild); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # last option: use the "*" wildcard | ||||||||||||||||||||||||||||
| 176 | 2 | return $map_using->("*", 1); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 4 | 1; | |||||||||||||||||||||||||||