This module provides classes and functions for parsing and processing URLs. This module supports username and passwords in URLs in order to support an arbitrary number of RFC combinations but this does not strictly conform to RFC1738.
The scope of URL in this module have not been limited to HTTP or any protocol for that matter. However, where deducable, the module tries to conform to the most appropriate URL for the specified scheme.
Constructing a URL is vey simple. Here is an example.
%> import url %> var link = url.Url('https', 'example.com', 9000) %> link.absolute_url() 'https://example.com:9000'
What each function and class method does are easy to deduce from their names.
For example, we can use the
parse() function to convert a URL string into a URL instance like below.
%> link = url.parse('https://example.com:9000') %> link.scheme 'https' %> link.port '9000'
- url.encode(url: string, strict: boolean)
- URL-encodes string
this function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page.
if strict mode is enabled, space character is encoded with the percent (%) sign in order to conform with RFC 3986. Otherwise, is is encoded with the plus (+) sign in order to align with the default encoding used by modern browsers.return stringdefualt strict: false
- strict mode is disabled by default
- url.decode(url: string)
- Decodes URL-encoded string
decodes any %## encoding in the given string. plus symbols (‘+’) are decoded to a space character.return string
- url.parse(url: string)
- parses given url string into a Url object return Url
Excpetion thrown when a url is malformed
- UrlMalformedException(message: string)
The Url class provides functionalities for parsing and processing URLs @serializable @printable
- The url scheme e.g. http, https, ftp, tcp etc.
- The host information contained in the url
- The port information contained in the url whenever the url doesn’t indicate, we try to make a best guess based on the scheme.
- The path of the URL. default /
- Hash information contained in the url and it’s beginning is indicated by the hash (#) sign. This value is especially relevant to some http/https urls and are usually references to the content of the document at the given url
- Query/Search information contained in the url and it’s beginning is indicated by the question (?) sign. This value is especially relevant to some http/https urls and are usually used to convey data to endpoint based on the GET method.
- Username information for authentication are sometimes embeded in urls. When such information exist, this property holds the information
- Password information for authentication are sometimes embeded in urls. When such information exist, this property holds the information
- Url(scheme: string, host: string [, port: string [, path: string [, query: string [, hash: string [, username: string [, password: string]]]]]])
- returns the url authority
The authority component is preceded by a double slash (“//”) and is terminated by the next slash (“/”), question mark (“?”), or number sign (“#”) character, or by the end of the URI.
- mailto scheme does not have an authority. For this reason, mailto schemes return an empty string as authority.
- returns true if the host of the url is a valid ipv4 address and false otherwise return bool
- returns true if the host of the url is a valid ipv6 address and false otherwise return bool
- returns absolute url string of the url object return string