File Coverage

blib/lib/URI/_server.pm
Criterion Covered Total %
statement 113 114 99.1
branch 66 72 91.6
condition 23 23 100.0
subroutine 14 15 93.3
pod 8 8 100.0
total 224 232 96.5


line stmt bran cond sub pod time code
1             package URI::_server;
2              
3 33     33   13887 use strict;
  33         85  
  33         914  
4 33     33   177 use warnings;
  33         67  
  33         837  
5              
6 33     33   158 use parent 'URI::_generic';
  33         59  
  33         195  
7              
8 33     33   1672 use URI::Escape qw(uri_unescape);
  33         69  
  33         49981  
9              
10             our $VERSION = '5.19';
11              
12             sub _uric_escape {
13 846     846   1384 my($class, $str) = @_;
14 846 100       4468 if ($str =~ m,^((?:$URI::scheme_re:)?)//([^/?\#]*)(.*)$,os) {
15 488         1601 my($scheme, $host, $rest) = ($1, $2, $3);
16 488 100       1224 my $ui = $host =~ s/(.*@)// ? $1 : "";
17 488 100       1140 my $port = $host =~ s/(:\d+)\z// ? $1 : "";
18 488 100       901 if (_host_escape($host)) {
19 5         21 $str = "$scheme//$ui$host$port$rest";
20             }
21             }
22 846         2389 return $class->SUPER::_uric_escape($str);
23             }
24              
25             sub _host_escape {
26 525     525   675 return if URI::HAS_RESERVED_SQUARE_BRACKETS and $_[0] !~ /[^$URI::uric]/;
27 519 100       3600 return if !URI::HAS_RESERVED_SQUARE_BRACKETS and $_[0] !~ /[^$URI::uric4host]/;
28 6         14 eval {
29 6         1067 require URI::_idna;
30 6         24 $_[0] = URI::_idna::encode($_[0]);
31             };
32 6 100       34 return 0 if $@;
33 5         15 return 1;
34             }
35              
36             sub as_iri {
37 15     15 1 2382 my $self = shift;
38 15         46 my $str = $self->SUPER::as_iri;
39 15 100       77 if ($str =~ /\bxn--/) {
40 5 50       69 if ($str =~ m,^((?:$URI::scheme_re:)?)//([^/?\#]*)(.*)$,os) {
41 5         21 my($scheme, $host, $rest) = ($1, $2, $3);
42 5 50       13 my $ui = $host =~ s/(.*@)// ? $1 : "";
43 5 50       20 my $port = $host =~ s/(:\d+)\z// ? $1 : "";
44 5         23 require URI::_idna;
45 5         13 $host = URI::_idna::decode($host);
46 5         18 $str = "$scheme//$ui$host$port$rest";
47             }
48             }
49 15         112 return $str;
50             }
51              
52             sub userinfo
53             {
54 94     94 1 566 my $self = shift;
55 94         208 my $old = $self->authority;
56              
57 94 100       199 if (@_) {
58 28         54 my $new = $old;
59 28 50       56 $new = "" unless defined $new;
60 28         85 $new =~ s/.*@//; # remove old stuff
61 28         49 my $ui = shift;
62 28 100       59 if (defined $ui) {
63 26         179 $ui =~ s/([^$URI::uric4user])/ URI::Escape::escape_char($1)/ego;
  12         34  
64 26         71 $new = "$ui\@$new";
65             }
66 28         72 $self->authority($new);
67             }
68 94 100 100     494 return undef if !defined($old) || $old !~ /(.*)@/;
69 76         220 return $1;
70             }
71              
72             sub host
73             {
74 479     479 1 5150 my $self = shift;
75 479         1226 my $old = $self->authority;
76 479 100       1135 if (@_) {
77 38         60 my $tmp = $old;
78 38 100       192 $tmp = "" unless defined $tmp;
79 38 100       200 my $ui = ($tmp =~ /(.*@)/) ? $1 : "";
80 38 100       168 my $port = ($tmp =~ /(:\d+)$/) ? $1 : "";
81 38         63 my $new = shift;
82 38 100       85 $new = "" unless defined $new;
83 38 100       86 if (length $new) {
84 37         93 $new =~ s/[@]/%40/g; # protect @
85 37 100 100     233 if ($new =~ /^[^:]*:\d*\z/ || $new =~ /]:\d*\z/) {
86 5 50       32 $new =~ s/(:\d*)\z// || die "Assert";
87 5         14 $port = $1;
88             }
89 37 100 100     198 $new = "[$new]" if $new =~ /:/ && $new !~ /^\[/; # IPv6 address
90 37         96 _host_escape($new);
91             }
92 38         153 $self->authority("$ui$new$port");
93             }
94 479 100       1078 return undef unless defined $old;
95 401         788 $old =~ s/.*@//;
96 401         705 $old =~ s/:\d+$//; # remove the port
97 401         682 $old =~ s{^\[(.*)\]$}{$1}; # remove brackets around IPv6 (RFC 3986 3.2.2)
98 401         1035 return uri_unescape($old);
99             }
100              
101             sub ihost
102             {
103 5     5 1 10 my $self = shift;
104 5         19 my $old = $self->host(@_);
105 5 100       20 if ($old =~ /(^|\.)xn--/) {
106 3         18 require URI::_idna;
107 3         8 $old = URI::_idna::decode($old);
108             }
109 5         26 return $old;
110             }
111              
112             sub _port
113             {
114 480     480   747 my $self = shift;
115 480         1016 my $old = $self->authority;
116 480 100       1040 if (@_) {
117 35         59 my $new = $old;
118 35         147 $new =~ s/:\d*$//;
119 35         78 my $port = shift;
120 35 100       109 $new .= ":$port" if defined $port;
121 35         85 $self->authority($new);
122             }
123 480 100 100     1987 return $1 if defined($old) && $old =~ /:(\d*)$/;
124 417         727 return;
125             }
126              
127             sub port
128             {
129 91     91 1 1914 my $self = shift;
130 91         256 my $port = $self->_port(@_);
131 91 100 100     460 $port = $self->default_port if !defined($port) || $port eq "";
132 91         290 $port;
133             }
134              
135             sub host_port
136             {
137 10     10 1 52 my $self = shift;
138 10         29 my $old = $self->authority;
139 10 100       32 $self->host(shift) if @_;
140 10 50       25 return undef unless defined $old;
141 10         30 $old =~ s/.*@//; # zap userinfo
142 10         18 $old =~ s/:$//; # empty port should be treated the same a no port
143 10 100       36 $old .= ":" . $self->port unless $old =~ /:\d+$/;
144 10         44 $old;
145             }
146              
147              
148 0     0 1 0 sub default_port { undef }
149              
150             sub canonical
151             {
152 388     388 1 532 my $self = shift;
153 388         852 my $other = $self->SUPER::canonical;
154 388   100     932 my $host = $other->host || "";
155 388         907 my $port = $other->_port;
156 388         690 my $uc_host = $host =~ /[A-Z]/;
157 388   100     856 my $def_port = defined($port) && ($port eq "" ||
158             $port == $self->default_port);
159 388 100 100     1138 if ($uc_host || $def_port) {
160 19 100       76 $other = $other->clone if $other == $self;
161 19 100       178 $other->host(lc $host) if $uc_host;
162 19 100       89 $other->port(undef) if $def_port;
163             }
164 388         875 $other;
165             }
166              
167             1;