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 local _meta = {__mode = "k"}
34 setmetatable(threadm, _meta)
35 setmetatable(threadi, _meta)
38 function Socket(ip, port)
39 local sock, err = socket.bind( ip, port )
42 sock:settimeout( 0, "t" )
48 function corecv(socket, ...)
49 threadi[socket] = true
52 local chunk, err, part = socket:receive(...)
54 if err ~= "timeout" then
55 threadi[socket] = false
56 return chunk, err, part
63 function cosend(socket, chunk, i, ...)
64 threadi[socket] = true
68 local stat, err, sent = socket:send(chunk, i, ...)
70 if err ~= "timeout" then
71 threadi[socket] = false
72 return stat, err, sent
74 i = sent and (sent + 1) or i
81 function register(socket, s_clhandler, s_errhandler)
82 table.insert(reading, socket)
83 clhandler[socket] = s_clhandler
84 erhandler[socket] = s_errhandler
95 if not THREAD_LIMIT or threadc < THREAD_LIMIT then
97 for i, server in ipairs(reading) do
98 local client = server:accept()
100 threadm[client] = now
101 threadc = threadc + 1
102 threads[client] = coroutine.create(clhandler[server])
107 for client, thread in pairs(threads) do
108 coroutine.resume(thread, client)
109 local now = os.time()
110 if coroutine.status(thread) == "dead" then
111 threadc = threadc - 1
112 threads[client] = nil
113 elseif threadm[client] and threadm[client] + THREAD_TIMEOUT < now then
114 threads[client] = nil
115 threadc = threadc - 1
117 elseif not threadi[client] then
118 threadm[client] = now
124 socket.sleep(THREAD_IDLEWAIT)