Comparison

plugins/mod_http.lua @ 6504:e1659f32852e

mod_http: For URLs that end with / or wildcard handlers, add a low-priority redirect from without to with slash
author Kim Alvefur <zash@zash.se>
date Tue, 04 Nov 2014 17:48:17 +0100
parent 6086:3b4fde51fa25
child 6530:35ebcb733c4c
comparison
equal deleted inserted replaced
6503:8437058c4226 6504:e1659f32852e
41 local function get_base_path(host_module, app_name, default_app_path) 41 local function get_base_path(host_module, app_name, default_app_path)
42 return (normalize_path(host_module:get_option("http_paths", {})[app_name] -- Host 42 return (normalize_path(host_module:get_option("http_paths", {})[app_name] -- Host
43 or module:get_option("http_paths", {})[app_name] -- Global 43 or module:get_option("http_paths", {})[app_name] -- Global
44 or default_app_path)) -- Default 44 or default_app_path)) -- Default
45 :gsub("%$(%w+)", { host = host_module.host }); 45 :gsub("%$(%w+)", { host = host_module.host });
46 end
47
48 local function redir_handler(event)
49 event.response.headers.location = event.request.path.."/";
50 return 301;
46 end 51 end
47 52
48 local ports_by_scheme = { http = 80, https = 443, }; 53 local ports_by_scheme = { http = 80, https = 443, };
49 54
50 -- Helper to deduce a module's external URL 55 -- Helper to deduce a module's external URL
97 local _handler = handler; 102 local _handler = handler;
98 handler = function (event) 103 handler = function (event)
99 local path = event.request.path:sub(base_path_len); 104 local path = event.request.path:sub(base_path_len);
100 return _handler(event, path); 105 return _handler(event, path);
101 end; 106 end;
107 module:hook_object_event(server, event_name:sub(1, -3), redir_handler, -1);
108 elseif event_name:sub(-1, -1) == "/" then
109 module:hook_object_event(server, event_name:sub(1, -2), redir_handler, -1);
102 end 110 end
103 if not app_handlers[event_name] then 111 if not app_handlers[event_name] then
104 app_handlers[event_name] = handler; 112 app_handlers[event_name] = handler;
105 module:hook_object_event(server, event_name, handler); 113 module:hook_object_event(server, event_name, handler);
106 else 114 else