From c30cf9abd4e1e46731e36552ae2f459a27c5cf46 Mon Sep 17 00:00:00 2001 From: Yuzo Date: Thu, 22 Jan 2015 11:34:00 +0800 Subject: add sock::readall() to ensure the response body is complete --- libs/luci-lib-httpclient/luasrc/httpclient.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/luci-lib-httpclient/luasrc/httpclient.lua b/libs/luci-lib-httpclient/luasrc/httpclient.lua index 94c2e9ecac..c84a03b50a 100644 --- a/libs/luci-lib-httpclient/luasrc/httpclient.lua +++ b/libs/luci-lib-httpclient/luasrc/httpclient.lua @@ -323,7 +323,7 @@ function request_raw(uri, options) end end - return response.code, response, linesrc(true), sock + return response.code, response, linesrc(true)..sock:readall(), sock end function cookie_parse(cookiestr) -- cgit v1.2.3 From b6d4f32dcc4bba2645d3758c3dc027fe0e2b4d14 Mon Sep 17 00:00:00 2001 From: Yuzo Date: Thu, 22 Jan 2015 12:50:58 +0800 Subject: send Cookie in a single header line, follow browser behavior --- libs/luci-lib-httpclient/luasrc/httpclient.lua | 34 +++++++++++++++----------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/libs/luci-lib-httpclient/luasrc/httpclient.lua b/libs/luci-lib-httpclient/luasrc/httpclient.lua index c84a03b50a..af79695244 100644 --- a/libs/luci-lib-httpclient/luasrc/httpclient.lua +++ b/libs/luci-lib-httpclient/luasrc/httpclient.lua @@ -176,20 +176,8 @@ function request_raw(uri, options) options.method = options.method or "POST" end - -- Assemble message - local message = {(options.method or "GET") .. " " .. path .. " " .. protocol} - - for k, v in pairs(headers) do - if type(v) == "string" or type(v) == "number" then - message[#message+1] = k .. ": " .. v - elseif type(v) == "table" then - for i, j in ipairs(v) do - message[#message+1] = k .. ": " .. j - end - end - end - if options.cookies then + local cookiedata = {} for _, c in ipairs(options.cookies) do local cdo = c.flags.domain local cpa = c.flags.path @@ -197,11 +185,29 @@ function request_raw(uri, options) and (cpa == path or cpa == "/" or cpa .. "/" == path:sub(#cpa+1)) and (not c.flags.secure or pr == "https") then - message[#message+1] = "Cookie: " .. c.key .. "=" .. c.value + cookiedata[#cookiedata+1] = c.key .. "=" .. c.value end end + if headers["Cookie"] then + headers["Cookie"] = headers["Cookie"] .. "; " .. table.concat(cookiedata, "; ") + else + headers["Cookie"] = table.concat(cookiedata, "; ") + end end + + -- Assemble message + local message = {(options.method or "GET") .. " " .. path .. " " .. protocol} + for k, v in pairs(headers) do + if type(v) == "string" or type(v) == "number" then + message[#message+1] = k .. ": " .. v + elseif type(v) == "table" then + for i, j in ipairs(v) do + message[#message+1] = k .. ": " .. j + end + end + end + message[#message+1] = "" message[#message+1] = "" -- cgit v1.2.3 From 3dbdff70960ec8c7de7d5a54721a560941627e0b Mon Sep 17 00:00:00 2001 From: Yuzo Date: Thu, 22 Jan 2015 13:12:48 +0800 Subject: add params support in options --- libs/luci-lib-httpclient/luasrc/httpclient.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libs/luci-lib-httpclient/luasrc/httpclient.lua b/libs/luci-lib-httpclient/luasrc/httpclient.lua index af79695244..c76cc542ed 100644 --- a/libs/luci-lib-httpclient/luasrc/httpclient.lua +++ b/libs/luci-lib-httpclient/luasrc/httpclient.lua @@ -97,7 +97,11 @@ end function request_raw(uri, options) options = options or {} local pr, auth, host, port, path - + + if options.params then + uri = uri .. '?' .. http.urlencode_params(options.params) + end + if uri:find("%[") then if uri:find("@") then pr, auth, host, port, path = uri:match("(%w+)://(.+)@(%b[]):?([0-9]*)(.*)") -- cgit v1.2.3