blib/lib/App/HTTPThis.pm

filter: Statementstmt 64.2% 79 / 123 Branchbran 44.4% 32 / 72 Conditioncond 63.8% 30 / 47 MC/DCmcdc 48.0% 12 / 25 Subroutinesub 73.3% 11 / 15 Podpod 100% 2 / 2 totaltotal 58.4% 166 / 284 CC 58 SCAR 35.7
CC
58
Cov
58%
_wsl_addresses
110.0
_wsl_default_gateway
56.0
new
29.3
CRAP 35.4
↑ Summary
1 package App::HTTPThis;
2  
3 # ABSTRACT: Export the current directory over HTTP
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Plack::App::DirectoryIndex;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Plack::Runner;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $VERSION = '1.0.1';
15  
16 =head1 NAME
17  
18 App::HTTPThis - A simple local web server.
19  
20 =head1 SYNOPSIS
21  
22 # Not to be used directly, see http_this command
23  
24 =head1 DESCRIPTION
25  
26 This class implements all the logic of the L<http_this> command.
27  
28 Actually, this is just a very thin wrapper around
29 L<Plack::App::DirectoryIndex>, that is where the magic really is.
30  
31 =head1 METHODS
32  
33 =head2 new
34  
35 Creates a new App::HTTPThis object, parsing the command line arguments
36 into object attribute values.
37  
38 =cut
39  
40 12 ▶sub new {
Subroutine new: called CC=25 SCAR=33.8
Pod documented
41 12 my $class = shift;
42 12 my $self = bless {host => '127.0.0.1', port => 7007, root => '.'}, $class;
43  
44 12 my $default_config_file = '.http_thisrc';
45  
46 12 ▶ my $config_file = $self->{config} || $ENV{HTTP_THIS_CONFIG};
Condition66%2/3
$self->{'config'} || $ENV{'HTTP_THIS_CONFIG'}
l!l&&r!l&&!r
084
MC/DC50%1/2
$self->{'config'} || $ENV{'HTTP_THIS_CONFIG'}
$self->{'config'} $ENV{'HTTP_THIS_CONFIG'}
Truth table66%2/3
A || B
A= $self->{'config'}B= $ENV{'HTTP_THIS_CONFIG'}
ABresult
1X1
011
000
47 {
48 12 my %early;
49 12 local @ARGV = @ARGV;
50 12 Getopt::Long::Configure(qw(pass_through));
51 12 ▶ GetOptions(\%early, "config=s") || pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%early, "config=s") or pod2usage(2) 12 0 12
52 12 Getopt::Long::Configure(qw(default));
53 12 ▶ $config_file = $early{config} if defined $early{config};
Branch: 1 branch
BranchTrueFalseTotal
if defined $early{"config"} 1 11 12
54 }
55  
56 # There are apparently OSes where $ENV{HOME} is undefined
57 12 for my $dir ('.', $ENV{HOME}) {
58 24 ▶ next unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 24 24
59 24 ▶ if (!$config_file && -f "$dir/$default_config_file") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $config_file and -f "$dir/$default_config_file") 0 24 24
Condition66%2/3
not $config_file and -f "$dir/$default_config_file"
!ll&&!rl&&r
1680
MC/DC0%0/2
not $config_file and -f "$dir/$default_config_file"
not $config_file -f "$dir/$default_config_file"
Truth table66%2/3
not(A) and B
A= not $config_fileB= -f "$dir/$default_config_file"
ABresult
0X0
100
111
60 0 $config_file = "$dir/$default_config_file";
61 0 last;
62 }
63 }
64  
65 12 ▶ if ($config_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config_file) 8 4 12
66 8 ▶ my $config = Config::Tiny->read($config_file)
Branch: 1 branch
BranchTrueFalseTotal
my $config = "Config::Tiny"->read($config_file) or die "FATAL: failed to read config file '${config_file}'\n" 8 0 8
67 or die "FATAL: failed to read config file '$config_file'\n";
68 8 for my $key (qw(port host name autoindex pretty wsl)) {
69 48 ▶ if (defined $config->{_}->{$key} && $config->{_}->{$key} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"_"}{$key} and $config->{"_"}{$key} ne "") 6 42 48
Condition66%2/3
defined $config->{"_"}{$key} and $config->{"_"}{$key} ne ""
!ll&&!rl&&r
4206
MC/DC50%1/2
defined $config->{"_"}{$key} and $config->{"_"}{$key} ne ""
defined $config->{"_"}{$key} $config->{"_"}{$key} ne ""
Truth table66%2/3
A and B
A= defined $config->{"_"}{$key}B= $config->{"_"}{$key} ne ""
ABresult
0X0
100
111
70 6 $self->{$key} = $config->{_}->{$key};
71 }
72 }
73 8 ▶ if ($config->{_}->{all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"_"}{"all"}) 2 6 8
74 2 $self->{host} = q{0.0.0.0};
75 }
76 8 delete $self->{config};
77 }
78  
79 12 my %cli;
80 12 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%cli, "help", "man", "config=s", "host=s", "port=i", "name=s", "autoindex!", "pretty!", "all|promiscuous", "wsl") or pod2usage(2) 12 0 12
81 \%cli, "help", "man", "config=s", "host=s", "port=i", "name=s", "autoindex!", "pretty!",
82 "all|promiscuous", "wsl"
83 ) || pod2usage(2);
84 12 ▶ pod2usage(1) if $cli{help};
Branch: 1 branch
BranchTrueFalseTotal
if $cli{"help"} 0 12 12
85 12 ▶ pod2usage(-verbose => 2) if $cli{man};
Branch: 1 branch
BranchTrueFalseTotal
if $cli{"man"} 0 12 12
86  
87 12 for my $key (keys %cli) {
88 8 $self->{$key} = $cli{$key};
89 }
90  
91 12 ▶ $self->{host} = q{0.0.0.0} if $self->{all};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"all"} 3 9 12
92 $self->{host} = $self->_wsl_host
93 12 ▶ if $cli{wsl} || ($self->{wsl} && !exists $cli{host} && !exists $cli{all});
Branch: 1 branch
BranchTrueFalseTotal
if $cli{"wsl"} or $self->{"wsl"} and not exists $cli{"host"} and not exists $cli{"all"} 2 10 12
Condition100%9/9
$cli{"wsl"} or $self->{"wsl"} and not exists $cli{"host"} and not exists $cli{"all"}
l!l&&r!l&&!r
1110
$self->{"wsl"} and not exists $cli{"host"} and not exists $cli{"all"}
!ll&&!rl&&r
911
$self->{"wsl"} and not exists $cli{"host"}
!ll&&!rl&&r
812
MC/DC100%4/4
$cli{"wsl"} or $self->{"wsl"} and not exists $cli{"host"} and not exists $cli{"all"}
$cli{"wsl"} $self->{"wsl"} not exists $cli{"host"} not exists $cli{"all"}
Truth table100%5/5
A or B and not(C) and not(D)
A= $cli{"wsl"}B= $self->{"wsl"}C= not exists $cli{"host"}D= not exists $cli{"all"}
ABCDresult
1XXX1
01111
00XX0
010X0
01100
94  
95 12 ▶ if (@ARGV > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@ARGV > 1) { } 0 12 12
elsif (@ARGV) { } 0 12 12
96 0 pod2usage("$0: Too many roots, only single root supported");
97 }
98 elsif (@ARGV) {
99 0 $self->{root} = shift @ARGV;
100 }
101  
102 12 return $self;
103 }
104  
105 =head2 run
106  
107 Start the HTTP server.
108  
109 =cut
110  
111 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
112 0 my ($self) = @_;
113  
114 0 my $runner = Plack::Runner->new;
115 $runner->parse_options(
116 '--host' => $self->{host},
117 '--port' => $self->{port},
118 '--env' => 'production',
119 0 ▶ '--server_ready' => sub { $self->_server_ready(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 '--autoindex' => 0,
121 '--pretty' => 0,
122 );
123  
124 my $app_config = {
125 root => $self->{root},
126 pretty => $self->{pretty},
127 0 dir_index => '',
128 };
129 0 ▶ $app_config->{dir_index} = 'index.html' if $self->{autoindex};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"autoindex"} 0 0 0
130  
131 0 eval {
132 0 $runner->run(Plack::App::DirectoryIndex->new( $app_config )->to_app);
133 };
134 0 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
135 0 ▶ die "FATAL: port $self->{port} is already in use, try another one\n"
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ /failed to listen to port/ 0 0 0
136 if $e =~ /failed to listen to port/;
137 0 die "FATAL: internal error - $e\n";
138 }
139 }
140  
141 3 ▶sub _wsl_host {
Subroutine _wsl_host: called CC=4 SCAR=13.9
142 3 my ($self) = @_;
143  
144 3 for my $addr ($self->_wsl_addresses) {
145 8 ▶ return $addr
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A(?:[0-9]{1,3}\.){3}[0-9]{1,3}\z/ and $addr ne "127.0.0.1" 2 6 8
Condition100%3/3
$addr =~ /\A(?:[0-9]{1,3}\.){3}[0-9]{1,3}\z/ and $addr ne "127.0.0.1"
!ll&&!rl&&r
332
MC/DC100%2/2
$addr =~ /\A(?:[0-9]{1,3}\.){3}[0-9]{1,3}\z/ and $addr ne "127.0.0.1"
$addr =~ /\A(?:[0-9]{1,3}\.){3}[0-9]{1,3}\z/ $addr ne "127.0.0.1"
Truth table100%3/3
A and B
A= $addr =~ /\A(?:[0-9]{1,3}\.){3}[0-9]{1,3}\z/B= $addr ne "127.0.0.1"
ABresult
0X0
100
111
146 if $addr =~ /\A(?:[0-9]{1,3}\.){3}[0-9]{1,3}\z/
147 && $addr ne '127.0.0.1';
148 }
149  
150 1 die "FATAL: cannot find a non-loopback IPv4 address for WSL\n";
151 }
152  
153 0 ▶sub _wsl_addresses {
Subroutine _wsl_addresses: not called CC=10 SCAR=47.0
154 0 my ($self) = @_;
155  
156 0 my @addresses;
157 my %seen;
158 0 my %seen_addr;
159  
160 0 for my $peer ($self->_wsl_default_gateway, '1.1.1.1') {
161 0 ▶ next unless defined $peer && length $peer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peer and length $peer 0 0 0
Condition0%0/3
defined $peer and length $peer
!ll&&!rl&&r
000
MC/DC0%0/2
defined $peer and length $peer
defined $peer length $peer
Truth table0%0/3
A and B
A= defined $peerB= length $peer
ABresult
0X0
100
111
162 0 ▶ next if $seen{$peer}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$peer}++ 0 0 0
163  
164 0 my $sock = IO::Socket::INET->new(
165 PeerAddr => $peer,
166 PeerPort => 9,
167 Proto => 'udp',
168 );
169 0 ▶ next unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
170  
171 0 my $addr = $sock->sockhost;
172 push @addresses, $addr
173 0 ▶ if defined $addr && length $addr && !$seen_addr{$addr}++;
Branch: 1 branch
BranchTrueFalseTotal
if defined $addr and length $addr and not $seen_addr{$addr}++ 0 0 0
Condition0%0/6
defined $addr and length $addr and not $seen_addr{$addr}++
!ll&&!rl&&r
000
defined $addr and length $addr
!ll&&!rl&&r
000
MC/DC0%0/3
defined $addr and length $addr and not $seen_addr{$addr}++
defined $addr length $addr not $seen_addr{$addr}++
Truth table0%0/4
A and B and not(C)
A= defined $addrB= length $addrC= not $seen_addr{$addr}++
ABCresult
0XX0
10X0
1100
1111
174 }
175  
176 0 ▶ die "FATAL: cannot determine WSL IP address from the network route\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @addresses 0 0 0
177 unless @addresses;
178  
179 0 return @addresses;
180 }
181  
182 0 ▶sub _wsl_default_gateway {
Subroutine _wsl_default_gateway: not called CC=7 SCAR=40.3
183 0 ▶ open my $fh, '<', '/proc/net/route' or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "/proc/net/route" or return 0 0 0
184  
185 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
186 0 ▶ next if $line =~ /\AIface\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\AIface\b/ 0 0 0
187  
188 0 my @fields = split ' ', $line;
189 0 ▶ next unless @fields >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields >= 3 0 0 0
190 0 ▶ next unless $fields[1] eq '00000000';
Branch: 1 branch
BranchTrueFalseTotal
unless $fields[1] eq "00000000" 0 0 0
191 0 ▶ next unless $fields[2] =~ /\A[0-9A-Fa-f]{8}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields[2] =~ /\A[0-9A-Fa-f]{8}\z/ 0 0 0
192  
193 0 return join '.', reverse map { hex } $fields[2] =~ /(..)/g;
194 }
195  
196 0 return;
197 }
198  
199 5 ▶sub _server_ready {
Subroutine _server_ready: called CC=13 SCAR=28.2
200 5 my ($self, $args) = @_;
201  
202 5 my $host = $args->{host};
203 5 ▶ my $proto = $args->{proto} || 'http';
Condition50%1/2
$args->{'proto'} || 'http'
l!l
50
MC/DC0%0/1
$args->{'proto'} || 'http'
$args->{'proto'}
Truth table50%1/2
A
A= $args->{'proto'}
Aresult
11
00
204 5 my $port = $args->{port};
205  
206 # An unspecified, zero, or wildcard host means the server is listening on
207 # every network interface, not just this machine.
208 5 ▶ my $all_interfaces =
Condition75%9/12
!defined($host) || $host eq '' || $host eq '0' || $host eq '0.0.0.0' || $host eq '::'
l!l&&r!l&&!r
212
not defined $host or $host eq "" or $host eq 0 or $host eq "0.0.0.0"
l!l&&r!l&&!r
113
not defined $host or $host eq "" or $host eq 0
l!l&&r!l&&!r
014
not defined $host or $host eq ""
l!l&&r!l&&!r
005
MC/DC60%3/5
!defined($host) || $host eq '' || $host eq '0' || $host eq '0.0.0.0' || $host eq '::'
not defined $host $host eq "" $host eq 0 $host eq "0.0.0.0" $host eq '::'
Truth table66%4/6
not(A) or B or C or D || E
A= not defined $hostB= $host eq ""C= $host eq 0D= $host eq "0.0.0.0"E= $host eq '::'
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
209 !defined $host
210 || $host eq ''
211 || $host eq '0'
212 || $host eq '0.0.0.0'
213 || $host eq '::';
214  
215 5 ▶ my $ipv6 = defined $host && $host eq '::';
Condition66%2/3
defined $host && $host eq '::'
!ll&&!rl&&r
041
MC/DC50%1/2
defined $host && $host eq '::'
defined $host $host eq '::'
Truth table66%2/3
A && B
A= defined $hostB= $host eq '::'
ABresult
0X0
100
111
216 5 ▶ my $loopback = $ipv6 ? '::1' : '127.0.0.1';
Branch: 1 branch
BranchTrueFalseTotal
$ipv6 ? : 1 4 5
217 5 ▶ my $display = $all_interfaces ? $loopback : $host;
Branch: 1 branch
BranchTrueFalseTotal
$all_interfaces ? : 3 2 5
218 5 ▶ my $url_host = $display =~ /:/ ? "[$display]" : $display;
Branch: 1 branch
BranchTrueFalseTotal
$display =~ /:/ ? : 1 4 5
219  
220 5 print "Exporting '$self->{root}', available at:\n";
221 5 print " $proto://$url_host:$port/\n";
222  
223 5 ▶ if ($all_interfaces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_interfaces) 3 2 5
224 3 print "\n";
225 3 print "WARNING: this server is reachable on all network interfaces, so\n";
226 3 print "other machines on your network can access it. To limit access to\n";
227 3 print "this computer only, restart with: --host $loopback\n";
228 }
229  
230 5 ▶ return unless my $name = $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless my $name = $self->{"name"} 5 0 5
231  
232 0 eval {
233 0 require Net::Rendezvous::Publish;
234 0 Net::Rendezvous::Publish->new->publish(
235 name => $name,
236 type => '_http._tcp',
237 port => $port,
238 domain => 'local',
239 );
240 };
241 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
242 0 print "\nWARNING: your server will not be published over Bonjour\n";
243 0 print " Install one of the Net::Rendezvous::Publish::Backend\n";
244 0 print " modules from CPAN\n"
245 }
246 }
247  
248 3 1;
249  
250  
251  
252 =head1 SEE ALSO
253  
254 L<http_this>, L<Plack>, L<Plack::App::DirectoryIndex>, and L<Net::Rendezvous::Publish>.
255  
256  
257 =head1 THANKS
258  
259 And the Oscar goes to: Tatsuhiko Miyagawa.
260  
261 For L<Plack>, L<Plack::App::Directory> and many many others.
↑ Summary