summaryrefslogtreecommitdiffhomepage
path: root/libs/httpclient/luasrc/httpclient.lua
diff options
context:
space:
mode:
authorSteven Barth <steven@midlink.org>2009-03-09 14:02:34 +0000
committerSteven Barth <steven@midlink.org>2009-03-09 14:02:34 +0000
commit73607dbd7d4bbebd99c163fcc337a1d62032a2d3 (patch)
tree528fc6e6830da27c73ea8250ed63c0ad7d8a1fb4 /libs/httpclient/luasrc/httpclient.lua
parent6129d031b0709610dfd85b709963fe6da64022a8 (diff)
Improved httpclient
Diffstat (limited to 'libs/httpclient/luasrc/httpclient.lua')
-rw-r--r--libs/httpclient/luasrc/httpclient.lua16
1 files changed, 15 insertions, 1 deletions
diff --git a/libs/httpclient/luasrc/httpclient.lua b/libs/httpclient/luasrc/httpclient.lua
index f3318a8cc..e75cc0373 100644
--- a/libs/httpclient/luasrc/httpclient.lua
+++ b/libs/httpclient/luasrc/httpclient.lua
@@ -22,6 +22,7 @@ local http = require "luci.http.protocol"
local date = require "luci.http.protocol.date"
local type, pairs, ipairs, tonumber = type, pairs, ipairs, tonumber
+local unpack = unpack
module "luci.httpclient"
@@ -160,6 +161,10 @@ function request_raw(uri, options)
options.method = options.method or "POST"
end
+ if type(options.body) == "function" then
+ options.method = options.method or "POST"
+ end
+
-- Assemble message
local message = {(options.method or "GET") .. " " .. path .. " " .. protocol}
@@ -194,6 +199,12 @@ function request_raw(uri, options)
if type(options.body) == "string" then
sock:sendall(options.body)
+ elseif type(options.body) == "function" then
+ local res = {options.body(sock)}
+ if not res[1] then
+ sock:close()
+ return unpack(res)
+ end
end
-- Create source and fetch response
@@ -201,12 +212,14 @@ function request_raw(uri, options)
local line, code, error = linesrc()
if not line then
+ sock:close()
return nil, code, error
end
- local protocol, status, msg = line:match("^(HTTP/[0-9.]+) ([0-9]+) (.*)")
+ local protocol, status, msg = line:match("^([%w./]+) ([0-9]+) (.*)")
if not protocol then
+ sock:close()
return nil, -3, "invalid response magic: " .. line
end
@@ -228,6 +241,7 @@ function request_raw(uri, options)
end
if not line then
+ sock:close()
return nil, -4, "protocol error"
end