* luci/httpd: Handle timeouts on socket writes correctly
[project/luci.git] / libs / httpd / luasrc / httpd.lua
1 --[[
2
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>
6
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
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17 module("luci.httpd", package.seeall)
18 require("socket")
19
20 THREAD_IDLEWAIT = 0.01
21 THREAD_TIMEOUT  = 90
22 THREAD_LIMIT    = nil
23
24 local reading   = {}
25 local clhandler = {}
26 local erhandler = {}
27
28 local threadc = 0
29 local threads = {}
30 local threadm = {}
31 local threadi = {}
32
33 function Socket(ip, port)
34         local sock, err = socket.bind( ip, port )
35
36         if sock then
37                 sock:settimeout( 0, "t" )
38         end
39
40         return sock, err
41 end
42
43 function corecv(socket, ...)
44         threadi[socket] = true
45
46         while true do
47                 local chunk, err, part = socket:receive(...)
48
49                 if err ~= "timeout" then
50                         threadi[socket] = false
51                         return chunk, err, part
52                 end
53  
54                 coroutine.yield()
55         end
56 end
57
58 function cosend(socket, chunk, i, ...)
59         threadi[socket] = true
60         i = i or 1
61
62         while true do
63                 local stat, err, sent = socket:send(chunk, i, ...)
64
65                 if err ~= "timeout" then
66                         threadi[socket] = false
67                         return stat, err, sent
68                 else
69                         i = sent and (sent + 1) or 1 
70                 end
71  
72                 coroutine.yield()
73         end
74 end
75
76 function register(socket, s_clhandler, s_errhandler)
77         table.insert(reading, socket)
78         clhandler[socket] = s_clhandler
79         erhandler[socket] = s_errhandler
80 end
81
82 function run()
83         while true do
84                 step()
85         end
86 end
87
88 function step()
89         local idle = true
90                 
91         if not THREAD_LIMIT or threadc < THREAD_LIMIT then
92                 local now = os.time()
93                 for i, server in ipairs(reading) do
94                         local client = server:accept()
95                         if client then
96                                 threadm[client] = now
97                                 threadc = threadc + 1
98                                 threads[client] = coroutine.create(clhandler[server])
99                         end
100                 end
101         end
102         
103         for client, thread in pairs(threads) do
104                 coroutine.resume(thread, client)
105                 local now = os.time()
106                 if coroutine.status(thread) == "dead" then
107                         threads[client] = nil
108                         threadc = threadc - 1
109                 elseif threadm[client] and threadm[client] + THREAD_TIMEOUT < now then
110                         threads[client] = nil
111                         threadc = threadc - 1   
112                         client:close()
113                 elseif not threadi[client] then 
114                         threadm[client] = now
115                         idle = false
116                 end
117         end
118         
119         if idle then
120                 socket.sleep(THREAD_IDLEWAIT)
121         end
122 end