plugins/mod_http.lua
changeset 4636 41983ec223f0
parent 4635 ea5215bd2783
child 4664 7438b3c68576
equal deleted inserted replaced
4635:ea5215bd2783 4636:41983ec223f0
    12 
    12 
    13 --[[function listener.associate_session(conn, session)
    13 --[[function listener.associate_session(conn, session)
    14 	sessions[conn] = session;
    14 	sessions[conn] = session;
    15 end]]
    15 end]]
    16 
    16 
    17 local handlers;
    17 local NULL = {};
       
    18 local handlers = {};
    18 
    19 
    19 function build_handlers()
    20 function build_handlers(host)
    20 	handlers = {};
    21 	if not hosts[host] then return; end
    21 	for _, item in ipairs(module:get_host_items("http-handler")) do
    22 	local h = {};
    22 		local previous = handlers[item.path];
    23 	handlers[host] = h;
    23 		if not previous and item.path then
    24 	
    24 			handlers[item.path] = item;
    25 	for mod_name, module in pairs(modulemanager.get_modules(host)) do
       
    26 		module = module.module;
       
    27 		if module.items then
       
    28 			for _, item in ipairs(module.items["http-handler"] or NULL) do
       
    29 				local previous = handlers[item.path];
       
    30 				if not previous and item.path then
       
    31 					h[item.path] = item;
       
    32 				end
       
    33 			end
    25 		end
    34 		end
    26 	end
    35 	end
       
    36 
       
    37 	return h;
    27 end
    38 end
    28 function clear_handlers()
    39 function clear_handlers(event)
    29 	handlers = nil;
    40 	handlers[event.source.host] = nil;
       
    41 end
       
    42 function get_handler(host, path)
       
    43 	local h = handlers[host] or build_handlers(host);
       
    44 	if h then
       
    45 		local item = h[path];
       
    46 		return item and item.handler;
       
    47 	end
    30 end
    48 end
    31 module:handle_items("http-handler", clear_handlers, clear_handlers, false);
    49 module:handle_items("http-handler", clear_handlers, clear_handlers, false);
    32 
    50 
    33 function http_handler(event)
    51 function http_handler(event)
    34 	local request, response = event.request, event.response;
    52 	local request, response = event.request, event.response;
    35 
    53 
    36 	if not handlers then build_handlers(); end
    54 	local handler = get_handler(request.headers.host:match("[^:]*"):lower(), request.path:match("[^?]*"));
    37 	local item = handlers[request.path:match("[^?]*")];
       
    38 	local handler = item and item.handler;
       
    39 	if handler then
    55 	if handler then
    40 		handler(request, response);
    56 		handler(request, response);
    41 		return true;
    57 		return true;
    42 	end
    58 	end
    43 end
    59 end