Add utility function get_domain_from_url

This commit is contained in:
Alfredo Cardigliano 2026-02-05 15:17:14 +01:00
parent 87260d2798
commit 2b379c33cc
2 changed files with 24 additions and 15 deletions

View file

@ -64,6 +64,28 @@ end
-- ###############################################
function get_domain_from_url(url)
-- Strips the prefix (either http:// or https://) and a possible port
-- Extract the domain, e.g.,
-- http://user:password@www.example.com/p1/p2 becomes www.example.com
-- http://www.example.com:3000/p1/p2 becomes www.example.com
-- See if domain has user and password encoded, i.e.,
-- http://user:password@www.example.com becomes www.example.com
local domain = url:match('^%w+://[^:]+:[^@]+@([^/:]+)')
if not domain then
-- Domain has no user and password encoded, i.e.,
-- http://www.example.com:3000/p1/p2
domain = url:match('^%w+://([^/:]+)')
end
return domain
end
-- ###############################################
-- TODO: improve this function
function jsonencode(what)
what = string.gsub(what, '"', "'")