* libs/httpd: Added Cache-Control header to LuCI
[project/luci.git] / libs / httpd / luasrc / httpd / server.lua
index 181ca24..7bfac68 100644 (file)
@@ -2,6 +2,7 @@
 
 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.
@@ -19,6 +20,7 @@ require("socket.http")
 require("luci.util")
 
 READ_BUFSIZE = 1024
+VERSION = 0.2
 
 
 VHost = luci.util.class()
@@ -90,7 +92,7 @@ end
 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" )
@@ -105,11 +107,15 @@ function Server.error_overload(self, socket)
 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 )
@@ -119,8 +125,6 @@ function Server.process( self, thread )
        local reading = { client }
 
        local message, err
-
-       socket.sleep(5)
        
        repeat
                -- parse headers
@@ -131,8 +135,6 @@ function Server.process( self, thread )
                        break
                end     
                
-               coroutine.yield()
-               
                -- keep-alive
                if message.http_version == 1.1 then
                        close = (message.env.HTTP_CONNECTION == "close")
@@ -176,8 +178,6 @@ function Server.process( self, thread )
                        break;
                end
                
-               coroutine.yield()
-               
                local response, sourceout = host:process(
                        message, sourcein, sinkerr,
                        client, io.stderr 
@@ -186,8 +186,6 @@ function Server.process( self, thread )
                        self:error( client, 500, "Error processing handler" )
                end
                
-               coroutine.yield()
-               
                -- Post process response
                local sinkmode = close and "close-when-done" or "keep-open"
                
@@ -215,6 +213,8 @@ function Server.process( self, thread )
                        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"