aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2023-06-03 16:15:52 +0200
committerKim Alvefur <zash@zash.se>2023-06-03 16:15:52 +0200
commit99906e5b9c212beaf6cbc21580d67a5806fa4f24 (patch)
tree17f8ef39c3d86523c5fb0d3e28a10e87ff0df706 /util
parent517f20b5230586df9baee3afb47982a54478d074 (diff)
downloadprosody-99906e5b9c212beaf6cbc21580d67a5806fa4f24.tar.gz
prosody-99906e5b9c212beaf6cbc21580d67a5806fa4f24.zip
util.http: Implement parser for RFC 7239 Forwarded header
Standardized and structured replacement for the X-Forwarded-For, X-Forwarded-Proto set of headers. Notably, this allows per-hop protocol information, unlike X-Forwarded-Proto which is always a single value for some reason.
Diffstat (limited to 'util')
-rw-r--r--util/http.lua33
1 files changed, 33 insertions, 0 deletions
diff --git a/util/http.lua b/util/http.lua
index 3852f91c..b21bf798 100644
--- a/util/http.lua
+++ b/util/http.lua
@@ -69,9 +69,42 @@ local function normalize_path(path, is_dir)
return path;
end
+--- Parse the RFC 7239 Forwarded header into array of key-value pairs.
+local function parse_forwarded(forwarded)
+ if type(forwarded) ~= "string" then
+ return nil;
+ end
+
+ local fwd = {}; -- array
+ local cur = {}; -- map, to which we add the next key-value pair
+ for key, quoted, value, delim in forwarded:gmatch("(%w+)%s*=%s*(\"?)([^,;\"]+)%2%s*(.?)") do
+ -- FIXME quoted quotes like "foo\"bar"
+ -- unlikely when only dealing with IP addresses
+ if quoted == '"' then
+ value = value:gsub("\\(.)", "%1");
+ end
+
+ cur[key:lower()] = value;
+ if delim == "" or delim == "," then
+ t_insert(fwd, cur)
+ if delim == "" then
+ -- end of the string
+ break;
+ end
+ cur = {};
+ elseif delim ~= ";" then
+ -- misparsed
+ return false;
+ end
+ end
+
+ return fwd;
+end
+
return {
urlencode = urlencode, urldecode = urldecode;
formencode = formencode, formdecode = formdecode;
contains_token = contains_token;
normalize_path = normalize_path;
+ parse_forwarded = parse_forwarded;
};