File Coverage

blib/lib/URI/Simple.pm
Criterion Covered Total %
statement 46 49 93.8
branch 9 12 75.0
condition 2 2 100.0
subroutine 10 10 100.0
pod 0 5 0.0
total 67 78 85.9


line stmt bran cond sub pod time code
1             package URI::Simple;
2 1     1   36724 use strict;
  1         3  
  1         58  
3 1     1   5 use warnings;
  1         2  
  1         730  
4            
5             #==========================================================================
6             # Regex
7             #==========================================================================
8             our $VERSION = '1.00';
9             my $REGEX = {
10             strictMode => 0,
11             key => ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","querystring","anchor"],
12            
13             q => {
14             name => "query",
15             parser => qr{(?:^|&)([^&=]*)=?([^&]*)}
16             },
17            
18             parser => {
19             strict => qr/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
20             loose => qr/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
21             }
22             };
23            
24             #==========================================================================
25             # Quick Accessors on load
26             #==========================================================================
27             my @subs = ( @ { $REGEX->{key} },'query' );
28             foreach my $method (@subs){
29             my $code = __PACKAGE__.'::'.$method;
30             {
31 1     1   7 no strict 'refs';
  1         17  
  1         1027  
32             *$code = sub {
33 16     16   275 my $self = shift;
34 16 100       56 return $method eq 'query' ? $self->{$method} : uri_decode($self->{$method});
35             };
36             }
37             }
38            
39             sub new {
40 4     4 0 17 my $class = shift;
41 4         7 my $url = shift;
42 4         6 my $isStrict = shift;
43 4         8 return bless ( parseUri($url,$isStrict) , $class);
44             }
45            
46 2     2 0 19 sub scheme { shift->{protocol} }
47 1     1 0 4 sub fragment { shift->{anchor} }
48            
49             #==========================================================================
50             # Parsing sub
51             #==========================================================================
52             sub parseUri {
53 4     4 0 6 my $str = shift;
54 4         5 my $mode = shift;
55 4         6 my $o = $REGEX;
56 4 50       13 my $m = $o->{parser}->{ $mode ? "loose" : "strict" };
57 4         11 my @m = _exec($m,$str);
58 4         9 my $uri = {};
59 4         8 my $i = 14;
60 4   100     10 while ($i--) { $uri->{ $o->{key}->[$i] } = $m[$i] || "" };
  56         190  
61 4         10 $uri->{ $o->{q}->{name} } = {};
62 4         6 my $p = $o->{q}->{parser};
63 4         60 my @q = $uri->{ $o->{key}->[12] } =~ /$p/g;
64            
65 4 100       13 if ($1){
66 3         9 while (@q){
67 6         8 my $value = pop @q;
68 6         8 my $key = pop @q;
69 6 100       17 if ( $uri->{ $o->{q}->{name} }->{$key} ){
70 1 50       4 if (ref $uri->{ $o->{q}->{name} }->{$key} eq 'ARRAY'){
71 0         0 push ( @{$uri->{ $o->{q}->{name} }->{$key}} , uri_decode($value) );
  0         0  
72             } else {
73 1         3 $uri->{ $o->{q}->{name} }->{$key} = [$uri->{ $o->{q}->{name} }->{$key} , uri_decode($value) ];
74             }
75             } else {
76 5         10 $uri->{ $o->{q}->{name} }->{$key} = uri_decode($value);
77             }
78             }
79             }
80            
81 4         19 return $uri;
82             }
83            
84             #==========================================================================
85             # javascript like exec function
86             #==========================================================================
87             sub _exec {
88 4     4   5 my ($expr,$string) = @_;
89 4         66 my @m = $string =~ $expr;
90            
91             #javascript exe method adds the whole matched strig
92             #to the results array as index[0]
93 4 50       14 if (@m){
94 4         21 unshift @m, substr $string,$-[0],$+[0];
95             }
96 4         26 return @m;
97             }
98            
99             sub uri_decode {
100 20     20 0 29 $_[0] =~ tr/+/ /;
101 20         29 $_[0] =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
  0         0  
102 20         568 return $_[0];
103             }
104            
105             1;
106            
107             __END__