comparison util/http.lua @ 13124:f15e23840780

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.
author Kim Alvefur <zash@zash.se>
date Sat, 03 Jun 2023 16:15:52 +0200
parents ff88b03c343f
children a4c47203a9eb
comparison
equal deleted inserted replaced
13123:dee26e4cfb2b 13124:f15e23840780
67 end 67 end
68 if path:sub(1,1) ~= "/" then path = "/"..path; end 68 if path:sub(1,1) ~= "/" then path = "/"..path; end
69 return path; 69 return path;
70 end 70 end
71 71
72 --- Parse the RFC 7239 Forwarded header into array of key-value pairs.
73 local function parse_forwarded(forwarded)
74 if type(forwarded) ~= "string" then
75 return nil;
76 end
77
78 local fwd = {}; -- array
79 local cur = {}; -- map, to which we add the next key-value pair
80 for key, quoted, value, delim in forwarded:gmatch("(%w+)%s*=%s*(\"?)([^,;\"]+)%2%s*(.?)") do
81 -- FIXME quoted quotes like "foo\"bar"
82 -- unlikely when only dealing with IP addresses
83 if quoted == '"' then
84 value = value:gsub("\\(.)", "%1");
85 end
86
87 cur[key:lower()] = value;
88 if delim == "" or delim == "," then
89 t_insert(fwd, cur)
90 if delim == "" then
91 -- end of the string
92 break;
93 end
94 cur = {};
95 elseif delim ~= ";" then
96 -- misparsed
97 return false;
98 end
99 end
100
101 return fwd;
102 end
103
72 return { 104 return {
73 urlencode = urlencode, urldecode = urldecode; 105 urlencode = urlencode, urldecode = urldecode;
74 formencode = formencode, formdecode = formdecode; 106 formencode = formencode, formdecode = formdecode;
75 contains_token = contains_token; 107 contains_token = contains_token;
76 normalize_path = normalize_path; 108 normalize_path = normalize_path;
109 parse_forwarded = parse_forwarded;
77 }; 110 };