Comparison

net/http.lua @ 677:93e5309c5430

Fix to prevent calling HTTP request callback twice with the same data
author Matthew Wild <mwild1@gmail.com>
date Thu, 08 Jan 2009 02:02:35 +0000
parent 646:90da4c9b34b5
child 678:1859edec2237
comparison
equal deleted inserted replaced
676:5e2dfeba1f14 677:93e5309c5430
25 end 25 end
26 26
27 local function request_reader(request, data, startpos) 27 local function request_reader(request, data, startpos)
28 if not data then 28 if not data then
29 if request.body then 29 if request.body then
30 log("debug", "Connection closed, but we have data, calling callback...");
30 request.callback(t_concat(request.body), request.code, request); 31 request.callback(t_concat(request.body), request.code, request);
31 else 32 elseif request.state ~= "completed" then
32 -- Error.. connection was closed prematurely 33 -- Error.. connection was closed prematurely
33 request.callback("connection-closed", 0, request); 34 request.callback("connection-closed", 0, request);
34 end 35 end
35 destroy_request(request); 36 destroy_request(request);
36 return; 37 return;
44 t_insert(request.body, data); 45 t_insert(request.body, data);
45 if request.bodylength then 46 if request.bodylength then
46 request.havebodylength = request.havebodylength + #data; 47 request.havebodylength = request.havebodylength + #data;
47 if request.havebodylength >= request.bodylength then 48 if request.havebodylength >= request.bodylength then
48 -- We have the body 49 -- We have the body
50 log("debug", "Have full body, calling callback");
49 if request.callback then 51 if request.callback then
50 request.callback(t_concat(request.body), request.code, request); 52 request.callback(t_concat(request.body), request.code, request);
51 end 53 end
54 request.body = nil;
55 request.state = "completed";
56 else
57 print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength);
52 end 58 end
53 print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength);
54 end 59 end
55 elseif request.state == "headers" then 60 elseif request.state == "headers" then
56 print("Reading headers...") 61 print("Reading headers...")
57 local pos = startpos; 62 local pos = startpos;
58 local headers = request.responseheaders or {}; 63 local headers = request.responseheaders or {};