* luci/httpd: fix 500 error case in luci handler, added copyright stuff
[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("ltn12")
21
22 Luci = luci.util.class(luci.httpd.module.Handler)
23 Response = luci.httpd.module.Response
24
25 function Luci.__init__(self)
26 luci.httpd.module.Handler.__init__(self)
27 end
28
29 function Luci.handle_head(self, ...)
30 local response, sourceout = self:handle_get(...)
31 return response
32 end
33
34 function Luci.handle_post(self, ...)
35 return self:handle_get(...)
36 end
37
38 function Luci.handle_get(self, request, sourcein, sinkerr)
39 local r = luci.http.Request(
40 request.env,
41 sourcein,
42 sinkerr
43 )
44
45 local res, id, data1, data2 = true, 0, nil, nil
46 local headers = {}
47 local status = 200
48
49 local x = coroutine.create(luci.dispatcher.httpdispatch)
50 while not id or id < 3 do
51 coroutine.yield()
52
53 res, id, data1, data2 = coroutine.resume(x, r)
54
55 if not res then
56 status = 500
57 headers["Content-Type"] = "text/plain"
58 local err = {id}
59 return Response( status, headers ), function() return table.remove(err) end
60 end
61
62 if id == 1 then
63 status = data1
64 elseif id == 2 then
65 headers[data1] = data2
66 end
67 end
68
69 local function iter()
70 local res, id, data = coroutine.resume(x)
71 if not res then
72 return nil, id
73 elseif not id then
74 return true
75 elseif id == 5 then
76 return nil
77 else
78 return data
79 end
80 end
81
82 return Response(status, headers), iter
83 end