Debian Codegraph @

bin/drbl-uriparse

raw
1 #!/usr/bin/perl
2 # License: GPL
3 # Author: Steven Shiau <steven _at_ clonezilla org>
4 # Description: Program to parse the URI to get the attribute.
5 #
6 # A URI (Uniform Resource Identifiers) is like:
7 # foo://username:[email protected]:8080/to/the/dir?name=singer#bird
8 # \_/ \______/ \______/ \______________/\_________/\__________/ \__/
9 # | | | | | | |
10 #scheme username password authority path query fragment
11 #
12 # Ref: https://svn.apache.org/repos/asf/labs/webarch/trunk/uri/rfc/rfc2396.html
13 # ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
14 # 12 3 4 5 6 7 8 9
15 # scheme = $2
16 # authority = $4
17 # path = $5
18 # query = $7
19 # fragment = $9
20 #
21 # E.g http://www.ics.uci.edu/pub/ietf/uri/#Related
22 # $1 = http:
23 # $2 = http
24 # $3 = //www.ics.uci.edu
25 # $4 = www.ics.uci.edu
26 # $5 = /pub/ietf/uri/
27 # $6 = <undefined>
28 # $7 = <undefined>
29 # $8 = #Related
30 # $9 = Related
31 #
32 # Input URI example:
33 # "http://www.ics.uci.edu:8042/pub/ietf/uri/#Related"
34 # "http://user:pwd@[email protected]:8042/pub/ietf/uri/#Related"
35 # "smb://[email protected]:8042/pub/ietf/uri/#Related"
36 # Samba with domain format: smb://[[domain;]username[:password]@]server[/share[/path]]
37 # e.g. "smb://workgrp;jack:[email protected]/share/samba/"
38 # Apparently the username can not contain the character ":"
39 #
40 #
41 our $usage="Usage: $0 URI ATTRIBUTE";
42 #
43 sub usage_details{
44 print "$usage\n".
45 "Parse Uniform Resource Identifiers (URI) then output the ATTRIBUTE\n".
46 "URI is like: http://user:mypwd\@host.mydomain.org:8042/pub/ietf/uri/#Related\n".
47 "ATTRIBUTE could be one of these: scheme, domain, port, path, query, fragment, username, password.\n".
48 "E.g. to get the username in URI smb://jack:loverose\@titanic.net:1911/to/new-york/\n".
49 "Run:\n".
50 "$0 smb://jack:loverose\@titanic.com:1911/to/new-york username\n";
51 } # end of usage_details
52
53 ##################
54 ###### MAIN ######
55 ##################
56 my $uri;
57 my $want;
58 # Must have argument
59 die "$usage\n" if $#ARGV<0;
60 $uri = shift(@ARGV);
61 $want = shift(@ARGV);
62
63 if( $uri =~ /^(-)?(-)?h(elp)?$/) {
64 usage_details();
65 exit;
66 }
67
68 my @uri_g = ($uri =~ m{^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?});
69 # 12 3 4 5 6 7 8
70 # Array index is from 0.
71 my $scheme = $uri_g[1];
72 my $authority = $uri_g[3];
73 my $path = $uri_g[4];
74 my $query = $uri_g[6];
75 my $fragment = $uri_g[8];
76
77 # authority may like: user:[email protected]:8042 -> extract username, password, IP address/FQDN, port
78 my @auth = ($authority =~ m{^(([^:]+):?(\S+)?@)?([^:]+):?([0-9]+)?});
79 # 12 3 4 5
80 # http://u:[email protected]:8042/pub/ietf/uri/#Related
81 # u:[email protected] u:[email protected] :8042 8042
82 my $username = $auth[1];
83 my $password = $auth[2];
84 my $domain = $auth[3];
85 my $port = $auth[4];
86
87 # Form the output hash
88 my %output = (
89 # Long attributes:
90 scheme => "$scheme",
91 domain => "$domain",
92 port => "$port",
93 path => "$path",
94 query => "$query",
95 fragment => "$fragment",
96 username => "$username",
97 password => "$password",
98 # Short attributes:
99 sc => "$scheme",
100 dm => "$domain",
101 pt => "$port",
102 ph => "$path",
103 qy => "$query",
104 fr => "$fragment",
105 user => "$username",
106 pw => "$password",
107 );
108
109 print "$output{$want}\n";