8f203e163f8725db5ef632ec08cfb24d0378f3a3
[project/luci.git] / libs / lucid-http / luasrc / lucid / http / handler / luci.lua
1 --[[
2 LuCId HTTP-Slave
3 (c) 2009 Steven Barth <steven@midlink.org>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12 ]]--
13
14 local cbi = require "luci.cbi"
15 local dsp = require "luci.dispatcher"
16 local util = require "luci.util"
17 local http = require "luci.http"
18 local ltn12 = require "luci.ltn12"
19 local srv = require "luci.lucid.http.server"
20 local coroutine = require "coroutine"
21 local type = type
22
23 --- LuCI web handler
24 -- @cstyle instance
25 module "luci.lucid.http.handler.luci"
26
27 --- Create a LuCI web handler.
28 -- @class function
29 -- @param name Name
30 -- @param prefix Dispatching prefix
31 -- @return LuCI web handler object
32 Luci = util.class(srv.Handler)
33
34 function Luci.__init__(self, name, prefix)
35 srv.Handler.__init__(self, name)
36 self.prefix = prefix
37
38 self.dsp_tree = dsp.createtree()
39 end
40
41 --- Handle a HEAD request.
42 -- @param request Request object
43 -- @return status code, header table, response source
44 function Luci.handle_HEAD(self, ...)
45 local stat, head = self:handle_GET(...)
46 return stat, head
47 end
48
49 --- Handle a POST request.
50 -- @param request Request object
51 -- @return status code, header table, response source
52 function Luci.handle_POST(self, ...)
53 return self:handle_GET(...)
54 end
55
56 --- Handle a GET request.
57 -- @param request Request object
58 -- @return status code, header table, response source
59 function Luci.handle_GET(self, request, sourcein)
60 local r = http.Request(
61 request.env,
62 sourcein
63 )
64
65 local res, id, data1, data2 = true, 0, nil, nil
66 local headers = {}
67 local status = 200
68 local active = true
69
70 local x = coroutine.create(dsp.httpdispatch)
71 while not id or id < 3 do
72 res, id, data1, data2 = coroutine.resume(x, r, self.prefix, self.dsp_tree)
73
74 if not res then
75 status = 500
76 headers["Content-Type"] = "text/plain"
77 return status, headers, ltn12.source.string(id)
78 end
79
80 if id == 1 then
81 status = data1
82 elseif id == 2 then
83 if not headers[data1] then
84 headers[data1] = data2
85 elseif type(headers[data1]) ~= "table" then
86 headers[data1] = {headers[data1], data2}
87 else
88 headers[data1][#headers[data1]+1] = data2
89 end
90 end
91 end
92
93 if id == 6 then
94 while (coroutine.resume(x)) do end
95 return status, headers, srv.IOResource(data1, data2)
96 end
97
98 local function iter()
99 local res, id, data = coroutine.resume(x)
100 if not res then
101 return nil, id
102 elseif not id or not active then
103 return true
104 elseif id == 5 then
105 active = false
106 while (coroutine.resume(x)) do end
107 return nil
108 elseif id == 4 then
109 return data
110 end
111 end
112
113 return status, headers, iter
114 end
115