Software /
code /
prosody
Comparison
net/http.lua @ 3470:0e59b5cdd57b
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 27 Aug 2010 18:33:45 +0100 |
parent | 2925:692b3c6c5bd2 |
child | 3540:bc139431830b |
comparison
equal
deleted
inserted
replaced
3469:011566d72331 | 3470:0e59b5cdd57b |
---|---|
15 | 15 |
16 local connlisteners_get = require "net.connlisteners".get; | 16 local connlisteners_get = require "net.connlisteners".get; |
17 local listener = connlisteners_get("httpclient") or error("No httpclient listener!"); | 17 local listener = connlisteners_get("httpclient") or error("No httpclient listener!"); |
18 | 18 |
19 local t_insert, t_concat = table.insert, table.concat; | 19 local t_insert, t_concat = table.insert, table.concat; |
20 local tonumber, tostring, pairs, xpcall, select, debug_traceback, char, format = | 20 local pairs, ipairs = pairs, ipairs; |
21 tonumber, tostring, pairs, xpcall, select, debug.traceback, string.char, string.format; | 21 local tonumber, tostring, xpcall, select, debug_traceback, char, format = |
22 tonumber, tostring, xpcall, select, debug.traceback, string.char, string.format; | |
22 | 23 |
23 local log = require "util.logger".init("http"); | 24 local log = require "util.logger".init("http"); |
24 | 25 |
25 module "http" | 26 module "http" |
26 | 27 |
27 function urlencode(s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end | 28 function urlencode(s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end |
28 function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end | 29 function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end |
30 | |
31 local function _formencodepart(s) | |
32 return s and (s:gsub("%W", function (c) | |
33 if c ~= " " then | |
34 return format("%%%02x", c:byte()); | |
35 else | |
36 return "+"; | |
37 end | |
38 end)); | |
39 end | |
40 function formencode(form) | |
41 local result = {}; | |
42 for _, field in ipairs(form) do | |
43 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value)); | |
44 end | |
45 return t_concat(result, "&"); | |
46 end | |
29 | 47 |
30 local function expectbody(reqt, code) | 48 local function expectbody(reqt, code) |
31 if reqt.method == "HEAD" then return nil end | 49 if reqt.method == "HEAD" then return nil end |
32 if code == 204 or code == 304 or code == 301 then return nil end | 50 if code == 204 or code == 304 or code == 301 then return nil end |
33 if code >= 100 and code < 200 then return nil end | 51 if code >= 100 and code < 200 then return nil end |