File Coverage

blib/lib/URI/_ldap.pm
Criterion Covered Total %
statement 83 88 94.3
branch 28 36 77.7
condition 3 9 33.3
subroutine 13 13 100.0
pod 0 6 0.0
total 127 152 83.5


line stmt bran cond sub pod time code
1             # Copyright (c) 1998 Graham Barr . All rights reserved.
2             # This program is free software; you can redistribute it and/or
3             # modify it under the same terms as Perl itself.
4              
5             package URI::_ldap;
6              
7 1     1   434 use strict;
  1         2  
  1         29  
8 1     1   5 use warnings;
  1         1  
  1         40  
9              
10             our $VERSION = '5.19';
11              
12 1     1   5 use URI::Escape qw(uri_unescape);
  1         2  
  1         1145  
13              
14             sub _ldap_elem {
15 39     39   55 my $self = shift;
16 39         48 my $elem = shift;
17 39         82 my $query = $self->query;
18 39 100       187 my @bits = (split(/\?/,defined($query) ? $query : ""),("")x4);
19 39         67 my $old = $bits[$elem];
20              
21 39 100       78 if (@_) {
22 17         21 my $new = shift;
23 17         32 $new =~ s/\?/%3F/g;
24 17         23 $bits[$elem] = $new;
25 17         45 $query = join("?",@bits);
26 17         63 $query =~ s/\?+$//;
27 17 100       39 $query = undef unless length($query);
28 17         40 $self->query($query);
29             }
30              
31 39         86 $old;
32             }
33              
34             sub dn {
35 8     8 0 38 my $old = shift->path(@_);
36 8         25 $old =~ s:^/::;
37 8         21 uri_unescape($old);
38             }
39              
40             sub attributes {
41 11     11 0 19 my $self = shift;
42 11 100       27 my $old = _ldap_elem($self,0, @_ ? join(",", map { my $tmp = $_; $tmp =~ s/,/%2C/g; $tmp } @_) : ());
  11         19  
  11         21  
  11         29  
43 11 100       34 return $old unless wantarray;
44 5         12 map { uri_unescape($_) } split(/,/,$old);
  12         25  
45             }
46              
47             sub _scope {
48 10     10   15 my $self = shift;
49 10         20 my $old = _ldap_elem($self,1, @_);
50 10 50 33     37 return undef unless defined wantarray && defined $old;
51 10         27 uri_unescape($old);
52             }
53              
54             sub scope {
55 9     9 0 19 my $old = &_scope;
56 9 100       25 $old = "base" unless length $old;
57 9         19 $old;
58             }
59              
60             sub _filter {
61 11     11   22 my $self = shift;
62 11         17 my $old = _ldap_elem($self,2, @_);
63 11 50 33     41 return undef unless defined wantarray && defined $old;
64 11         27 uri_unescape($old); # || "(objectClass=*)";
65             }
66              
67             sub filter {
68 10     10 0 20 my $old = &_filter;
69 10 100       21 $old = "(objectClass=*)" unless length $old;
70 10         22 $old;
71             }
72              
73             sub extensions {
74 7     7 0 12 my $self = shift;
75 7         8 my @ext;
76 7         16 while (@_) {
77 5         10 my $key = shift;
78 5         7 my $value = shift;
79 5 50       8 push(@ext, join("=", map { $_="" unless defined; s/,/%2C/g; $_ } $key, $value));
  10         19  
  10         15  
  10         26  
80             }
81 7 100       22 @ext = join(",", @ext) if @ext;
82 7         16 my $old = _ldap_elem($self,3, @ext);
83 7 100       18 return $old unless wantarray;
84 4         10 map { uri_unescape($_) } map { /^([^=]+)=(.*)$/ } split(/,/,$old);
  12         24  
  6         26  
85             }
86              
87             sub canonical
88             {
89 2     2 0 31 my $self = shift;
90 2         5 my $other = $self->_nonldap_canonical;
91              
92             # The stuff below is not as efficient as one might hope...
93              
94 2 50       6 $other = $other->clone if $other == $self;
95              
96 2         20 $other->dn(_normalize_dn($other->dn));
97              
98             # Should really know about mixed case "postalAddress", etc...
99 2         5 $other->attributes(map lc, $other->attributes);
100              
101             # Lowercase scope, remove default
102 2         6 my $old_scope = $other->scope;
103 2         48 my $new_scope = lc($old_scope);
104 2 50       41 $new_scope = "" if $new_scope eq "base";
105 2 50       14 $other->scope($new_scope) if $new_scope ne $old_scope;
106              
107             # Remove filter if default
108 2         5 my $old_filter = $other->filter;
109 2 50 33     14 $other->filter("") if lc($old_filter) eq "(objectclass=*)" ||
110             lc($old_filter) eq "objectclass=*";
111              
112             # Lowercase extensions types and deal with known extension values
113 2         5 my @ext = $other->extensions;
114 2         12 for (my $i = 0; $i < @ext; $i += 2) {
115 4         8 my $etype = $ext[$i] = lc($ext[$i]);
116 4 100       14 if ($etype =~ /^!?bindname$/) {
117 2         6 $ext[$i+1] = _normalize_dn($ext[$i+1]);
118             }
119             }
120 2 50       8 $other->extensions(@ext) if @ext;
121            
122 2         11 $other;
123             }
124              
125             sub _normalize_dn # RFC 2253
126             {
127 4     4   7 my $dn = shift;
128              
129 4         13 return $dn;
130             # The code below will fail if the "+" or "," is embedding in a quoted
131             # string or simply escaped...
132              
133 0           my @dn = split(/([+,])/, $dn);
134 0           for (@dn) {
135 0           s/^([a-zA-Z]+=)/lc($1)/e;
  0            
136             }
137 0           join("", @dn);
138             }
139              
140             1;