net/http.lua

changeset 618
69de678792d6
parent 617
b17fd66b7e10
child 619
6d720aba51cb
equal deleted inserted replaced
617:b17fd66b7e10 618:69de678792d6
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 request.callback(request.code, t_concat(request.body)); 30 request.callback(request.code, t_concat(request.body), request);
31 else 31 else
32 -- Error.. connection was closed prematurely 32 -- Error.. connection was closed prematurely
33 request.callback(0, "connection-closed"); 33 request.callback(0, "connection-closed", request);
34 end 34 end
35 destroy_request(request); 35 destroy_request(request);
36 return; 36 return;
37 end 37 end
38 if request.state == "body" then 38 if request.state == "body" then
45 if request.bodylength then 45 if request.bodylength then
46 request.havebodylength = request.havebodylength + #data; 46 request.havebodylength = request.havebodylength + #data;
47 if request.havebodylength >= request.bodylength then 47 if request.havebodylength >= request.bodylength then
48 -- We have the body 48 -- We have the body
49 if request.callback then 49 if request.callback then
50 request.callback(request.code, t_concat(request.body)); 50 request.callback(request.code, t_concat(request.body), request);
51 end 51 end
52 end 52 end
53 print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength); 53 print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength);
54 end 54 end
55 elseif request.state == "headers" then 55 elseif request.state == "headers" then
76 end 76 end
77 elseif request.state == "status" then 77 elseif request.state == "status" then
78 print("Reading status...") 78 print("Reading status...")
79 local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos); 79 local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos);
80 if not code then 80 if not code then
81 return request.callback(0, "invalid-status-line"); 81 return request.callback(0, "invalid-status-line", request);
82 end 82 end
83 83
84 request.responsecode, request.responseversion = code, http; 84 request.responsecode, request.responseversion = code, http;
85 85
86 if request.onlystatus or not expectbody(request, tonumber(code)) then 86 if request.onlystatus or not expectbody(request, tonumber(code)) then
87 if request.callback then 87 if request.callback then
88 request.callback(code, nil); 88 request.callback(code, nil, request);
89 end 89 end
90 destroy_request(request); 90 destroy_request(request);
91 return; 91 return;
92 end 92 end
93 93

mercurial