Comparison

net/httpserver.lua @ 1632:4a70cbb0fad7

net.httpserver: Allow response.body to be a non-string
author Matthew Wild <mwild1@gmail.com>
date Wed, 05 Aug 2009 03:07:45 +0100
parent 1608:fb53fe17af36
child 1661:33b1aee4b77f
comparison
equal deleted inserted replaced
1626:7f17d0d00fbb 1632:4a70cbb0fad7
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 then
40 local body = tostring(response.body);
40 log("debug", "Sending response to %s", request.id); 41 log("debug", "Sending response to %s", request.id);
41 resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"}; 42 resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"};
42 local h = response.headers; 43 local h = response.headers;
43 if h then 44 if h then
44 for k, v in pairs(h) do 45 for k, v in pairs(h) do
46 t_insert(resp, ": "); 47 t_insert(resp, ": ");
47 t_insert(resp, v); 48 t_insert(resp, v);
48 t_insert(resp, "\r\n"); 49 t_insert(resp, "\r\n");
49 end 50 end
50 end 51 end
51 if response.body and not (h and h["Content-Length"]) then 52 if not (h and h["Content-Length"]) then
52 t_insert(resp, "Content-Length: "); 53 t_insert(resp, "Content-Length: ");
53 t_insert(resp, #response.body); 54 t_insert(resp, #body);
54 t_insert(resp, "\r\n"); 55 t_insert(resp, "\r\n");
55 end 56 end
56 t_insert(resp, "\r\n"); 57 t_insert(resp, "\r\n");
57 58
58 if response.body and request.method ~= "HEAD" then 59 if request.method ~= "HEAD" then
59 t_insert(resp, response.body); 60 t_insert(resp, body);
60 end 61 end
61 else 62 else
62 -- Response we have is just a string (the body) 63 -- Response we have is just a string (the body)
63 log("debug", "Sending response to %s: %s", request.id or "<none>", response or "<none>"); 64 log("debug", "Sending response to %s: %s", request.id or "<none>", response or "<none>");
64 65