/pathetic

Parse URI paths

Primary LanguageLuaMIT LicenseMIT

Pathetic

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


Documentation


pathetic:parse(path_str)

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"
}

pathetic:get_path(path_str)

Returns a path's base path part, unescaped.

pathetic:get_path("/hello%2Fworld?lang=lua")
"/hello/world/"

pathetic:get_raw_path(path_str)

Returns a path's base path part, without unescaping.

pathetic:get_path("/hello%2Fworld?lang=lua")
"/hello%2Fworld/"

pathetic:get_query(path_str)

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"
}

pathetic:get_raw_query(path_str)

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"

pathetic:get_fragment(path_str)

Returns a path's fragment part, unescaped.

pathetic:get_path("/hello%2Fworld?lang=lua#docs%2Finfo")
"docs/info"

pathetic:get_raw_fragment(path_str)

Returns a path's fragment part, without unescaping.

pathetic:get_path("/hello%2Fworld?lang=lua#docs%2Finfo")
"docs%2Finfo"

pathetic:parse_query(query_str)

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"
}

pathetic:unescape(pct_encoded_str)

Returns string unescaping any percent encoding.

pathetic:unescape("hello%20world%21")
"hello world!"

pathetic:escape(str)

Returns a string with an RFC3986 reserved chars percent encoded.

pathetic:escape("hello world!")
"hello%20world%21"