Mercurial > prosody-modules
comparison mod_register_json/register_json/mod_register_json.lua @ 991:929dcf3c4bcb
mod_register_json: small code cleanup & optimization.
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Mon, 29 Apr 2013 23:39:21 +0200 |
parents | 7c04c5856daa |
children | 794817421fc6 |
comparison
equal
deleted
inserted
replaced
990:17ba2c59d661 | 991:929dcf3c4bcb |
---|---|
85 response.status_code = code | 85 response.status_code = code |
86 response:send(handle(code, message)) | 86 response:send(handle(code, message)) |
87 end | 87 end |
88 | 88 |
89 local function handle_req(event) | 89 local function handle_req(event) |
90 local response = event.response | |
91 local request = event.request | 90 local request = event.request |
92 local body = request.body | |
93 if secure and not request.secure then return nil end | 91 if secure and not request.secure then return nil end |
94 | 92 |
95 if request.method ~= "POST" then | 93 if request.method ~= "POST" then |
96 return http_response(event, 405, "Bad method.", {["Allow"] = "POST"}) | 94 return http_response(event, 405, "Bad method.", {["Allow"] = "POST"}) |
97 end | 95 end |
98 | 96 |
99 local req_body | 97 local req_body |
100 -- We check that what we have is valid JSON wise else we throw an error... | 98 -- We check that what we have is valid JSON wise else we throw an error... |
101 if not pcall(function() req_body = json_decode(b64_decode(body)) end) then | 99 if not pcall(function() req_body = json_decode(b64_decode(request.body)) end) then |
102 module:log("debug", "Data submitted for user registration by %s failed to Decode.", user) | 100 module:log("debug", "Data submitted for user registration by %s failed to Decode.", user) |
103 return http_response(event, 400, "Decoding failed.") | 101 return http_response(event, 400, "Decoding failed.") |
104 else | 102 else |
105 -- Decode JSON data and check that all bits are there else throw an error | 103 -- Decode JSON data and check that all bits are there else throw an error |
106 if req_body["username"] == nil or req_body["password"] == nil or req_body["ip"] == nil or req_body["mail"] == nil or | 104 if req_body["username"] == nil or req_body["password"] == nil or req_body["ip"] == nil or req_body["mail"] == nil or |
187 local data = f:read("*a") ; f:close() | 185 local data = f:read("*a") ; f:close() |
188 return data | 186 return data |
189 end | 187 end |
190 | 188 |
191 local function r_template(event, type) | 189 local function r_template(event, type) |
192 local response = event.response | |
193 | |
194 local data = open_file(files_base..type.."_t.html") | 190 local data = open_file(files_base..type.."_t.html") |
195 if data then | 191 if data then |
196 data = data:gsub("%%REG%-URL", base_path.."verify/") | 192 data = data:gsub("%%REG%-URL", base_path.."verify/") |
197 return data | 193 return data |
198 else return http_response(event, 500, "Failed to obtain template.") end | 194 else return http_response(event, 500, "Failed to obtain template.") end |
199 end | 195 end |
200 | 196 |
201 local function handle_verify(event, path) | 197 local function handle_verify(event, path) |
202 local response = event.response | |
203 local request = event.request | 198 local request = event.request |
204 local body = request.body | 199 local body = request.body |
205 if secure and not request.secure then return nil end | 200 if secure and not request.secure then return nil end |
206 | 201 |
207 local valid_files = { | 202 local valid_files = { |
220 if data then return data | 215 if data then return data |
221 else return http_response(event, 404, "Not found.") end | 216 else return http_response(event, 404, "Not found.") end |
222 end | 217 end |
223 elseif request.method == "POST" then | 218 elseif request.method == "POST" then |
224 if path == "" then | 219 if path == "" then |
225 if not request.body then return http_response(event, 400, "Bad Request.") end | 220 if not body then return http_response(event, 400, "Bad Request.") end |
226 local uuid = urldecode(request.body):match("^uuid=(.*)$") | 221 local uuid = urldecode(body):match("^uuid=(.*)$") |
227 | 222 |
228 if not pending[uuid] then | 223 if not pending[uuid] then |
229 return r_template(event, "fail") | 224 return r_template(event, "fail") |
230 else | 225 else |
231 local username, password, ip = | 226 local username, password, ip = |