* luci/libs/httpd: fix spurious Overload errors in luci-httpd
[project/luci.git] / libs / httpd / luasrc / httpd / handler / luci.lua
1 --[[
2
3 HTTP server implementation for LuCI - luci handler
4 (c) 2008 Steven Barth <steven@midlink.org>
5
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
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 module("luci.httpd.handler.luci", package.seeall)
17
18 require("luci.dispatcher")
19 require("luci.http")
20 require("luci.http.protocol.date")
21 local ltn12 = require("luci.ltn12")
22
23 Luci = luci.util.class(luci.httpd.module.Handler)
24 Response = luci.httpd.module.Response
25
26 function Luci.__init__(self, limit)
27         luci.httpd.module.Handler.__init__(self)
28         self.limit = limit or 5
29         self.running = {}
30         setmetatable(self.running, {__mode = "k"})
31 end
32
33 function Luci.handle_head(self, ...)
34         local response, sourceout = self:handle_get(...)
35         return response
36 end
37
38 function Luci.handle_post(self, ...)
39         return self:handle_get(...)
40 end
41
42 function Luci.handle_get(self, request, sourcein, sinkerr)
43         local reaped  = false
44         local running = 0
45
46         for _, v in pairs(self.running) do
47                 if v then running = running + 1 end
48         end
49
50         if self.limit and running >= self.limit then
51                 for k, v in ipairs(self.running) do
52                         if coroutine.status(k) == "dead" then
53                                 self.running[k] = nil
54                                 running = running - 1
55                                 reaped  = true
56                         end
57                 end
58
59                 if reaped then collectgarbage() end
60
61                 if running >= self.limit then
62                         return self:failure(503, "Overload %i/%i" % { running, self.limit } )
63                 end
64         end
65
66         self.running[coroutine.running()] = true
67
68         local r = luci.http.Request(
69                 request.env,
70                 sourcein,
71                 sinkerr
72         )
73
74         local res, id, data1, data2 = true, 0, nil, nil
75         local headers = {}
76         local status = 200
77         local active = true
78
79         local x = coroutine.create(luci.dispatcher.httpdispatch)
80         while not id or id < 3 do
81                 coroutine.yield()
82
83                 res, id, data1, data2 = coroutine.resume(x, r)
84
85                 if not res then
86                         status = 500
87                         headers["Content-Type"] = "text/plain"
88                         local err = {id}
89                         return Response( status, headers ), function() return table.remove(err) end
90                 end
91
92                 if id == 1 then
93                         status = data1
94                 elseif id == 2 then
95                         headers[data1] = data2
96                 end
97         end
98
99         local function iter()
100                 local res, id, data = coroutine.resume(x)
101                 if not res then
102                         return nil, id
103                 elseif not id or not active then
104                         return true
105                 elseif id == 5 then
106                         active = false
107
108                         while (coroutine.resume(x)) do
109                         end
110
111                         return nil
112                 elseif id == 4 then
113                         return data
114                 end
115                 if coroutine.status(x) == "dead" then
116                         return nil
117                 end
118         end
119
120         return Response(status, headers), iter
121 end