3 HTTP server implementation for LuCI - core
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
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
16 module("luci.httpd", package.seeall)
20 function Socket(ip, port)
21 local sock, err = socket.bind( ip, port )
24 sock:settimeout( 0, "t" )
30 Thread = luci.util.class()
32 function Thread.__init__(self, socket, func)
34 self.routine = coroutine.create(func)
35 self.stamp = os.time()
39 function Thread.touched(self)
43 function Thread.iswaiting(self)
47 function Thread.receive(self, ...)
48 local chunk, err, part
53 chunk, err, part = self.socket:receive(...)
54 until err ~= "timeout"
57 return chunk, err, part
60 function Thread.resume(self, ...)
61 return coroutine.resume(self.routine, self, ...)
64 function Thread.status(self)
65 return coroutine.status(self.routine)
68 function Thread.touch(self)
69 self.stamp = os.time()
72 Daemon = luci.util.class()
74 function Daemon.__init__(self, threadlimit, waittime, timeout)
81 setmetatable(self.waiting, {__mode = "v"})
84 self.threadlimit = threadlimit
85 self.waittime = waittime or 0.1
86 self.timeout = timeout or 90
89 function Daemon.dprint(self, msg)
91 io.stderr:write("[daemon] " .. msg .. "\n")
95 function Daemon.register(self, sock, clhandler, errhandler)
96 table.insert( self.reading, sock )
97 self.handler[sock] = { clhandler = clhandler, errhandler = errhandler }
100 function Daemon.run(self)
106 function Daemon.step(self)
107 local input, output, err = socket.select( self.reading, nil, 0 )
108 local working = false
110 -- accept new connections
111 for i, connection in ipairs(input) do
113 local sock = connection:accept()
117 if not self.threadlimit or self.threadc < self.threadlimit then
120 self:dprint("Accepted incoming connection from " .. sock:getpeername())
123 local t = Thread(sock, self.handler[connection].clhandler)
124 self.threads[sock] = t
125 self.threadc = self.threadc + 1
128 self:dprint("Created " .. tostring(t))
134 self:dprint("Rejected incoming connection from " .. sock:getpeername())
137 if self.handler[connection].errhandler then
138 self.handler[connection].errhandler( sock )
146 -- create client handler
147 for sock, thread in pairs( self.threads ) do
148 local now = os.time()
151 if thread:status() == "dead" then
153 self:dprint("Completed " .. tostring(thread))
156 self.threadc = self.threadc - 1
157 self.threads[sock] = nil
159 elseif os.difftime(now, thread:touched()) > self.timeout then
160 self.threads[sock] = nil
162 -- resume working threads
163 elseif not thread:iswaiting() then
165 self:dprint("Resuming " .. tostring(thread))
168 local stat, err = thread:resume()
171 if not thread:iswaiting() then
174 table.insert(self.waiting, sock)
179 self:dprint(tostring(thread) .. " returned")
181 self:dprint("Error in " .. tostring(thread) .. " " .. err)
187 -- check for data on waiting threads
188 input, output, err = socket.select( self.waiting, nil, 0 )
190 for i, sock in ipairs(input) do
191 self.threads[sock]:resume()
192 self.threads[sock]:touch()
194 if not self.threads[sock]:iswaiting() then
195 for i, s in ipairs(self.waiting) do
197 table.remove(self.waiting, i)
207 if err == "timeout" and not working then
208 socket.sleep(self.waittime)