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 46     46   25258 use strict;
  46         119  
  46         1774  
4 46     46   237 use warnings;
  46         83  
  46         2556  
5              
6 46     46   259 use parent 'URI::_generic';
  46         80  
  46         367  
7              
8 46     46   3313 use URI::Escape qw(uri_unescape);
  46         94  
  46         86113  
9              
10             our $VERSION = '5.34';
11              
12             sub _uric_escape {
13 864     864   1668 my($class, $str) = @_;
14 864 100       6926 if ($str =~ m,^((?:$URI::scheme_re:)?)//([^/?\#]*)(.*)$,os) {
15 505         2023 my($scheme, $host, $rest) = ($1, $2, $3);
16 505 100       1498 my $ui = $host =~ s/(.*@)// ? $1 : "";
17 505 100       1169 my $port = $host =~ s/(:\d+)\z// ? $1 : "";
18 505 100       1056 if (_host_escape($host)) {
19 5         19 $str = "$scheme//$ui$host$port$rest";
20             }
21             }
22 864         2974 return $class->SUPER::_uric_escape($str);
23             }
24              
25             sub _host_escape {
26 543     543   805 return if URI::HAS_RESERVED_SQUARE_BRACKETS and $_[0] !~ /[^$URI::uric]/;
27 537 100       5703 return if !URI::HAS_RESERVED_SQUARE_BRACKETS and $_[0] !~ /[^$URI::uric4host]/;
28 6         12 eval {
29 6         1327 require URI::_idna;
30 6         29 $_[0] = URI::_idna::encode($_[0]);
31             };
32 6 100       25 return 0 if $@;
33 5         15 return 1;
34             }
35              
36             sub as_iri {
37 15     15 1 2877 my $self = shift;
38 15         66 my $str = $self->SUPER::as_iri;
39 15 100       64 if ($str =~ /\bxn--/) {
40 5 50       83 if ($str =~ m,^((?:$URI::scheme_re:)?)//([^/?\#]*)(.*)$,os) {
41 5         26 my($scheme, $host, $rest) = ($1, $2, $3);
42 5 50       21 my $ui = $host =~ s/(.*@)// ? $1 : "";
43 5 50       16 my $port = $host =~ s/(:\d+)\z// ? $1 : "";
44 5         41 require URI::_idna;
45 5         16 $host = URI::_idna::decode($host);
46 5         16 $str = "$scheme//$ui$host$port$rest";
47             }
48             }
49 15         64 return $str;
50             }
51              
52             sub userinfo
53             {
54 136     136 1 630 my $self = shift;
55 136         402 my $old = $self->authority;
56              
57 136 100       315 if (@_) {
58 39         96 my $new = $old;
59 39 50       101 $new = "" unless defined $new;
60 39         156 $new =~ s/.*@//; # remove old stuff
61 39         75 my $ui = shift;
62 39 100       119 if (defined $ui) {
63 35         548 $ui =~ s/([^$URI::uric4user])/ URI::Escape::escape_char($1)/ego;
  16         53  
64 35         94 $new = "$ui\@$new";
65             }
66 39         111 $self->authority($new);
67             }
68 136 100 100     890 return undef if !defined($old) || $old !~ /(.*)@/;
69 107         360 return $1;
70             }
71              
72             sub host
73             {
74 491     491 1 7362 my $self = shift;
75 491         1403 my $old = $self->authority;
76 491 100       1080 if (@_) {
77 39         72 my $tmp = $old;
78 39 100       98 $tmp = "" unless defined $tmp;
79 39 100       194 my $ui = ($tmp =~ /(.*@)/) ? $1 : "";
80 39 100       211 my $port = ($tmp =~ /(:\d+)$/) ? $1 : "";
81 39         76 my $new = shift;
82 39 100       128 $new = "" unless defined $new;
83 39 100       110 if (length $new) {
84 38         82 $new =~ s/[@]/%40/g; # protect @
85 38 100 100     285 if ($new =~ /^[^:]*:\d*\z/ || $new =~ /]:\d*\z/) {
86 5 50       37 $new =~ s/(:\d*)\z// || die "Assert";
87 5         14 $port = $1;
88             }
89 38 100 100     192 $new = "[$new]" if $new =~ /:/ && $new !~ /^\[/; # IPv6 address
90 38         98 _host_escape($new);
91             }
92 39         170 $self->authority("$ui$new$port");
93             }
94 491 100       1173 return undef unless defined $old;
95 412         1005 $old =~ s/.*@//;
96 412         858 $old =~ s/:\d+$//; # remove the port
97 412         789 $old =~ s{^\[(.*)\]$}{$1}; # remove brackets around IPv6 (RFC 3986 3.2.2)
98 412         1224 return uri_unescape($old);
99             }
100              
101             sub ihost
102             {
103 5     5 1 12 my $self = shift;
104 5         20 my $old = $self->host(@_);
105 5 100       28 if ($old =~ /(^|\.)xn--/) {
106 3         19 require URI::_idna;
107 3         17 $old = URI::_idna::decode($old);
108             }
109 5         26 return $old;
110             }
111              
112             sub _port
113             {
114 509     509   696 my $self = shift;
115 509         1114 my $old = $self->authority;
116 509 100       1013 if (@_) {
117 45         97 my $new = $old;
118 45         190 $new =~ s/:\d*$//;
119 45         82 my $port = shift;
120 45 100       134 $new .= ":$port" if defined $port;
121 45         139 $self->authority($new);
122             }
123 509 100 100     2129 return $1 if defined($old) && $old =~ /:(\d*)$/;
124 435         805 return;
125             }
126              
127             sub port
128             {
129 117     117 1 3046 my $self = shift;
130 117         421 my $port = $self->_port(@_);
131 117 100 100     664 $port = $self->default_port if !defined($port) || $port eq "";
132 117         388 $port;
133             }
134              
135             sub host_port
136             {
137 10     10 1 40 my $self = shift;
138 10         32 my $old = $self->authority;
139 10 100       37 $self->host(shift) if @_;
140 10 50       21 return undef unless defined $old;
141 10         29 $old =~ s/.*@//; # zap userinfo
142 10         17 $old =~ s/:$//; # empty port should be treated the same a no port
143 10 100       58 $old .= ":" . $self->port unless $old =~ /:\d+$/;
144 10         36 $old;
145             }
146              
147              
148 0     0 1 0 sub default_port { undef }
149              
150             sub canonical
151             {
152 391     391 1 548 my $self = shift;
153 391         956 my $other = $self->SUPER::canonical;
154 391   100     956 my $host = $other->host || "";
155 391         870 my $port = $other->_port;
156 391         720 my $uc_host = $host =~ /[A-Z]/;
157 391   100     868 my $def_port = defined($port) && ($port eq "" ||
158             $port == $self->default_port);
159 391 100 100     1252 if ($uc_host || $def_port) {
160 19 100       69 $other = $other->clone if $other == $self;
161 19 100       167 $other->host(lc $host) if $uc_host;
162 19 100       116 $other->port(undef) if $def_port;
163             }
164 391         1148 $other;
165             }
166              
167             1;