Mercurial > prosody-hg
comparison plugins/mod_http.lua @ 4797:e239668aa6d2
Merge 0.9->trunk
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sun, 29 Apr 2012 02:10:55 +0100 |
| parents | b2ed4e1bcb6e |
| children | 6c8074f47ca4 |
comparison
equal
deleted
inserted
replaced
| 4796:04a34287dc12 | 4797:e239668aa6d2 |
|---|---|
| 1 -- Prosody IM | |
| 2 -- Copyright (C) 2008-2012 Matthew Wild | |
| 3 -- Copyright (C) 2008-2012 Waqas Hussain | |
| 4 -- | |
| 5 -- This project is MIT/X11 licensed. Please see the | |
| 6 -- COPYING file in the source package for more information. | |
| 7 -- | |
| 8 | |
| 9 module:set_global(); | |
| 10 module:depends("http_errors"); | |
| 11 | |
| 12 local server = require "net.http.server"; | |
| 13 | |
| 14 server.set_default_host(module:get_option_string("http_default_host")); | |
| 15 | |
| 16 local function normalize_path(path) | |
| 17 if path:sub(1,1) ~= "/" then path = "/"..path; end | |
| 18 if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end | |
| 19 return path; | |
| 20 end | |
| 21 | |
| 22 local function get_http_event(host, app_path, key) | |
| 23 local method, path = key:match("^(%S+)%s+(.+)$"); | |
| 24 if not method then -- No path specified, default to "" (base path) | |
| 25 method, path = key, ""; | |
| 26 end | |
| 27 if method:sub(1,1) == "/" then | |
| 28 return nil; | |
| 29 end | |
| 30 return method:upper().." "..host..app_path..path; | |
| 31 end | |
| 32 | |
| 33 local function get_base_path(host_module, app_name, default_app_path) | |
| 34 return host_module:get_option("http_paths", {})[app_name] -- Host | |
| 35 or module:get_option("http_paths", {})[app_name] -- Global | |
| 36 or default_app_path; -- Default | |
| 37 end | |
| 38 | |
| 39 function module.add_host(module) | |
| 40 local host = module.host; | |
| 41 local apps = {}; | |
| 42 module.environment.apps = apps; | |
| 43 local function http_app_added(event) | |
| 44 local app_name = event.item.name; | |
| 45 local default_app_path = event.item.default_path or "/"..app_name; | |
| 46 local app_path = normalize_path(get_base_path(module, app_name, default_app_path)); | |
| 47 if not app_name then | |
| 48 -- TODO: Link to docs | |
| 49 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)"); | |
| 50 return; | |
| 51 end | |
| 52 apps[app_name] = apps[app_name] or {}; | |
| 53 local app_handlers = apps[app_name]; | |
| 54 for key, handler in pairs(event.item.route or {}) do | |
| 55 local event_name = get_http_event(host, app_path, key); | |
| 56 if event_name then | |
| 57 if type(handler) ~= "function" then | |
| 58 local data = handler; | |
| 59 handler = function () return data; end | |
| 60 elseif event_name:sub(-2, -1) == "/*" then | |
| 61 local base_path = event_name:match("/(.+)/*$"); | |
| 62 local _handler = handler; | |
| 63 handler = function (event) | |
| 64 local path = event.request.path:sub(#base_path+1); | |
| 65 return _handler(event, path); | |
| 66 end; | |
| 67 end | |
| 68 if not app_handlers[event_name] then | |
| 69 app_handlers[event_name] = handler; | |
| 70 module:hook_object_event(server, event_name, handler); | |
| 71 else | |
| 72 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name); | |
| 73 end | |
| 74 else | |
| 75 module:log("error", "Invalid route in %s, %q. See http://prosody.im/doc/developers/http#routes", app_name, key); | |
| 76 end | |
| 77 end | |
| 78 end | |
| 79 | |
| 80 local function http_app_removed(event) | |
| 81 local app_handlers = apps[event.item.name]; | |
| 82 apps[event.item.name] = nil; | |
| 83 for event, handler in pairs(app_handlers) do | |
| 84 module:unhook_object_event(server, event, handler); | |
| 85 end | |
| 86 end | |
| 87 | |
| 88 module:handle_items("http-provider", http_app_added, http_app_removed); | |
| 89 | |
| 90 server.add_host(host); | |
| 91 function module.unload() | |
| 92 server.remove_host(host); | |
| 93 end | |
| 94 end | |
| 95 | |
| 96 module:add_item("net-provider", { | |
| 97 name = "http"; | |
| 98 listener = server.listener; | |
| 99 default_port = 5280; | |
| 100 multiplex = { | |
| 101 pattern = "^[A-Z]"; | |
| 102 }; | |
| 103 }); | |
| 104 | |
| 105 module:add_item("net-provider", { | |
| 106 name = "https"; | |
| 107 listener = server.listener; | |
| 108 default_port = 5281; | |
| 109 encryption = "ssl"; | |
| 110 multiplex = { | |
| 111 pattern = "^[A-Z]"; | |
| 112 }; | |
| 113 }); |
