annotate mod_data_access/mod_data_access.lua @ 455:52f2188ec47d

mod_default_vcard: Sets initial vCard from data enterd on registration
author Kim Alvefur <zash@zash.se>
date Sat, 15 Oct 2011 13:43:37 +0200
parents 84caab2bc02c
children bbea8081c865
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- HTTP Access to datamanager
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- By Kim Alvefur <zash@zash.se>
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
4 local t_concat = table.concat;
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local jid_prep = require "util.jid".prep;
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local jid_split = require "util.jid".split;
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local um_test_pw = require "core.usermanager".test_password;
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local is_admin = require "core.usermanager".is_admin
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local dm_load = require "util.datamanager".load;
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
10 local dm_store = require "util.datamanager".store;
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local dm_list_load = require "util.datamanager".list_load;
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
12 local dm_list_append = require "util.datamanager".list_append;
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local b64_decode = require "util.encodings".base64.decode;
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
14 local http = require "net.http";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
15 local urldecode = http.urldecode;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
16 local urlencode = http.urlencode;
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local function http_response(code, message, extra_headers)
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local response = {
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 status = code .. " " .. message;
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 body = message .. "\n"; }
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 if extra_headers then response.headers = extra_headers; end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 return response
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local encoders = {
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 lua = require "util.serialization".serialize,
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 json = require "util.json".encode
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 };
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
29 local decoders = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
30 lua = require "util.serialization".deserialize,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
31 json = require "util.json".decode,
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
32 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
33 local content_type_map = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
34 ["text/x-lua"] = "lua"; lua = "text/x-lua";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
35 ["application/json"] = "json"; json = "application/json";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
36 }
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 --[[
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 encoders.xml = function(data)
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 return "<?xml version='1.0' encoding='utf-8'?><todo:write-this-serializer/>";
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end --]]
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
42 local allowed_methods = {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
43 GET = true, "GET",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
44 PUT = true, "PUT",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
45 POST = true, "POST",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
46 }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
47
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 local function handle_request(method, body, request)
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
49 if not allowed_methods[method] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
50 return http_response(405, "Method Not Allowed", {["Allow"] = t_concat(allowed_methods, ", ")});
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
51 end
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 if not request.headers["authorization"] then
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 return http_response(401, "Unauthorized",
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 {["WWW-Authenticate"]='Basic realm="WallyWorld"'})
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 local user, password = b64_decode(request.headers.authorization
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 :match("[^ ]*$") or ""):match("([^:]*):(.*)");
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 user = jid_prep(user);
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 if not user or not password then return http_response(400, "Bad Request"); end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 local user_node, user_host = jid_split(user)
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 if not hosts[user_host] then return http_response(401, "Unauthorized"); end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 module:log("debug", "authz %s", user)
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 if not um_test_pw(user_node, user_host, password) then
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 return http_response(401, "Unauthorized");
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 module:log("debug", "spliting path");
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 local path = {};
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 for i in string.gmatch(request.url.path, "[^/]+") do
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 table.insert(path, i);
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 table.remove(path, 1); -- the first /data
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 module:log("debug", "split path, got %d parts: %s", #path, table.concat(path, ", "));
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 if #path < 3 then
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 module:log("debug", "since we need at least 3 parts, adding %s/%s", user_host, user_node);
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 table.insert(path, 1, user_node);
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 table.insert(path, 1, user_host);
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 --return http_response(400, "Bad Request");
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 if #path < 3 then
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 return http_response(404, "Not Found");
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
88 local p_host, p_user, p_store, p_type = unpack(path);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
89
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
90 if not p_store or not p_store:match("^[%a_]+$") then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
91 return http_response(404, "Not Found");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
92 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
93
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 if user_host ~= path[1] or user_node ~= path[2] then
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 -- To only give admins acces to anything, move the inside of this block after authz
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
96 module:log("debug", "%s wants access to %s@%s[%s], is admin?", user, p_user, p_host, p_store)
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
97 if not is_admin(user, p_host) then
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 return http_response(403, "Forbidden");
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
102 if method == "GET" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
103 local data = dm_load(p_user, p_host, p_store);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
104
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
105 data = data or dm_load_list(p_user, p_host, p_store);
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106
455
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
107 --TODO Use the Accept header
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
108 content_type = p_type or "json";
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
109 if data and encoders[content_type] then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
110 return {
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
111 status = "200 OK",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
112 body = encoders[content_type](data) .. "\n",
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
113 headers = {["content-type"] = content_type_map[content_type].."; charset=utf-8"}
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
114 };
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
115 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
116 return http_response(404, "Not Found");
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
117 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
118 else -- POST or PUT
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
119 if not body then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
120 return http_response(400, "Bad Request")
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
121 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
122 local content_type, content = request.headers["content-type"], body;
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
123 content_type = content_type and content_type_map[content_type]
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
124 module:log("debug", "%s: %s", content_type, tostring(content));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
125 content = content_type and decoders[content_type] and decoders[content_type](content);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
126 module:log("debug", "%s: %s", type(content), tostring(content));
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
127 if not content then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
128 return http_response(400, "Bad Request")
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
129 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
130 local ok, err
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
131 if method == "PUT" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
132 ok, err = dm_store(p_user, p_host, p_store, content);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
133 elseif method == "POST" then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
134 ok, err = dm_list_append(p_user, p_host, p_store, content);
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
135 end
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
136 if ok then
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
137 return http_response(201, "Created", { Location = t_concat({"/data",p_host,p_user,p_store}, "/") });
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
138 else
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
139 return { status = "500 Internal Server Error", body = err }
52f2188ec47d mod_default_vcard: Sets initial vCard from data enterd on registration
Kim Alvefur <zash@zash.se>
parents: 318
diff changeset
140 end
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 local function setup()
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 local ports = module:get_option("data_access_ports") or { 5280 };
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 require "net.httpserver".new_from_config(ports, handle_request, { base = "data" });
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 if prosody.start_time then -- already started
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 setup();
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 else
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 prosody.events.add_handler("server-started", setup);
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 end