Perlbal/Plugin/Vhosts.pm

filter: Statementstmt 68.2% 56 / 82 Branchbran 65.7% 25 / 38 Conditioncond 63.1% 12 / 19 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 72.7% 8 / 11 Podpod 0.0% 0 / 6 totaltotal 61.9% 104 / 168 CC 28 SCAR 22.2
CC
28
Cov
67%
vhost_selector
16.0
dumpconfig
12.0
__ANON__
7.4
CRAP 9.2
« Perlbal/Socket.pm ↑ Summary Perlbal/ClientHTTP.pm »
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
BranchTrueFalseTotal
unless ($selname ||= $mc->{"ctx"}{"last_created"}) 0 10 10
Condition66%2/3
$selname ||= $mc->{"ctx"}{"last_created"}
l!l&&r!l&&!r
730
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"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless $ss and $ss->{"role"} eq "selector" 1 9 10
Condition66%2/3
$ss and $ss->{"role"} eq "selector"
!ll&&!rl&&r
109
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"
ABresult
0X0
100
111
34  
35 9 $host = lc $host;
36 9 ▶ return $mc->err("invalid host pattern: '$host'")
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /^[\w\-\_\.\*\;\:]+$/ 1 8 9
37 unless $host =~ /^[\w\-\_\.\*\;\:]+$/;
38  
39 8 ▶ $ss->{extra_config}->{_vhosts} ||= {};
Condition50%1/2
$ss->{"extra_config"}{"_vhosts"} ||= {}
l!l
80
MC/DC0%0/1
$ss->{"extra_config"}{"_vhosts"} ||= {}
$ss->{"extra_config"}{"_vhosts"}
Truth table50%1/2
A
A= $ss->{"extra_config"}{"_vhosts"}
Aresult
11
00
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
BranchTrueFalseTotal
unless ($svc and $svc->{"role"} eq "selector") 0 4 4
Condition33%1/3
$svc and $svc->{"role"} eq "selector"
!ll&&!rl&&r
004
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $vhosts 0 0 0
84  
85 0 my @return;
86  
87 0 ▶ while (my ($vhost, $target) = each %$vhosts) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$vhosts) 0 0 0
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
BranchTrueFalseTotal
unless $req 0 89 89
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
BranchTrueFalseTotal
$vhost and $vhost =~ s/\.$// 17 72 89
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"} ||= {}
l!l
890
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"}
Aresult
11
00
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
BranchTrueFalseTotal
if ($vhost and $uri =~ m[^/__using/([\w\.]+)(?:/\w+)(?:\?.*)?$]) 0 89 89
Condition66%2/3
$vhost and $uri =~ m[^/__using/([\w\.]+)(?:/\w+)(?:\?.*)?$]
!ll&&!rl&&r
72170
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+)(?:\?.*)?$]
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$svc_name ? : 0 0 0
119 0 ▶ unless ($svc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc) 0 0 0
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
BranchTrueFalseTotal
$map_name ? : 87 16 103
135  
136 103 ▶ return 0 unless $svc || $force;
Branch: 1 branch
BranchTrueFalseTotal
unless $svc or $force 14 89 103
Condition100%3/3
$svc or $force
l!l&&r!l&&!r
87214
MC/DC100%2/2
$svc or $force
$svc $force
Truth table100%3/3
A or B
A= $svcB= $force
ABresult
1X1
011
000
137  
138 89 ▶ unless ($svc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svc) 2 87 89
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
BranchTrueFalseTotal
unless $vhost 72 17 89
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
BranchTrueFalseTotal
if &$map_using($vhost) 12 5 17
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
BranchTrueFalseTotal
if &$map_using($wild) 0 5 5
169  
170 # now peel away subdomains
171 5 ▶ while ($wild =~ s/^\*\.[\w\-\_]+/*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wild =~ s/^\*\.[\w\-\_]+/*/) 7 2 9
172 7 ▶ return if $map_using->($wild);
Branch: 1 branch
BranchTrueFalseTotal
if &$map_using($wild) 3 4 7
173 }
174  
175 # last option: use the "*" wildcard
176 2 return $map_using->("*", 1);
177 }
178  
179 4 1;
« Perlbal/Socket.pm ↑ Summary Perlbal/ClientHTTP.pm »