232883256e4e295d9931ff5efb4254266f6e9744
[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 = "v"})
31 end
32
33 function Luci.handle_head(self, ...)
34         local response, sourceout = self:handle_get(...)
35         self.running = self.running - 1
36         return response
37 end
38
39 function Luci.handle_post(self, ...)
40         return self:handle_get(...)
41 end
42
43 function Luci.handle_get(self, request, sourcein, sinkerr)
44         if self.limit and #self.running >= self.limit then
45                 return self:failure(503, "Overload")
46         end
47         table.insert(self.running, coroutine.running())
48         
49         local r = luci.http.Request(
50                 request.env,
51                 sourcein,
52                 sinkerr
53         )
54
55         local res, id, data1, data2 = true, 0, nil, nil
56         local headers = {}
57         local status = 200
58         local active = true
59
60         local x = coroutine.create(luci.dispatcher.httpdispatch)
61         while not id or id < 3 do
62                 coroutine.yield()
63
64                 res, id, data1, data2 = coroutine.resume(x, r)
65
66                 if not res then
67                         status = 500
68                         headers["Content-Type"] = "text/plain"
69                         local err = {id}
70                         self.running = self.running - 1
71                         return Response( status, headers ), function() return table.remove(err) end
72                 end
73
74                 if id == 1 then
75                         status = data1
76                 elseif id == 2 then
77                         headers[data1] = data2
78                 end
79         end
80
81         local function iter()
82                 local res, id, data = coroutine.resume(x)
83                 if not res then
84                         return nil, id
85                 elseif not id or not active then
86                         return true
87                 elseif id == 5 then
88                         active = false
89                         return true
90                 elseif id == 4 then
91                         return data
92                 end
93                 if coroutine.status(x) == "dead" then
94                         return nil
95                 end
96         end
97
98         headers["Expires"] = luci.http.protocol.date.to_http( os.time() )
99         headers["Date"]    = headers["Expires"]
100         headers["Cache-Control"] = "no-cache"
101
102         return Response(status, headers), iter
103 end