Manual of Lua-URI: _util
lua-uri-_util - Utility functions for Lua URI library
This module contains various utility functions used by the rest of the library. They are mostly intended only for internal use, and are subject to change in future versions, but the URI encoding and decoding functions may be more widely useful.
On loading, the module returns a table containing the functions, but like all the modules in this library it does not install itself into any global variables.
local Util = require "uri._util"
The following functions can be found in the table returned from
- uri_encode(text, pattern)
Use URI encoding (or 'percent encoding') to encode any unsafe characters in
patternis specified then it should be part of a Lua pattern which can be enclosed in square brackets to make a character class. Usually it will start with
^so that the rest of the characters will be considered the 'safe' ones, not to be encoded. Any character matched by the pattern will be encoded.
print(Util.uri_encode("foo bar!")) ---> foo%20bar! print(Util.uri_encode("foo bar!", "^A-Za-z0-9")) ---> foo%20bar%21
The default pattern is:
- uri_decode(text, pattern)
Decode any URI encoding in
patternis nil then all encoded characters will be decoded. If a pattern is supplied then it should be in the same form as for
uri_encode. Any character not matched by the pattern will be left encoded as it was.
print(Util.uri_decode("foo%20bar%21")) ---> foo bar! print(Util.uri_decode("foo%20bar%21", "^!")) ---> foo bar%21
Removes single and double dot segments from a URI path.
This is the 'remove_dot_segments' algorithm from RFC 3986 section 5.2.4. The value of
pathis used as the input buffer, and the contents of the output buffer are returned.
- split(pattern, str, max)
Split the string
patternmatches it, returning the pieces as individual strings in an array. If
maxis not nil, then stop splitting after that many pieces have been created.
Calling this function is the same as calling Lua's built in
requirefunction, except that if a module called
namecannot be found, it returns nil instead of throwing an exception. If loading the module is successful then the result of
requireis returned. An exception is thrown if any error occurs loading the module other than it not being found.
- subclass_of(class, baseclass)
Sets up the metatable and a few other things for the table
classso that it will be a subclass of
baseclass. This is used by the classes in this library to implement inheritance.
- do_class_changing_change(uri, baseclass, changedesc, newvalue, changefunc)
This is used when a mutator method changes something about a URI which leads it to need to belong to a different class.
uriis the URI object to change,
baseclassis the class to reset it to before making the change,
changedescis a description to be included in an error message if necessary,
newvalueis the new value to be set (which must be a string, as it is also included in error messages), and
changefuncis a function which is called with a temporary URI object it should adjust and
- uri_part_not_allowed (class, method)
This should be called in scheme-specific classes where certain parts of URIs are not allowed to be present (e.g., the 'host' part in a URN). It will override the named method in the class with one which throws an exception if an attempt is made to set the part to anything other than nil. If the rest of the code for the scheme keeps objects internally consistent then the new method should always return nil, although when a URI is being validated during the
initmethod's execution, it may return other things, which can be used to detect disallowed parts in a URI being parsed.