Comparison

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
parent 318:84caab2bc02c
child 461:bbea8081c865
comparison
equal deleted inserted replaced
454:3f101f7a26d0 455:52f2188ec47d
1 -- HTTP Access to datamanager 1 -- HTTP Access to datamanager
2 -- By Kim Alvefur <zash@zash.se> 2 -- By Kim Alvefur <zash@zash.se>
3 3
4 local t_concat = table.concat;
4 local jid_prep = require "util.jid".prep; 5 local jid_prep = require "util.jid".prep;
5 local jid_split = require "util.jid".split; 6 local jid_split = require "util.jid".split;
6 local um_test_pw = require "core.usermanager".test_password; 7 local um_test_pw = require "core.usermanager".test_password;
7 local is_admin = require "core.usermanager".is_admin 8 local is_admin = require "core.usermanager".is_admin
8 local dm_load = require "util.datamanager".load; 9 local dm_load = require "util.datamanager".load;
10 local dm_store = require "util.datamanager".store;
9 local dm_list_load = require "util.datamanager".list_load; 11 local dm_list_load = require "util.datamanager".list_load;
12 local dm_list_append = require "util.datamanager".list_append;
10 local b64_decode = require "util.encodings".base64.decode; 13 local b64_decode = require "util.encodings".base64.decode;
11 --local urldecode = require "net.http".urldecode; 14 local http = require "net.http";
12 --[[local urlparams = --require "net.http".getQueryParams or whatever MattJ names it 15 local urldecode = http.urldecode;
13 function(s) 16 local urlencode = http.urlencode;
14 if not s:match("=") then return urldecode(s); end
15 local r = {}
16 s:gsub("([^=&]*)=([^&]*)", function(k,v)
17 r[ urldecode(k) ] = urldecode(v);
18 return nil
19 end)
20 return r
21 end;
22 --]]
23
24 local function http_response(code, message, extra_headers) 17 local function http_response(code, message, extra_headers)
25 local response = { 18 local response = {
26 status = code .. " " .. message; 19 status = code .. " " .. message;
27 body = message .. "\n"; } 20 body = message .. "\n"; }
28 if extra_headers then response.headers = extra_headers; end 21 if extra_headers then response.headers = extra_headers; end
31 24
32 local encoders = { 25 local encoders = {
33 lua = require "util.serialization".serialize, 26 lua = require "util.serialization".serialize,
34 json = require "util.json".encode 27 json = require "util.json".encode
35 }; 28 };
29 local decoders = {
30 lua = require "util.serialization".deserialize,
31 json = require "util.json".decode,
32 };
33 local content_type_map = {
34 ["text/x-lua"] = "lua"; lua = "text/x-lua";
35 ["application/json"] = "json"; json = "application/json";
36 }
36 --[[ 37 --[[
37 encoders.xml = function(data) 38 encoders.xml = function(data)
38 return "<?xml version='1.0' encoding='utf-8'?><todo:write-this-serializer/>"; 39 return "<?xml version='1.0' encoding='utf-8'?><todo:write-this-serializer/>";
39 end --]] 40 end --]]
40 41
42 local allowed_methods = {
43 GET = true, "GET",
44 PUT = true, "PUT",
45 POST = true, "POST",
46 }
47
41 local function handle_request(method, body, request) 48 local function handle_request(method, body, request)
42 if request.method ~= "GET" then 49 if not allowed_methods[method] then
43 return http_response(405, "Method Not Allowed", {["Allow"] = "GET"}); 50 return http_response(405, "Method Not Allowed", {["Allow"] = t_concat(allowed_methods, ", ")});
44 end -- TODO Maybe PUT? 51 end
45 52
46 if not request.headers["authorization"] then 53 if not request.headers["authorization"] then
47 return http_response(401, "Unauthorized", 54 return http_response(401, "Unauthorized",
48 {["WWW-Authenticate"]='Basic realm="WallyWorld"'}) 55 {["WWW-Authenticate"]='Basic realm="WallyWorld"'})
49 end 56 end
76 83
77 if #path < 3 then 84 if #path < 3 then
78 return http_response(404, "Not Found"); 85 return http_response(404, "Not Found");
79 end 86 end
80 87
88 local p_host, p_user, p_store, p_type = unpack(path);
89
90 if not p_store or not p_store:match("^[%a_]+$") then
91 return http_response(404, "Not Found");
92 end
93
81 if user_host ~= path[1] or user_node ~= path[2] then 94 if user_host ~= path[1] or user_node ~= path[2] then
82 -- To only give admins acces to anything, move the inside of this block after authz 95 -- To only give admins acces to anything, move the inside of this block after authz
83 module:log("debug", "%s wants access to %s@%s[%s], is admin?", user, path[2], path[1], path[3]) 96 module:log("debug", "%s wants access to %s@%s[%s], is admin?", user, p_user, p_host, p_store)
84 if not is_admin(user, path[1]) then 97 if not is_admin(user, p_host) then
85 return http_response(403, "Forbidden"); 98 return http_response(403, "Forbidden");
86 end 99 end
87 end 100 end
88 101
89 local data = dm_load(path[2], path[1], path[3]); 102 if method == "GET" then
90 103 local data = dm_load(p_user, p_host, p_store);
91 data = data or dm_list_load(path[2], path[1], path[3]);
92 104
93 if data and encoders[path[4] or "json"] then 105 data = data or dm_load_list(p_user, p_host, p_store);
94 return { 106
95 status = "200 OK", 107 --TODO Use the Accept header
96 body = encoders[path[4] or "json"](data) .. "\n", 108 content_type = p_type or "json";
97 headers = {["content-type"] = "text/plain; charset=utf-8"} 109 if data and encoders[content_type] then
98 --headers = {["content-type"] = encoders[data[4] or "json"].mime .. "; charset=utf-8"} 110 return {
99 -- FIXME a little nicer that the above 111 status = "200 OK",
100 -- Also, would be cooler to use the Accept header, but parsing it ... 112 body = encoders[content_type](data) .. "\n",
101 }; 113 headers = {["content-type"] = content_type_map[content_type].."; charset=utf-8"}
102 else 114 };
103 return http_response(404, "Not Found"); 115 else
116 return http_response(404, "Not Found");
117 end
118 else -- POST or PUT
119 if not body then
120 return http_response(400, "Bad Request")
121 end
122 local content_type, content = request.headers["content-type"], body;
123 content_type = content_type and content_type_map[content_type]
124 module:log("debug", "%s: %s", content_type, tostring(content));
125 content = content_type and decoders[content_type] and decoders[content_type](content);
126 module:log("debug", "%s: %s", type(content), tostring(content));
127 if not content then
128 return http_response(400, "Bad Request")
129 end
130 local ok, err
131 if method == "PUT" then
132 ok, err = dm_store(p_user, p_host, p_store, content);
133 elseif method == "POST" then
134 ok, err = dm_list_append(p_user, p_host, p_store, content);
135 end
136 if ok then
137 return http_response(201, "Created", { Location = t_concat({"/data",p_host,p_user,p_store}, "/") });
138 else
139 return { status = "500 Internal Server Error", body = err }
140 end
104 end 141 end
105 end 142 end
106 143
107 local function setup() 144 local function setup()
108 local ports = module:get_option("data_access_ports") or { 5280 }; 145 local ports = module:get_option("data_access_ports") or { 5280 };