Software /
code /
prosody
Comparison
plugins/mod_http.lua @ 4667:d0cfc49f3f2b
mod_http: Support for default_path in apps
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 23 Apr 2012 18:23:49 +0100 |
parent | 4664:7438b3c68576 |
child | 4669:0e0a72679f77 |
comparison
equal
deleted
inserted
replaced
4666:fb522fbd495e | 4667:d0cfc49f3f2b |
---|---|
15 if path:sub(1,1) ~= "/" then path = "/"..path; end | 15 if path:sub(1,1) ~= "/" then path = "/"..path; end |
16 if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end | 16 if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end |
17 return path; | 17 return path; |
18 end | 18 end |
19 | 19 |
20 local function get_http_event(http_module, app_name, key) | 20 local function get_http_event(host, app_path, key) |
21 local method, path = key:match("^(%S+)%s+(.+)$"); | 21 local method, path = key:match("^(%S+)%s+(.+)$"); |
22 if not method and key:sub(1,1) == "/" then | 22 if not method and key:sub(1,1) == "/" then |
23 method, path = "GET", key; | 23 method, path = "GET", key; |
24 else | 24 else |
25 return nil; | 25 return nil; |
26 end | 26 end |
27 path = normalize_path(path); | 27 path = normalize_path(path); |
28 local app_path = normalize_path(http_module:get_option_string(app_name.."_http_path", "/"..app_name)); | 28 return method:upper().." "..host..app_path..path; |
29 return method:upper().." "..http_module.host..app_path..path; | |
30 end | 29 end |
31 | 30 |
32 function module.add_host(module) | 31 function module.add_host(module) |
32 local host = module.host; | |
33 local apps = {}; | 33 local apps = {}; |
34 module.environment.apps = apps; | 34 module.environment.apps = apps; |
35 local function http_app_added(event) | 35 local function http_app_added(event) |
36 local app_name = event.item.name; | 36 local app_name = event.item.name; |
37 local default_app_path = event.item.default_path or "/"..app_name; | |
38 local app_path = normalize_path(module:get_option_string(app_name.."_http_path", default_app_path)); | |
37 if not app_name then | 39 if not app_name then |
38 -- TODO: Link to docs | 40 -- TODO: Link to docs |
39 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)"); | 41 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)"); |
40 return; | 42 return; |
41 end | 43 end |
42 apps[app_name] = apps[app_name] or {}; | 44 apps[app_name] = apps[app_name] or {}; |
43 local app_handlers = apps[app_name]; | 45 local app_handlers = apps[app_name]; |
44 for key, handler in pairs(event.item.route or {}) do | 46 for key, handler in pairs(event.item.route or {}) do |
45 local event_name = get_http_event(module, app_name, key); | 47 local event_name = get_http_event(host, app_path, key); |
46 if event_name then | 48 if event_name then |
47 if not app_handlers[event_name] then | 49 if not app_handlers[event_name] then |
48 app_handlers[event_name] = handler; | 50 app_handlers[event_name] = handler; |
49 server.add_handler(event_name, handler); | 51 server.add_handler(event_name, handler); |
50 else | 52 else |