Comparison

net/http.lua @ 2675:ab643a77da2d

net.http: Update print()s to log()s - don't ask how this came to be, I have no idea :)
author Matthew Wild <mwild1@gmail.com>
date Fri, 19 Feb 2010 03:27:18 +0000
parent 2673:61ae351c19b5
child 2925:692b3c6c5bd2
comparison
equal deleted inserted replaced
2674:a1fdfd7318df 2675:ab643a77da2d
15 15
16 local connlisteners_get = require "net.connlisteners".get; 16 local connlisteners_get = require "net.connlisteners".get;
17 local listener = connlisteners_get("httpclient") or error("No httpclient listener!"); 17 local listener = connlisteners_get("httpclient") or error("No httpclient listener!");
18 18
19 local t_insert, t_concat = table.insert, table.concat; 19 local t_insert, t_concat = table.insert, table.concat;
20 local tonumber, tostring, pairs, xpcall, select, debug_traceback, char, format = 20 local tonumber, tostring, pairs, xpcall, select, debug_traceback, char, format =
21 tonumber, tostring, pairs, xpcall, select, debug.traceback, string.char, string.format; 21 tonumber, tostring, pairs, xpcall, select, debug.traceback, string.char, string.format;
22 22
23 local log = require "util.logger".init("http"); 23 local log = require "util.logger".init("http");
24 local print = function () end
25 24
26 module "http" 25 module "http"
27 26
28 function urlencode(s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end 27 function urlencode(s) return s and (s:gsub("%W", function (c) return format("%%%02x", c:byte()); end)); end
29 function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end 28 function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); end
49 request.body = nil; 48 request.body = nil;
50 request.state = "completed"; 49 request.state = "completed";
51 return; 50 return;
52 end 51 end
53 if request.state == "body" and request.state ~= "completed" then 52 if request.state == "body" and request.state ~= "completed" then
54 print("Reading body...") 53 log("debug", "Reading body...")
55 if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.responseheaders["content-length"]); end 54 if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.responseheaders["content-length"]); end
56 if startpos then 55 if startpos then
57 data = data:sub(startpos, -1) 56 data = data:sub(startpos, -1)
58 end 57 end
59 t_insert(request.body, data); 58 t_insert(request.body, data);
66 request.callback(t_concat(request.body), request.code, request); 65 request.callback(t_concat(request.body), request.code, request);
67 end 66 end
68 request.body = nil; 67 request.body = nil;
69 request.state = "completed"; 68 request.state = "completed";
70 else 69 else
71 print("", "Have "..request.havebodylength.." bytes out of "..request.bodylength); 70 log("debug", "Have "..request.havebodylength.." bytes out of "..request.bodylength);
72 end 71 end
73 end 72 end
74 elseif request.state == "headers" then 73 elseif request.state == "headers" then
75 print("Reading headers...") 74 log("debug", "Reading headers...")
76 local pos = startpos; 75 local pos = startpos;
77 local headers, headers_complete = request.responseheaders; 76 local headers, headers_complete = request.responseheaders;
78 if not headers then 77 if not headers then
79 headers = {}; 78 headers = {};
80 request.responseheaders = headers; 79 request.responseheaders = headers;
82 for line in data:sub(startpos, -1):gmatch("(.-)\r\n") do 81 for line in data:sub(startpos, -1):gmatch("(.-)\r\n") do
83 startpos = startpos + #line + 2; 82 startpos = startpos + #line + 2;
84 local k, v = line:match("(%S+): (.+)"); 83 local k, v = line:match("(%S+): (.+)");
85 if k and v then 84 if k and v then
86 headers[k:lower()] = v; 85 headers[k:lower()] = v;
87 --print("Header: "..k:lower().." = "..v); 86 --log("debug", "Header: "..k:lower().." = "..v);
88 elseif #line == 0 then 87 elseif #line == 0 then
89 headers_complete = true; 88 headers_complete = true;
90 break; 89 break;
91 else 90 else
92 print("Unhandled header line: "..line); 91 log("warn", "Unhandled header line: "..line);
93 end 92 end
94 end 93 end
95 if not headers_complete then return; end 94 if not headers_complete then return; end
96 -- Reached the end of the headers 95 -- Reached the end of the headers
97 if not expectbody(request, request.code) then 96 if not expectbody(request, request.code) then
101 request.state = "body"; 100 request.state = "body";
102 if #data > startpos then 101 if #data > startpos then
103 return request_reader(request, data, startpos); 102 return request_reader(request, data, startpos);
104 end 103 end
105 elseif request.state == "status" then 104 elseif request.state == "status" then
106 print("Reading status...") 105 log("debug", "Reading status...")
107 local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos); 106 local http, code, text, linelen = data:match("^HTTP/(%S+) (%d+) (.-)\r\n()", startpos);
108 code = tonumber(code); 107 code = tonumber(code);
109 if not code then 108 if not code then
110 log("warn", "Invalid HTTP status line, telling callback then closing"); 109 log("warn", "Invalid HTTP status line, telling callback then closing");
111 local ret = request.callback("invalid-status-line", 0, request); 110 local ret = request.callback("invalid-status-line", 0, request);