From 8d2bb74a6a3e34e09c0361d5f22e6df66c51c74d Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sat, 21 Apr 2012 17:38:48 +0500 Subject: mod_http: Provide HTTP service. --- plugins/mod_http.lua | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 plugins/mod_http.lua (limited to 'plugins/mod_http.lua') diff --git a/plugins/mod_http.lua b/plugins/mod_http.lua new file mode 100644 index 00000000..4713cf13 --- /dev/null +++ b/plugins/mod_http.lua @@ -0,0 +1,69 @@ +-- Prosody IM +-- Copyright (C) 2008-2010 Matthew Wild +-- Copyright (C) 2008-2010 Waqas Hussain +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + +module:set_global(); + +--local sessions = module:shared("sessions"); + +--[[function listener.associate_session(conn, session) + sessions[conn] = session; +end]] + +local handlers; + +function build_handlers() + handlers = {}; + for _, item in ipairs(module:get_host_items("http-handler")) do + local previous = handlers[item.path]; + if not previous and item.path then + handlers[item.path] = item; + end + end +end +function clear_handlers() + handlers = nil; +end +module:handle_items("http-handler", clear_handlers, clear_handlers, false); + +function http_handler(event) + local request, response = event.request, event.response; + + if not handlers then build_handlers(); end + local item = handlers[request.path:match("[^?]*")]; + local handler = item and item.handler; + if handler then + handler(request, response); + return true; + end +end + +local server = require "net.http.server"; +local listener = server.listener; +server.add_handler("*", http_handler); +function module.unload() + server.remove_handler("*", http_handler); +end +--require "net.http.server".listen_on(8080); + +module:add_item("net-provider", { + name = "http"; + listener = listener; + default_port = 5280; + multiplex = { + pattern = "^[A-Z]"; + }; +}); + +module:add_item("net-provider", { + name = "https"; + listener = listener; + encryption = "ssl"; + multiplex = { + pattern = "^[A-Z]"; + }; +}); -- cgit v1.2.3 From 4ce0cecfe4dbd9e1bfe71a16c81478c574070645 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sat, 21 Apr 2012 18:23:44 +0500 Subject: mod_http: Include handlers of non-global modules. --- plugins/mod_http.lua | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'plugins/mod_http.lua') diff --git a/plugins/mod_http.lua b/plugins/mod_http.lua index 4713cf13..24ef8ab3 100644 --- a/plugins/mod_http.lua +++ b/plugins/mod_http.lua @@ -14,28 +14,44 @@ module:set_global(); sessions[conn] = session; end]] -local handlers; +local NULL = {}; +local handlers = {}; -function build_handlers() - handlers = {}; - for _, item in ipairs(module:get_host_items("http-handler")) do - local previous = handlers[item.path]; - if not previous and item.path then - handlers[item.path] = item; +function build_handlers(host) + if not hosts[host] then return; end + local h = {}; + handlers[host] = h; + + for mod_name, module in pairs(modulemanager.get_modules(host)) do + module = module.module; + if module.items then + for _, item in ipairs(module.items["http-handler"] or NULL) do + local previous = handlers[item.path]; + if not previous and item.path then + h[item.path] = item; + end + end end end + + return h; +end +function clear_handlers(event) + handlers[event.source.host] = nil; end -function clear_handlers() - handlers = nil; +function get_handler(host, path) + local h = handlers[host] or build_handlers(host); + if h then + local item = h[path]; + return item and item.handler; + end end module:handle_items("http-handler", clear_handlers, clear_handlers, false); function http_handler(event) local request, response = event.request, event.response; - if not handlers then build_handlers(); end - local item = handlers[request.path:match("[^?]*")]; - local handler = item and item.handler; + local handler = get_handler(request.headers.host:match("[^:]*"):lower(), request.path:match("[^?]*")); if handler then handler(request, response); return true; -- cgit v1.2.3 From c51dcd332fe2e74486fffa2cb3e7b9851813b544 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 23 Apr 2012 14:16:59 +0100 Subject: mod_http: Revamp module for new API and config --- plugins/mod_http.lua | 106 ++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 52 deletions(-) (limited to 'plugins/mod_http.lua') diff --git a/plugins/mod_http.lua b/plugins/mod_http.lua index 24ef8ab3..d8e1e10a 100644 --- a/plugins/mod_http.lua +++ b/plugins/mod_http.lua @@ -1,6 +1,6 @@ -- Prosody IM --- Copyright (C) 2008-2010 Matthew Wild --- Copyright (C) 2008-2010 Waqas Hussain +-- Copyright (C) 2008-2012 Matthew Wild +-- Copyright (C) 2008-2012 Waqas Hussain -- -- This project is MIT/X11 licensed. Please see the -- COPYING file in the source package for more information. @@ -8,67 +8,68 @@ module:set_global(); ---local sessions = module:shared("sessions"); +local parse_url = require "socket.url".parse; +local server = require "net.http.server"; ---[[function listener.associate_session(conn, session) - sessions[conn] = session; -end]] +local function normalize_path(path) + if path:sub(1,1) ~= "/" then path = "/"..path; end + if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end + return path; +end -local NULL = {}; -local handlers = {}; +local function get_http_event(http_module, app_name, key) + local method, path = key:match("^(%S+)%s+(.+)$"); + if not method and key:sub(1,1) == "/" then + method, path = "GET", key; + else + return nil; + end + path = normalize_path(path); + local app_path = normalize_path(http_module:get_option_string(app_name.."_http_path", "/"..app_name)); + return method:upper().." "..http_module.host..app_path..path; +end -function build_handlers(host) - if not hosts[host] then return; end - local h = {}; - handlers[host] = h; - - for mod_name, module in pairs(modulemanager.get_modules(host)) do - module = module.module; - if module.items then - for _, item in ipairs(module.items["http-handler"] or NULL) do - local previous = handlers[item.path]; - if not previous and item.path then - h[item.path] = item; +function module.add_host(module) + local apps = {}; + module.environment.apps = apps; + local function http_app_added(event) + local app_name = event.item.name; + if not app_name then + -- TODO: Link to docs + module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)"); + return; + end + apps[app_name] = apps[app_name] or {}; + local app_handlers = apps[app_name]; + for key, handler in pairs(event.item.route or {}) do + local event_name = get_http_event(module, app_name, key); + if event_name then + if not app_handlers[event_name] then + app_handlers[event_name] = handler; + server.add_handler(event_name, handler); + else + module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name); end + else + module:log("error", "Invalid route in %s: %q", app_name, key); end end end - - return h; -end -function clear_handlers(event) - handlers[event.source.host] = nil; -end -function get_handler(host, path) - local h = handlers[host] or build_handlers(host); - if h then - local item = h[path]; - return item and item.handler; - end -end -module:handle_items("http-handler", clear_handlers, clear_handlers, false); - -function http_handler(event) - local request, response = event.request, event.response; - - local handler = get_handler(request.headers.host:match("[^:]*"):lower(), request.path:match("[^?]*")); - if handler then - handler(request, response); - return true; + + local function http_app_removed(event) + local app_handlers = apps[event.item.name]; + apps[event.item.name] = nil; + for event, handler in pairs(app_handlers) do + server.remove_handler(event, handler); + end end + + module:handle_items("http-provider", http_app_added, http_app_removed); end -local server = require "net.http.server"; -local listener = server.listener; -server.add_handler("*", http_handler); -function module.unload() - server.remove_handler("*", http_handler); -end ---require "net.http.server".listen_on(8080); - module:add_item("net-provider", { name = "http"; - listener = listener; + listener = server.listener; default_port = 5280; multiplex = { pattern = "^[A-Z]"; @@ -77,7 +78,8 @@ module:add_item("net-provider", { module:add_item("net-provider", { name = "https"; - listener = listener; + listener = server.listener; + default_port = 5281; encryption = "ssl"; multiplex = { pattern = "^[A-Z]"; -- cgit v1.2.3 From f28df31a1bdc6546b271c67f2b11185f9fc39583 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 23 Apr 2012 18:23:49 +0100 Subject: mod_http: Support for default_path in apps --- plugins/mod_http.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'plugins/mod_http.lua') diff --git a/plugins/mod_http.lua b/plugins/mod_http.lua index d8e1e10a..cd1984c0 100644 --- a/plugins/mod_http.lua +++ b/plugins/mod_http.lua @@ -17,7 +17,7 @@ local function normalize_path(path) return path; end -local function get_http_event(http_module, app_name, key) +local function get_http_event(host, app_path, key) local method, path = key:match("^(%S+)%s+(.+)$"); if not method and key:sub(1,1) == "/" then method, path = "GET", key; @@ -25,15 +25,17 @@ local function get_http_event(http_module, app_name, key) return nil; end path = normalize_path(path); - local app_path = normalize_path(http_module:get_option_string(app_name.."_http_path", "/"..app_name)); - return method:upper().." "..http_module.host..app_path..path; + return method:upper().." "..host..app_path..path; end function module.add_host(module) + local host = module.host; local apps = {}; module.environment.apps = apps; local function http_app_added(event) local app_name = event.item.name; + local default_app_path = event.item.default_path or "/"..app_name; + local app_path = normalize_path(module:get_option_string(app_name.."_http_path", default_app_path)); if not app_name then -- TODO: Link to docs module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)"); @@ -42,7 +44,7 @@ function module.add_host(module) apps[app_name] = apps[app_name] or {}; local app_handlers = apps[app_name]; for key, handler in pairs(event.item.route or {}) do - local event_name = get_http_event(module, app_name, key); + local event_name = get_http_event(host, app_path, key); if event_name then if not app_handlers[event_name] then app_handlers[event_name] = handler; -- cgit v1.2.3 From a3ceb27baef9490381162c206e89b7d2f67ef3c7 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 23 Apr 2012 21:34:05 +0100 Subject: mod_http: Pass portion of path that matched wildcard to wildcard handlers, as a second parameter --- plugins/mod_http.lua | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'plugins/mod_http.lua') diff --git a/plugins/mod_http.lua b/plugins/mod_http.lua index cd1984c0..3dcb14d7 100644 --- a/plugins/mod_http.lua +++ b/plugins/mod_http.lua @@ -46,6 +46,14 @@ function module.add_host(module) for key, handler in pairs(event.item.route or {}) do local event_name = get_http_event(host, app_path, key); if event_name then + if event_name:sub(-2, -1) == "/*" then + local base_path = event_name:match("/(.+)/*$"); + local _handler = handler; + handler = function (event) + local path = event.request.path:sub(#base_path+1); + return _handler(event, path); + end; + end if not app_handlers[event_name] then app_handlers[event_name] = handler; server.add_handler(event_name, handler); -- cgit v1.2.3 From 904d33cce45285aeb581b4b22cea418204ccbbed Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 24 Apr 2012 16:02:30 +0100 Subject: mod_http: Fix specifying method in app route keys --- plugins/mod_http.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'plugins/mod_http.lua') diff --git a/plugins/mod_http.lua b/plugins/mod_http.lua index 3dcb14d7..6da4db24 100644 --- a/plugins/mod_http.lua +++ b/plugins/mod_http.lua @@ -19,10 +19,11 @@ end local function get_http_event(host, app_path, key) local method, path = key:match("^(%S+)%s+(.+)$"); - if not method and key:sub(1,1) == "/" then + if not method then + if key:sub(1,1) ~= "/" then + return nil; + end method, path = "GET", key; - else - return nil; end path = normalize_path(path); return method:upper().." "..host..app_path..path; -- cgit v1.2.3