util/http.lua

Mon, 04 Jan 2016 17:03:19 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 04 Jan 2016 17:03:19 +0000
changeset 18
8050134f35b3
parent 0
d363a6692a10
permissions
-rw-r--r--

clients: Include request in the new-client event

0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2013 Florian Zeitz
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 --
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local format, char = string.format, string.char;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local pairs, ipairs, tonumber = pairs, ipairs, tonumber;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local t_insert, t_concat = table.insert, table.concat;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local function urlencode(s)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 return s and (s:gsub("[^a-zA-Z0-9.~_-]", function (c) return format("%%%02x", c:byte()); end));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local function urldecode(s)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local function _formencodepart(s)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return s and (s:gsub("%W", function (c)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if c ~= " " then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 return format("%%%02x", c:byte());
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return "+";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local function formencode(form)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local result = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if form[1] then -- Array of ordered { name, value }
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 for _, field in ipairs(form) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 else -- Unordered map of name -> value
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 for name, value in pairs(form) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 t_insert(result, _formencodepart(name).."=".._formencodepart(value));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return t_concat(result, "&");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local function formdecode(s)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if not s:match("=") then return urldecode(s); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local r = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 for k, v in s:gmatch("([^=&]*)=([^&]*)") do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 k, v = urldecode(k), urldecode(v);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 t_insert(r, { name = k, value = v });
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 r[k] = v;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 return r;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local function contains_token(field, token)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 field = ","..field:gsub("[ \t]", ""):lower()..",";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 return field:find(","..token:lower()..",", 1, true) ~= nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return {
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 urlencode = urlencode, urldecode = urldecode;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 formencode = formencode, formdecode = formdecode;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 contains_token = contains_token;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 };

mercurial