Annotate

mod_http_upload/mod_http_upload.lua @ 1850:e3a0ebe671cc

mod_http_upload: Include failure reason in error response
author Kim Alvefur <zash@zash.se>
date Mon, 14 Sep 2015 12:45:00 +0200
parent 1849:5244c9b0b297
child 1851:03c5639882a7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- mod_http_upload
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 --
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- Copyright (C) 2015 Kim Alvefur
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 --
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 -- This file is MIT/X11 licensed.
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 --
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 -- Implementation of HTTP Upload file transfer mechanism used by Conversations
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 --
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 -- imports
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local st = require"util.stanza";
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local lfs = require"lfs";
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local uuid = require"util.uuid".generate;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
1815
abacf6698d97 mod_http_upload: Include join_path function, should make it work with 0.9.x
Kim Alvefur <zash@zash.se>
parents: 1805
diff changeset
15 local function join_path(a, b)
1817
1971ff719e72 mod_http_upload: Fix typo (thanks mo)
Kim Alvefur <zash@zash.se>
parents: 1815
diff changeset
16 return a .. package.config:sub(1,1) .. b;
1815
abacf6698d97 mod_http_upload: Include join_path function, should make it work with 0.9.x
Kim Alvefur <zash@zash.se>
parents: 1805
diff changeset
17 end
abacf6698d97 mod_http_upload: Include join_path function, should make it work with 0.9.x
Kim Alvefur <zash@zash.se>
parents: 1805
diff changeset
18
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 -- depends
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 module:depends("http");
1805
25c28644fae8 mod_http_upload: Depend on mod_disco, allows it to be discovered when set up as a component
Kim Alvefur <zash@zash.se>
parents: 1772
diff changeset
21 module:depends("disco");
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 -- namespace
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local xmlns_http_upload = "eu:siacs:conversations:http:upload";
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 module:add_feature(xmlns_http_upload);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 -- state
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local pending_slots = module:shared("upload_slots");
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local storage_path = join_path(prosody.paths.data, module.name);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 lfs.mkdir(storage_path);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 -- hooks
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 module:hook("iq/host/"..xmlns_http_upload..":request", function (event)
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local stanza, origin = event.stanza, event.origin;
1848
e5243fa16210 mod_http_upload: Cache first-level child <request> in local variable
Kim Alvefur <zash@zash.se>
parents: 1817
diff changeset
37 local request = stanza.tags[1];
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 -- local clients only
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 if origin.type ~= "c2s" then
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 origin.send(st.error_reply(stanza, "cancel", "not-authorized"));
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 return true;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 -- validate
1848
e5243fa16210 mod_http_upload: Cache first-level child <request> in local variable
Kim Alvefur <zash@zash.se>
parents: 1817
diff changeset
44 local filename = request:get_child_text("filename");
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 if not filename or filename:find("/") then
1850
e3a0ebe671cc mod_http_upload: Include failure reason in error response
Kim Alvefur <zash@zash.se>
parents: 1849
diff changeset
46 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid filename"));
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 return true;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local reply = st.reply(stanza);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 reply:tag("slot", { xmlns = xmlns_http_upload });
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 local random = uuid();
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 pending_slots[random.."/"..filename] = origin.full_jid;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 local url = module:http_url() .. "/" .. random .. "/" .. filename;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 reply:tag("get"):text(url):up();
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 reply:tag("put"):text(url):up();
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 origin.send(reply);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 return true;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 -- http service
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 local function upload_data(event, path)
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 if not pending_slots[path] then
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 return 401;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 local random, filename = path:match("^([^/]+)/([^/]+)$");
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 if not random then
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 return 400;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 local dirname = join_path(storage_path, random);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 if not lfs.mkdir(dirname) then
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 module:log("error", "Could not create directory %s for upload", dirname);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 return 500;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 local full_filename = join_path(dirname, filename);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 local fh, ferr = io.open(full_filename, "w");
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 if not fh then
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 module:log("error", "Could not open file %s for upload: %s", full_filename, ferr);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 return 500;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 local ok, err = fh:write(event.request.body);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 if not ok then
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 module:log("error", "Could not write to file %s for upload: %s", full_filename, err);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 os.remove(full_filename);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 return 500;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 ok, err = fh:close();
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 if not ok then
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 module:log("error", "Could not write to file %s for upload: %s", full_filename, err);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 os.remove(full_filename);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 return 500;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 module:log("info", "File uploaded by %s to slot %s", pending_slots[path], random);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 pending_slots[path] = nil;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 return 200;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 local serve_uploaded_files = module:depends("http_files").serve(storage_path);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 local function size_only(request, data)
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 request.headers.content_size = #data;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 return 200;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 local function serve_head(event, path)
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 event.send = size_only;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 return serve_uploaded_files(event, path);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 module:provides("http", {
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 route = {
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 ["GET /*"] = serve_uploaded_files;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 ["HEAD /*"] = serve_head;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 ["PUT /*"] = upload_data;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 };
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 });
1849
5244c9b0b297 mod_http_upload: Log a message with the upload URL and storage path for easy discovery
Kim Alvefur <zash@zash.se>
parents: 1848
diff changeset
116
5244c9b0b297 mod_http_upload: Log a message with the upload URL and storage path for easy discovery
Kim Alvefur <zash@zash.se>
parents: 1848
diff changeset
117 module:log("info", "URL: <%s>; Storage path: %s", module:http_url(), storage_path);