Globally reduce copyright headers
[project/luci.git] / modules / luci-base / luasrc / sgi / uhttpd.lua
1 -- Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 require "nixio.util"
5 require "luci.http"
6 require "luci.sys"
7 require "luci.dispatcher"
8 require "luci.ltn12"
9
10 function handle_request(env)
11         exectime = os.clock()
12         local renv = {
13                 CONTENT_LENGTH  = env.CONTENT_LENGTH,
14                 CONTENT_TYPE    = env.CONTENT_TYPE,
15                 REQUEST_METHOD  = env.REQUEST_METHOD,
16                 REQUEST_URI     = env.REQUEST_URI,
17                 PATH_INFO       = env.PATH_INFO,
18                 SCRIPT_NAME     = env.SCRIPT_NAME:gsub("/+$", ""),
19                 SCRIPT_FILENAME = env.SCRIPT_NAME,
20                 SERVER_PROTOCOL = env.SERVER_PROTOCOL,
21                 QUERY_STRING    = env.QUERY_STRING
22         }
23
24         local k, v
25         for k, v in pairs(env.headers) do
26                 k = k:upper():gsub("%-", "_")
27                 renv["HTTP_" .. k] = v
28         end
29
30         local len = tonumber(env.CONTENT_LENGTH) or 0
31         local function recv()
32                 if len > 0 then
33                         local rlen, rbuf = uhttpd.recv(4096)
34                         if rlen >= 0 then
35                                 len = len - rlen
36                                 return rbuf
37                         end
38                 end
39                 return nil
40         end
41
42         local send = uhttpd.send
43
44         local req = luci.http.Request(
45                 renv, recv, luci.ltn12.sink.file(io.stderr)
46         )
47         
48
49         local x = coroutine.create(luci.dispatcher.httpdispatch)
50         local hcache = { }
51         local active = true
52
53         while coroutine.status(x) ~= "dead" do
54                 local res, id, data1, data2 = coroutine.resume(x, req)
55
56                 if not res then
57                         send("Status: 500 Internal Server Error\r\n")
58                         send("Content-Type: text/plain\r\n\r\n")
59                         send(tostring(id))
60                         break
61                 end
62
63                 if active then
64                         if id == 1 then
65                                 send("Status: ")
66                                 send(tostring(data1))
67                                 send(" ")
68                                 send(tostring(data2))
69                                 send("\r\n")
70                         elseif id == 2 then
71                                 hcache[data1] = data2
72                         elseif id == 3 then
73                                 for k, v in pairs(hcache) do
74                                         send(tostring(k))
75                                         send(": ")
76                                         send(tostring(v))
77                                         send("\r\n")
78                                 end
79                                 send("\r\n")
80                         elseif id == 4 then
81                                 send(tostring(data1 or ""))
82                         elseif id == 5 then
83                                 active = false
84                         elseif id == 6 then
85                                 data1:copyz(nixio.stdout, data2)
86                         end
87                 end
88         end
89 end