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   14444 use strict;
  34         70  
  34         988  
4 34     34   168 use warnings;
  34         61  
  34         903  
5              
6 34     34   154 use parent 'URI::_generic';
  34         70  
  34         197  
7              
8 34     34   1741 use URI::Escape qw(uri_unescape);
  34         73  
  34         51514  
9              
10             our $VERSION = '5.20';
11              
12             sub _uric_escape {
13 849     849   1410 my($class, $str) = @_;
14 849 100       4437 if ($str =~ m,^((?:$URI::scheme_re:)?)//([^/?\#]*)(.*)$,os) {
15 491         1633 my($scheme, $host, $rest) = ($1, $2, $3);
16 491 100       1270 my $ui = $host =~ s/(.*@)// ? $1 : "";
17 491 100       1141 my $port = $host =~ s/(:\d+)\z// ? $1 : "";
18 491 100       915 if (_host_escape($host)) {
19 5         35 $str = "$scheme//$ui$host$port$rest";
20             }
21             }
22 849         2523 return $class->SUPER::_uric_escape($str);
23             }
24              
25             sub _host_escape {
26 528     528   658 return if URI::HAS_RESERVED_SQUARE_BRACKETS and $_[0] !~ /[^$URI::uric]/;
27 522 100       3835 return if !URI::HAS_RESERVED_SQUARE_BRACKETS and $_[0] !~ /[^$URI::uric4host]/;
28 6         13 eval {
29 6         1044 require URI::_idna;
30 6         25 $_[0] = URI::_idna::encode($_[0]);
31             };
32 6 100       29 return 0 if $@;
33 5         16 return 1;
34             }
35              
36             sub as_iri {
37 15     15 1 1870 my $self = shift;
38 15         58 my $str = $self->SUPER::as_iri;
39 15 100       70 if ($str =~ /\bxn--/) {
40 5 50       68 if ($str =~ m,^((?:$URI::scheme_re:)?)//([^/?\#]*)(.*)$,os) {
41 5         21 my($scheme, $host, $rest) = ($1, $2, $3);
42 5 50       15 my $ui = $host =~ s/(.*@)// ? $1 : "";
43 5 50       13 my $port = $host =~ s/(:\d+)\z// ? $1 : "";
44 5         24 require URI::_idna;
45 5         14 $host = URI::_idna::decode($host);
46 5         21 $str = "$scheme//$ui$host$port$rest";
47             }
48             }
49 15         75 return $str;
50             }
51              
52             sub userinfo
53             {
54 94     94 1 450 my $self = shift;
55 94         201 my $old = $self->authority;
56              
57 94 100       207 if (@_) {
58 28         45 my $new = $old;
59 28 50       58 $new = "" unless defined $new;
60 28         86 $new =~ s/.*@//; # remove old stuff
61 28         55 my $ui = shift;
62 28 100       57 if (defined $ui) {
63 26         180 $ui =~ s/([^$URI::uric4user])/ URI::Escape::escape_char($1)/ego;
  12         44  
64 26         68 $new = "$ui\@$new";
65             }
66 28         61 $self->authority($new);
67             }
68 94 100 100     523 return undef if !defined($old) || $old !~ /(.*)@/;
69 76         223 return $1;
70             }
71              
72             sub host
73             {
74 481     481 1 4826 my $self = shift;
75 481         1104 my $old = $self->authority;
76 481 100       1039 if (@_) {
77 38         58 my $tmp = $old;
78 38 100       191 $tmp = "" unless defined $tmp;
79 38 100       198 my $ui = ($tmp =~ /(.*@)/) ? $1 : "";
80 38 100       155 my $port = ($tmp =~ /(:\d+)$/) ? $1 : "";
81 38         60 my $new = shift;
82 38 100       72 $new = "" unless defined $new;
83 38 100       82 if (length $new) {
84 37         82 $new =~ s/[@]/%40/g; # protect @
85 37 100 100     237 if ($new =~ /^[^:]*:\d*\z/ || $new =~ /]:\d*\z/) {
86 5 50       29 $new =~ s/(:\d*)\z// || die "Assert";
87 5         14 $port = $1;
88             }
89 37 100 100     168 $new = "[$new]" if $new =~ /:/ && $new !~ /^\[/; # IPv6 address
90 37         76 _host_escape($new);
91             }
92 38         158 $self->authority("$ui$new$port");
93             }
94 481 100       1047 return undef unless defined $old;
95 403         869 $old =~ s/.*@//;
96 403         740 $old =~ s/:\d+$//; # remove the port
97 403         679 $old =~ s{^\[(.*)\]$}{$1}; # remove brackets around IPv6 (RFC 3986 3.2.2)
98 403         988 return uri_unescape($old);
99             }
100              
101             sub ihost
102             {
103 5     5 1 12 my $self = shift;
104 5         13 my $old = $self->host(@_);
105 5 100       21 if ($old =~ /(^|\.)xn--/) {
106 3         18 require URI::_idna;
107 3         10 $old = URI::_idna::decode($old);
108             }
109 5         29 return $old;
110             }
111              
112             sub _port
113             {
114 488     488   683 my $self = shift;
115 488         948 my $old = $self->authority;
116 488 100       973 if (@_) {
117 39         62 my $new = $old;
118 39         151 $new =~ s/:\d*$//;
119 39         67 my $port = shift;
120 39 100       113 $new .= ":$port" if defined $port;
121 39         97 $self->authority($new);
122             }
123 488 100 100     2024 return $1 if defined($old) && $old =~ /:(\d*)$/;
124 421         873 return;
125             }
126              
127             sub port
128             {
129 98     98 1 2433 my $self = shift;
130 98         276 my $port = $self->_port(@_);
131 98 100 100     468 $port = $self->default_port if !defined($port) || $port eq "";
132 98         278 $port;
133             }
134              
135             sub host_port
136             {
137 10     10 1 27 my $self = shift;
138 10         30 my $old = $self->authority;
139 10 100       29 $self->host(shift) if @_;
140 10 50       20 return undef unless defined $old;
141 10         28 $old =~ s/.*@//; # zap userinfo
142 10         17 $old =~ s/:$//; # empty port should be treated the same a no port
143 10 100       37 $old .= ":" . $self->port unless $old =~ /:\d+$/;
144 10         29 $old;
145             }
146              
147              
148 0     0 1 0 sub default_port { undef }
149              
150             sub canonical
151             {
152 389     389 1 523 my $self = shift;
153 389         842 my $other = $self->SUPER::canonical;
154 389   100     948 my $host = $other->host || "";
155 389         868 my $port = $other->_port;
156 389         672 my $uc_host = $host =~ /[A-Z]/;
157 389   100     787 my $def_port = defined($port) && ($port eq "" ||
158             $port == $self->default_port);
159 389 100 100     1178 if ($uc_host || $def_port) {
160 19 100       72 $other = $other->clone if $other == $self;
161 19 100       186 $other->host(lc $host) if $uc_host;
162 19 100       62 $other->port(undef) if $def_port;
163             }
164 389         836 $other;
165             }
166              
167             1;