Link Search Menu Expand Document

url

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.

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'

Functions


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 string
defualt 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

Classes


class UrlMalformedException < Exception


Excpetion thrown when a url is malformed

class UrlMalformedException methods


UrlMalformedException(message: string)
constructor

class Url


The Url class provides functionalities for parsing and processing URLs @serializable @printable

class Url properties


Url.scheme
The url scheme e.g. http, https, ftp, tcp etc.
Url.host
The host information contained in the url
Url.port
The port information contained in the url whenever the url doesn’t indicate, we try to make a best guess based on the scheme.
Url.path
The path of the URL.
default /
Url.hash
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
Url.query
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.
Url.username
Username information for authentication are sometimes embeded in urls. When such information exist, this property holds the information
Url.password
Password information for authentication are sometimes embeded in urls. When such information exist, this property holds the information

class Url methods


Url(scheme: string, host: string [, port: string [, path: string [, query: string [, hash: string [, username: string [, password: string]]]]]])
constructor
authority()
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.
return string
host_is_ipv4()
returns true if the host of the url is a valid ipv4 address and false otherwise
return bool
host_is_ipv6()
returns true if the host of the url is a valid ipv6 address and false otherwise
return bool
absolute_url()
returns absolute url string of the url object
return string

Back to top

Copyright © 2021 Ore Richard Muyiwa. Distributed under the MIT license.