Changeset

2767:e1edf643fbb1

mod_http_upload: Send a correct response on namespace :0. Fixes #968.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 19 Sep 2017 22:07:51 +0200
parents 2766:314cebb3071e
children 2768:2af42a3af131
files mod_http_upload/mod_http_upload.lua
diffstat 1 files changed, 38 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/mod_http_upload/mod_http_upload.lua	Tue Sep 19 18:47:18 2017 +0200
+++ b/mod_http_upload/mod_http_upload.lua	Tue Sep 19 22:07:51 2017 +0200
@@ -114,35 +114,27 @@
 	-- local clients only
 	if origin.type ~= "c2s" then
 		module:log("debug", "Request for upload slot from a %s", origin.type);
-		origin.send(st.error_reply(stanza, "cancel", "not-authorized"));
-		return true;
+		return nil, st.error_reply(stanza, "cancel", "not-authorized");
 	end
 	-- validate
 	if not filename or filename:find("/") then
 		module:log("debug", "Filename %q not allowed", filename or "");
-		origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid filename"));
-		return true;
+		return nil, st.error_reply(stanza, "modify", "bad-request", "Invalid filename");
 	end
 	expire(username, host);
 	if not filesize then
 		module:log("debug", "Missing file size");
-		origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing or invalid file size"));
-		return true;
+		return nil, st.error_reply(stanza, "modify", "bad-request", "Missing or invalid file size");
 	elseif filesize > file_size_limit then
 		module:log("debug", "File too large (%d > %d)", filesize, file_size_limit);
-		origin.send(st.error_reply(stanza, "modify", "not-acceptable", "File too large")
+		return nil, st.error_reply(stanza, "modify", "not-acceptable", "File too large")
 			:tag("file-too-large", {xmlns=xmlns})
-				:tag("max-file-size"):text(tostring(file_size_limit)));
-		return true;
+				:tag("max-file-size"):text(tostring(file_size_limit));
 	elseif not check_quota(username, host, filesize) then
 		module:log("debug", "Upload of %dB by %s would exceed quota", filesize, origin.full_jid);
-		origin.send(st.error_reply(stanza, "wait", "resource-constraint", "Quota reached"));
-		return true;
+		return nil, st.error_reply(stanza, "wait", "resource-constraint", "Quota reached");
 	end
 
-	local reply = st.reply(stanza);
-	reply:tag("slot", { xmlns = xmlns });
-
 	local random_dir;
 	repeat random_dir = uuid();
 	until lfs.mkdir(join_path(storage_path, random_dir))
@@ -152,8 +144,7 @@
 		filename = filename, dir = random_dir, size = filesize, time = os.time() });
 
 	if not ok then
-		origin.send(st.error_reply(stanza, "wait", "internal-server-failure"));
-		return true;
+		return nil, st.error_reply(stanza, "wait", "internal-server-failure");
 	end
 
 	local slot = random_dir.."/"..filename;
@@ -163,6 +154,8 @@
 		pending_slots[slot] = nil;
 	end);
 
+	origin.log("debug", "Given upload slot %q", slot);
+
 	local base_url = module:http_url();
 	local slot_url = url.parse(base_url);
 	slot_url.path = url.parse_path(slot_url.path or "/");
@@ -171,11 +164,7 @@
 	slot_url.path.is_directory = false;
 	slot_url.path = url.build_path(slot_url.path);
 	slot_url = url.build(slot_url);
-	reply:tag("get"):text(slot_url):up();
-	reply:tag("put"):text(slot_url):up();
-	origin.send(reply);
-	origin.log("debug", "Given upload slot %q", slot);
-	return true;
+	return slot_url;
 end
 
 -- hooks
@@ -184,7 +173,20 @@
 	local request = stanza.tags[1];
 	local filename = request.attr.filename;
 	local filesize = tonumber(request.attr.size);
-	return handle_request(origin, stanza, namespace, filename, filesize);
+
+	local slot_url, err = handle_request(origin, stanza, namespace, filename, filesize);
+	if not slot_url then
+		origin.send(err);
+		return true;
+	end
+
+	local reply = st.reply(stanza)
+		:tag("slot", { xmlns = namespace })
+			:tag("get", { url = slot_url }):up()
+			:tag("put", { url = slot_url }):up()
+		:up();
+	origin.send(reply);
+	return true;
 end);
 
 module:hook("iq/host/"..legacy_namespace..":request", function (event)
@@ -192,7 +194,20 @@
 	local request = stanza.tags[1];
 	local filename = request:get_child_text("filename");
 	local filesize = tonumber(request:get_child_text("size"));
-	return handle_request(origin, stanza, legacy_namespace, filename, filesize);
+
+	local slot_url, err = handle_request(origin, stanza, legacy_namespace, filename, filesize);
+	if not slot_url then
+		origin.send(err);
+		return true;
+	end
+
+	local reply = st.reply(stanza)
+		:tag("slot", { xmlns = legacy_namespace })
+			:tag("get"):text(slot_url):up()
+			:tag("put"):text(slot_url):up()
+		:up();
+	origin.send(reply);
+	return true;
 end);
 
 -- http service