Comparison

net/http/server.lua @ 10392:7a95f27ac9d6

net.http.server: Factor out handling of event response for easier reuse
author Kim Alvefur <zash@zash.se>
date Fri, 01 Nov 2019 22:25:54 +0100
parent 10326:cd1c73c2bdec
child 10393:0ea7b4769096
comparison
equal deleted inserted replaced
10391:986349fc0f9e 10392:7a95f27ac9d6
168 t[k] = v; 168 t[k] = v;
169 return v; 169 return v;
170 end 170 end
171 }); 171 });
172 172
173 local function handle_result(request, response, result)
174 if result == nil then
175 result = 404;
176 end
177
178 if result == true then
179 return;
180 end
181
182 local body;
183 local result_type = type(result);
184 if result_type == "number" then
185 response.status_code = result;
186 if result >= 400 then
187 body = events.fire_event("http-error", { request = request, response = response, code = result });
188 end
189 elseif result_type == "string" then
190 body = result;
191 elseif result_type == "table" then
192 for k, v in pairs(result) do
193 if k ~= "headers" then
194 response[k] = v;
195 else
196 for header_name, header_value in pairs(v) do
197 response.headers[header_name] = header_value;
198 end
199 end
200 end
201 end
202 response:send(body);
203 end
204
173 function _M.hijack_response(response, listener) -- luacheck: ignore 205 function _M.hijack_response(response, listener) -- luacheck: ignore
174 error("TODO"); 206 error("TODO");
175 end 207 end
176 function handle_request(conn, request, finish_cb) 208 function handle_request(conn, request, finish_cb)
177 --log("debug", "handler: %s", request.path); 209 --log("debug", "handler: %s", request.path);
259 local host_head_event = "GET "..host..request.path:match("[^?]*"); 291 local host_head_event = "GET "..host..request.path:match("[^?]*");
260 log("debug", "Firing event: %s", host_head_event); 292 log("debug", "Firing event: %s", host_head_event);
261 result = events.fire_event(host_head_event, payload); 293 result = events.fire_event(host_head_event, payload);
262 end 294 end
263 end 295 end
264 if result ~= nil then 296
265 if result ~= true then 297 return handle_result(request, response, result);
266 local body; 298 end
267 local result_type = type(result); 299
268 if result_type == "number" then
269 response.status_code = result;
270 if result >= 400 then
271 payload.code = result;
272 body = events.fire_event("http-error", payload);
273 end
274 elseif result_type == "string" then
275 body = result;
276 elseif result_type == "table" then
277 for k, v in pairs(result) do
278 if k ~= "headers" then
279 response[k] = v;
280 else
281 for header_name, header_value in pairs(v) do
282 response.headers[header_name] = header_value;
283 end
284 end
285 end
286 end
287 response:send(body);
288 end
289 return;
290 end
291
292 -- if handler not called, return 404
293 response.status_code = 404;
294 payload.code = 404;
295 response:send(events.fire_event("http-error", payload));
296 end
297 local function prepare_header(response) 300 local function prepare_header(response)
298 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); 301 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]);
299 local headers = response.headers; 302 local headers = response.headers;
300 local output = { status_line }; 303 local output = { status_line };
301 for k,v in pairs(headers) do 304 for k,v in pairs(headers) do