* luci/httpd: fix 500 error case in luci handler, added copyright stuff
[project/luci.git] / libs / httpd / luasrc / httpd / handler / luci.lua
index e4916bd..49a9abc 100644 (file)
@@ -1,4 +1,20 @@
+--[[
+
+HTTP server implementation for LuCI - luci handler
+(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.
+You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+
+$Id$
+
+]]--
+
 module("luci.httpd.handler.luci", package.seeall)
+
 require("luci.dispatcher")
 require("luci.http")
 require("ltn12")
@@ -10,7 +26,16 @@ function Luci.__init__(self)
        luci.httpd.module.Handler.__init__(self)
 end
 
-function Luci.handle(self, request, sourcein, sinkerr) 
+function Luci.handle_head(self, ...)
+       local response, sourceout = self:handle_get(...)
+       return response
+end
+
+function Luci.handle_post(self, ...)
+       return self:handle_get(...)
+end
+
+function Luci.handle_get(self, request, sourcein, sinkerr)     
        local r = luci.http.Request(
                request.env,
                sourcein,
@@ -22,7 +47,7 @@ function Luci.handle(self, request, sourcein, sinkerr)
        local status = 200
        
        local x = coroutine.create(luci.dispatcher.httpdispatch)
-       while id < 3 do
+       while not id or id < 3 do
                coroutine.yield()
                
                res, id, data1, data2 = coroutine.resume(x, r)
@@ -31,7 +56,7 @@ function Luci.handle(self, request, sourcein, sinkerr)
                        status = 500
                        headers["Content-Type"] = "text/plain"
                        local err = {id}
-                       return status, headers, function() local x = table.remove(err) return x end
+                       return Response( status, headers ), function() return table.remove(err) end
                end
                
                if id == 1 then
@@ -45,6 +70,8 @@ function Luci.handle(self, request, sourcein, sinkerr)
                local res, id, data = coroutine.resume(x)
                if not res then
                        return nil, id
+               elseif not id then
+                       return true
                elseif id == 5 then
                        return nil
                else
@@ -53,4 +80,4 @@ function Luci.handle(self, request, sourcein, sinkerr)
        end
        
        return Response(status, headers), iter
-end
\ No newline at end of file
+end