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 |
47 t_insert(resp, ": "); |
47 t_insert(resp, ": "); |
48 t_insert(resp, v); |
48 t_insert(resp, v); |
49 t_insert(resp, "\r\n"); |
49 t_insert(resp, "\r\n"); |
50 end |
50 end |
51 end |
51 end |
52 if not (h and h["Content-Length"]) then |
52 if body and not (h and h["Content-Length"]) then |
53 t_insert(resp, "Content-Length: "); |
53 t_insert(resp, "Content-Length: "); |
54 t_insert(resp, #body); |
54 t_insert(resp, #body); |
55 t_insert(resp, "\r\n"); |
55 t_insert(resp, "\r\n"); |
56 end |
56 end |
57 t_insert(resp, "\r\n"); |
57 t_insert(resp, "\r\n"); |
58 |
58 |
59 if request.method ~= "HEAD" then |
59 if body and request.method ~= "HEAD" then |
60 t_insert(resp, body); |
60 t_insert(resp, body); |
61 end |
61 end |
62 else |
62 else |
63 -- Response we have is just a string (the body) |
63 -- Response we have is just a string (the body) |
64 log("debug", "Sending 200 response to %s", request.id or "<none>"); |
64 log("debug", "Sending 200 response to %s", request.id or "<none>"); |