Expand/Shrink

url

The file builtins\url.e (not an autoinclude) contains some basic routines for dealing with urls.

The code is fairly straightforward and open to enhancements/suggestions.

Since it is all just text processing, it is perfectly compatible with pwa/p2js.

Example:

include url.e
?parse_url("http://user:pass@www.debian.org:80/index.html?name=John&age=39#tag")
?parse_url("mailto:John.Doe@example.com")
?decode_url("Fred+%26+Ethel")
-- output:
-- {"http","www.debian.org",80'P',"/index.html","user","pass","name=John&age=39","tag"}
-- {"mailto",0,0,"John.Doe@example.com",0,0,0,0}
-- "Fred & Ethel"
Elements returned by parse_url() are 0 when not available/recognised, apart from a protocol of "".
Some further examples can be found at https://rosettacode.org/mw/index.php?title=URL_parser#Phix.

Constants

global enum URL_PROTOCOL,  -- [1] The protocol of the URL, aka scheme, eg "http"
URL_HOSTNAME,  -- [2] The hostname of the URL, aka domain, eg "www.example.com"
URL_PORT,  -- [3] The TCP port that the URL will connect to, eg 80
URL_PATH,  -- [4] The protocol-specific pathname of the URL, eg "/index.html"
URL_USER,  -- [5] The username of the URL, eg "user"
URL_PASSWORD,  -- [6] The password of the URL, eg "pass"
URL_QUERY_STRING,  -- [7] The HTTP query string, eg "name=John&age=39"
URL_FRAGMENT,  -- [8] The #name part, eg "tag"

Routines

object res = 
parse_url(string url) -- Parse a URL returning its various elements.
Returns 0 if the url could not be parsed.
Unlike Euphoria, there is no option to convert the query string to a map, it is always a string here, or 0.
string desc = 
url_element_desc(integer idx) -- Returns "scheme", "domain", "port", .. "fragment", matching the above constants
(Sufficiently trivial that you’d just rewrite your own rather than fiddle/fret with it.)
string url =  decode_url(string url) -- Convert all encoded entities to their decoded counter parts