annotate mod_register_json/mod_register_json.lua @ 392:369e665ae471

mod_muc_log_http: Show proper time period in the headline.
author Waqas Hussain <waqas20@gmail.com>
date Mon, 25 Jul 2011 02:21:40 +0500
parents 16da8cd69715
children ea6641deec12
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
1 -- Expose a simple servlet to handle user registrations from web pages
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
2 -- via JSON.
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
3 --
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
4 -- A Good chunk of the code is from mod_data_access.lua by Kim Alvefur
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
5 -- aka Zash.
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
6
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
7 local jid_prep = require "util.jid".prep;
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
8 local jid_split = require "util.jid".split;
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
9 local usermanager = require "core.usermanager";
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
10 local b64_decode = require "util.encodings".base64.decode;
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
11 local json_decode = require "util.json".decode;
370
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
12 local httpserver = require "net.httpserver";
364
1edd3f7c749c mod_register_json: fixed missing declared variable.
Marco Cirillo <maranda@lightwitch.org>
parents: 363
diff changeset
13 local os_time = os.time;
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
14
357
59345fd38ad9 mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents: 356
diff changeset
15 module.host = "*" -- HTTP/BOSH Servlets need to be global.
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
16
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
17 -- Pick up configuration.
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
18
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
19 local set_realm_name = module:get_option("reg_servlet_realm") or "Restricted";
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
20 local throttle_time = module:get_option("reg_servlet_ttime") or false;
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
21 local whitelist = module:get_option("reg_servlet_wl") or {};
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
22 local blacklist = module:get_option("reg_servlet_bl") or {};
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
23 local recent_ips = {};
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
24
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
25 -- Begin
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
26
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
27 for _, ip in ipairs(whitelist) do whitelisted_ips[ip] = true; end
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
28 for _, ip in ipairs(blacklist) do blacklisted_ips[ip] = true; end
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
29
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
30 local function http_response(code, message, extra_headers)
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
31 local response = {
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
32 status = code .. " " .. message;
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
33 body = message .. "\n"; }
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
34 if extra_headers then response.headers = extra_headers; end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
35 return response
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
36 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
37
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
38 local function handle_req(method, body, request)
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
39 if request.method ~= "POST" then
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
40 return http_response(405, "Bad method...", {["Allow"] = "POST"});
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
41 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
42 if not request.headers["authorization"] then
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
43 return http_response(401, "No... No...",
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
44 {["WWW-Authenticate"]='Basic realm="'.. set_realm_name ..'"'})
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
45 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
46
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
47 local user, password = b64_decode(request.headers.authorization
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
48 :match("[^ ]*$") or ""):match("([^:]*):(.*)");
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
49 user = jid_prep(user);
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
50 if not user or not password then return http_response(400, "What's this..?"); end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
51 local user_node, user_host = jid_split(user)
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
52 if not hosts[user_host] then return http_response(401, "Negative."); end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
53
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
54 module:log("warn", "%s is authing to submit a new user registration data", user)
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
55 if not usermanager.test_password(user_node, user_host, password) then
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
56 module:log("warn", "%s failed authentication", user)
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
57 return http_response(401, "Who the hell are you?! Guards!");
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
58 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
59
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
60 local req_body;
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
61 -- We check that what we have is valid JSON wise else we throw an error...
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
62 if not pcall(function() req_body = json_decode(body) end) then
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
63 module:log("debug", "JSON data submitted for user registration by %s failed to Decode.", user);
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
64 return http_response(400, "JSON Decoding failed.");
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
65 else
363
72a5778579c5 mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents: 362
diff changeset
66 -- Decode JSON data and check that all bits are there else throw an error
72a5778579c5 mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents: 362
diff changeset
67 req_body = json_decode(body);
72a5778579c5 mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents: 362
diff changeset
68 if req_body["username"] == nil or req_body["password"] == nil or req_body["host"] == nil or req_body["ip"] == nil then
72a5778579c5 mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents: 362
diff changeset
69 module:log("debug", "%s supplied an insufficent number of elements or wrong elements for the JSON registration", user);
72a5778579c5 mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents: 362
diff changeset
70 return http_response(400, "Invalid syntax.");
72a5778579c5 mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents: 362
diff changeset
71 end
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
72 -- Check if user is an admin of said host
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
73 if not usermanager.is_admin(user, req_body["host"]) then
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
74 module:log("warn", "%s tried to submit registration data for %s but he's not an admin", user, req_body["host"]);
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
75 return http_response(401, "I obey only to my masters... Have a nice day.");
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
76 else
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
77 -- Checks for both Throttling/Whitelist and Blacklist (basically copycatted from prosody's register.lua code)
362
bd0a8c032163 mod_register_json: Typo fix.
Marco Cirillo <maranda@lightwitch.org>
parents: 361
diff changeset
78 if blacklist[req_body["ip"]] then module:log("warn", "Attempt of reg. submission to the JSON servlet from blacklisted address: %s", req_body["ip"]); return http_response(403, "The specified address is blacklisted, sorry sorry."); end
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
79 if throttle_time and not whitelist[req_body["ip"]] then
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
80 if not recent_ips[req_body["ip"]] then
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
81 recent_ips[req_body["ip"]] = { time = os_time(), count = 1 };
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
82 else
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
83 local ip = recent_ips[req_body["ip"]];
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
84 ip.count = ip.count + 1;
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
85
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
86 if os_time() - ip.time < throttle_time then
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
87 ip.time = os_time();
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
88 module:log("warn", "JSON Registration request from %s has been throttled.", req_body["ip"]);
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
89 return http_response(503, "Woah... How many users you want to register..? Request throttled, wait a bit and try again.");
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
90 end
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
91 ip.time = os_time();
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
92 end
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
93 end
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
94
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
95 -- We first check if the supplied username for registration is already there.
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
96 if not usermanager.user_exists(req_body["username"], req_body["host"]) then
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
97 usermanager.create_user(req_body["username"], req_body["password"], req_body["host"]);
365
1d95b69c2c81 mod_register_json: Corrected typo.
Marco Cirillo <maranda@lightwitch.org>
parents: 364
diff changeset
98 module:log("debug", "%s registration data submission for %s is successful", user, req_body["username"]);
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
99 return http_response(200, "Done.");
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
100 else
365
1d95b69c2c81 mod_register_json: Corrected typo.
Marco Cirillo <maranda@lightwitch.org>
parents: 364
diff changeset
101 module:log("debug", "%s registration data submission for %s failed (user already exists)", user, req_body["username"]);
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
102 return http_response(409, "User already exists.");
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
103 end
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
104 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
105 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
106 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
107
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
108 -- Set it up!
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
109 local function setup()
370
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
110 local ports = module:get_option("reg_servlet_ports") or { 9280 };
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
111 local port_number, base_name, ssl_table;
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
112 for _, opts in ipairs(ports) do
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
113 if type(opts) == "number" then
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
114 port_number, base_name = opts, "register_account";
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
115 elseif type(opts) == "table" then
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
116 port_number, base_name, ssl_table = opts.port or 9280, opts.path or "register_account", opts.ssl or nil;
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
117 elseif type(opts) == "string" then
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
118 base_name, port_number = opts, 9280;
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
119 end
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
120 end
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
121
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
122 if ssl_table == nil then
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
123 ports = { { port = port_number } };
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
124 httpserver.new_from_config(ports, handle_req, { base = base_name });
369
29a8828243ce mod_register_json: Fixed http listener creation syntax. (Please document that in the API, that would avoid my brain overheating, thank you.)
Marco Cirillo <maranda@lightwitch.org>
parents: 365
diff changeset
125 else
370
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
126 if port_number == 9280 then port_number = 9443; end
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
127 ports = { { port = port_number, ssl = ssl_table } };
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
128 httpserver.new_from_config(ports, handle_req, { base = base_name });
357
59345fd38ad9 mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents: 356
diff changeset
129 end
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
130 end
369
29a8828243ce mod_register_json: Fixed http listener creation syntax. (Please document that in the API, that would avoid my brain overheating, thank you.)
Marco Cirillo <maranda@lightwitch.org>
parents: 365
diff changeset
131
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
132 if prosody.start_time then -- already started
369
29a8828243ce mod_register_json: Fixed http listener creation syntax. (Please document that in the API, that would avoid my brain overheating, thank you.)
Marco Cirillo <maranda@lightwitch.org>
parents: 365
diff changeset
133 setup();
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
134 else
369
29a8828243ce mod_register_json: Fixed http listener creation syntax. (Please document that in the API, that would avoid my brain overheating, thank you.)
Marco Cirillo <maranda@lightwitch.org>
parents: 365
diff changeset
135 prosody.events.add_handler("server-started", setup);
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
136 end