Skip to content

Commit

Permalink
support chunk encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
findstr committed Aug 6, 2016
1 parent c8b5582 commit 5e9a92d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lualib/http/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
29 changes: 24 additions & 5 deletions lualib/http/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 13 additions & 4 deletions lualib/http/stream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5e9a92d

Please sign in to comment.