Comparison

net/http/server.lua @ 6602:61b6a4fc65f1

Merge 0.9->0.10
author Matthew Wild <mwild1@gmail.com>
date Thu, 26 Mar 2015 16:48:39 +0000
parent 6529:a2dadaa9137f
parent 6589:015f2b4e2f9a
child 6605:03a43bf3ecd2
comparison
equal deleted inserted replaced
6587:54306208f30b 6602:61b6a4fc65f1
187 status_code = 200; 187 status_code = 200;
188 headers = { date = date_header, connection = response_conn_header }; 188 headers = { date = date_header, connection = response_conn_header };
189 persistent = persistent; 189 persistent = persistent;
190 conn = conn; 190 conn = conn;
191 send = _M.send_response; 191 send = _M.send_response;
192 done = _M.finish_response;
193 finish_cb = finish_cb; 192 finish_cb = finish_cb;
194 }; 193 };
195 conn._http_open_response = response; 194 conn._http_open_response = response;
196 195
197 local host = (request.headers.host or ""):match("[^:]+"); 196 local host = (request.headers.host or ""):match("[^:]+");
207 err_code, err = 404, "Unknown host: "..host; 206 err_code, err = 404, "Unknown host: "..host;
208 else 207 else
209 err_code, err = 400, "Missing or invalid 'Host' header"; 208 err_code, err = 400, "Missing or invalid 'Host' header";
210 end 209 end
211 end 210 end
212 211
213 if err then 212 if err then
214 response.status_code = err_code; 213 response.status_code = err_code;
215 response:send(events.fire_event("http-error", { code = err_code, message = err })); 214 response:send(events.fire_event("http-error", { code = err_code, message = err }));
216 return; 215 return;
217 end 216 end
218 217
219 local event = request.method.." "..host..request.path:match("[^?]*"); 218 local event = request.method.." "..host..request.path:match("[^?]*");
220 local payload = { request = request, response = response }; 219 local payload = { request = request, response = response };
221 log("debug", event); 220 log("debug", "Firing event: %s", event);
222 local result = events.fire_event(event, payload); 221 local result = events.fire_event(event, payload);
223 if result ~= nil then 222 if result ~= nil then
224 if result ~= true then 223 if result ~= true then
225 local body; 224 local body;
226 local result_type = type(result); 225 local result_type = type(result);
249 248
250 -- if handler not called, return 404 249 -- if handler not called, return 404
251 response.status_code = 404; 250 response.status_code = 404;
252 response:send(events.fire_event("http-error", { code = 404 })); 251 response:send(events.fire_event("http-error", { code = 404 }));
253 end 252 end
254 local function prepare_header(response) 253 function _M.send_response(response, body)
254 if response.finished then return; end
255 response.finished = true;
256 response.conn._http_open_response = nil;
257
255 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); 258 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]);
256 local headers = response.headers; 259 local headers = response.headers;
260 body = body or response.body or "";
261 headers.content_length = #body;
262
257 local output = { status_line }; 263 local output = { status_line };
258 for k,v in pairs(headers) do 264 for k,v in pairs(headers) do
259 t_insert(output, headerfix[k]..v); 265 t_insert(output, headerfix[k]..v);
260 end 266 end
261 t_insert(output, "\r\n\r\n"); 267 t_insert(output, "\r\n\r\n");
262 return output;
263 end
264 _M.prepare_header = prepare_header;
265 function _M.send_response(response, body)
266 if response.finished then return; end
267 body = body or response.body or "";
268 response.headers.content_length = #body;
269 local output = prepare_header(response);
270 t_insert(output, body); 268 t_insert(output, body);
269
271 response.conn:write(t_concat(output)); 270 response.conn:write(t_concat(output));
272 response:done();
273 end
274 function _M.finish_response(response)
275 if response.finished then return; end
276 response.finished = true;
277 response.conn._http_open_response = nil;
278 if response.on_destroy then 271 if response.on_destroy then
279 response:on_destroy(); 272 response:on_destroy();
280 response.on_destroy = nil; 273 response.on_destroy = nil;
281 end 274 end
282 if response.persistent then 275 if response.persistent then