diff --git a/lualib/http/url.lua b/lualib/http/url.lua index ae74b099..72a9c7e5 100644 --- a/lualib/http/url.lua +++ b/lualib/http/url.lua @@ -20,7 +20,17 @@ end function url.parse_query(q) local r = {} for k,v in q:gmatch "(.-)=([^&]*)&?" do - r[decode(k)] = decode(v) + local dk, dv = decode(k), decode(v) + local oldv = r[dk] + if oldv then + if type(oldv) ~= "table" then + r[dk] = {oldv, dv} + else + oldv[#oldv+1] = dv + end + else + r[dk] = dv + end end return r end diff --git a/test/testhttp.lua b/test/testhttp.lua index 5391574f..9c0ce789 100644 --- a/test/testhttp.lua +++ b/test/testhttp.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local httpc = require "http.httpc" +local httpurl = require "http.url" local dns = require "skynet.dns" local function http_test(protocol) @@ -44,11 +45,25 @@ local function http_head_test() end end +local function http_url_test() + local url = "http://baidu.com/get?k1=1&k2=2&k4=a%20space&k5=b%20space&k5=b%20space&k5=b%20space" + local path, query = httpurl.parse(url) + print("url", path, query) + local qret = httpurl.parse_query(query) + for k, v in pairs(qret) do + print(k, v) + end + assert(#qret["k5"] == 3) + assert(qret[1] == qret[2]) + assert(qret[1] == qret[3]) +end + local function main() dns.server() http_stream_test() http_head_test() + http_url_test() http_test("http") if not pcall(require,"ltls.c") then