aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2010-08-27 18:33:45 +0100
committerMatthew Wild <mwild1@gmail.com>2010-08-27 18:33:45 +0100
commite724cea3952f3101b818f4d94d3584d237c74b78 (patch)
tree05a05176f32cedaba9105cb54ebe4e88cedb2157
parentfff700e7df31e157506b651826505c49c7de3c62 (diff)
downloadprosody-e724cea3952f3101b818f4d94d3584d237c74b78.tar.gz
prosody-e724cea3952f3101b818f4d94d3584d237c74b78.zip
net.http: Add http.formencode() for www-form-urlencoded from an array of fields (thanks dersd)
-rw-r--r--net/http.lua22
1 files changed, 20 insertions, 2 deletions
diff --git a/net/http.lua b/net/http.lua
index 0634d773..11407c4d 100644
--- a/net/http.lua
+++ b/net/http.lua
@@ -17,8 +17,9 @@ local connlisteners_get = require "net.connlisteners".get;
local listener = connlisteners_get("httpclient") or error("No httpclient listener!");
local t_insert, t_concat = table.insert, table.concat;
-local tonumber, tostring, pairs, xpcall, select, debug_traceback, char, format =
- tonumber, tostring, pairs, xpcall, select, debug.traceback, string.char, string.format;
+local pairs, ipairs = pairs, ipairs;
+local tonumber, tostring, xpcall, select, debug_traceback, char, format =
+ tonumber, tostring, xpcall, select, debug.traceback, string.char, string.format;
local log = require "util.logger".init("http");
@@ -27,6 +28,23 @@ module "http"
function urlencode(s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end
function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end
+local function _formencodepart(s)
+ return s and (s:gsub("%W", function (c)
+ if c ~= " " then
+ return format("%%%02x", c:byte());
+ else
+ return "+";
+ end
+ end));
+end
+function formencode(form)
+ local result = {};
+ for _, field in ipairs(form) do
+ t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value));
+ end
+ return t_concat(result, "&");
+end
+
local function expectbody(reqt, code)
if reqt.method == "HEAD" then return nil end
if code == 204 or code == 304 or code == 301 then return nil end