diff --git a/lualib/http/client.lua b/lualib/http/client.lua index 43fcabfc..033a0525 100644 --- a/lualib/http/client.lua +++ b/lualib/http/client.lua @@ -22,7 +22,8 @@ local function send_request(fd, method, host, abs, header, body) table.insert(header, 1, string.format("%s %s HTTP/1.1", method, abs)) table.insert(header, string.format("Host: %s", host)) table.insert(header, string.format("Content-Length: %d", #body)) - table.insert(header, string.format("User-Agent: Silly/0.2")) + table.insert(header, "User-Agent: Silly/0.2") + table.insert(header, "Connection: keep-alive") tmp = tmp .. table.concat(header, "\r\n") tmp = tmp .. "\r\n\r\n" tmp = tmp .. body @@ -45,7 +46,7 @@ local function recv_response(fd) return status end local ver, status= first:match("HTTP/([%d|.]+)%s+(%d+)") - return status, header, body, ver + return tonumber(status), header, body, ver end local function process(uri, header, body) diff --git a/lualib/http/server.lua b/lualib/http/server.lua index 12400fa1..2db080ff 100644 --- a/lualib/http/server.lua +++ b/lualib/http/server.lua @@ -73,6 +73,21 @@ local http_err_msg = { [599] = "Network Connect Timeout Error", } +local function parse_uri(str) + local form = {} + local start = string.find(str, "?", 1, true) + if not start then + return str, form + end + assert(start > 1) + local uri = string.sub(str, 1, start - 1) + local f = string.sub(str, start + 1) + for k, v in string.gmatch(f, "(%w+)=(%w+)") do + form[k] = v + end + return uri, form +end + local function httpd(fd, handler) socket.limit(fd, 1024 * 512) local readl = function() @@ -102,15 +117,21 @@ local function httpd(fd, handler) local method, uri, ver = first:match("(%w+)%s+(.-)%s+HTTP/([%d|.]+)\r\n") assert(method and uri and ver) header.method = method - header.URI = uri header.version = ver + header.uri, header.form = parse_uri(uri) if tonumber(ver) > 1.1 then write(505, {}, "") socket.close(fd) return end - + + if header["Content-Type"] == "application/x-www-form-urlencoded" then + for k, v in string.gmatch(body, "(%w+)=(%w+)") do + header.form[k] = v + end + body = "" + end handler(header, body, write) end end @@ -125,9 +146,7 @@ end function server.send(fd, status, header, body) local tmp = string.format("HTTP/1.1 %d %s\r\n", status, http_err_msg[status]) - for _, v in pairs(header) do - tmp = tmp .. v .. "\r\n" - end + tmp = tmp .. table.concat(header, "\r\n") tmp = tmp .. string.format("Content-Length: %d\r\n", #body) tmp = tmp .. "\r\n" tmp = tmp .. body diff --git a/lualib/http/stream.lua b/lualib/http/stream.lua index cf72b101..0db3f2f0 100644 --- a/lualib/http/stream.lua +++ b/lualib/http/stream.lua @@ -21,11 +21,20 @@ function stream.recv_request(readl, readn) return nil end end - - if header["Transfer-Encoding"] then - return 501 + local encoding = header["Transfer-Encoding"] + if encoding then + if encoding ~= "chunked" then + return 501 + end + while true do + local n = readl() + local sz = tonumber(n, 16) + if not sz or sz == 0 then + break + end + body = body .. readn(sz) + end end - if header["Content-Length"] then local len = tonumber(header["Content-Length"]) if len > 4096 then