This is native URL parser :p
without regex, com objects - as always, because written by me. ;>
So... I long time tested it, and all works fine, i think.
var %url = http://username:password@www.example.org:443/page?query#fragment
//echo -a $urlinf(%url).scheme = http
//echo -a $urlinf(%url).ssl = $true (because http port is 443, also ssl always return $true if first right symbols in scheme is "s", like: https ftps ircs)
//echo -a $urlinf(%url).user = username
//echo -a $urlinf(%url).pass = password
//echo -a $urlinf(%url).host = www.example.org
//echo -a $urlinf(%url).port = 443
//echo -a $urlinf(%url).domain = www.example.org:443 (for "Host" HTTP header, if non-default 80 port that returns "host:port")
//echo -a $urlinf(%url).path = /page
//echo -a $urlinf(%url).query = ?query
//echo -a $urlinf(%url).fragment = #fragment
//echo -a $urlinf(%url).request (for GET /page?query HTTP header, contains .path and (if it needed) .query props)
By default .query returns ? and .fragment returns #
Also, snippet contains some protect from 'idiots' :), like: http://example.org?query - url without /page, but .request returns /?query and .path returns just /. works fine ;)
Also, example, if (irc.* iswm irc.example.org) .scheme returns irc. great? yep)..
Enjoy!
alias urlinf {
var %scheme, %ssl = $false, %user, %pass, %address = $1, %port = 0, %request = /, %path = /, %query = ?, %fragment = $(#,)
if ($pos(%address,://)) {
var %:// = $v1, %address = $mid(%address,$calc(%:// + 3))
if (%:// > 1) {
var %scheme = $gettok($1,1,58)
if (*s iswm %scheme) var %scheme = $mid(%scheme,1,-1), %ssl = $true
}
}
if ($pos(%address,$(#,))) var %fragment = $mid(%address,$v1), %address = $gettok(%address,1,35)
if ($pos(%address,/)) {
var %request = $mid(%address,$v1), %path = %request, %address = $gettok(%address,1,47)
if ($pos(%path,?)) var %query = $mid(%path,$v1), %path = $gettok(%path,1,63)
}
elseif ($pos(%address,?)) var %query = $mid(%address,$v1), %request = $+(/,%query), %address = $gettok(%address,1,63)
if (*?:?*@* iswm %address) var %user = $gettok(%address,1,58), %pass = $gettok($gettok(%address,1,64),2,58)
if ($pos(%address,@)) var %address = $gettok(%address,2,64)
var %host = $gettok(%address,1,58), %port = $gettok(%address,2,58)
if (www.* iswm %host) var %scheme = http
elseif (ftp.* iswm %host) var %scheme = ftp
elseif (irc.* iswm %host) var %scheme = irc
if (%scheme == http) {
var %domain = %host
if (%port == 443) var %ssl = $true
if (%port == 80) var %domain = %host
elseif (%port isnum 1-65536) var %domain = $+(%host,:,%port)
}
return % [ $+ [ $prop ] ]
}
Firstmate, if my post appears to be ignorant or disparaging (at least to you), I apologize. But my main aim is to tell people to like regex. For your info, I'm still learning about regex, and I know I won't be good at mastering it. Learning is a long, winding road that it's never enough. Again, I'm not trying to get people withdrawn from using other methods, identifiers, or commands made available to simulate regex. They're also good for scripting purposes but will come in a limitation as Hawkee suggested, hence not universal.
Jethro, that's a very ignorant statement. For one, you seem to think everyone will like RegEx. Can't it be possible that he has learned and understood it, but would rather use a different scripting method?
Granted, RegEx is a powerful tool, there's no need to belittle those that don't wish to use it.
I concur with Hawkee wholeheartedly. Regex is universal in just about every programming language known to date. By using tokens (other than the ones made solely for certain programming languages) that will only work for MSL and nothing else.
Aucun50, you don't like using regex is because you don't want or bother to understand how it works. Once you get some understanding of it, you'll find yourself fancying it more than you think.