3 HTTP server implementation for LuCI - luci handler
4 (c) 2008 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
16 module("luci.httpd.handler.luci", package.seeall)
18 require("luci.dispatcher")
20 require("luci.http.protocol.date")
21 local ltn12 = require("luci.ltn12")
23 Luci = luci.util.class(luci.httpd.module.Handler)
24 Response = luci.httpd.module.Response
26 function Luci.__init__(self, limit)
27 luci.httpd.module.Handler.__init__(self)
28 self.limit = limit or 5
30 setmetatable(self.running, {__mode = "v"})
33 function Luci.handle_head(self, ...)
34 local response, sourceout = self:handle_get(...)
38 function Luci.handle_post(self, ...)
39 return self:handle_get(...)
42 function Luci.handle_get(self, request, sourcein, sinkerr)
43 if self.limit and #self.running >= self.limit then
44 for k, v in ipairs(self.running) do
45 if coroutine.status(v) == "dead" then
50 if #self.running >= self.limit then
51 return self:failure(503, "Overload")
54 table.insert(self.running, coroutine.running())
56 local r = luci.http.Request(
62 local res, id, data1, data2 = true, 0, nil, nil
67 local x = coroutine.create(luci.dispatcher.httpdispatch)
68 while not id or id < 3 do
71 res, id, data1, data2 = coroutine.resume(x, r)
75 headers["Content-Type"] = "text/plain"
77 return Response( status, headers ), function() return table.remove(err) end
83 headers[data1] = data2
88 local res, id, data = coroutine.resume(x)
91 elseif not id or not active then
96 while (coroutine.resume(x)) do
103 if coroutine.status(x) == "dead" then
108 return Response(status, headers), iter