Skip to content

Instantly share code, notes, and snippets.

@C1aud3
Created May 6, 2020 14:55
Show Gist options
  • Save C1aud3/1b311194f5f11b5a03673d4bc757f5d6 to your computer and use it in GitHub Desktop.
Save C1aud3/1b311194f5f11b5a03673d4bc757f5d6 to your computer and use it in GitHub Desktop.
lua hostname parser
-- Extract hostname from URI
-- Returns a string containing the hostname
-- Returns nil if the hostname is not found
function _M.find_hostname(subject)
-- See https://openresty-reference.readthedocs.io/en/latest/Lua_Nginx_API/#ngxrematch for options definition.
-- In short, j=PCRE JIT compilation, o=compile-once mode, i=case-insensitive mode
local options = "joi"
-- See https://tools.ietf.org/html/rfc2396#appendix-B
-- The regex from RFC2396 was tweaked a little bit to create an optional (0 to 1 times) capturing group around the
-- two forward slashes (//)? to allow consistent results for URIs that do not have a scheme.
local captures, err = ngx.re.match(subject, "^(([^:/?#]+):)?((//)?([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?", options)
-- captures[0] holds the whole substring being matched
-- captures[1] holds the first parenthesized sub-pattern's capturing
-- captures[2] the second, and so on...
-- For example, http://www.ics.uci.edu/pub/ietf/uri/#Related will give the following results:
-- scheme = $2 = captures[2] = http
-- authority = $5 = captures[5] = www.ics.uci.edu
-- path = $6 = captures[6] = /pub/ietf/uri/
-- query = $8 = captures[8] = <undefined> (false)
-- fragment = $10 = captures[10] = Related
-- Also, the same url but without the scheme, www.ics.uci.edu/pub/ietf/uri/#Relate, will give the following results:
-- scheme = $2 = captures[2] = <undefined> (false)
-- authority = $5 = captures[5] = www.ics.uci.edu
-- path = $6 = captures[6] = /pub/ietf/uri/
-- query = $8 = captures[8] = <undefined> (false)
-- fragment = $10 = captures[10] = Related
if captures and captures[5] then
return captures[5]
end
return nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment