Mercurial > prosody-hg
comparison net/http/server.lua @ 11527:eaff6e548f12
net.http.server: Split out method for sending only the header
Makes it easier to reuse, e.g. for SSE or websockets or other custom
responses.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 24 Apr 2021 10:50:24 +0200 |
| parents | d30c44a829c1 |
| children | 1cdaf21584da |
comparison
equal
deleted
inserted
replaced
| 11526:15a3db955ad3 | 11527:eaff6e548f12 |
|---|---|
| 254 status_code = 200; | 254 status_code = 200; |
| 255 headers = { date = date_header, connection = response_conn_header }; | 255 headers = { date = date_header, connection = response_conn_header }; |
| 256 persistent = persistent; | 256 persistent = persistent; |
| 257 conn = conn; | 257 conn = conn; |
| 258 send = _M.send_response; | 258 send = _M.send_response; |
| 259 write_headers = _M.write_headers; | |
| 259 send_file = _M.send_file; | 260 send_file = _M.send_file; |
| 260 done = _M.finish_response; | 261 done = _M.finish_response; |
| 261 finish_cb = finish_cb; | 262 finish_cb = finish_cb; |
| 262 }; | 263 }; |
| 263 conn._http_open_response = response; | 264 conn._http_open_response = response; |
| 327 end | 328 end |
| 328 t_insert(output, "\r\n\r\n"); | 329 t_insert(output, "\r\n\r\n"); |
| 329 return output; | 330 return output; |
| 330 end | 331 end |
| 331 _M.prepare_header = prepare_header; | 332 _M.prepare_header = prepare_header; |
| 332 function _M.send_head_response(response) | 333 function _M.write_headers(response) |
| 333 if response.finished then return; end | 334 if response.finished then return; end |
| 334 local output = prepare_header(response); | 335 local output = prepare_header(response); |
| 335 response.conn:write(t_concat(output)); | 336 response.conn:write(t_concat(output)); |
| 337 end | |
| 338 function _M.send_head_response(response) | |
| 339 if response.finished then return; end | |
| 340 _M.write_headers(response); | |
| 336 response:done(); | 341 response:done(); |
| 337 end | 342 end |
| 338 function _M.send_response(response, body) | 343 function _M.send_response(response, body) |
| 339 if response.finished then return; end | 344 if response.finished then return; end |
| 340 body = body or response.body or ""; | 345 body = body or response.body or ""; |
| 379 if f.close then f:close(); end | 384 if f.close then f:close(); end |
| 380 incomplete[response.conn] = nil; | 385 incomplete[response.conn] = nil; |
| 381 return response:done(); | 386 return response:done(); |
| 382 end | 387 end |
| 383 end | 388 end |
| 384 response.conn:write(t_concat(prepare_header(response))); | 389 _M.write_headers(response); |
| 385 return true; | 390 return true; |
| 386 end | 391 end |
| 387 function _M.finish_response(response) | 392 function _M.finish_response(response) |
| 388 if response.finished then return; end | 393 if response.finished then return; end |
| 389 response.finished = true; | 394 response.finished = true; |
