Fix to prevent calling HTTP request callback twice with the same data

Thu, 08 Jan 2009 02:02:35 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 08 Jan 2009 02:02:35 +0000
changeset 677
93e5309c5430
parent 676
5e2dfeba1f14
child 678
1859edec2237

Fix to prevent calling HTTP request callback twice with the same data

net/http.lua file | annotate | diff | comparison | revisions
--- a/net/http.lua	Wed Jan 07 17:41:27 2009 +0000
+++ b/net/http.lua	Thu Jan 08 02:02:35 2009 +0000
@@ -27,8 +27,9 @@
 local function request_reader(request, data, startpos)
 	if not data then
 		if request.body then
+			log("debug", "Connection closed, but we have data, calling callback...");
 			request.callback(t_concat(request.body), request.code, request);
-		else
+		elseif request.state ~= "completed" then
 			-- Error.. connection was closed prematurely
 			request.callback("connection-closed", 0, request);
 		end
@@ -46,11 +47,15 @@
 			request.havebodylength = request.havebodylength + #data;
 			if request.havebodylength >= request.bodylength then
 				-- We have the body
+				log("debug", "Have full body, calling callback");
 				if request.callback then
 					request.callback(t_concat(request.body), request.code, request);
 				end
+				request.body = nil;
+				request.state = "completed";
+			else
+				print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength);
 			end
-			print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength);
 		end
 	elseif request.state == "headers" then
 		print("Reading headers...")

mercurial