Comparison

net/http.lua @ 6780:647adfd8f738

net.*: Remove use of module() function
author Kim Alvefur <zash@zash.se>
date Sat, 21 Feb 2015 10:45:24 +0100
parent 6501:71b6e8b48a12
child 6791:e813e8cf6046
child 6823:3b07f38d70f5
comparison
equal deleted inserted replaced
6779:6236668da30a 6780:647adfd8f738
22 tonumber, tostring, xpcall, select, debug.traceback; 22 tonumber, tostring, xpcall, select, debug.traceback;
23 local assert, error = assert, error 23 local assert, error = assert, error
24 24
25 local log = require "util.logger".init("http"); 25 local log = require "util.logger".init("http");
26 26
27 module "http" 27 local _ENV = nil;
28 28
29 local requests = {}; -- Open requests 29 local requests = {}; -- Open requests
30 30
31 local listener = { default_port = 80, default_mode = "*a" }; 31 local listener = { default_port = 80, default_mode = "*a" };
32 32
72 requests[conn] = nil; 72 requests[conn] = nil;
73 end 73 end
74 74
75 function listener.ondetach(conn) 75 function listener.ondetach(conn)
76 requests[conn] = nil; 76 requests[conn] = nil;
77 end
78
79 local function destroy_request(request)
80 if request.conn then
81 request.conn = nil;
82 request.handler:close()
83 end
77 end 84 end
78 85
79 local function request_reader(request, data, err) 86 local function request_reader(request, data, err)
80 if not request.parser then 87 if not request.parser then
81 local function error_cb(reason) 88 local function error_cb(reason)
105 end 112 end
106 request.parser:feed(data); 113 request.parser:feed(data);
107 end 114 end
108 115
109 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end 116 local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end
110 function request(u, ex, callback) 117 local function request(u, ex, callback)
111 local req = url.parse(u); 118 local req = url.parse(u);
112 119
113 if not (req and req.host) then 120 if not (req and req.host) then
114 callback(nil, 0, req); 121 callback(nil, 0, req);
115 return nil, "invalid-url"; 122 return nil, "invalid-url";
187 194
188 requests[req.handler] = req; 195 requests[req.handler] = req;
189 return req; 196 return req;
190 end 197 end
191 198
192 function destroy_request(request) 199 return {
193 if request.conn then 200 request = request;
194 request.conn = nil; 201
195 request.handler:close() 202 -- COMPAT
196 end 203 urlencode = util_http.urlencode;
197 end 204 urldecode = util_http.urldecode;
198 205 formencode = util_http.formencode;
199 local urlencode, urldecode = util_http.urlencode, util_http.urldecode; 206 formdecode = util_http.formdecode;
200 local formencode, formdecode = util_http.formencode, util_http.formdecode; 207 };
201
202 _M.urlencode, _M.urldecode = urlencode, urldecode;
203 _M.formencode, _M.formdecode = formencode, formdecode;
204
205 return _M;