build: split into luci and luci-addons packages
[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 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"
20 local type = type
21
22 --- LuCI web handler
23 -- @cstyle instance
24 module "luci.lucid.http.handler.luci"
25
26 --- Create a LuCI web handler.
27 -- @class function
28 -- @param name Name
29 -- @param prefix Dispatching prefix
30 -- @return LuCI web handler object
31 Luci = util.class(srv.Handler)
32
33 function Luci.__init__(self, name, prefix)
34         srv.Handler.__init__(self, name)
35         self.prefix = prefix
36         dsp.indexcache = "/tmp/luci-indexcache"
37 end
38
39 --- Handle a HEAD request.
40 -- @param request Request object
41 -- @return status code, header table, response source
42 function Luci.handle_HEAD(self, ...)
43         local stat, head = self:handle_GET(...)
44         return stat, head
45 end
46
47 --- Handle a POST request.
48 -- @param request Request object
49 -- @return status code, header table, response source
50 function Luci.handle_POST(self, ...)
51         return self:handle_GET(...)
52 end
53
54 --- Handle a GET request.
55 -- @param request Request object
56 -- @return status code, header table, response source
57 function Luci.handle_GET(self, request, sourcein)
58         local r = http.Request(
59                 request.env,
60                 sourcein
61         )
62
63         local res, id, data1, data2 = true, 0, nil, nil
64         local headers = {}
65         local status = 200
66         local active = true
67
68         local x = coroutine.create(dsp.httpdispatch)
69         while not id or id < 3 do
70                 res, id, data1, data2 = coroutine.resume(x, r, self.prefix)
71
72                 if not res then
73                         status = 500
74                         headers["Content-Type"] = "text/plain"
75                         return status, headers, ltn12.source.string(id)
76                 end
77
78                 if id == 1 then
79                         status = data1
80                 elseif id == 2 then
81                         if not headers[data1] then
82                                 headers[data1] = data2
83                         elseif type(headers[data1]) ~= "table" then
84                                 headers[data1] = {headers[data1], data2}
85                         else
86                                 headers[data1][#headers[data1]+1] = data2
87                         end
88                 end
89         end
90         
91         if id == 6 then
92                 while (coroutine.resume(x)) do end
93                 return status, headers, srv.IOResource(data1, data2)
94         end
95
96         local function iter()
97                 local res, id, data = coroutine.resume(x)
98                 if not res then
99                         return nil, id
100                 elseif not id or not active then
101                         return true
102                 elseif id == 5 then
103                         active = false
104                         while (coroutine.resume(x)) do end
105                         return nil
106                 elseif id == 4 then
107                         return data
108                 end
109         end
110
111         return status, headers, iter
112 end
113