HTTP server implementation for LuCI - helper class
(c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
+(c) 2008 Steven Barth <steven@midlink.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
require("luci.util")
READ_BUFSIZE = 1024
+VERSION = 0.2
VHost = luci.util.class()
function Server.error(self, socket, code, msg)
hcode = tostring(code)
- socket:send( "HTTP/1.1 " .. hcode .. " " ..
+ socket:send( "HTTP/1.0 " .. hcode .. " " ..
luci.http.protocol.statusmsg[code] .. "\r\n" )
socket:send( "Connection: close\r\n" )
socket:send( "Content-Type: text/plain\r\n\r\n" )
end
-function Server.process( self, thread )
+function Server.process( self, client )
-- Setup sockets and sources
- local client = thread.socket
+ local thread = {
+ receive = function(self, ...) return luci.httpd.corecv(client, ...) end
+ }
+
client:settimeout( 0 )
+
local sourcein = ltn12.source.empty()
local sourcehdr = luci.http.protocol.header_source( thread )
local sinkerr = ltn12.sink.file( io.stderr )
local reading = { client }
local message, err
-
- socket.sleep(5)
repeat
-- parse headers
break
end
- coroutine.yield()
-
-- keep-alive
if message.http_version == 1.1 then
close = (message.env.HTTP_CONNECTION == "close")
break;
end
- coroutine.yield()
-
local response, sourceout = host:process(
message, sourcein, sinkerr,
client, io.stderr
self:error( client, 500, "Error processing handler" )
end
- coroutine.yield()
-
-- Post process response
local sinkmode = close and "close-when-done" or "keep-open"
tostring(response.status) .. " " ..
luci.http.protocol.statusmsg[response.status] .. "\r\n"
+ header = header .. "Server: LuCI HTTPd/" .. tostring(VERSION) .. "\r\n"
+
for k,v in pairs(response.headers) do
header = header .. k .. ": " .. v .. "\r\n"