2 LuCI - Server Gateway Interface for the uHTTPd server
4 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
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
10 http://www.apache.org/licenses/LICENSE-2.0
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.
23 require "luci.dispatcher"
26 function handle_request(env)
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
41 for k, v in pairs(env.headers) do
42 k = k:upper():gsub("%-", "_")
43 renv["HTTP_" .. k] = v
46 local len = env.CONTENT_LENGTH or 0
49 local rlen, rbuf = uhttpd.recv(4096)
58 local function send(...)
59 return uhttpd.send(...)
62 local function sendc(...)
63 if env.HTTP_VERSION > 1.0 then
64 return uhttpd.sendc(...)
66 return uhttpd.send(...)
70 local req = luci.http.Request(
71 renv, recv, luci.ltn12.sink.file(io.stderr)
75 local x = coroutine.create(luci.dispatcher.httpdispatch)
79 if env.HTTP_VERSION > 1.0 then
80 hcache["Transfer-Encoding"] = "chunked"
83 while coroutine.status(x) ~= "dead" do
84 local res, id, data1, data2 = coroutine.resume(x, req)
87 send(env.SERVER_PROTOCOL)
88 send(" 500 Internal Server Error\r\n")
89 send("Content-Type: text/plain\r\n\r\n")
96 send(env.SERVER_PROTOCOL)
100 send(tostring(data2))
103 hcache[data1] = data2
105 for k, v in pairs(hcache) do
113 sendc(tostring(data1 or ""))
117 data1:copyz(nixio.stdout, data2)