Comparison

net/http/server.lua @ 4683:c1374e083c97

net.http.server: Handle results returned by handlers, and send as a response. Also removes explicit firing of '*', which can now be done via wildcard events.
author Matthew Wild <mwild1@gmail.com>
date Tue, 24 Apr 2012 19:07:12 +0100
parent 4682:9d90c70b6358
child 4688:8d275c86a04f
comparison
equal deleted inserted replaced
4682:9d90c70b6358 4683:c1374e083c97
166 local host = request.headers.host; 166 local host = request.headers.host;
167 if host then 167 if host then
168 host = host:match("[^:]*"):lower(); 168 host = host:match("[^:]*"):lower();
169 local event = request.method.." "..host..request.path:match("[^?]*"); 169 local event = request.method.." "..host..request.path:match("[^?]*");
170 local payload = { request = request, response = response }; 170 local payload = { request = request, response = response };
171 --[[repeat 171 --log("debug", "Firing event: %s", event);
172 if events.fire_event(event, payload) ~= nil then return; end 172 local result = events.fire_event(event, payload);
173 event = (event:sub(-1) == "/") and event:sub(1, -1) or event:gsub("[^/]*$", ""); 173 if result ~= nil then
174 if event:sub(-1) == "/" then 174 if result ~= true then
175 event = event:sub(1, -1); 175 local code, body = 200, "";
176 else 176 local result_type = type(result);
177 event = event:gsub("[^/]*$", ""); 177 if result_type == "number" then
178 end 178 response.status_code = result;
179 until not event:find("/", 1, true);]] 179 elseif result_type == "string" then
180 --log("debug", "Event: %s", event); 180 body = result;
181 if events.fire_event(event, payload) ~= nil then return; end 181 elseif result_type == "table" then
182 -- TODO try adding/stripping / at the end, but this needs to work via an HTTP redirect 182 body = result.body;
183 if events.fire_event("*", payload) ~= nil then return; end 183 result.body = nil;
184 for k, v in pairs(result) do
185 response[k] = v;
186 end
187 end
188 response:send(body);
189 end
190 return;
191 end
184 end 192 end
185 193
186 -- if handler not called, fallback to legacy httpserver handlers 194 -- if handler not called, fallback to legacy httpserver handlers
187 _M.legacy_handler(request, response); 195 _M.legacy_handler(request, response);
188 end 196 end