aboutsummaryrefslogtreecommitdiff
path: root/challenge-017/duane-powell/perl5/ch-2.pl
blob: 64b73d968d120f5344ba81f50511bd2aec38fc9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/perl
use Modern::Perl;
use URI;

# Create a script to parse URL and print the components of URL. 
# According to Wiki page, https://en.wikipedia.org/wiki/URL
# the URL syntax is as below:
# scheme:[//[userinfo@]host[:port]]path[?query][#fragment]
# For example: jdbc:mysql://user:password@localhost:3306/pwc?profile=true#h1
#
#  scheme:   jdbc:mysql
#  userinfo: user:password
#  host:     localhost
#  port:     3306
#  path:     /pwc
#  query:    profile=true
#  fragment: h1

# uri examples from https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
my @uri = qw( 
https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top
ldap://[2001:db8::7]/c=GB?objectClass?one
mailto:John.Doe@example.com
news:comp.infosystems.www.servers.unix
tel:+1-816-555-1212
telnet://192.0.2.16:80/
urn:oasis:names:specification:docbook:dtd:xml:4.1.2
jdbc:mysql://user:password@localhost:3306/pwc?profile=true#h1
);

foreach (@uri) {
	my $uri = $_;

	# hack:
	# URI module not recognizing jdbc scheme. So if the uri 
	# matches jdbc:database:// convert to http scheme temporarily 
	my $jdbc;
	if ($uri =~ m!^(jdbc:[A-Za-z0-9]+)://!) {
		$jdbc = $1;
		$uri =~ s/$jdbc/http/;
	}

	# eval URI methods because they may not exists. See https://metacpan.org/pod/URI
	# which states "The methods available for a specific URI object depend on the scheme"
	my $u = URI->new($uri);
	my ($scheme,$userinfo,$host,$port,$path,$query,$fragment);
	eval { $scheme   = $u->scheme   }; $scheme   ||= ''; 
	eval { $userinfo = $u->userinfo }; $userinfo ||= '';
	eval { $host     = $u->host     }; $host     ||= '';
	eval { $port     = $u->port     }; $port     ||= '';
	eval { $path     = $u->path     }; $path     ||= '';
	eval { $query    = $u->query    }; $query    ||= '';
	eval { $fragment = $u->fragment }; $fragment ||= '';

	# restore uri and scheme values if we hacked them.
	if ($jdbc) {
		$scheme = $jdbc;
		$uri = $_;
	}

	my $out = <<"OUT";
uri:      $uri
scheme:   $scheme 
userinfo: $userinfo 
host:     $host
port:     $port
path:     $path
query:    $query
fragment: $fragment
OUT

	say $out;
}

__END__

./ch2.pl
uri:      https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top
scheme:   https 
userinfo: john.doe
host:     www.example.com
port:     123
path:     /forum/questions/
query:    tag=networking&order=newest
fragment: top

uri:      ldap://[2001:db8::7]/c=GB?objectClass?one
scheme:   ldap 
userinfo:  
host:     2001:db8::7
port:     389
path:     /c=GB
query:    objectClass?one
fragment: 

uri:      mailto:John.Doe@example.com
scheme:   mailto 
userinfo:  
host:     
port:     
path:     John.Doe@example.com
query:    
fragment: 

...

uri:      http://user:password@localhost:3306/pwc?profile=true#h1
scheme:   jdbc:mysql 
userinfo: user:password 
host:     localhost
port:     3306
path:     /pwc
query:    profile=true
fragment: h1