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