Software /
code /
prosody
Comparison
util/http.lua @ 5471:34bfd26525f5
util.http: Refactor and import all necessary functions
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 12 Apr 2013 20:26:35 +0100 |
parent | 5458:84162b81c863 |
child | 9504:cfbea3064aa9 |
comparison
equal
deleted
inserted
replaced
5470:a62c1d4ec4ab | 5471:34bfd26525f5 |
---|---|
3 -- | 3 -- |
4 -- This project is MIT/X11 licensed. Please see the | 4 -- This project is MIT/X11 licensed. Please see the |
5 -- COPYING file in the source package for more information. | 5 -- COPYING file in the source package for more information. |
6 -- | 6 -- |
7 | 7 |
8 local http = {}; | 8 local format, char = string.format, string.char; |
9 local pairs, ipairs, tonumber = pairs, ipairs, tonumber; | |
10 local t_insert, t_concat = table.insert, table.concat; | |
9 | 11 |
10 function http.urlencode(s) | 12 local function urlencode(s) |
11 return s and (s:gsub("[^a-zA-Z0-9.~_-]", function (c) return format("%%%02x", c:byte()); end)); | 13 return s and (s:gsub("[^a-zA-Z0-9.~_-]", function (c) return format("%%%02x", c:byte()); end)); |
12 end | 14 end |
13 function http.urldecode(s) | 15 local function urldecode(s) |
14 return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); | 16 return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); |
15 end | 17 end |
16 | 18 |
17 local function _formencodepart(s) | 19 local function _formencodepart(s) |
18 return s and (s:gsub("%W", function (c) | 20 return s and (s:gsub("%W", function (c) |
22 return "+"; | 24 return "+"; |
23 end | 25 end |
24 end)); | 26 end)); |
25 end | 27 end |
26 | 28 |
27 function http.formencode(form) | 29 local function formencode(form) |
28 local result = {}; | 30 local result = {}; |
29 if form[1] then -- Array of ordered { name, value } | 31 if form[1] then -- Array of ordered { name, value } |
30 for _, field in ipairs(form) do | 32 for _, field in ipairs(form) do |
31 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value)); | 33 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value)); |
32 end | 34 end |
36 end | 38 end |
37 end | 39 end |
38 return t_concat(result, "&"); | 40 return t_concat(result, "&"); |
39 end | 41 end |
40 | 42 |
41 function http.formdecode(s) | 43 local function formdecode(s) |
42 if not s:match("=") then return urldecode(s); end | 44 if not s:match("=") then return urldecode(s); end |
43 local r = {}; | 45 local r = {}; |
44 for k, v in s:gmatch("([^=&]*)=([^&]*)") do | 46 for k, v in s:gmatch("([^=&]*)=([^&]*)") do |
45 k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20"); | 47 k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20"); |
46 k, v = urldecode(k), urldecode(v); | 48 k, v = urldecode(k), urldecode(v); |
48 r[k] = v; | 50 r[k] = v; |
49 end | 51 end |
50 return r; | 52 return r; |
51 end | 53 end |
52 | 54 |
53 function http.contains_token(field, token) | 55 local function contains_token(field, token) |
54 field = ","..field:gsub("[ \t]", ""):lower()..","; | 56 field = ","..field:gsub("[ \t]", ""):lower()..","; |
55 return field:find(","..token:lower()..",", 1, true) ~= nil; | 57 return field:find(","..token:lower()..",", 1, true) ~= nil; |
56 end | 58 end |
57 | 59 |
58 | 60 return { |
59 | 61 urlencode = urlencode, urldecode = urldecode; |
60 return http; | 62 formencode = formencode, formdecode = formdecode; |
63 contains_token = contains_token; | |
64 }; |