db8780f7ecd6e52ef8c46def66515d5f3368f1f9
[project/luci.git] / libs / sgi-uhttpd / luasrc / sgi / uhttpd.lua
1 --[[
2 LuCI - Server Gateway Interface for the uHTTPd server
3
4 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at 
9
10         http://www.apache.org/licenses/LICENSE-2.0 
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 ]]--
19
20 require "nixio.util"
21 require "luci.http"
22 require "luci.sys"
23 require "luci.dispatcher"
24 require "luci.ltn12"
25
26 function handle_request(env)
27         exectime = os.clock()
28         local renv = {
29                 CONTENT_LENGTH  = env.CONTENT_LENGTH,
30                 CONTENT_TYPE    = env.CONTENT_TYPE,
31                 REQUEST_METHOD  = env.REQUEST_METHOD,
32                 REQUEST_URI     = env.REQUEST_URI,
33                 PATH_INFO       = env.PATH_INFO,
34                 SCRIPT_NAME     = env.SCRIPT_NAME:gsub("/+$", ""),
35                 SCRIPT_FILENAME = env.SCRIPT_NAME,
36                 SERVER_PROTOCOL = env.SERVER_PROTOCOL,
37                 QUERY_STRING    = env.QUERY_STRING
38         }
39
40         local k, v
41         for k, v in pairs(env.headers) do
42                 k = k:upper():gsub("%-", "_")
43                 renv["HTTP_" .. k] = v
44         end
45
46         local len = tonumber(env.CONTENT_LENGTH) or 0
47         local function recv()
48                 if len > 0 then
49                         local rlen, rbuf = uhttpd.recv(4096)
50                         if rlen >= 0 then
51                                 len = len - rlen
52                                 return rbuf
53                         end
54                 end
55                 return nil
56         end
57
58         local send = uhttpd.send
59
60         local req = luci.http.Request(
61                 renv, recv, luci.ltn12.sink.file(io.stderr)
62         )
63         
64
65         local x = coroutine.create(luci.dispatcher.httpdispatch)
66         local hcache = { }
67         local active = true
68
69         while coroutine.status(x) ~= "dead" do
70                 local res, id, data1, data2 = coroutine.resume(x, req)
71
72                 if not res then
73                         send("Status: 500 Internal Server Error\r\n")
74                         send("Content-Type: text/plain\r\n\r\n")
75                         send(tostring(id))
76                         break
77                 end
78
79                 if active then
80                         if id == 1 then
81                                 send("Status: ")
82                                 send(tostring(data1))
83                                 send(" ")
84                                 send(tostring(data2))
85                                 send("\r\n")
86                         elseif id == 2 then
87                                 hcache[data1] = data2
88                         elseif id == 3 then
89                                 for k, v in pairs(hcache) do
90                                         send(tostring(k))
91                                         send(": ")
92                                         send(tostring(v))
93                                         send("\r\n")
94                                 end
95                                 send("\r\n")
96                         elseif id == 4 then
97                                 send(tostring(data1 or ""))
98                         elseif id == 5 then
99                                 active = false
100                         elseif id == 6 then
101                                 data1:copyz(nixio.stdout, data2)
102                         end
103                 end
104         end
105 end