File Coverage

blib/lib/Protocol/WebSocket/URL.pm
Criterion Covered Total %
statement 39 39 100.0
branch 21 24 87.5
condition 9 13 69.2
subroutine 9 9 100.0
pod 7 7 100.0
total 85 92 92.3


line stmt bran cond sub pod time code
1             package Protocol::WebSocket::URL;
2              
3 15     15   753 use strict;
  15         27  
  15         414  
4 15     15   67 use warnings;
  15         25  
  15         6475  
5              
6             sub new {
7 212     212 1 818 my $class = shift;
8 212 50       349 $class = ref $class if ref $class;
9              
10 212         445 my $self = {@_};
11 212         312 bless $self, $class;
12              
13 212   100     656 $self->{secure} ||= 0;
14              
15 212         424 return $self;
16             }
17              
18 255 100   255 1 703 sub secure { @_ > 1 ? $_[0]->{secure} = $_[1] : $_[0]->{secure} }
19              
20 285 100   285 1 658 sub host { @_ > 1 ? $_[0]->{host} = $_[1] : $_[0]->{host} }
21 260 100   260 1 619 sub port { @_ > 1 ? $_[0]->{port} = $_[1] : $_[0]->{port} }
22              
23             sub resource_name {
24 243 100   243 1 660 @_ > 1 ? $_[0]->{resource_name} = $_[1] : $_[0]->{resource_name};
25             }
26              
27             sub parse {
28 31     31 1 64 my $self = shift;
29 31         42 my $string = shift;
30              
31 31         194 my ($scheme) = $string =~ m{^(wss?)://};
32 31 50       86 return unless $scheme;
33              
34 31 100       89 $self->secure(1) if $scheme =~ m/ss$/;
35              
36 31         460 my ($host, $port) = $string =~ m{^$scheme://([^:\/]+)(?::(\d+))?(?:|\/|$)};
37 31 50 33     150 $host = '/' unless defined $host && $host ne '';
38 31         86 $self->host($host);
39 31 100 66     113 $port ||= $self->secure ? 443 : 80;
40 31         75 $self->port($port);
41              
42             # path and query
43 31         236 my ($pnq) = $string =~ m{^$scheme://(?:.*?)(/.*)$};
44 31 100 66     122 $pnq = '/' unless defined $pnq && $pnq ne '';
45 31         85 $self->resource_name($pnq);
46              
47 31         119 return $self;
48             }
49              
50             sub to_string {
51 181     181 1 245 my $self = shift;
52              
53 181         221 my $string = '';
54              
55 181         252 $string .= 'ws';
56 181 100       259 $string .= 's' if $self->secure;
57 181         231 $string .= '://';
58 181         261 $string .= $self->host;
59 181 100       279 $string .= ':' . $self->port if defined $self->port;
60 181   100     287 $string .= $self->resource_name || '/';
61              
62 181         592 return $string;
63             }
64              
65             1;
66             __END__