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")
26 VHost = luci.util.class()
28 function VHost.__init__(self, handler)
29 self.handler = handler
33 function VHost.process(self, request, sourcein, sinkerr, ...)
34 local handler = self.handler
36 local uri = request.env.REQUEST_URI:match("^([^?]*)")
39 request.env.SCRIPT_NAME = ""
42 request.env.PATH_INFO = uri
44 for k, dhandler in pairs(self.dhandler) do
45 if k == uri or k.."/" == uri:sub(1, #k+1) then
47 request.env.SCRIPT_NAME = k
48 request.env.PATH_INFO = uri:sub(#k+1)
54 return handler:process(request, sourcein, sinkerr, ...)
59 function VHost.set_default_handler(self, handler)
60 self.handler = handler
64 function VHost.set_handler(self, match, handler)
65 self.dhandler[match] = handler
70 Server = luci.util.class()
72 function Server.__init__(self, host)
77 function Server.set_default_vhost(self, vhost)
82 function Server.set_vhost(self, name, vhost)
83 self.vhosts[name] = vhost
86 function Server.create_daemon_handlers(self)
87 return function(...) return self:process(...) end,
88 function(...) return self:error_overload(...) end
92 function Server.error(self, socket, code, msg)
93 hcode = tostring(code)
95 socket:send( "HTTP/1.0 " .. hcode .. " " ..
96 luci.http.protocol.statusmsg[code] .. "\r\n" )
97 socket:send( "Connection: close\r\n" )
98 socket:send( "Content-Type: text/plain\r\n\r\n" )
101 socket:send( "HTTP-Error " .. code .. ": " .. msg .. "\r\n" )
105 function Server.error_overload(self, socket)
106 self:error(socket, 503, "Too many simultaneous connections")
110 function Server.process( self, client )
112 -- Setup sockets and sources
114 receive = function(self, ...) return luci.httpd.corecv(client, ...) end,
115 send = function(self, ...) return luci.httpd.cosend(client, ...) end,
116 close = function(self, ...) return client:close(...) end,
117 getfd = function(self, ...) return client:getfd(...) end,
118 dirty = function(self, ...) return client:dirty(...) end
121 client:settimeout( 0 )
123 local sourcein = ltn12.source.empty()
124 local sourcehdr = luci.http.protocol.header_source( thread )
125 local sinkerr = ltn12.sink.file( io.stderr )
129 local reading = { client }
135 message, err = luci.http.protocol.parse_message_header( sourcehdr )
138 self:error( thread, 400, err )
143 if message.http_version == 1.1 then
144 close = (message.env.HTTP_CONNECTION == "close")
146 close = not message.env.HTTP_CONNECTION or message.env.HTTP_CONNECTION == "close"
149 if message.request_method == "get" or message.request_method == "head" then
152 elseif message.request_method == "post" then
153 -- If we have a HTTP/1.1 client and an Expect: 100-continue header then
154 -- respond with HTTP 100 Continue message
155 if message.http_version == 1.1 and message.headers['Expect'] and
156 message.headers['Expect'] == '100-continue'
158 thread:send("HTTP/1.1 100 Continue\r\n\r\n")
161 if message.headers['Transfer-Encoding'] and
162 message.headers['Transfer-Encoding'] ~= "identity" then
163 sourcein = socket.source("http-chunked", thread)
164 elseif message.env.CONTENT_LENGTH then
165 sourcein = socket.source("by-length", thread,
166 tonumber(message.env.CONTENT_LENGTH))
168 self:error( thread, 411, luci.http.protocol.statusmsg[411] )
173 self:error( thread, 405, luci.http.protocol.statusmsg[405] )
179 local host = self.vhosts[message.env.HTTP_HOST] or self.host
181 self:error( thread, 500, "Unable to find matching host" )
185 local response, sourceout = host:process(
186 message, sourcein, sinkerr,
190 self:error( thread, 500, "Error processing handler" )
193 -- Post process response
194 local sinkmode = close and "close-when-done" or "keep-open"
197 if not response.headers["Content-Length"] then
198 if message.http_version == 1.1 then
199 response.headers["Transfer-Encoding"] = "chunked"
200 sinkmode = "http-chunked"
203 sinkmode = "close-when-done"
209 response.headers["Connection"] = "close"
213 local sinkout = socket.sink(sinkmode, thread)
216 message.env.SERVER_PROTOCOL .. " " ..
217 tostring(response.status) .. " " ..
218 luci.http.protocol.statusmsg[response.status] .. "\r\n"
220 header = header .. "Server: LuCI HTTPd/" .. tostring(VERSION) .. "\r\n"
223 for k,v in pairs(response.headers) do
224 header = header .. k .. ": " .. v .. "\r\n"
227 thread:send(header .. "\r\n")
233 eof = not ltn12.pump.step(sourceout, sinkout)