GSoC Commit #1: LuCId + HTTP-Server
[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 module "luci.lucid.http.handler.luci"
23
24 Luci = util.class(srv.Handler)
25
26 function Luci.__init__(self, name, prefix)
27         srv.Handler.__init__(self, name)
28         self.prefix = prefix
29 end
30
31 function Luci.handle_HEAD(self, ...)
32         local stat, head = self:handle_GET(...)
33         return stat, head
34 end
35
36 function Luci.handle_POST(self, ...)
37         return self:handle_GET(...)
38 end
39
40 function Luci.handle_GET(self, request, sourcein)
41         local r = http.Request(
42                 request.env,
43                 sourcein
44         )
45
46         local res, id, data1, data2 = true, 0, nil, nil
47         local headers = {}
48         local status = 200
49         local active = true
50
51         local x = coroutine.create(dsp.httpdispatch)
52         while not id or id < 3 do
53                 res, id, data1, data2 = coroutine.resume(x, r, self.prefix)
54
55                 if not res then
56                         status = 500
57                         headers["Content-Type"] = "text/plain"
58                         return status, headers, ltn12.source.string(id)
59                 end
60
61                 if id == 1 then
62                         status = data1
63                 elseif id == 2 then
64                         if not headers[data1] then
65                                 headers[data1] = data2
66                         elseif type(headers[data1]) ~= "table" then
67                                 headers[data1] = {headers[data1], data2}
68                         else
69                                 headers[data1][#headers[data1]+1] = data2
70                         end
71                 end
72         end
73         
74         if id == 6 then
75                 while (coroutine.resume(x)) do end
76                 return status, headers, srv.IOResource(data1, data2)
77         end
78
79         local function iter()
80                 local res, id, data = coroutine.resume(x)
81                 if not res then
82                         return nil, id
83                 elseif not id or not active then
84                         return true
85                 elseif id == 5 then
86                         active = false
87                         while (coroutine.resume(x)) do end
88                         return nil
89                 elseif id == 4 then
90                         return data
91                 end
92         end
93
94         return status, headers, iter
95 end
96