3 HTTP server implementation for LuCI - helper class
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
5 (c) 2008 Steven Barth <steven@midlink.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
17 module("luci.httpd.server", package.seeall)
19 require("socket.http")
25 VHost = luci.util.class()
27 function VHost.__init__(self, handler)
28 self.handler = handler
32 function VHost.process(self, request, sourcein, sinkerr, ...)
33 local handler = self.handler
35 local uri = request.env.REQUEST_URI:match("^([^?]*)")
38 request.env.SCRIPT_NAME = ""
41 request.env.PATH_INFO = uri
43 for k, dhandler in pairs(self.dhandler) do
44 if k == uri or k.."/" == uri:sub(1, #k+1) then
46 request.env.SCRIPT_NAME = k
47 request.env.PATH_INFO = uri:sub(#k+1)
53 return handler:process(request, sourcein, sinkerr, ...)
58 function VHost.set_default_handler(self, handler)
59 self.handler = handler
63 function VHost.set_handler(self, match, handler)
64 self.dhandler[match] = handler
69 Server = luci.util.class()
71 function Server.__init__(self, host)
76 function Server.set_default_vhost(self, vhost)
81 function Server.set_vhost(self, name, vhost)
82 self.vhosts[name] = vhost
85 function Server.create_daemon_handlers(self)
86 return function(...) return self:process(...) end,
87 function(...) return self:error_overload(...) end
91 function Server.error(self, socket, code, msg)
92 hcode = tostring(code)
94 socket:send( "HTTP/1.0 " .. hcode .. " " ..
95 luci.http.protocol.statusmsg[code] .. "\r\n" )
96 socket:send( "Connection: close\r\n" )
97 socket:send( "Content-Type: text/plain\r\n\r\n" )
100 socket:send( "HTTP-Error " .. code .. ": " .. msg .. "\r\n" )
104 function Server.error_overload(self, socket)
105 self:error(socket, 503, "Too many simultaneous connections")
109 function Server.process( self, client )
111 -- Setup sockets and sources
113 receive = function(self, ...) return luci.httpd.corecv(client, ...) end
116 client:settimeout( 0 )
118 local sourcein = ltn12.source.empty()
119 local sourcehdr = luci.http.protocol.header_source( thread )
120 local sinkerr = ltn12.sink.file( io.stderr )
124 local reading = { client }
130 message, err = luci.http.protocol.parse_message_header( sourcehdr )
133 self:error( client, 400, err )
138 if message.http_version == 1.1 then
139 close = (message.env.HTTP_CONNECTION == "close")
141 close = not message.env.HTTP_CONNECTION or message.env.HTTP_CONNECTION == "close"
144 if message.request_method == "get" or message.request_method == "head" then
147 elseif message.request_method == "post" then
148 -- If we have a HTTP/1.1 client and an Expect: 100-continue header then
149 -- respond with HTTP 100 Continue message
150 if message.http_version == 1.1 and message.headers['Expect'] and
151 message.headers['Expect'] == '100-continue'
153 client:send("HTTP/1.1 100 Continue\r\n\r\n")
156 if message.headers['Transfer-Encoding'] and
157 message.headers['Transfer-Encoding'] ~= "identity" then
158 sourcein = socket.source("http-chunked", thread)
159 elseif message.env.CONTENT_LENGTH then
160 sourcein = socket.source("by-length", thread,
161 tonumber(message.env.CONTENT_LENGTH))
163 self:error( client, 411, luci.http.protocol.statusmsg[411] )
168 self:error( client, 405, luci.http.protocol.statusmsg[405] )
174 local host = self.vhosts[message.env.HTTP_HOST] or self.host
176 self:error( client, 500, "Unable to find matching host" )
180 local response, sourceout = host:process(
181 message, sourcein, sinkerr,
185 self:error( client, 500, "Error processing handler" )
188 -- Post process response
189 local sinkmode = close and "close-when-done" or "keep-open"
192 if not response.headers["Content-Length"] then
193 if message.http_version == 1.1 then
194 response.headers["Transfer-Encoding"] = "chunked"
195 sinkmode = "http-chunked"
198 sinkmode = "close-when-done"
204 response.headers["Connection"] = "close"
208 local sinkout = socket.sink(sinkmode, client)
211 message.env.SERVER_PROTOCOL .. " " ..
212 tostring(response.status) .. " " ..
213 luci.http.protocol.statusmsg[response.status] .. "\r\n"
216 for k,v in pairs(response.headers) do
217 header = header .. k .. ": " .. v .. "\r\n"
220 client:send(header .. "\r\n")
226 eof = not ltn12.pump.step(sourceout, sinkout)