dd5b81548ec2e453795248b81859f1c00f8ccbcc
[project/luci.git] / libs / httpd / luasrc / httpd / server.lua
1 --[[
2
3 HTTP server implementation for LuCI - helper class
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.server", package.seeall)
18 require("socket")
19 require("socket.http")
20 require("luci.util")
21
22 READ_BUFSIZE = 1024
23
24
25 VHost = luci.util.class()
26
27 function VHost.__init__(self, handler)
28         self.handler = handler
29         self.dhandler = {}
30 end
31
32 function VHost.process(self, request, sourcein, sinkerr, ...)
33         local handler = self.handler
34
35         local uri = request.env.REQUEST_URI:match("^([^?]*)")
36
37         -- SCRIPT_NAME
38         request.env.SCRIPT_NAME = ""
39
40         -- Call URI part
41         request.env.PATH_INFO = uri
42
43         for k, dhandler in pairs(self.dhandler) do
44                 if k == uri or k.."/" == uri:sub(1, #k+1) then
45                         handler = dhandler
46                         request.env.SCRIPT_NAME = k
47                         request.env.PATH_INFO   = uri:sub(#k+1)
48                         break;
49                 end
50         end
51
52         if handler then
53                 return handler:process(request, sourcein, sinkerr, ...)
54         end
55 end
56
57
58 function VHost.set_default_handler(self, handler)
59         self.handler = handler
60 end
61
62
63 function VHost.set_handler(self, match, handler)
64         self.dhandler[match] = handler
65 end
66
67
68
69 Server = luci.util.class()
70
71 function Server.__init__(self, host)
72         self.host = host
73         self.vhosts = {}
74 end
75
76 function Server.set_default_vhost(self, vhost)
77         self.host = vhost
78 end
79
80 -- Sets a vhost
81 function Server.set_vhost(self, name, vhost)
82         self.vhosts[name] = vhost
83 end
84
85 function Server.create_daemon_handlers(self)
86         return function(...) return self:process(...) end,
87                 function(...) return self:error_overload(...) end
88 end
89
90
91 function Server.error(self, socket, code, msg)
92         hcode = tostring(code)
93         
94         socket:send( "HTTP/1.0 " .. hcode .. " " ..
95          luci.http.protocol.statusmsg[code] .. "\r\n" )
96         socket:send( "Connection: close\r\n" )
97         socket:send( "Content-Type: text/plain\r\n\r\n" )
98
99         if msg then
100                 socket:send( "HTTP-Error " .. code .. ": " .. msg .. "\r\n" )
101         end
102 end
103
104 function Server.error_overload(self, socket)
105         self:error(socket, 503, "Too many simultaneous connections")
106 end
107
108
109 function Server.process( self, thread )
110
111         -- Setup sockets and sources
112         local client = thread.socket
113         
114         client:settimeout( 0 )
115         
116         local sourcein  = ltn12.source.empty()
117         local sourcehdr = luci.http.protocol.header_source( thread )
118         local sinkerr   = ltn12.sink.file( io.stderr )
119         
120         local close = false
121         
122         local reading = { client }
123
124         local message, err
125         
126         repeat
127                 -- parse headers
128                 message, err = luci.http.protocol.parse_message_header( sourcehdr )
129
130                 if not message then
131                         self:error( client, 400, err )
132                         break
133                 end     
134                 
135                 -- keep-alive
136                 if message.http_version == 1.1 then
137                         close = (message.env.HTTP_CONNECTION == "close")
138                 else
139                         close = not message.env.HTTP_CONNECTION or message.env.HTTP_CONNECTION == "close"
140                 end
141         
142                 if message.request_method == "get" or message.request_method == "head" then
143                         -- Be happy
144                         
145                 elseif message.request_method == "post" then
146                         -- If we have a HTTP/1.1 client and an Expect: 100-continue header then
147                         -- respond with HTTP 100 Continue message
148                         if message.http_version == 1.1 and message.headers['Expect'] and
149                                 message.headers['Expect'] == '100-continue'
150                         then
151                                 client:send("HTTP/1.1 100 Continue\r\n\r\n")
152                         end
153                         
154                         if message.headers['Transfer-Encoding'] and
155                          message.headers['Transfer-Encoding'] ~= "identity" then
156                                 sourcein = socket.source("http-chunked", thread)
157                         elseif message.env.CONTENT_LENGTH then
158                                 sourcein = socket.source("by-length", thread,
159                                  tonumber(message.env.CONTENT_LENGTH))
160                         else
161                                 self:error( client, 411, luci.http.protocol.statusmsg[411] )
162                                 break;
163                         end
164                         
165                 else
166                         self:error( client, 405, luci.http.protocol.statusmsg[405] )
167                         break;
168                         
169                 end
170
171
172                 local host = self.vhosts[message.env.HTTP_HOST] or self.host
173                 if not host then
174                         self:error( client, 500, "Unable to find matching host" )
175                         break;
176                 end
177                 
178                 local response, sourceout = host:process(
179                         message, sourcein, sinkerr,
180                         client, io.stderr 
181                 )
182                 if not response then
183                         self:error( client, 500, "Error processing handler" )
184                 end
185                 
186                 -- Post process response
187                 local sinkmode = close and "close-when-done" or "keep-open"
188                 
189                 if sourceout then
190                         if not response.headers["Content-Length"] then
191                                 if message.http_version == 1.1 then
192                                         response.headers["Transfer-Encoding"] = "chunked"
193                                         sinkmode = "http-chunked"
194                                 else
195                                         close = true
196                                         sinkmode = "close-when-done"
197                                 end
198                         end
199                 end
200                 
201                 if close then
202                         response.headers["Connection"] = "close"
203                 end
204                 
205                 
206                 local sinkout = socket.sink(sinkmode, client)
207                 
208                 local header =
209                         message.env.SERVER_PROTOCOL .. " " ..
210                         tostring(response.status) .. " " ..
211                         luci.http.protocol.statusmsg[response.status] .. "\r\n"
212
213                 
214                 for k,v in pairs(response.headers) do
215                         header = header .. k .. ": " .. v .. "\r\n"
216                 end
217                 
218                 client:send(header .. "\r\n")
219                 
220                 if sourceout then
221                         local eof = false
222                         repeat
223                                 coroutine.yield()
224                                 eof = not ltn12.pump.step(sourceout, sinkout)
225                         until eof
226                 end
227         until close
228         
229         client:close()
230 end