3 HTTP server implementation for LuCI - core
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", package.seeall)
20 THREAD_IDLEWAIT = 0.01
33 function Socket(ip, port)
34 local sock, err = socket.bind( ip, port )
37 sock:settimeout( 0, "t" )
43 function corecv(socket, ...)
44 threadi[socket] = true
47 local chunk, err, part = socket:receive(...)
49 if err ~= "timeout" then
50 threadi[socket] = false
51 return chunk, err, part
58 function cosend(socket, chunk, i, ...)
59 threadi[socket] = true
63 local stat, err, sent = socket:send(chunk, i, ...)
65 if err ~= "timeout" then
66 threadi[socket] = false
67 return stat, err, sent
69 i = sent and (sent + 1) or i
76 function register(socket, s_clhandler, s_errhandler)
77 table.insert(reading, socket)
78 clhandler[socket] = s_clhandler
79 erhandler[socket] = s_errhandler
90 if not THREAD_LIMIT or threadc < THREAD_LIMIT then
92 for i, server in ipairs(reading) do
93 local client = server:accept()
97 threads[client] = coroutine.create(clhandler[server])
102 for client, thread in pairs(threads) do
103 coroutine.resume(thread, client)
104 local now = os.time()
105 if coroutine.status(thread) == "dead" then
106 threads[client] = nil
107 threadc = threadc - 1
108 threadm[client] = nil
109 threadi[client] = nil
110 elseif threadm[client] and threadm[client] + THREAD_TIMEOUT < now then
111 threads[client] = nil
112 threadc = threadc - 1
114 elseif not threadi[client] then
115 threadm[client] = now
121 socket.sleep(THREAD_IDLEWAIT)