Helper library build to parse http URI paths as described in https://tools.ietf.org/html/rfc3986
Originally designed to be used with lua-http
luarocks install pathetic
Only dependency is LPeg.
Demo made with Fengari, LuLPeg and inspect.lua
Returns the path parsed into a table, unescaped. Query is parsed into a subtable, unescaped and any keys appearing more than once with different values are gathered into a subtable.
pathetic:parse("/hello/world?lang=lua%21&lang=english&lib=pathetic#docs%2Finfo")
{
fragment = "docs/info",
path = "/hello/world",
query = {
lang = { "lua!", "english" },
lib = "pathetic"
},
raw_fragment = "docs%2Finfo",
raw_path = "/hello/world",
raw_query = "lang=lua%21&lang=english&lib=pathetic"
}
Returns a path's base path part, unescaped.
pathetic:get_path("/hello%2Fworld?lang=lua")
"/hello/world/"
Returns a path's base path part, without unescaping.
pathetic:get_path("/hello%2Fworld?lang=lua")
"/hello%2Fworld/"
Returns a path's query string parsed into a table with key and values unescaped. Any keys appearing more than once with different values are gathered into a subtable.
pathetic:get_query("/hello/world?lang=lua%20lang&lib=english&lib=pathetic")
{
lang = { "lua lang", "english" },
lib = "pathetic"
}
Returns a path's raw query string, without unescaping.
pathetic:get_raw_query("/hello/world?lang=lua%20lang&lib=pathetic")
"lang=lua%20lang&lib=pathetic"
Returns a path's fragment part, unescaped.
pathetic:get_path("/hello%2Fworld?lang=lua#docs%2Finfo")
"docs/info"
Returns a path's fragment part, without unescaping.
pathetic:get_path("/hello%2Fworld?lang=lua#docs%2Finfo")
"docs%2Finfo"
Returns a query string parsed into a table, unescaped. Any keys appearing more than once with different values are gathered into a table.
pathetic:parse("lang=lua%20lang&lang=english&lib=pathetic")
{
lang = { "lua lang", "english" },
lib = "pathetic"
}
Returns string unescaping any percent encoding.
pathetic:unescape("hello%20world%21")
"hello world!"
Returns a string with an RFC3986 reserved chars percent encoded.
pathetic:escape("hello world!")
"hello%20world%21"