4cd3649c62d82ed3f323b01ca49b625a8cfb0ee1
[oweals/luci.git] / modules / luci-base / luasrc / sgi / uhttpd.lua
1 -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.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                 DOCUMENT_ROOT   = env.DOCUMENT_ROOT,
23                 HTTPS           = env.HTTPS,
24                 REDIRECT_STATUS = env.REDIRECT_STATUS,
25                 REMOTE_ADDR     = env.REMOTE_ADDR,
26                 REMOTE_NAME     = env.REMOTE_NAME,
27                 REMOTE_PORT     = env.REMOTE_PORT,
28                 REMOTE_USER     = env.REMOTE_USER,
29                 SERVER_ADDR     = env.SERVER_ADDR,
30                 SERVER_NAME     = env.SERVER_NAME,
31                 SERVER_PORT     = env.SERVER_PORT
32         }
33
34         local k, v
35         for k, v in pairs(env.headers) do
36                 k = k:upper():gsub("%-", "_")
37                 renv["HTTP_" .. k] = v
38         end
39
40         local len = tonumber(env.CONTENT_LENGTH) or 0
41         local function recv()
42                 if len > 0 then
43                         local rlen, rbuf = uhttpd.recv(4096)
44                         if rlen >= 0 then
45                                 len = len - rlen
46                                 return rbuf
47                         end
48                 end
49                 return nil
50         end
51
52         local send = uhttpd.send
53
54         local req = luci.http.Request(
55                 renv, recv, luci.ltn12.sink.file(io.stderr)
56         )
57
58
59         local x = coroutine.create(luci.dispatcher.httpdispatch)
60         local hcache = { }
61         local active = true
62
63         while coroutine.status(x) ~= "dead" do
64                 local res, id, data1, data2 = coroutine.resume(x, req)
65
66                 if not res then
67                         send("Status: 500 Internal Server Error\r\n")
68                         send("Content-Type: text/plain\r\n\r\n")
69                         send(tostring(id))
70                         break
71                 end
72
73                 if active then
74                         if id == 1 then
75                                 send("Status: ")
76                                 send(tostring(data1))
77                                 send(" ")
78                                 send(tostring(data2))
79                                 send("\r\n")
80                         elseif id == 2 then
81                                 hcache[data1] = data2
82                         elseif id == 3 then
83                                 for k, v in pairs(hcache) do
84                                         send(tostring(k))
85                                         send(": ")
86                                         send(tostring(v))
87                                         send("\r\n")
88                                 end
89                                 send("\r\n")
90                         elseif id == 4 then
91                                 send(tostring(data1 or ""))
92                         elseif id == 5 then
93                                 active = false
94                         elseif id == 6 then
95                                 data1:copyz(nixio.stdout, data2)
96                         end
97                 end
98         end
99 end