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.getidletime(self)
40 return os.difftime(os.time(), self.stamp)
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, timeout)
81 setmetatable(self.waiting, {__mode = "v"})
84 self.threadlimit = threadlimit
85 self.timeout = timeout or 0.1
88 function Daemon.dprint(self, msg)
90 io.stderr:write("[daemon] " .. msg .. "\n")
94 function Daemon.register(self, sock, clhandler, errhandler)
95 table.insert( self.reading, sock )
96 self.handler[sock] = { clhandler = clhandler, errhandler = errhandler }
99 function Daemon.run(self)
105 function Daemon.step(self)
106 local input, output, err = socket.select( self.reading, nil, 0 )
107 local working = false
109 -- accept new connections
110 for i, connection in ipairs(input) do
112 local sock = connection:accept()
116 if not self.threadlimit or self.threadc < self.threadlimit then
119 self:dprint("Accepted incoming connection from " .. sock:getpeername())
122 local t = Thread(sock, self.handler[connection].clhandler)
123 self.threads[sock] = t
124 self.threadc = self.threadc + 1
127 self:dprint("Created " .. tostring(t))
133 self:dprint("Rejected incoming connection from " .. sock:getpeername())
136 if self.handler[connection].errhandler then
137 self.handler[connection].errhandler( sock )
145 -- create client handler
146 for sock, thread in pairs( self.threads ) do
149 if thread:status() == "dead" then
151 self:dprint("Completed " .. tostring(thread))
154 self.threadc = self.threadc - 1
155 self.threads[sock] = nil
156 -- resume working threads
157 elseif not thread:iswaiting() then
159 self:dprint("Resuming " .. tostring(thread))
162 local stat, err = thread:resume()
165 if not thread:iswaiting() then
168 table.insert(self.waiting, sock)
173 self:dprint(tostring(thread) .. " returned")
175 self:dprint("Error in " .. tostring(thread) .. " " .. err)
181 -- check for data on waiting threads
182 input, output, err = socket.select( self.waiting, nil, 0 )
184 for i, sock in ipairs(input) do
185 self.threads[sock]:resume()
186 self.threads[sock]:touch()
188 if not self.threads[sock]:iswaiting() then
189 for i, s in ipairs(self.waiting) do
191 table.remove(self.waiting, i)
201 if err == "timeout" and not working then
202 socket.sleep(self.timeout)