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 dsp = require "luci.dispatcher"
15 local util = require "luci.util"
16 local http = require "luci.http"
17 local ltn12 = require "luci.ltn12"
18 local srv = require "luci.lucid.http.server"
19 local coroutine = require "coroutine"
24 module "luci.lucid.http.handler.luci"
26 --- Create a LuCI web handler.
29 -- @param prefix Dispatching prefix
30 -- @return LuCI web handler object
31 Luci = util.class(srv.Handler)
33 function Luci.__init__(self, name, prefix)
34 srv.Handler.__init__(self, name)
38 --- Handle a HEAD request.
39 -- @param request Request object
40 -- @return status code, header table, response source
41 function Luci.handle_HEAD(self, ...)
42 local stat, head = self:handle_GET(...)
46 --- Handle a POST request.
47 -- @param request Request object
48 -- @return status code, header table, response source
49 function Luci.handle_POST(self, ...)
50 return self:handle_GET(...)
53 --- Handle a GET request.
54 -- @param request Request object
55 -- @return status code, header table, response source
56 function Luci.handle_GET(self, request, sourcein)
57 local r = http.Request(
62 local res, id, data1, data2 = true, 0, nil, nil
67 local x = coroutine.create(dsp.httpdispatch)
68 while not id or id < 3 do
69 res, id, data1, data2 = coroutine.resume(x, r, self.prefix)
73 headers["Content-Type"] = "text/plain"
74 return status, headers, ltn12.source.string(id)
80 if not headers[data1] then
81 headers[data1] = data2
82 elseif type(headers[data1]) ~= "table" then
83 headers[data1] = {headers[data1], data2}
85 headers[data1][#headers[data1]+1] = data2
91 while (coroutine.resume(x)) do end
92 return status, headers, srv.IOResource(data1, data2)
96 local res, id, data = coroutine.resume(x)
99 elseif not id or not active then
103 while (coroutine.resume(x)) do end
110 return status, headers, iter