diff options
Diffstat (limited to 'net/http.lua')
-rw-r--r-- | net/http.lua | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/net/http.lua b/net/http.lua index 9d2f9b96..09b04e61 100644 --- a/net/http.lua +++ b/net/http.lua @@ -30,7 +30,7 @@ function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char( local function expectbody(reqt, code) if reqt.method == "HEAD" then return nil end - if code == 204 or code == 304 then return nil end + if code == 204 or code == 304 or code == 301 then return nil end if code >= 100 and code < 200 then return nil end return 1 end @@ -43,6 +43,7 @@ local function request_reader(request, data, startpos) elseif request.state ~= "completed" then -- Error.. connection was closed prematurely request.callback("connection-closed", 0, request); + return; end destroy_request(request); request.body = nil; @@ -73,22 +74,31 @@ local function request_reader(request, data, startpos) elseif request.state == "headers" then print("Reading headers...") local pos = startpos; - local headers = request.responseheaders or {}; + local headers, headers_complete = request.responseheaders; + if not headers then + headers = {}; + request.responseheaders = headers; + end for line in data:sub(startpos, -1):gmatch("(.-)\r\n") do startpos = startpos + #line + 2; local k, v = line:match("(%S+): (.+)"); if k and v then headers[k:lower()] = v; - print("Header: "..k:lower().." = "..v); + --print("Header: "..k:lower().." = "..v); elseif #line == 0 then - request.responseheaders = headers; + headers_complete = true; break; else print("Unhandled header line: "..line); end end + if not headers_complete then return; end -- Reached the end of the headers - request.state = "body"; + if not expectbody(request, request.code) then + request.callback(nil, request.code, request); + return; + end + request.state = "body"; if #data > startpos then return request_reader(request, data, startpos); end @@ -97,12 +107,15 @@ local function request_reader(request, data, startpos) local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos); code = tonumber(code); if not code then - return request.callback("invalid-status-line", 0, request); + log("warn", "Invalid HTTP status line, telling callback then closing"); + local ret = request.callback("invalid-status-line", 0, request); + destroy_request(request); + return ret; end request.code, request.responseversion = code, http; - if request.onlystatus or not expectbody(request, code) then + if request.onlystatus then if request.callback then request.callback(nil, code, request); end @@ -200,7 +213,7 @@ end function destroy_request(request) if request.conn then request.handler.close() - listener.disconnect(request.conn, "closed"); + listener.disconnect(request.handler, "closed"); end end |