d816aae13bb6a31c0eb22476451b55a5e6df7f42
[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 end
37
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(...)
43         return stat, head
44 end
45
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(...)
51 end
52
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(
58                 request.env,
59                 sourcein
60         )
61
62         local res, id, data1, data2 = true, 0, nil, nil
63         local headers = {}
64         local status = 200
65         local active = true
66
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)
70
71                 if not res then
72                         status = 500
73                         headers["Content-Type"] = "text/plain"
74                         return status, headers, ltn12.source.string(id)
75                 end
76
77                 if id == 1 then
78                         status = data1
79                 elseif id == 2 then
80                         if not headers[data1] then
81                                 headers[data1] = data2
82                         elseif type(headers[data1]) ~= "table" then
83                                 headers[data1] = {headers[data1], data2}
84                         else
85                                 headers[data1][#headers[data1]+1] = data2
86                         end
87                 end
88         end
89         
90         if id == 6 then
91                 while (coroutine.resume(x)) do end
92                 return status, headers, srv.IOResource(data1, data2)
93         end
94
95         local function iter()
96                 local res, id, data = coroutine.resume(x)
97                 if not res then
98                         return nil, id
99                 elseif not id or not active then
100                         return true
101                 elseif id == 5 then
102                         active = false
103                         while (coroutine.resume(x)) do end
104                         return nil
105                 elseif id == 4 then
106                         return data
107                 end
108         end
109
110         return status, headers, iter
111 end
112