3 (c) 2009 Steven Barth <steven@midlink.org>
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
9 http://www.apache.org/licenses/LICENSE-2.0
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"
25 module "luci.lucid.http.handler.luci"
27 --- Create a LuCI web handler.
30 -- @param prefix Dispatching prefix
31 -- @return LuCI web handler object
32 Luci = util.class(srv.Handler)
34 function Luci.__init__(self, name, prefix)
35 srv.Handler.__init__(self, name)
38 self.dsp_tree = dsp.createtree()
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(...)
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(...)
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(
65 local res, id, data1, data2 = true, 0, nil, nil
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)
76 headers["Content-Type"] = "text/plain"
77 return status, headers, ltn12.source.string(id)
83 if not headers[data1] then
84 headers[data1] = data2
85 elseif type(headers[data1]) ~= "table" then
86 headers[data1] = {headers[data1], data2}
88 headers[data1][#headers[data1]+1] = data2
94 while (coroutine.resume(x)) do end
95 return status, headers, srv.IOResource(data1, data2)
99 local res, id, data = coroutine.resume(x)
102 elseif not id or not active then
106 while (coroutine.resume(x)) do end
113 return status, headers, iter