mod_http_upload/mod_http_upload.lua
changeset 2211 c45ad4b7aaa3
parent 2197 40824a38d505
child 2212 e654d6e1fb50
equal deleted inserted replaced
2210:724809023c92 2211:c45ad4b7aaa3
    48 module:hook("iq/host/"..xmlns_http_upload..":request", function (event)
    48 module:hook("iq/host/"..xmlns_http_upload..":request", function (event)
    49 	local stanza, origin = event.stanza, event.origin;
    49 	local stanza, origin = event.stanza, event.origin;
    50 	local request = stanza.tags[1];
    50 	local request = stanza.tags[1];
    51 	-- local clients only
    51 	-- local clients only
    52 	if origin.type ~= "c2s" then
    52 	if origin.type ~= "c2s" then
       
    53 		module:log("debug", "Request for upload slot from a %s", origin.type);
    53 		origin.send(st.error_reply(stanza, "cancel", "not-authorized"));
    54 		origin.send(st.error_reply(stanza, "cancel", "not-authorized"));
    54 		return true;
    55 		return true;
    55 	end
    56 	end
    56 	-- validate
    57 	-- validate
    57 	local filename = request:get_child_text("filename");
    58 	local filename = request:get_child_text("filename");
    58 	if not filename or filename:find("/") then
    59 	if not filename or filename:find("/") then
       
    60 		module:log("debug", "Filename %q not allowed", filename or "");
    59 		origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid filename"));
    61 		origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid filename"));
    60 		return true;
    62 		return true;
    61 	end
    63 	end
    62 	local filesize = tonumber(request:get_child_text("size"));
    64 	local filesize = tonumber(request:get_child_text("size"));
    63 	if not filesize then
    65 	if not filesize then
       
    66 		module:log("debug", "Missing file size");
    64 		origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing or invalid file size"));
    67 		origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing or invalid file size"));
    65 		return true;
    68 		return true;
    66 	elseif filesize > file_size_limit then
    69 	elseif filesize > file_size_limit then
    67 		origin.send(st.error_reply(stanza, "modify", "not-acceptable", "File too large",
    70 		origin.send(st.error_reply(stanza, "modify", "not-acceptable", "File too large",
    68 			st.stanza("file-too-large", {xmlns=xmlns_http_upload})
    71 			st.stanza("file-too-large", {xmlns=xmlns_http_upload})
    75 	pending_slots[random.."/"..filename] = origin.full_jid;
    78 	pending_slots[random.."/"..filename] = origin.full_jid;
    76 	local url = module:http_url() .. "/" .. random .. "/" .. urlencode(filename);
    79 	local url = module:http_url() .. "/" .. random .. "/" .. urlencode(filename);
    77 	reply:tag("get"):text(url):up();
    80 	reply:tag("get"):text(url):up();
    78 	reply:tag("put"):text(url):up();
    81 	reply:tag("put"):text(url):up();
    79 	origin.send(reply);
    82 	origin.send(reply);
       
    83 	origin.log("debug", "Given upload slot %q", random);
    80 	return true;
    84 	return true;
    81 end);
    85 end);
    82 
    86 
    83 -- http service
    87 -- http service
    84 local function upload_data(event, path)
    88 local function upload_data(event, path)
    86 		module:log("warn", "Attempt to upload to unknown slot %q", path);
    90 		module:log("warn", "Attempt to upload to unknown slot %q", path);
    87 		return; -- 404
    91 		return; -- 404
    88 	end
    92 	end
    89 	local random, filename = path:match("^([^/]+)/([^/]+)$");
    93 	local random, filename = path:match("^([^/]+)/([^/]+)$");
    90 	if not random then
    94 	if not random then
       
    95 		module:log("warn", "Invalid file path %q", path);
    91 		return 400;
    96 		return 400;
    92 	end
    97 	end
    93 	if #event.request.body > file_size_limit then
    98 	if #event.request.body > file_size_limit then
    94 		module:log("warn", "Uploaded file too large %d bytes", #event.request.body);
    99 		module:log("warn", "Uploaded file too large %d bytes", #event.request.body);
    95 		return 400;
   100 		return 400;