Software / code / prosody
Comparison
net/httpserver.lua @ 2481:9b407a6acf39
net.httpserver: Make it possible to return responses with no body
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 21 Jan 2010 13:14:52 +0000 |
| parent | 2480:3596d181cfc3 |
| child | 2483:2f235c57d713 |
comparison
equal
deleted
inserted
replaced
| 2480:3596d181cfc3 | 2481:9b407a6acf39 |
|---|---|
| 34 end | 34 end |
| 35 | 35 |
| 36 local function send_response(request, response) | 36 local function send_response(request, response) |
| 37 -- Write status line | 37 -- Write status line |
| 38 local resp; | 38 local resp; |
| 39 if response.body then | 39 if response.body or response.headers then |
| 40 local body = tostring(response.body); | 40 local body = response.body and tostring(response.body); |
| 41 log("debug", "Sending response to %s", request.id); | 41 log("debug", "Sending response to %s", request.id); |
| 42 resp = { "HTTP/1.0 "..(response.status or "200 OK").."\r\n" }; | 42 resp = { "HTTP/1.0 "..(response.status or "200 OK").."\r\n" }; |
| 43 local h = response.headers; | 43 local h = response.headers; |
| 44 if h then | 44 if h then |
| 45 for k, v in pairs(h) do | 45 for k, v in pairs(h) do |
| 46 t_insert(resp, k..": "..v.."\r\n"); | 46 t_insert(resp, k..": "..v.."\r\n"); |
| 47 end | 47 end |
| 48 end | 48 end |
| 49 if not (h and h["Content-Length"]) then | 49 if body and not (h and h["Content-Length"]) then |
| 50 t_insert(resp, "Content-Length: "..#body.."\r\n"); | 50 t_insert(resp, "Content-Length: "..#body.."\r\n"); |
| 51 end | 51 end |
| 52 t_insert(resp, "\r\n"); | 52 t_insert(resp, "\r\n"); |
| 53 | 53 |
| 54 if request.method ~= "HEAD" then | 54 if body and request.method ~= "HEAD" then |
| 55 t_insert(resp, body); | 55 t_insert(resp, body); |
| 56 end | 56 end |
| 57 request.write(t_concat(resp)); | 57 request.write(t_concat(resp)); |
| 58 else | 58 else |
| 59 -- Response we have is just a string (the body) | 59 -- Response we have is just a string (the body) |