Fix sgi-wsapi and sgi-luci to be standards compliant
[project/luci.git] / libs / httpd / luasrc / httpd / handler / luci.lua
index 49a9abc0de9a12cce9dec48209e13925297d5b20..18715111f8c42c74d175615dcb090dc624ef1564 100644 (file)
@@ -1,7 +1,7 @@
 --[[
 
 HTTP server implementation for LuCI - luci handler
-(c) 2008 Steven Barth <steven@midlink.org>     
+(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.
@@ -17,17 +17,22 @@ module("luci.httpd.handler.luci", package.seeall)
 
 require("luci.dispatcher")
 require("luci.http")
-require("ltn12")
+require("luci.http.protocol.date")
+local ltn12 = require("luci.ltn12")
 
 Luci = luci.util.class(luci.httpd.module.Handler)
 Response = luci.httpd.module.Response
 
-function Luci.__init__(self)
+function Luci.__init__(self, limit)
        luci.httpd.module.Handler.__init__(self)
+       self.limit = limit or 5
+       self.running = {}
+       setmetatable(self.running, {__mode = "v"})
 end
 
 function Luci.handle_head(self, ...)
        local response, sourceout = self:handle_get(...)
+       self.running = self.running - 1
        return response
 end
 
@@ -35,37 +40,43 @@ function Luci.handle_post(self, ...)
        return self:handle_get(...)
 end
 
-function Luci.handle_get(self, request, sourcein, sinkerr)     
+function Luci.handle_get(self, request, sourcein, sinkerr)
+       if self.limit and #self.running >= self.limit then
+               return self:failure(503, "Overload")
+       end
+       table.insert(self.running, coroutine.running())
+       
        local r = luci.http.Request(
                request.env,
                sourcein,
                sinkerr
        )
-               
+
        local res, id, data1, data2 = true, 0, nil, nil
        local headers = {}
        local status = 200
-       
+
        local x = coroutine.create(luci.dispatcher.httpdispatch)
        while not id or id < 3 do
                coroutine.yield()
-               
+
                res, id, data1, data2 = coroutine.resume(x, r)
-               
+
                if not res then
                        status = 500
                        headers["Content-Type"] = "text/plain"
                        local err = {id}
+                       self.running = self.running - 1
                        return Response( status, headers ), function() return table.remove(err) end
                end
-               
+
                if id == 1 then
                        status = data1
                elseif id == 2 then
                        headers[data1] = data2
                end
        end
-       
+
        local function iter()
                local res, id, data = coroutine.resume(x)
                if not res then
@@ -73,11 +84,18 @@ function Luci.handle_get(self, request, sourcein, sinkerr)
                elseif not id then
                        return true
                elseif id == 5 then
-                       return nil
-               else
+                       return true
+               elseif id == 4 then
                        return data
                end
+               if coroutine.status(x) == "dead" then
+                       return nil
+               end
        end
-       
+
+       headers["Expires"] = luci.http.protocol.date.to_http( os.time() )
+       headers["Date"]    = headers["Expires"]
+       headers["Cache-Control"] = "no-cache"
+
        return Response(status, headers), iter
 end