Improved httpclient
This commit is contained in:
parent
6129d031b0
commit
73607dbd7d
1 changed files with 15 additions and 1 deletions
|
@ -22,6 +22,7 @@ local http = require "luci.http.protocol"
|
||||||
local date = require "luci.http.protocol.date"
|
local date = require "luci.http.protocol.date"
|
||||||
|
|
||||||
local type, pairs, ipairs, tonumber = type, pairs, ipairs, tonumber
|
local type, pairs, ipairs, tonumber = type, pairs, ipairs, tonumber
|
||||||
|
local unpack = unpack
|
||||||
|
|
||||||
module "luci.httpclient"
|
module "luci.httpclient"
|
||||||
|
|
||||||
|
@ -160,6 +161,10 @@ function request_raw(uri, options)
|
||||||
options.method = options.method or "POST"
|
options.method = options.method or "POST"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if type(options.body) == "function" then
|
||||||
|
options.method = options.method or "POST"
|
||||||
|
end
|
||||||
|
|
||||||
-- Assemble message
|
-- Assemble message
|
||||||
local message = {(options.method or "GET") .. " " .. path .. " " .. protocol}
|
local message = {(options.method or "GET") .. " " .. path .. " " .. protocol}
|
||||||
|
|
||||||
|
@ -194,6 +199,12 @@ function request_raw(uri, options)
|
||||||
|
|
||||||
if type(options.body) == "string" then
|
if type(options.body) == "string" then
|
||||||
sock:sendall(options.body)
|
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
|
end
|
||||||
|
|
||||||
-- Create source and fetch response
|
-- Create source and fetch response
|
||||||
|
@ -201,12 +212,14 @@ function request_raw(uri, options)
|
||||||
local line, code, error = linesrc()
|
local line, code, error = linesrc()
|
||||||
|
|
||||||
if not line then
|
if not line then
|
||||||
|
sock:close()
|
||||||
return nil, code, error
|
return nil, code, error
|
||||||
end
|
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
|
if not protocol then
|
||||||
|
sock:close()
|
||||||
return nil, -3, "invalid response magic: " .. line
|
return nil, -3, "invalid response magic: " .. line
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -228,6 +241,7 @@ function request_raw(uri, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not line then
|
if not line then
|
||||||
|
sock:close()
|
||||||
return nil, -4, "protocol error"
|
return nil, -4, "protocol error"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue