Annotate

plugins/mod_http.lua @ 4724:a8c234332258

mod_http: Allow a route value to be static data rather than a handler function
author Matthew Wild <mwild1@gmail.com>
date Fri, 27 Apr 2012 19:02:36 +0100
parent 4721:1c6c4c53f08a
child 4736:3514338c59c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4635
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 -- Prosody IM
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
2 -- Copyright (C) 2008-2012 Matthew Wild
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
3 -- Copyright (C) 2008-2012 Waqas Hussain
4635
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 module:set_global();
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
11 local server = require "net.http.server";
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
12
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
13 local function normalize_path(path)
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
14 if path:sub(1,1) ~= "/" then path = "/"..path; end
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
15 if path:sub(-1,-1) == "/" then path = path:sub(1, -2); end
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
16 return path;
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
17 end
4635
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18
4667
d0cfc49f3f2b mod_http: Support for default_path in apps
Matthew Wild <mwild1@gmail.com>
parents: 4664
diff changeset
19 local function get_http_event(host, app_path, key)
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
20 local method, path = key:match("^(%S+)%s+(.+)$");
4721
1c6c4c53f08a mod_http: Routes now require a method to be specified, but the path has become optional (defaults to the base path with no trailing '/'
Matthew Wild <mwild1@gmail.com>
parents: 4720
diff changeset
21 if not method then -- No path specified, default to "" (base path)
1c6c4c53f08a mod_http: Routes now require a method to be specified, but the path has become optional (defaults to the base path with no trailing '/'
Matthew Wild <mwild1@gmail.com>
parents: 4720
diff changeset
22 method, path = key, "";
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
23 end
4721
1c6c4c53f08a mod_http: Routes now require a method to be specified, but the path has become optional (defaults to the base path with no trailing '/'
Matthew Wild <mwild1@gmail.com>
parents: 4720
diff changeset
24 if method:sub(1,1) == "/" then
1c6c4c53f08a mod_http: Routes now require a method to be specified, but the path has become optional (defaults to the base path with no trailing '/'
Matthew Wild <mwild1@gmail.com>
parents: 4720
diff changeset
25 return nil;
1c6c4c53f08a mod_http: Routes now require a method to be specified, but the path has become optional (defaults to the base path with no trailing '/'
Matthew Wild <mwild1@gmail.com>
parents: 4720
diff changeset
26 end
4667
d0cfc49f3f2b mod_http: Support for default_path in apps
Matthew Wild <mwild1@gmail.com>
parents: 4664
diff changeset
27 return method:upper().." "..host..app_path..path;
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
28 end
4635
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29
4702
5a85e541de1a mod_http: Switch to single option for specifying HTTP app bases, http_paths. Keys are app/module names, values are base paths.
Matthew Wild <mwild1@gmail.com>
parents: 4696
diff changeset
30 local function get_base_path(host_module, app_name, default_app_path)
5a85e541de1a mod_http: Switch to single option for specifying HTTP app bases, http_paths. Keys are app/module names, values are base paths.
Matthew Wild <mwild1@gmail.com>
parents: 4696
diff changeset
31 return host_module:get_option("http_paths", {})[app_name] -- Host
5a85e541de1a mod_http: Switch to single option for specifying HTTP app bases, http_paths. Keys are app/module names, values are base paths.
Matthew Wild <mwild1@gmail.com>
parents: 4696
diff changeset
32 or module:get_option("http_paths", {})[app_name] -- Global
5a85e541de1a mod_http: Switch to single option for specifying HTTP app bases, http_paths. Keys are app/module names, values are base paths.
Matthew Wild <mwild1@gmail.com>
parents: 4696
diff changeset
33 or default_app_path; -- Default
5a85e541de1a mod_http: Switch to single option for specifying HTTP app bases, http_paths. Keys are app/module names, values are base paths.
Matthew Wild <mwild1@gmail.com>
parents: 4696
diff changeset
34 end
5a85e541de1a mod_http: Switch to single option for specifying HTTP app bases, http_paths. Keys are app/module names, values are base paths.
Matthew Wild <mwild1@gmail.com>
parents: 4696
diff changeset
35
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
36 function module.add_host(module)
4667
d0cfc49f3f2b mod_http: Support for default_path in apps
Matthew Wild <mwild1@gmail.com>
parents: 4664
diff changeset
37 local host = module.host;
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
38 local apps = {};
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
39 module.environment.apps = apps;
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
40 local function http_app_added(event)
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
41 local app_name = event.item.name;
4667
d0cfc49f3f2b mod_http: Support for default_path in apps
Matthew Wild <mwild1@gmail.com>
parents: 4664
diff changeset
42 local default_app_path = event.item.default_path or "/"..app_name;
4702
5a85e541de1a mod_http: Switch to single option for specifying HTTP app bases, http_paths. Keys are app/module names, values are base paths.
Matthew Wild <mwild1@gmail.com>
parents: 4696
diff changeset
43 local app_path = normalize_path(get_base_path(module, app_name, default_app_path));
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
44 if not app_name then
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
45 -- TODO: Link to docs
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
46 module:log("error", "HTTP app has no 'name', add one or use module:provides('http', app)");
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
47 return;
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
48 end
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
49 apps[app_name] = apps[app_name] or {};
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
50 local app_handlers = apps[app_name];
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
51 for key, handler in pairs(event.item.route or {}) do
4667
d0cfc49f3f2b mod_http: Support for default_path in apps
Matthew Wild <mwild1@gmail.com>
parents: 4664
diff changeset
52 local event_name = get_http_event(host, app_path, key);
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
53 if event_name then
4724
a8c234332258 mod_http: Allow a route value to be static data rather than a handler function
Matthew Wild <mwild1@gmail.com>
parents: 4721
diff changeset
54 if type(handler) ~= "function" then
a8c234332258 mod_http: Allow a route value to be static data rather than a handler function
Matthew Wild <mwild1@gmail.com>
parents: 4721
diff changeset
55 local data = handler;
a8c234332258 mod_http: Allow a route value to be static data rather than a handler function
Matthew Wild <mwild1@gmail.com>
parents: 4721
diff changeset
56 handler = function () return data; end
a8c234332258 mod_http: Allow a route value to be static data rather than a handler function
Matthew Wild <mwild1@gmail.com>
parents: 4721
diff changeset
57 elseif event_name:sub(-2, -1) == "/*" then
4669
0e0a72679f77 mod_http: Pass portion of path that matched wildcard to wildcard handlers, as a second parameter
Matthew Wild <mwild1@gmail.com>
parents: 4667
diff changeset
58 local base_path = event_name:match("/(.+)/*$");
0e0a72679f77 mod_http: Pass portion of path that matched wildcard to wildcard handlers, as a second parameter
Matthew Wild <mwild1@gmail.com>
parents: 4667
diff changeset
59 local _handler = handler;
0e0a72679f77 mod_http: Pass portion of path that matched wildcard to wildcard handlers, as a second parameter
Matthew Wild <mwild1@gmail.com>
parents: 4667
diff changeset
60 handler = function (event)
0e0a72679f77 mod_http: Pass portion of path that matched wildcard to wildcard handlers, as a second parameter
Matthew Wild <mwild1@gmail.com>
parents: 4667
diff changeset
61 local path = event.request.path:sub(#base_path+1);
0e0a72679f77 mod_http: Pass portion of path that matched wildcard to wildcard handlers, as a second parameter
Matthew Wild <mwild1@gmail.com>
parents: 4667
diff changeset
62 return _handler(event, path);
0e0a72679f77 mod_http: Pass portion of path that matched wildcard to wildcard handlers, as a second parameter
Matthew Wild <mwild1@gmail.com>
parents: 4667
diff changeset
63 end;
0e0a72679f77 mod_http: Pass portion of path that matched wildcard to wildcard handlers, as a second parameter
Matthew Wild <mwild1@gmail.com>
parents: 4667
diff changeset
64 end
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
65 if not app_handlers[event_name] then
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
66 app_handlers[event_name] = handler;
4696
4700e318add1 mod_http: Use module:hook/unhook_event_object() so that handlers get unregistered if mod_http is unloaded
Matthew Wild <mwild1@gmail.com>
parents: 4678
diff changeset
67 module:hook_object_event(server, event_name, handler);
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
68 else
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
69 module:log("warn", "App %s added handler twice for '%s', ignoring", app_name, event_name);
4636
41983ec223f0 mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents: 4635
diff changeset
70 end
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
71 else
4720
fddc2797a96a mod_http: Link to docs on routes in error message
Matthew Wild <mwild1@gmail.com>
parents: 4717
diff changeset
72 module:log("error", "Invalid route in %s, %q. See http://prosody.im/doc/developers/http#routes", app_name, key);
4636
41983ec223f0 mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents: 4635
diff changeset
73 end
4635
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 end
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 end
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
76
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
77 local function http_app_removed(event)
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
78 local app_handlers = apps[event.item.name];
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
79 apps[event.item.name] = nil;
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
80 for event, handler in pairs(app_handlers) do
4696
4700e318add1 mod_http: Use module:hook/unhook_event_object() so that handlers get unregistered if mod_http is unloaded
Matthew Wild <mwild1@gmail.com>
parents: 4678
diff changeset
81 module:unhook_object_event(server, event, handler);
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
82 end
4636
41983ec223f0 mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents: 4635
diff changeset
83 end
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
84
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
85 module:handle_items("http-provider", http_app_added, http_app_removed);
4635
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 end
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 module:add_item("net-provider", {
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 name = "http";
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
90 listener = server.listener;
4635
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 default_port = 5280;
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 multiplex = {
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 pattern = "^[A-Z]";
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 };
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 });
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 module:add_item("net-provider", {
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 name = "https";
4664
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
99 listener = server.listener;
7438b3c68576 mod_http: Revamp module for new API and config
Matthew Wild <mwild1@gmail.com>
parents: 4636
diff changeset
100 default_port = 5281;
4635
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 encryption = "ssl";
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 multiplex = {
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 pattern = "^[A-Z]";
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 };
ea5215bd2783 mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 });