Library url

URI parsing, composition, and relative URL resolution.

A URL is represented as a table with the following entries:

  • scheme
  • fragment
  • query
  • params
  • authority
  • userinfo
  • path
  • port
  • password
These correspond to these parts of a URL (some may be nil):
scheme://userinfo@password:authority:port/path;params?query#fragment

Authors:

  • Diego Nehab
  • Eddie Bell <ejlbell@gmail.com>

Source: https://svn.nmap.org/nmap/nselib/url.lua

Functions

absolute (base_url, relative_url)

Builds an absolute URL from a base and a relative URL according to RFC 2396.

build (parsed)

Rebuilds a parsed URL from its components.

build_path (parsed, unsafe)

Builds a path component from its segments, escaping protected characters.

build_query (query)

Builds a query string from a table.

escape (s)

Encodes a string into its escaped hexadecimal representation.

get_default_port (scheme)

Provides the default port for a given URI scheme.

get_default_scheme (port)

Provides the default URI scheme for a given port.

parse (url, default)

Parses a URL and returns a table with all its parts according to RFC 3986.

parse_path (path)

Breaks a path into its segments, unescaping the segments.

parse_query (query)

Breaks a query string into name/value pairs.

unescape (s)

Decodes an escaped hexadecimal string.

Functions

absolute (base_url, relative_url)

Builds an absolute URL from a base and a relative URL according to RFC 2396.

Parameters

base_url
A base URL.
relative_url
A relative URL.

Return value:

The corresponding absolute URL.
build (parsed)

Rebuilds a parsed URL from its components.

Components are protected if any reserved or disallowed characters are found.

Parameters

parsed
Parsed URL, as returned by parse.

Return value:

A string with the corresponding URL.
build_path (parsed, unsafe)

Builds a path component from its segments, escaping protected characters.

Parameters

parsed
Path segments.
unsafe
If true, segments are not protected before path is built.

Return value:

The corresponding path string
build_query (query)

Builds a query string from a table.

This is the inverse of parse_query. Both the parameter name and value are subject to URL encoding.

Parameters

query
A dictionary table where table['name'] = value.

Return value:

A query string (like "name=value2&name=value2").
escape (s)

Encodes a string into its escaped hexadecimal representation.

Parameters

s
Binary string to be encoded.

Return value:

Escaped representation of string.
get_default_port (scheme)

Provides the default port for a given URI scheme.

Parameters

scheme
for determining the port, such as "http" or "https".

Return value:

A port number as an integer, such as 443 for scheme "https", or nil in case of an undefined scheme
get_default_scheme (port)

Provides the default URI scheme for a given port.

Parameters

port
A port number as a number or port table

Return value:

scheme for addressing the port, such as "http" or "https".
parse (url, default)

Parses a URL and returns a table with all its parts according to RFC 3986.

The following grammar describes the names given to the URL parts.

<url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment>
<authority> ::= <userinfo>@<host>:<port>
<userinfo> ::= <user>[:<password>]
<path> :: = {<segment>/}<segment>

The leading / in /<path> is considered part of <path>.

If the host contains non-ASCII characters, the Punycode-encoded version of the host name will be in the ascii_host field of the returned table.

Parameters

url
URL of request.
default
Table with default values for each field.

Return value:

A table with the following fields, where RFC naming conventions have been preserved: scheme, authority, userinfo, user, password, host, ascii_host, port, path, params, query, and fragment.
parse_path (path)

Breaks a path into its segments, unescaping the segments.

Parameters

path
A path to break.

Return value:

A table with one entry per segment.
parse_query (query)

Breaks a query string into name/value pairs.

This function takes a <query> of the form "name1=value1&name2=value2" and returns a table containing the name-value pairs, with the name as the key and the value as its associated value. Both the name and the value are subject to URL decoding.

Parameters

query
Query string.

Return value:

A table of name-value pairs following the pattern table["name"] = value.
unescape (s)

Decodes an escaped hexadecimal string.

Parameters

s
Hexadecimal-encoded string.

Return value:

Decoded string.