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 34     34   14549 use strict;
  34         77  
  34         958  
4 34     34   162 use warnings;
  34         63  
  34         847  
5              
6 34     34   157 use parent 'URI::_generic';
  34         64  
  34         189  
7              
8 34     34   1794 use URI::Escape qw(uri_unescape);
  34         68  
  34         53640  
9              
10             our $VERSION = '5.21';
11              
12             sub _uric_escape {
13 849     849   1409 my($class, $str) = @_;
14 849 100       4427 if ($str =~ m,^((?:$URI::scheme_re:)?)//([^/?\#]*)(.*)$,os) {
15 491         1575 my($scheme, $host, $rest) = ($1, $2, $3);
16 491 100       1254 my $ui = $host =~ s/(.*@)// ? $1 : "";
17 491 100       1101 my $port = $host =~ s/(:\d+)\z// ? $1 : "";
18 491 100       856 if (_host_escape($host)) {
19 5         17 $str = "$scheme//$ui$host$port$rest";
20             }
21             }
22 849         2351 return $class->SUPER::_uric_escape($str);
23             }
24              
25             sub _host_escape {
26 528     528   711 return if URI::HAS_RESERVED_SQUARE_BRACKETS and $_[0] !~ /[^$URI::uric]/;
27 522 100       3603 return if !URI::HAS_RESERVED_SQUARE_BRACKETS and $_[0] !~ /[^$URI::uric4host]/;
28 6         12 eval {
29 6         941 require URI::_idna;
30 6         26 $_[0] = URI::_idna::encode($_[0]);
31             };
32 6 100       29 return 0 if $@;
33 5         13 return 1;
34             }
35              
36             sub as_iri {
37 15     15 1 1892 my $self = shift;
38 15         43 my $str = $self->SUPER::as_iri;
39 15 100       63 if ($str =~ /\bxn--/) {
40 5 50       63 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       12 my $port = $host =~ s/(:\d+)\z// ? $1 : "";
44 5         23 require URI::_idna;
45 5         15 $host = URI::_idna::decode($host);
46 5         18 $str = "$scheme//$ui$host$port$rest";
47             }
48             }
49 15         75 return $str;
50             }
51              
52             sub userinfo
53             {
54 94     94 1 449 my $self = shift;
55 94         194 my $old = $self->authority;
56              
57 94 100       201 if (@_) {
58 28         46 my $new = $old;
59 28 50       62 $new = "" unless defined $new;
60 28         86 $new =~ s/.*@//; # remove old stuff
61 28         47 my $ui = shift;
62 28 100       58 if (defined $ui) {
63 26         183 $ui =~ s/([^$URI::uric4user])/ URI::Escape::escape_char($1)/ego;
  12         35  
64 26         63 $new = "$ui\@$new";
65             }
66 28         65 $self->authority($new);
67             }
68 94 100 100     472 return undef if !defined($old) || $old !~ /(.*)@/;
69 76         214 return $1;
70             }
71              
72             sub host
73             {
74 481     481 1 4165 my $self = shift;
75 481         1070 my $old = $self->authority;
76 481 100       1028 if (@_) {
77 38         67 my $tmp = $old;
78 38 100       175 $tmp = "" unless defined $tmp;
79 38 100       205 my $ui = ($tmp =~ /(.*@)/) ? $1 : "";
80 38 100       166 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         90 $new =~ s/[@]/%40/g; # protect @
85 37 100 100     234 if ($new =~ /^[^:]*:\d*\z/ || $new =~ /]:\d*\z/) {
86 5 50       40 $new =~ s/(:\d*)\z// || die "Assert";
87 5         15 $port = $1;
88             }
89 37 100 100     172 $new = "[$new]" if $new =~ /:/ && $new !~ /^\[/; # IPv6 address
90 37         79 _host_escape($new);
91             }
92 38         151 $self->authority("$ui$new$port");
93             }
94 481 100       1051 return undef unless defined $old;
95 403         782 $old =~ s/.*@//;
96 403         746 $old =~ s/:\d+$//; # remove the port
97 403         664 $old =~ s{^\[(.*)\]$}{$1}; # remove brackets around IPv6 (RFC 3986 3.2.2)
98 403         980 return uri_unescape($old);
99             }
100              
101             sub ihost
102             {
103 5     5 1 10 my $self = shift;
104 5         15 my $old = $self->host(@_);
105 5 100       21 if ($old =~ /(^|\.)xn--/) {
106 3         17 require URI::_idna;
107 3         8 $old = URI::_idna::decode($old);
108             }
109 5         24 return $old;
110             }
111              
112             sub _port
113             {
114 488     488   646 my $self = shift;
115 488         1012 my $old = $self->authority;
116 488 100       1017 if (@_) {
117 39         63 my $new = $old;
118 39         143 $new =~ s/:\d*$//;
119 39         77 my $port = shift;
120 39 100       104 $new .= ":$port" if defined $port;
121 39         96 $self->authority($new);
122             }
123 488 100 100     1902 return $1 if defined($old) && $old =~ /:(\d*)$/;
124 421         765 return;
125             }
126              
127             sub port
128             {
129 98     98 1 2785 my $self = shift;
130 98         266 my $port = $self->_port(@_);
131 98 100 100     433 $port = $self->default_port if !defined($port) || $port eq "";
132 98         338 $port;
133             }
134              
135             sub host_port
136             {
137 10     10 1 49 my $self = shift;
138 10         32 my $old = $self->authority;
139 10 100       31 $self->host(shift) if @_;
140 10 50       31 return undef unless defined $old;
141 10         27 $old =~ s/.*@//; # zap userinfo
142 10         17 $old =~ s/:$//; # empty port should be treated the same a no port
143 10 100       40 $old .= ":" . $self->port unless $old =~ /:\d+$/;
144 10         34 $old;
145             }
146              
147              
148 0     0 1 0 sub default_port { undef }
149              
150             sub canonical
151             {
152 389     389 1 615 my $self = shift;
153 389         810 my $other = $self->SUPER::canonical;
154 389   100     998 my $host = $other->host || "";
155 389         872 my $port = $other->_port;
156 389         671 my $uc_host = $host =~ /[A-Z]/;
157 389   100     801 my $def_port = defined($port) && ($port eq "" ||
158             $port == $self->default_port);
159 389 100 100     1196 if ($uc_host || $def_port) {
160 19 100       67 $other = $other->clone if $other == $self;
161 19 100       184 $other->host(lc $host) if $uc_host;
162 19 100       63 $other->port(undef) if $def_port;
163             }
164 389         830 $other;
165             }
166              
167             1;