Software /
code /
prosody
Comparison
net/http/server.lua @ 6071:420c0d3b8583
net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
author | Daurnimator <quae@daurnimator.com> |
---|---|
date | Tue, 15 Apr 2014 18:07:45 +0100 |
parent | 5776:bd0ff8ae98a8 |
child | 6082:d0e824a21861 |
comparison
equal
deleted
inserted
replaced
6069:446148cad35e | 6071:420c0d3b8583 |
---|---|
183 status_code = 200; | 183 status_code = 200; |
184 headers = { date = date_header, connection = response_conn_header }; | 184 headers = { date = date_header, connection = response_conn_header }; |
185 persistent = persistent; | 185 persistent = persistent; |
186 conn = conn; | 186 conn = conn; |
187 send = _M.send_response; | 187 send = _M.send_response; |
188 done = _M.finish_response; | |
188 finish_cb = finish_cb; | 189 finish_cb = finish_cb; |
189 }; | 190 }; |
190 conn._http_open_response = response; | 191 conn._http_open_response = response; |
191 | 192 |
192 local host = (request.headers.host or ""):match("[^:]+"); | 193 local host = (request.headers.host or ""):match("[^:]+"); |
244 | 245 |
245 -- if handler not called, return 404 | 246 -- if handler not called, return 404 |
246 response.status_code = 404; | 247 response.status_code = 404; |
247 response:send(events.fire_event("http-error", { code = 404 })); | 248 response:send(events.fire_event("http-error", { code = 404 })); |
248 end | 249 end |
250 local function prepare_header(response) | |
251 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); | |
252 local headers = response.headers; | |
253 local output = { status_line }; | |
254 for k,v in pairs(headers) do | |
255 t_insert(output, headerfix[k]..v); | |
256 end | |
257 t_insert(output, "\r\n\r\n"); | |
258 t_insert(output, body); | |
259 return output; | |
260 end | |
261 _M.prepare_header = prepare_header; | |
249 function _M.send_response(response, body) | 262 function _M.send_response(response, body) |
263 if response.finished then return; end | |
264 body = body or response.body or ""; | |
265 headers.content_length = #body; | |
266 local output = prepare_header(respone); | |
267 t_insert(output, body); | |
268 response.conn:write(t_concat(output)); | |
269 response:finish(); | |
270 end | |
271 function _M.finish_response(response) | |
250 if response.finished then return; end | 272 if response.finished then return; end |
251 response.finished = true; | 273 response.finished = true; |
252 response.conn._http_open_response = nil; | 274 response.conn._http_open_response = nil; |
253 | |
254 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); | |
255 local headers = response.headers; | |
256 body = body or response.body or ""; | |
257 headers.content_length = #body; | |
258 | |
259 local output = { status_line }; | |
260 for k,v in pairs(headers) do | |
261 t_insert(output, headerfix[k]..v); | |
262 end | |
263 t_insert(output, "\r\n\r\n"); | |
264 t_insert(output, body); | |
265 | |
266 response.conn:write(t_concat(output)); | |
267 if response.on_destroy then | 275 if response.on_destroy then |
268 response:on_destroy(); | 276 response:on_destroy(); |
269 response.on_destroy = nil; | 277 response.on_destroy = nil; |
270 end | 278 end |
271 if response.persistent then | 279 if response.persistent then |