From 4c3707ec7bb896d1efaccf63eed18aee8cd88ef8 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 21 Jan 2010 13:10:13 +0000 Subject: net.httpserver: Close connection on invalid HTTP status line --- net/httpserver.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'net/httpserver.lua') diff --git a/net/httpserver.lua b/net/httpserver.lua index ddb4475c..1341bfbb 100644 --- a/net/httpserver.lua +++ b/net/httpserver.lua @@ -175,7 +175,10 @@ local function request_reader(request, data, startpos) log("debug", "Reading request line...") local method, path, http, linelen = data:match("^(%S+) (%S+) HTTP/(%S+)\r\n()", startpos); if not method then - return call_callback(request, "invalid-status-line"); + log("warn", "Invalid HTTP status line, telling callback then closing"); + local ret = call_callback(request, "invalid-status-line"); + request:destroy(); + return ret; end request.method, request.path, request.httpversion = method, path, http; -- cgit v1.2.3 From e79181eab31b87bbab91ff2cc73f43d529d4b2dc Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 21 Jan 2010 13:14:52 +0000 Subject: net.httpserver: Make it possible to return responses with no body --- net/httpserver.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'net/httpserver.lua') diff --git a/net/httpserver.lua b/net/httpserver.lua index 1341bfbb..e16d4de6 100644 --- a/net/httpserver.lua +++ b/net/httpserver.lua @@ -36,8 +36,8 @@ end local function send_response(request, response) -- Write status line local resp; - if response.body then - local body = tostring(response.body); + if response.body or response.headers then + local body = response.body and tostring(response.body); log("debug", "Sending response to %s", request.id); resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"}; local h = response.headers; @@ -49,14 +49,14 @@ local function send_response(request, response) t_insert(resp, "\r\n"); end end - if not (h and h["Content-Length"]) then + if body and not (h and h["Content-Length"]) then t_insert(resp, "Content-Length: "); t_insert(resp, #body); t_insert(resp, "\r\n"); end t_insert(resp, "\r\n"); - if request.method ~= "HEAD" then + if body and request.method ~= "HEAD" then t_insert(resp, body); end else -- cgit v1.2.3 From c103c2bb6b528a64b032e55cfb97d0f41dbf695d Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 21 Jan 2010 14:53:01 +0000 Subject: net.httpserver: More robust handling of headers split across multiple packets --- net/httpserver.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'net/httpserver.lua') diff --git a/net/httpserver.lua b/net/httpserver.lua index e16d4de6..c4be59bf 100644 --- a/net/httpserver.lua +++ b/net/httpserver.lua @@ -146,22 +146,29 @@ local function request_reader(request, data, startpos) elseif request.state == "headers" then log("debug", "Reading headers...") local pos = startpos; - local headers = request.headers or {}; + local headers, headers_complete = request.headers; + if not headers then + headers = {}; + request.headers = headers; + end + for line in data:gmatch("(.-)\r\n") do startpos = (startpos or 1) + #line + 2; local k, v = line:match("(%S+): (.+)"); if k and v then headers[k:lower()] = v; --- log("debug", "Header: "..k:lower().." = "..v); + --log("debug", "Header: '"..k:lower().."' = '"..v.."'"); elseif #line == 0 then - request.headers = headers; + headers_complete = true; break; else log("debug", "Unhandled header line: "..line); end end - if not expectbody(request) then + if not headers_complete then return; end + + if not expectbody(request) then call_callback(request); return; end -- cgit v1.2.3