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.isdead(self)
65 return coroutine.status(self.routine) == "dead"
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.remove_dead(self, thread)
91 self:dprint("Completed " .. tostring(thread))
94 self.threadc = self.threadc - 1
95 self.threads[thread.socket] = nil
98 function Daemon.kill_timedout(self)
101 for sock, thread in pairs(self.threads) do
102 if os.difftime(now, thread:touched()) > self.timeout then
103 self.threads[sock] = nil
104 self.threadc = self.threadc - 1
110 function Daemon.dprint(self, msg)
112 io.stderr:write("[daemon] " .. msg .. "\n")
116 function Daemon.register(self, sock, clhandler, errhandler)
117 table.insert( self.reading, sock )
118 self.handler[sock] = { clhandler = clhandler, errhandler = errhandler }
121 function Daemon.run(self)
127 function Daemon.step(self)
128 local input, output, err = socket.select( self.reading, nil, 0 )
129 local working = false
131 -- accept new connections
132 for i, connection in ipairs(input) do
134 local sock = connection:accept()
138 if not self.threadlimit or self.threadc < self.threadlimit then
141 self:dprint("Accepted incoming connection from " .. sock:getpeername())
144 local t = Thread(sock, self.handler[connection].clhandler)
145 self.threads[sock] = t
146 self.threadc = self.threadc + 1
149 self:dprint("Created " .. tostring(t))
157 self:dprint("Rejected incoming connection from " .. sock:getpeername())
160 if self.handler[connection].errhandler then
161 self.handler[connection].errhandler( sock )
169 -- create client handler
170 for sock, thread in pairs( self.threads ) do
171 -- resume working threads
172 if not thread:iswaiting() then
174 self:dprint("Resuming " .. tostring(thread))
177 local stat, err = thread:resume()
178 if stat and not thread:isdead() then
180 if not thread:iswaiting() then
183 table.insert(self.waiting, sock)
186 self:remove_dead(thread)
190 self:dprint(tostring(thread) .. " returned")
192 self:dprint("Error in " .. tostring(thread) .. " " .. err)
198 -- check for data on waiting threads
199 input, output, err = socket.select( self.waiting, nil, 0 )
201 for i, sock in ipairs(input) do
202 local thread = self.threads[sock]
204 if thread:isdead() then
205 self:remove_dead(thread)
209 if not thread:iswaiting() then
210 for i, s in ipairs(self.waiting) do
212 table.remove(self.waiting, i)
223 if err == "timeout" and not working then
225 socket.sleep(self.waittime)