Mercurial > prosody-hg
comparison net/http/server.lua @ 4688:8d275c86a04f
net.http.server: Remove legacy compatibility
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 25 Apr 2012 14:58:10 +0100 |
| parents | c1374e083c97 |
| children | e8c357259993 |
comparison
equal
deleted
inserted
replaced
| 4687:bd3a852b949a | 4688:8d275c86a04f |
|---|---|
| 158 if not request.headers.host then | 158 if not request.headers.host then |
| 159 response.status_code = 400; | 159 response.status_code = 400; |
| 160 response.headers.content_type = "text/html"; | 160 response.headers.content_type = "text/html"; |
| 161 response:send("<html><head>400 Bad Request</head><body>400 Bad Request: No Host header.</body></html>"); | 161 response:send("<html><head>400 Bad Request</head><body>400 Bad Request: No Host header.</body></html>"); |
| 162 else | 162 else |
| 163 -- TODO call handler | |
| 164 --response.headers.content_type = "text/plain"; | |
| 165 --response:send("host="..(request.headers.host or "").."\npath="..request.path.."\n"..(request.body or "")); | |
| 166 local host = request.headers.host; | 163 local host = request.headers.host; |
| 167 if host then | 164 if host then |
| 168 host = host:match("[^:]*"):lower(); | 165 host = host:match("[^:]*"):lower(); |
| 169 local event = request.method.." "..host..request.path:match("[^?]*"); | 166 local event = request.method.." "..host..request.path:match("[^?]*"); |
| 170 local payload = { request = request, response = response }; | 167 local payload = { request = request, response = response }; |
| 189 end | 186 end |
| 190 return; | 187 return; |
| 191 end | 188 end |
| 192 end | 189 end |
| 193 | 190 |
| 194 -- if handler not called, fallback to legacy httpserver handlers | 191 -- if handler not called, return 404 |
| 195 _M.legacy_handler(request, response); | 192 response.status_code = 404; |
| 193 response.headers.content_type = "text/html"; | |
| 194 response:send("<html><head><title>404 Not Found</title></head><body>404 Not Found: No such page.</body></html>"); | |
| 196 end | 195 end |
| 197 end | 196 end |
| 198 function _M.send_response(response, body) | 197 function _M.send_response(response, body) |
| 199 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); | 198 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); |
| 200 local headers = response.headers; | 199 local headers = response.headers; |
| 213 response:finish_cb(); | 212 response:finish_cb(); |
| 214 else | 213 else |
| 215 response.conn:close(); | 214 response.conn:close(); |
| 216 end | 215 end |
| 217 end | 216 end |
| 218 function _M.legacy_handler(request, response) | |
| 219 log("debug", "Invoking legacy handler"); | |
| 220 local base = request.path:match("^/([^/?]+)"); | |
| 221 local legacy_server = legacy_httpserver and legacy_httpserver.new.http_servers[5280]; | |
| 222 local handler = legacy_server and legacy_server.handlers[base]; | |
| 223 if not handler then handler = legacy_httpserver and legacy_httpserver.set_default_handler.default_handler; end | |
| 224 if handler then | |
| 225 -- add legacy properties to request object | |
| 226 request.url = { path = request.path }; | |
| 227 request.handler = response.conn; | |
| 228 request.id = tostring{}:match("%x+$"); | |
| 229 local headers = {}; | |
| 230 for k,v in pairs(request.headers) do | |
| 231 headers[k:gsub("_", "-")] = v; | |
| 232 end | |
| 233 request.headers = headers; | |
| 234 function request:send(resp) | |
| 235 if self.destroyed then return; end | |
| 236 if resp.body or resp.headers then | |
| 237 if resp.headers then | |
| 238 for k,v in pairs(resp.headers) do response.headers[k] = v; end | |
| 239 end | |
| 240 response:send(resp.body) | |
| 241 else | |
| 242 response:send(resp) | |
| 243 end | |
| 244 self.sent = true; | |
| 245 self:destroy(); | |
| 246 end | |
| 247 function request:destroy() | |
| 248 if self.destroyed then return; end | |
| 249 if not self.sent then return self:send(""); end | |
| 250 self.destroyed = true; | |
| 251 if self.on_destroy then | |
| 252 log("debug", "Request has destroy callback"); | |
| 253 self:on_destroy(); | |
| 254 else | |
| 255 log("debug", "Request has no destroy callback"); | |
| 256 end | |
| 257 end | |
| 258 local r = handler(request.method, request.body, request); | |
| 259 if r ~= true then | |
| 260 request:send(r); | |
| 261 end | |
| 262 else | |
| 263 log("debug", "No handler found"); | |
| 264 response.status_code = 404; | |
| 265 response.headers.content_type = "text/html"; | |
| 266 response:send("<html><head><title>404 Not Found</title></head><body>404 Not Found: No such page.</body></html>"); | |
| 267 end | |
| 268 end | |
| 269 | |
| 270 function _M.add_handler(event, handler, priority) | 217 function _M.add_handler(event, handler, priority) |
| 271 events.add_handler(event, handler, priority); | 218 events.add_handler(event, handler, priority); |
| 272 end | 219 end |
| 273 function _M.remove_handler(event, handler) | 220 function _M.remove_handler(event, handler) |
| 274 events.remove_handler(event, handler); | 221 events.remove_handler(event, handler); |
