net/httpserver.lua

changeset 2399
0325f241a26c
parent 2371
7ddb8ce0cd0b
child 2480
3596d181cfc3
equal deleted inserted replaced
2398:44f694ce6aec 2399:0325f241a26c
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 local body = 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); 46 t_insert(resp, k..": "..v.."\r\n");
47 t_insert(resp, ": ");
48 t_insert(resp, v);
49 t_insert(resp, "\r\n");
50 end 47 end
51 end 48 end
52 if not (h and h["Content-Length"]) then 49 if not (h and h["Content-Length"]) then
53 t_insert(resp, "Content-Length: "); 50 t_insert(resp, "Content-Length: "..#body.."\r\n");
54 t_insert(resp, #body);
55 t_insert(resp, "\r\n");
56 end 51 end
57 t_insert(resp, "\r\n"); 52 t_insert(resp, "\r\n");
58 53
59 if request.method ~= "HEAD" then 54 if request.method ~= "HEAD" then
60 t_insert(resp, body); 55 t_insert(resp, body);
61 end 56 end
57 request.write(t_concat(resp));
62 else 58 else
63 -- Response we have is just a string (the body) 59 -- Response we have is just a string (the body)
64 log("debug", "Sending 200 response to %s", request.id or "<none>"); 60 log("debug", "Sending 200 response to %s", request.id or "<none>");
65 61
66 resp = { "HTTP/1.0 200 OK\r\n" }; 62 local resp = "HTTP/1.0 200 OK\r\n"
67 t_insert(resp, "Connection: close\r\n"); 63 .. "Connection: close\r\n"
68 t_insert(resp, "Content-Type: text/html\r\n"); 64 .. "Content-Type: text/html\r\n"
69 t_insert(resp, "Content-Length: "); 65 .. "Content-Length: "..#response.."\r\n"
70 t_insert(resp, #response); 66 .. "\r\n"
71 t_insert(resp, "\r\n\r\n"); 67 .. response;
72 68
73 t_insert(resp, response); 69 request.write(resp);
74 end 70 end
75 request.write(t_concat(resp));
76 if not request.stayopen then 71 if not request.stayopen then
77 request:destroy(); 72 request:destroy();
78 end 73 end
79 end 74 end
80 75

mercurial