* luci/httpd: add initial server implementation
[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
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
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 require("ltn12")
17 require("socket")
18
19 require("luci.util")
20 require("luci.http.protocol")
21 require("luci.httpd.server")
22
23
24 local srv  = luci.httpd.server
25 local host = "0.0.0.0"
26 local port = 50000
27
28
29 server = socket.bind(host, port)
30 server:settimeout( 0, "t" )
31
32 reading = { server }
33 running = { }
34
35
36 while true do
37
38         local input = socket.select( reading, nil, 0.1 )
39
40         -- accept new connections
41         for i, connection in ipairs(input) do
42
43                 local sock = connection:accept()
44
45                 -- check capacity
46                 if #running < srv.MAX_CLIENTS then
47
48                         table.insert( running, {
49                                 coroutine.create( srv.client_handler ),
50                                 sock
51                         } )
52
53                 -- reject client
54                 else
55                         srv.error503( sock )
56                 end
57         end
58
59         -- create client handler
60         for i, client in ipairs( running ) do
61
62                 -- reap dead clients
63                 if coroutine.status( client[1] ) == "dead" then
64                         table.remove( running, i )
65                 end
66
67                 coroutine.resume( client[1], client[2] )
68         end
69 end