Debian Codegraph @

src/.svn/text-base/uri.pl.svn-base

raw
1 #BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net)
2 #BSD All rights reserved.
3 #BSD
4 #BSD Redistribution and use in source and binary forms, with or without
5 #BSD modification, are permitted provided that the following conditions
6 #BSD are met:
7 #BSD
8 #BSD - Redistributions of source code must retain the above copyright
9 #BSD notice, this list of conditions and the following disclaimer.
10 #BSD
11 #BSD - Redistributions in binary form must reproduce the above copyright
12 #BSD notice, this list of conditions and the following disclaimer in the
13 #BSD documentation and/or other materials provided with the distribution.
14 #BSD
15 #BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 #BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 #BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 #BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 #BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 #BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 #BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 #BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 #BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 #BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 #BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 #BSD POSSIBILITY OF SUCH DAMAGE.
27
28 #################################################################
29
30 =item B<uri_split>
31
32 Params: $uri_string [, \%request_hash]
33
34 Return: @uri_parts
35
36 Return an array of the following values, in order: uri, protocol, host,
37 port, params, frag, user, password. Values not defined are given an undef
38 value. If a %request hash is passed in, then uri_split() will also set
39 the appropriate values in the hash.
40
41 Note: uri_split() will only set the %request hash if the protocol
42 is HTTP or HTTPS!
43
44 =cut
45
46 sub uri_split {
47 my ( $uri, $work ) = ( shift, '', 0 );
48 my ($hr) = shift;
49
50 # (uri,protocol,host,port,params,frag,user,pass)
51 my @res = ( undef, undef, undef, 0, undef, undef, undef, undef );
52
53 return undef if ( !defined $uri );
54
55 # remove fragments
56 ( $uri, $res[5] ) = split( '#', $uri, 2 ) if ( index( $uri, '#', 0 ) >= 0 );
57
58 # get scheme and net_loc
59 my $net_loc = undef;
60 if ( $uri =~ s/^([-+.a-z0-9A-Z]+):// ) {
61 $res[1] = lc($1);
62 if ( substr( $uri, 0, 2 ) eq '//' ) {
63 my $w = index( $uri, '/', 2 );
64 if ( $w >= 0 ) {
65 $net_loc = substr( $uri, 2, $w - 2 );
66 $uri = substr( $uri, $w, length($uri) - $w );
67 }
68 else {
69 ( $net_loc = $uri ) =~ tr#/##d;
70 $uri = '/';
71 }
72 }
73 }
74
75 # parse net_loc info
76 if ( defined $net_loc ) {
77 if ( index( $net_loc, '@', 0 ) >= 0 ) {
78 ( $res[6], $net_loc ) = split( /\@/, $net_loc, 2 );
79 if ( index( $res[6], ':', 0 ) >= 0 ) {
80 ( $res[6], $res[7] ) = split( ':', $res[6], 2 );
81 }
82 }
83 $res[3] = $1 if ( $net_loc =~ s/:([0-9]+)$// );
84 $res[2] = $net_loc;
85 }
86
87 # remove query info
88 ( $uri, $res[4] ) = split( '\?', $uri, 2 )
89 if ( index( $uri, '?', 0 ) >= 0 );
90
91 # whatever is left over is the uri
92 $res[0] = $uri;
93
94 if ( $res[3] == 0 && defined $res[1] ) {
95 $res[3] = 80 if ( $res[1] eq 'http' );
96 $res[3] = 443 if ( $res[1] eq 'https' );
97 }
98
99 my $rel_uri = 0;
100 $rel_uri++
101 if ( $res[3] == 0
102 && !defined $res[2]
103 && !defined $res[1]
104 && $res[0] ne '' );
105 return @res if ( $res[3] == 0 && !$rel_uri );
106
107 if ( defined $hr && ref($hr) ) {
108
109 $$hr{whisker}->{uri} = $res[0] if ( defined $res[0] );
110 if ( defined $res[4] ) {
111 $$hr{whisker}->{parameters} = $res[4];
112 }
113 else { delete $$hr{whisker}->{parameters}; }
114
115 return @res if ($rel_uri);
116
117 if ( $res[1] eq 'https' ) {
118 $$hr{whisker}->{ssl} = 1;
119 } else { $$hr{whisker}->{ssl} = 0; }
120 $$hr{whisker}->{host} = $res[2] if ( defined $res[2] );
121 $$hr{whisker}->{port} = $res[3];
122
123 if ( defined $res[6] ) {
124 $$hr{whisker}->{uri_user} = $res[6];
125 }
126 else { delete $$hr{whisker}->{uri_user}; }
127 if ( defined $res[7] ) {
128 $$hr{whisker}->{uri_password} = $res[7];
129 }
130 else { delete $$hr{whisker}->{uri_password}; }
131 }
132
133 return @res;
134 }
135
136 #################################################################
137
138 =item B<uri_join>
139
140 Params: @vals
141
142 Return: $url
143
144 Takes the @vals array output from http_split_uri, and returns a single
145 scalar/string with them joined again, in the form of:
146 protocol://user:pass@host:port/uri?params#frag
147
148 =cut
149
150 sub uri_join {
151 my @V = @_;
152 my $URL;
153
154 $URL .= $V[1] . ':' if defined $V[1];
155 if ( defined $V[2] ) {
156 $URL .= '//';
157 if ( defined $V[6] ) {
158 $URL .= $V[6];
159 $URL .= ':' . $V[7] if defined $V[7];
160 $URL .= '@';
161 }
162 $URL .= $V[2];
163 }
164
165 if ( $V[3] > 0 ) {
166 my $no = 0;
167 $no++ if ( $V[3] == 80 && defined $V[1] && $V[1] eq 'http' );
168 $no++ if ( $V[3] == 443 && defined $V[1] && $V[1] eq 'https' );
169 $URL .= ':' . $V[3] if ( !$no );
170 }
171
172 $URL .= $V[0];
173 $URL .= '?' . $V[4] if defined $V[4];
174 $URL .= '#' . $V[5] if defined $V[5];
175 return $URL;
176 }
177
178 #################################################################
179
180 =item B<uri_absolute>
181
182 Params: $uri, $base_uri [, $normalize_flag ]
183
184 Return: $absolute_uri
185
186 Double checks that the given $uri is in absolute form (that is,
187 "http://host/file"), and if not (it's in the form "/file"), then
188 it will append the given $base_uri to make it absolute. This
189 provides a compatibility similar to that found in the URI
190 subpackage.
191
192 If $normalize_flag is set to 1, then the output will be passed
193 through uri_normalize before being returned.
194
195 =cut
196
197 sub uri_absolute {
198 my ( $uri, $buri, $norm ) = @_;
199 return undef if ( !defined $uri || !defined $buri );
200
201 return $uri if ( $uri =~ m#^[-+.a-z0-9A-Z]+://# );
202
203 if ( substr( $uri, 0, 1 ) eq '/' ) {
204 if ( $buri =~ m#^[-+.a-z0-9A-Z]+://# ) {
205 my @p = uri_split($buri);
206 $buri = "$p[1]://$p[2]";
207 $buri .= ":$p[3]" if ( ($p[1] eq 'http' && $p[3] != 80) ||
208 ($p[1] eq 'https' && $p[3] != 443) );
209
210 # $buri.='/';
211 }
212 else { # ah suck, base URI isn't absolute...
213 return $uri;
214 }
215 }
216 else {
217 $buri =~ s/[?#].*$//; # remove params and fragments
218 $buri .= '/' if ( $buri =~ m#^[a-z]+://[^/]+$#i );
219 $buri =~ s#/[^/]*$#/#;
220 }
221
222 return uri_normalize("$buri$uri")
223 if ( defined $norm && $norm > 0 );
224 return $buri . $uri;
225 }
226
227 #################################################################
228
229 =item B<uri_normalize>
230
231 Params: $uri [, $fix_windows_slashes ]
232
233 Return: $normalized_uri [ undef on error ]
234
235 Takes the given $uri and does any /./ and /../ dereferencing in
236 order to come up with the correct absolute URL. If the $fix_
237 windows_slashes parameter is set to 1, all \ (back slashes) will
238 be converted to / (forward slashes).
239
240 Non-http/https URIs return an error.
241
242 =cut
243
244 sub uri_normalize {
245 my ( $host, $uri, $win ) = ( '', @_ );
246
247 $uri =~ tr#\\#/# if ( defined $win && $win > 0 );
248
249 if ( $uri =~ s#^([-+.a-z0-9A-Z]+:)## ) {
250 return undef if ( $1 ne 'http:' && $1 ne 'https:' );
251 $host = $1;
252 return undef unless ( $uri =~ s#^(//[^/]+)## );
253 $host .= $1;
254 }
255 return "$host/" if ( $uri eq '' || $uri eq '/' );
256
257 # fast path check
258 return "$host$uri" if ( index( $uri, '/.' ) < 0 );
259
260 my $extra = '';
261 $extra = $1 if($uri =~ s/([?#].*)$//); # remove params and fragments
262
263 # parse order/steps as defined in RFC 1808
264 1 while ( $uri =~ s#/\./#/# || $uri =~ s#//#/# );
265 $uri =~ s#/\.$#/#;
266 1 while ( $uri =~ s#[^/]+/\.\./## );
267 1 while ( $uri =~ s#^/\.\./#/# );
268 $uri =~ s#[^/]*/\.\.$##;
269 $uri ||= '/';
270 return $host . $uri . $extra;
271 }
272
273 #################################################################
274
275 =item B<uri_get_dir>
276
277 Params: $uri
278
279 Return: $uri_directory
280
281 Will take a URI and return the directory base of it, i.e. /rfp/page.php
282 will return /rfp/.
283
284 =cut
285
286 sub uri_get_dir {
287 my ( $w, $URL ) = ( 0, shift );
288
289 return undef if ( !defined $URL );
290 $URL = substr( $URL, 0, $w ) if ( ( $w = index( $URL, '#' ) ) >= 0 );
291 $URL = substr( $URL, 0, $w ) if ( ( $w = index( $URL, '?' ) ) >= 0 );
292 return $URL if ( substr( $URL, -1, 1 ) eq '/' );
293
294 if ( ( $w = rindex( $URL, '/' ) ) >= 0 ) {
295 $URL = substr( $URL, 0, $w + 1 );
296 }
297 else {
298 $URL = '';
299 }
300 return $URL;
301 }
302
303 #################################################################
304
305 =item B<uri_strip_path_parameters>
306
307 Params: $uri [, \%param_hash]
308
309 Return: $stripped_uri
310
311 This function removes all URI path parameters of the form
312
313 /blah1;foo=bar/blah2;baz
314
315 and returns the stripped URI ('/blah1/blah2'). If the optional
316 parameter hash reference is provided, the stripped parameters
317 are saved in the form of 'blah1'=>'foo=bar', 'blah2'=>'baz'.
318
319 Note: only the last value of a duplicate name is saved into the
320 param_hash, if provided. So a $uri of '/foo;A/foo;B/' will result
321 in a single hash entry of 'foo'=>'B'.
322
323 =cut
324
325 sub uri_strip_path_parameters {
326 my ( $uri, $hr ) = @_;
327 my $s = 0;
328 $s++ if ( defined $hr && ref($hr) );
329
330 my @p = split( /\//, $uri, -1 );
331 map {
332 if (s/;(.*)$//) { $$hr{$_} = $1 if ($s); }
333 } @p;
334
335 return join( '/', @p );
336 }
337
338 #################################################################
339
340 =item B<uri_parse_parameters>
341
342 Params: $parameter_string [, $decode, $multi_flag ]
343
344 Return: \%parameter_hash
345
346 This function takes a string in the form of:
347
348 foo=1&bar=2&baz=3&foo=4
349
350 And parses it into a hash. In the above example, the element 'foo'
351 has two values (1 and 4). If $multi_flag is set to 1, then the
352 'foo' hash entry will hold an anonymous array of both values.
353 Otherwise, the default is to just contain the last value (in this
354 case, '4').
355
356 If $decode is set to 1, then normal hex decoding is done on the
357 characters, where needed (both the name and value are decoded).
358
359 Note: if a URL parameter name appears without a value, then the
360 value will be set to undef. E.g. for the string "foo=1&bar&baz=2",
361 the 'bar' hash element will have an undef value.
362
363 =cut
364
365 sub uri_parse_parameters {
366 my ( $str, $decode, $multi ) = @_;
367 my %P;
368 if( $str !~ tr/=&// ){
369 $P{$str} = undef;
370 return \%P;
371 }
372
373 $multi ||= 0;
374 $decode ||= 0;
375 foreach ( split( /&/, $str ) ) {
376 my ( $name, $value ) = split( /=/, $_, 2 );
377 if ($decode) {
378 $name = uri_unescape($name);
379 $value = uri_unescape($value);
380 }
381 if ( defined $P{$name} && $multi ) {
382 if ( ref( $P{$name} ) ) { push @{ $P{$name} }, $value; }
383 else { $P{$name} = [ $P{$name}, $value ]; }
384 }
385 else {
386 $P{$name} = $value;
387 }
388 }
389 return \%P;
390 }
391
392 #################################################################
393
394 =item B<uri_escape>
395
396 Params: $data
397
398 Return: $encoded_data
399
400 This function encodes the given $data so it is safe to be used in URIs.
401
402 =cut
403
404 sub uri_escape {
405 my $data = shift;
406 return undef if ( !defined $data );
407 $data =~ s/\%/\%25/g;
408 $data =~ s/([+?&=#;@\\\/])/sprintf("%%%02x",ord($1))/eg;
409 $data =~ tr/ /+/;
410 $data =~ s/([^!-~])/sprintf("%%%02x",ord($1))/eg;
411 return $data;
412 }
413
414 #################################################################
415
416 =item B<uri_unescape>
417
418 Params: $encoded_data
419
420 Return: $data
421
422 This function decodes the given $data out of URI format.
423
424 =cut
425
426 sub uri_unescape {
427 my $data = shift;
428 return undef if ( !defined $data );
429 $data =~ tr/+/ /;
430 $data =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
431 return $data;
432 }
433
434 #################################################################
435