Mercurial > prosody-hg
comparison plugins/mod_http.lua @ 4684:dc70c4ffb66d
Merge timber->trunk - thanks everyone!
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 24 Apr 2012 21:59:20 +0100 |
| parents | 9613673f916a |
| children | 4700e318add1 |
comparison
equal
deleted
inserted
replaced
| 4529:12621337471f | 4684:dc70c4ffb66d |
|---|---|
| 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 | |
| 11 local parse_url = require "socket.url".parse; | |
| 12 local server = require "net.http.server"; | |
| 13 | |
| 14 local function normalize_path(path) | |
| 15 if path:sub(1,1) ~= "/" then path = "/"..path; end | |
| 16 if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end | |
| 17 return path; | |
| 18 end | |
| 19 | |
| 20 local function get_http_event(host, app_path, key) | |
| 21 local method, path = key:match("^(%S+)%s+(.+)$"); | |
| 22 if not method then | |
| 23 if key:sub(1,1) ~= "/" then | |
| 24 return nil; | |
| 25 end | |
| 26 method, path = "GET", key; | |
| 27 end | |
| 28 path = normalize_path(path); | |
| 29 return method:upper().." "..host..app_path..path; | |
| 30 end | |
| 31 | |
| 32 function module.add_host(module) | |
| 33 local host = module.host; | |
| 34 local apps = {}; | |
| 35 module.environment.apps = apps; | |
| 36 local function http_app_added(event) | |
| 37 local app_name = event.item.name; | |
| 38 local default_app_path = event.item.default_path or "/"..app_name; | |
| 39 local app_path = normalize_path(module:get_option_string(app_name.."_http_path", default_app_path)); | |
| 40 if not app_name then | |
| 41 -- TODO: Link to docs | |
| 42 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)"); | |
| 43 return; | |
| 44 end | |
| 45 apps[app_name] = apps[app_name] or {}; | |
| 46 local app_handlers = apps[app_name]; | |
| 47 for key, handler in pairs(event.item.route or {}) do | |
| 48 local event_name = get_http_event(host, app_path, key); | |
| 49 if event_name then | |
| 50 if event_name:sub(-2, -1) == "/*" then | |
| 51 local base_path = event_name:match("/(.+)/*$"); | |
| 52 local _handler = handler; | |
| 53 handler = function (event) | |
| 54 local path = event.request.path:sub(#base_path+1); | |
| 55 return _handler(event, path); | |
| 56 end; | |
| 57 end | |
| 58 if not app_handlers[event_name] then | |
| 59 app_handlers[event_name] = handler; | |
| 60 server.add_handler(event_name, handler); | |
| 61 else | |
| 62 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name); | |
| 63 end | |
| 64 else | |
| 65 module:log("error", "Invalid route in %s: %q", app_name, key); | |
| 66 end | |
| 67 end | |
| 68 end | |
| 69 | |
| 70 local function http_app_removed(event) | |
| 71 local app_handlers = apps[event.item.name]; | |
| 72 apps[event.item.name] = nil; | |
| 73 for event, handler in pairs(app_handlers) do | |
| 74 server.remove_handler(event, handler); | |
| 75 end | |
| 76 end | |
| 77 | |
| 78 module:handle_items("http-provider", http_app_added, http_app_removed); | |
| 79 end | |
| 80 | |
| 81 module:add_item("net-provider", { | |
| 82 name = "http"; | |
| 83 listener = server.listener; | |
| 84 default_port = 5280; | |
| 85 multiplex = { | |
| 86 pattern = "^[A-Z]"; | |
| 87 }; | |
| 88 }); | |
| 89 | |
| 90 module:add_item("net-provider", { | |
| 91 name = "https"; | |
| 92 listener = server.listener; | |
| 93 default_port = 5281; | |
| 94 encryption = "ssl"; | |
| 95 multiplex = { | |
| 96 pattern = "^[A-Z]"; | |
| 97 }; | |
| 98 }); |
