blib/lib/dbgp-helper/DB/DbgrURI.pm

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 161 CC 23 SCAR 36.3
CC
23
Cov
0%
canonicalizeURI
90.0
wrap_rel2abs
30.0
final_check
30.0
CRAP 37.8
« blib/lib/dbgp-helper/DB/DbgrCommon.pm ↑ Summary blib/lib/Devel/Debug/DBGp.pm »
1 # DbgrURI.pm -- Manage Session filenames and URIs in this module
2 #
3 # Copyright (c) 2004-2006 ActiveState Software Inc.
4 # All rights reserved.
5 #
6 # Xdebug compatibility, UNIX domain socket support and misc fixes
7 # by Mattia Barbon <mattia@barbon.org>
8 #
9 # This software (the Perl-DBGP package) is covered by the Artistic License
10 # (http://www.opensource.org/licenses/artistic-license.php).
11  
12 package DB::DbgrURI;
13  
14 use DB::DbgrCommon;
15 use DB::OSType;
16  
17 use File::Spec::Functions qw(
18 canonpath
19 catdir
20 splitpath
21 splitdir
22 catpath
23 rel2abs
24 );
25  
26 use strict;
27 use vars qw($VERSION
28 @ISA
29 @EXPORT
30 @EXPORT_OK
31 %mem_uriToFilename
32 %mem_canonicalizeURI
33 %filenameToURI
34 $ldebug
35 $main_cwd
36 %module_paths
37 );
38  
39 $VERSION = 0.10;
40 require Exporter;
41 @ISA = qw(Exporter);
42 # @EXPORT = qw( );
43 @EXPORT_OK = qw(canonicalizeFName
44 canonicalizeURI
45 filenameToURI
46 uriToFilename
47 );
48  
49 # Memoize things we look up often
50 %mem_uriToFilename = ();
51 %filenameToURI = ();
52  
53 # ---------------- Configuring routine
54  
55 sub init {
56 0 ▶ my %options = @_;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
57 0 $ldebug = delete $options{ldebug};
58 0 $main_cwd = delete $options{cwd};
59 }
60  
61 # ---------------- Exported Routines
62  
63 sub canonicalizeFName {
64 0 ▶ my $fname = shift;
Subroutine canonicalizeFName: not called CC=1 SCAR=6.9
Pod undocumented
65 0 $fname = canonpath($fname);
66 0 if (IS_WIN32) {
67 # Map everything to one case on Windows
68 $fname = lc $fname;
69 $fname =~ s@\\@/@g;
70 }
71 0 return $fname;
72 }
73  
74 # The best way is to first unescape the URI, and then
75 # re-escape it so that incoming URIs follow the format
76 # used internally.
77 # Memoize because we have more queries than URIs we query on.
78  
79 sub canonicalizeURI {
80 0 ▶ my ($bFileURI) = @_;
Subroutine canonicalizeURI: not called CC=9 SCAR=45.0
Pod undocumented
81 0 ▶ return $mem_canonicalizeURI{$bFileURI} if $mem_canonicalizeURI{$bFileURI};
Branch: 1 branch
BranchTrueFalseTotal
if $DB::DbgrURI::mem_canonicalizeURI{$bFileURI} 0 0 0
82 0 ▶ if ($bFileURI =~ m@^dbgp://@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bFileURI =~ m[^dbgp://]) 0 0 0
83 0 return ($mem_canonicalizeURI{$bFileURI} = $bFileURI);
84 }
85 0 local $@;
86 0 my $new_URI = eval { filenameToURI(uriToFilename($bFileURI), 1) };
87 0 ▶ if ($@ || !$new_URI) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ or not $new_URI) { } 0 0 0
elsif ($new_URI ne $bFileURI) { } 0 0 0
Condition0%0/3
$@ or not $new_URI
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $new_URI
$@ not $new_URI
Truth table0%0/3
A or not(B)
A= $@B= not $new_URI
ABresult
1X1
011
000
88 0 $new_URI = $bFileURI;
89 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
90 0 ▶ dblog($@) if $ldebug;
Branch: 1 branch
BranchTrueFalseTotal
if $DB::DbgrURI::ldebug 0 0 0
91 }
92 } elsif ($new_URI ne $bFileURI) {
93 0 ▶ dblog("canonicalizeURI($bFileURI) ==> $new_URI") if $ldebug;
Branch: 1 branch
BranchTrueFalseTotal
if $DB::DbgrURI::ldebug 0 0 0
94 }
95 0 $mem_canonicalizeURI{$bFileURI} = $new_URI;
96 0 return $new_URI;
97 }
98  
99 # wrap_rel2abs -- first try resolving relative paths vs.
100 # the current directory,
101 # then vs. the process's original directory
102 # then finally return the last determined path.
103 #
104 # Always ensure the path exists before memoizing it.
105 #
106 # Sometimes we return a non-existent dir, as in this case:
107  
108 =example
109  
110 chdir($dir1);
111 require ('foo.pm'); # Defines package Foo
112 chdir($dir2);
113 # This line finally steps into Foo
114 my $obj = Foo->new(); # Komodo debugger can't find it,
115 # returns a dbgp URI
116  
117 =cut
118  
119 sub wrap_rel2abs {
120 0 ▶ my $bFileName = shift;
Subroutine wrap_rel2abs: not called CC=5 SCAR=34.0
Pod undocumented
121 0 my $absFileName = rel2abs($bFileName);
122 0 ▶ if (-f $absFileName) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $absFileName) 0 0 0
123 0 return $module_paths{$bFileName} = $absFileName;
124 }
125 0 ▶ if ($main_cwd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DB::DbgrURI::main_cwd) 0 0 0
126 0 my $absFileName2 = rel2abs($bFileName, $main_cwd);
127 0 ▶ if (-f $absFileName2) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $absFileName2) 0 0 0
128 0 return $module_paths{$bFileName} = $absFileName;
129 }
130 }
131 0 ▶ if ($module_paths{$bFileName}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DB::DbgrURI::module_paths{$bFileName}) 0 0 0
132 0 return $module_paths{$bFileName};
133 }
134 # Return the original guess.
135 0 return $absFileName;
136 }
137  
138 sub filenameToURI {
139 0 ▶ my ($bFileName, $foldCase) = @_; # possibly backslashed name
Subroutine filenameToURI: not called CC=3 SCAR=24.8
Pod undocumented
140 0 my $origFileName = $bFileName;
141 0 my $fullKey = "$origFileName:$foldCase";
142 0 ▶ return $filenameToURI{$fullKey} if $filenameToURI{$fullKey};
Branch: 1 branch
BranchTrueFalseTotal
if $DB::DbgrURI::filenameToURI{$fullKey} 0 0 0
143 # dblog("filenameToURI - 0 - [$bFileName]");
144 0 my $start;
145 0 ▶ $start = "filenameToURI($bFileName) => " if $ldebug;
Branch: 1 branch
BranchTrueFalseTotal
if $ldebug 0 0 0
146 0 my $uri;
147 0 my $canon_URI;
148 0 my $leadingSlashes;
149 0 if (IS_WIN32) {
150 if ($bFileName =~ /^\w:[\\\/]/) {
151 # drive-letter -- colon -- path
152 $bFileName = final_check(canonpath($bFileName));
153 $bFileName = _encode_win_file_parts($bFileName, $foldCase);
154 $bFileName =~ s,\\,/,g;
155 $leadingSlashes = "///";
156 } elsif ($bFileName =~ /^\\\\/) {
157 # It's a UNC path
158 # Remove extra "." and "x\\.." components
159 $bFileName = final_check(canonpath(substr($bFileName, 1)));
160 # dblog("filenameToURI - 1 - [$bFileName // $1]");
161 $bFileName = '\\' . _encode_unix_file_parts($bFileName);
162 # dblog("filenameToURI - 2 - [$bFileName]");
163 $bFileName =~ s,\\,/,g;
164 # dblog("filenameToURI - 3 - [$bFileName]");
165 $leadingSlashes = "";
166 } else {
167 $bFileName = final_check(canonpath(wrap_rel2abs($bFileName)));
168 $bFileName = _encode_win_file_parts($bFileName, $foldCase);
169 $bFileName =~ s,\\,/,g;
170 $leadingSlashes = ($bFileName =~ /^\//) ? "//" : "///";
171 }
172 } else {
173 # Unix pathnames go through the mount table,
174 # so we never have a hostname here
175 0 $bFileName = final_check(canonpath(wrap_rel2abs($bFileName)));
176 0 $bFileName = _encode_unix_file_parts($bFileName);
177 0 $leadingSlashes = "//";
178 }
179 0 $canon_URI = "file:$leadingSlashes$bFileName";
180 # dblog("$start$canon_URI") if $ldebug;
181 0 $filenameToURI{$fullKey} = $canon_URI;
182 0 return $canon_URI;
183 }
184  
185 # Because canonpath doesn't fold inner "/x/../" sequences
186 sub final_check {
187 0 ▶ my ($fname) = @_;
Subroutine final_check: not called CC=5 SCAR=34.0
Pod undocumented
188 0 ▶ return $fname unless $fname =~ m@/\.\./@;
Branch: 1 branch
BranchTrueFalseTotal
unless $fname =~ m[/\.\./] 0 0 0
189 0 my $i = 0;
190 0 my @pieces = split(/\//, $fname, -1); #Keep trailing empty fields
191 0 ▶ while ($i <= $#pieces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#pieces) 0 0 0
192 0 ▶ if ($pieces[$i] eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pieces[$i] eq '..') { } 0 0 0
193 0 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
194 0 splice @pieces, 0, 1;
195 } else {
196 0 splice @pieces, $i - 1, 2;
197 0 $i--;
198 }
199 } else {
200 0 $i++;
201 }
202 }
203 0 return join("/", @pieces);
204 }
205  
206 # This one always returns an absolute forward-slashed filename
207 # Even UNC paths appear as "//<host>/<path>
208  
209 sub uriToFilename {
210 0 ▶ my ($bFileURI) = @_;
Subroutine uriToFilename: not called CC=4 SCAR=30.0
Pod undocumented
211 0 my $origURI = $bFileURI;
212 0 ▶ return $mem_uriToFilename{$origURI} if $mem_uriToFilename{$origURI};
Branch: 1 branch
BranchTrueFalseTotal
if $DB::DbgrURI::mem_uriToFilename{$origURI} 0 0 0
213 # Assume all file:// URIs are local
214
215 0 my $filename;
216 # Assume a dbgp URI denotes either a path starting with a
217 # drive letter, a Unix-type path, or a UNC path
218
219 0 ▶ if ($bFileURI =~ m@^file://(.*)$@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bFileURI =~ m[^file://(.*)$]) { } 0 0 0
220 0 my $path = $1;
221 0 my $path2;
222 0 ▶ if ($path =~ m@^/(\w:/.*)$@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^/(\w:/.*)$]) { } 0 0 0
223 0 $path2 = $1;
224 } elsif (IS_WIN32 && $path =~ m@^[^/]+/@) {
225 # It's a UNC path, so we need to put a slash back
226 $path2 = "/$path";
227 } else {
228 0 $path2 = $path;
229 }
230 0 my $file_part = _uri_decode($path2);
231 0 $filename = $file_part;
232 } else {
233 0 die "uriToFilename: Can't process URI $bFileURI";
234 }
235 0 $mem_uriToFilename{$origURI} = $filename;
236 0 return $filename;
237 }
238  
239 sub _encode_win_file_parts {
240 0 ▶ my ($full_win_name, $foldCase) = @_;
Subroutine _encode_win_file_parts: not called CC=2 SCAR=17.9
241 # dblog("_encode_win_file_parts, \$full_win_name = $full_win_name");
242 0 my ($volume, $directories, $path) = splitpath($full_win_name);
243 # dblog("_encode_win_file_parts, \$directories = $directories");
244 0 my @dir_parts = map { _uri_encode($_) } splitdir($directories);
245 # dblog("_encode_win_file_parts: split into @dir_parts");
246 0 my $new_name = catpath($volume,
247 catdir(@dir_parts),
248 _uri_encode($path));
249 0 ▶ $new_name = lc $new_name if $foldCase;
Branch: 1 branch
BranchTrueFalseTotal
if $foldCase 0 0 0
250 # dblog("_encode_win_file_parts: map [$full_win_name] => [$new_name]");
251 0 return $new_name;
252 }
253  
254 # Like Windows, but this kind of filename has no initial volume
255  
256 sub _encode_unix_file_parts {
257 0 ▶ my ($full_name) = @_;
Subroutine _encode_unix_file_parts: not called CC=1 SCAR=6.9
258 0 my @dir_parts = map { _uri_encode($_) } splitdir($full_name);
259 0 my $new_name = catdir(@dir_parts);
260 # dblog("_encode_unix_file_parts: map [$full_name] => [$new_name]");
261 0 return $new_name;
262 }
263  
264  
265 sub _uri_decode {
266 0 ▶ my $todecode = shift;
Subroutine _uri_decode: not called CC=1 SCAR=6.9
267 0 $todecode =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge;
268 0 return $todecode;
269 }
270  
271 sub _uri_encode {
272 0 ▶ my $toencode = shift;
Subroutine _uri_encode: not called CC=1 SCAR=6.9
273 # force bytes while preserving backward compatibility -- dankogai
274 0 $toencode = pack("C*", unpack("C*", $toencode));
275 0 $toencode=~s/([^a-zA-Z0-9_.-])/sprintf('%%%02x', ord($1))/eg;
276 0 return $toencode;
277 }
278  
279 unless (caller()) {
280 require Cwd;
281 my $cwd = Cwd::getcwd();
282 $cwd = lc $cwd if IS_WIN32;
283 $cwd =~ s,\\,/,g;
284 $cwd =~ s,/[^/]+$,,;
285 $cwd =~ s@^/@@;
286 my @fnames = (['\\\\host\\a\\b\\c\\d\\e.ext1',
287 'file://host/a/b/c/d/e.ext1'],
288 ["c:/a/z.ext2",
289 'file:///c:/a/z.ext2'],
290 ["..\\a\\b\\z.ext3",
291 "file:///$cwd/a/b/z.ext3"],
292 [
293 "/a/b",
294 "file:///a/b"
295 ],
296 [
297 "\\a\\b\\z.ext5",
298 "file:///a/b/z.ext5"
299 ],
300 [
301 "/skippy/the/bush/../happy/kangaroo.pl",
302 "file:///skippy/the/happy/kangaroo.pl"
303 ],
304 );
305 print IS_WIN32 ? "is win32\n" : "isn't win32\n";
306 foreach my $obj (@fnames) {
307 my $fname = $obj->[0];
308 print "Testing $fname...";
309 if (!IS_WIN32) {
310 if ($fname =~ m/^\\\\/) {
311 print "Skipping UNC path $fname\n";
312 next;
313 } elsif ($fname =~ /^\w:/) {
314 print "Skipping DOS file $fname\n";
315 next;
316 }
317 $fname =~ s@\\@/@g;
318 } else {
319 }
320 my $uri = filenameToURI($fname, 1);
321 if ($uri ne $obj->[1]) {
322 print "\n filenameToURI($fname) => [$uri], expecting [$obj->[1]]\n";
323 } else {
324 print " ok\n";
325 }
326 }
327 }
328  
329 1;
« blib/lib/dbgp-helper/DB/DbgrCommon.pm ↑ Summary blib/lib/Devel/Debug/DBGp.pm »