annotate mod_data_access/mod_data_access.lua @ 538:de64963d8e7a

mod_ircd: Made the requirement of setting the conference_server a bit more "aggressive", and the resulting error banner for not setting it also.
author Marco Cirillo <maranda@lightwitch.org>
date Sun, 08 Jan 2012 17:25:34 +0000
parents b84493ef1d1d
children dd7d30c175d4
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
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
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;
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
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;
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
12 local dm_list_store = require "util.datamanager".list_store;
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
13 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
14 local b64_decode = require "util.encodings".base64.decode;
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
15 local http = require "net.http";
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
16 local urldecode = http.urldecode;
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
17 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
18 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
19 local response = {
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 status = code .. " " .. message;
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 body = message .. "\n"; }
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 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
23 return response
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local encoders = {
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 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
28 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
29 };
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
30 local decoders = {
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
31 lua = require "util.serialization".deserialize,
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
32 json = require "util.json".decode,
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
33 };
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
34 local content_type_map = {
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
35 ["text/x-lua"] = "lua"; lua = "text/x-lua";
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
36 ["application/json"] = "json"; json = "application/json";
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
37 }
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 --[[
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 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
40 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
41 end --]]
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
43 local allowed_methods = {
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
44 GET = true, "GET",
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
45 PUT = true, "PUT",
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
46 POST = true, "POST",
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
47 }
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
48
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local function handle_request(method, body, request)
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
50 if not allowed_methods[method] then
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
51 return http_response(405, "Method Not Allowed", {["Allow"] = t_concat(allowed_methods, ", ")});
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
52 end
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 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
55 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
56 {["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
57 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 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
59 :match("[^ ]*$") or ""):match("([^:]*):(.*)");
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 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
61 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
62 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
63 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
64
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 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
66 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
67 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
68 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 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
71 local path = {};
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 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
73 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
74 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 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
76 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
77
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 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
79 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
80 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
81 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
82 --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
83 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 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
86 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
87 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
89 local p_host, p_user, p_store, p_type = unpack(path);
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
90
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
91 if not p_store or not p_store:match("^[%a_]+$") then
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
92 return http_response(404, "Not Found");
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
93 end
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
94
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 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
96 -- To only give admins acces to anything, move the inside of this block after authz
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
97 module:log("debug", "%s wants access to %s@%s[%s], is admin?", user, p_user, p_host, p_store)
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
98 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
99 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
100 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
103 if method == "GET" then
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
104 local data = dm_load(p_user, p_host, p_store);
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
105
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
106 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
107
486
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
108 --TODO Use the Accept header
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
109 content_type = p_type or "json";
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
110 if data and encoders[content_type] then
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
111 return {
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
112 status = "200 OK",
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
113 body = encoders[content_type](data) .. "\n",
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
114 headers = {["content-type"] = content_type_map[content_type].."; charset=utf-8"}
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
115 };
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
116 else
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
117 return http_response(404, "Not Found");
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
118 end
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
119 else -- POST or PUT
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
120 if not body then
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
121 return http_response(400, "Bad Request")
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
122 end
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
123 local content_type, content = request.headers["content-type"], body;
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
124 content_type = content_type and content_type_map[content_type]
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
125 module:log("debug", "%s: %s", content_type, tostring(content));
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
126 content = content_type and decoders[content_type] and decoders[content_type](content);
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
127 module:log("debug", "%s: %s", type(content), tostring(content));
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
128 if not content then
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
129 return http_response(400, "Bad Request")
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
130 end
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
131 local ok, err
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
132 if method == "PUT" then
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
133 ok, err = dm_store(p_user, p_host, p_store, content);
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
134 elseif method == "POST" then
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
135 ok, err = dm_list_append(p_user, p_host, p_store, content);
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
136 elseif method == "DELETE" then
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
137 dm_store(p_user, p_host, p_store, nil);
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
138 dm_list_store(p_user, p_host, p_store, nil);
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
139 end
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
140 if ok then
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
141 return http_response(201, "Created", { Location = t_concat({"/data",p_host,p_user,p_store}, "/") });
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
142 else
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
143 return { status = "500 Internal Server Error", body = err }
b84493ef1d1d mod_data_access: Implement PUT and POST.
Kim Alvefur <zash@zash.se>
parents: 461
diff changeset
144 end
318
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 local function setup()
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 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
150 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
151 end
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 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
153 setup();
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 else
84caab2bc02c mod_data_access: New plugin providing a HTTP interface to Prosodys datamanager
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 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
156 end