net/http.lua

changeset 646
90da4c9b34b5
parent 633
fe1e01a06729
child 677
93e5309c5430
equal deleted inserted replaced
645:d0a8ff9ba3e0 646:90da4c9b34b5
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), request); 30 request.callback(t_concat(request.body), request.code, request);
31 else 31 else
32 -- Error.. connection was closed prematurely 32 -- Error.. connection was closed prematurely
33 request.callback(0, "connection-closed", request); 33 request.callback("connection-closed", 0, 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), request); 50 request.callback(t_concat(request.body), request.code, 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
75 return request_reader(request, data, startpos); 75 return request_reader(request, data, startpos);
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 code = tonumber(code);
80 if not code then 81 if not code then
81 return request.callback(0, "invalid-status-line", request); 82 return request.callback("invalid-status-line", 0, request);
82 end 83 end
83 84
84 request.code, request.responseversion = code, http; 85 request.code, request.responseversion = code, http;
85 86
86 if request.onlystatus or not expectbody(request, tonumber(code)) then 87 if request.onlystatus or not expectbody(request, code) then
87 if request.callback then 88 if request.callback then
88 request.callback(code, nil, request); 89 request.callback(nil, code, request);
89 end 90 end
90 destroy_request(request); 91 destroy_request(request);
91 return; 92 return;
92 end 93 end
93 94
97 return request_reader(request, data, linelen); 98 return request_reader(request, data, linelen);
98 end 99 end
99 end 100 end
100 end 101 end
101 102
102 function request(u, callback, ex) 103 function request(u, ex, callback)
103 local req = url.parse(u); 104 local req = url.parse(u);
104 105
105 local custom_headers, body; 106 local custom_headers, body;
106 local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" } 107 local default_headers = { ["Host"] = req.host, ["User-Agent"] = "Prosody XMPP Server" }
107 108

mercurial