annotate mod_http_upload/mod_http_upload.lua @ 2641:2d83e6c8160b

mod_http_upload: add support for XEP-0363 version 0.3
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 22 Mar 2017 19:47:52 +0100
parents a7ef9b765891
children 45ef16ebe565
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.
2065
624e3fed6f92 mod_http_upload: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 2053
diff changeset
6 --
1772
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";
2479
a440f0514c2a Backed out changeset f48e9e25aec4, util.uuid.get_nibbles is unavailable in 0.9
Kim Alvefur <zash@zash.se>
parents: 2478
diff changeset
13 local uuid = require"util.uuid".generate;
2286
0a3f526779a1 mod_http_upload: Construct the upload slot URL using the LuaSocket URL library (fixes #717)
Kim Alvefur <zash@zash.se>
parents: 2285
diff changeset
14 local url = require "socket.url";
2066
cb74e4ab13f0 mod_http_upload: Advertise maximum file size in disco#info (Per XEP-0363 v0.2)
Kim Alvefur <zash@zash.se>
parents: 2065
diff changeset
15 local dataform = require "util.dataforms".new;
2472
595d6a25bd7a mod_http_upload: Record upload slot metadata (fixes #718)
Kim Alvefur <zash@zash.se>
parents: 2471
diff changeset
16 local datamanager = require "util.datamanager";
1912
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
17 local t_concat = table.concat;
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
18 local t_insert = table.insert;
1914
b01d60dfa405 mod_http_upload: Add missing local of string.upper (Thanks Thilo)
Kim Alvefur <zash@zash.se>
parents: 1912
diff changeset
19 local s_upper = string.upper;
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
2475
54c42b097984 mod_http_upload: Change join_path to match behaviour of util.paths in 0.10
Kim Alvefur <zash@zash.se>
parents: 2474
diff changeset
21 local function join_path(...)
2476
024a4143baef mod_http_upload: Add missing return statement
Kim Alvefur <zash@zash.se>
parents: 2475
diff changeset
22 return table.concat({ ... }, package.config:sub(1,1));
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
23 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
24
1851
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
25 -- config
2053
40056a27f394 mod_http_upload: Lower default size limit to 1MB
Kim Alvefur <zash@zash.se>
parents: 1967
diff changeset
26 local file_size_limit = module:get_option_number(module.name .. "_file_size_limit", 1024 * 1024); -- 1 MB
1851
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
27
2285
f1923bf329a3 mod_http_upload: Warn if upload size limit set higher than body size limit in http parser (applies to 0.10+)
Kim Alvefur <zash@zash.se>
parents: 2231
diff changeset
28 --- sanity
f1923bf329a3 mod_http_upload: Warn if upload size limit set higher than body size limit in http parser (applies to 0.10+)
Kim Alvefur <zash@zash.se>
parents: 2231
diff changeset
29 local parser_body_limit = module:context("*"):get_option_number("http_max_content_size", 10*1024*1024);
f1923bf329a3 mod_http_upload: Warn if upload size limit set higher than body size limit in http parser (applies to 0.10+)
Kim Alvefur <zash@zash.se>
parents: 2231
diff changeset
30 if file_size_limit > parser_body_limit then
f1923bf329a3 mod_http_upload: Warn if upload size limit set higher than body size limit in http parser (applies to 0.10+)
Kim Alvefur <zash@zash.se>
parents: 2231
diff changeset
31 module:log("warn", "%s_file_size_limit exceeds HTTP parser limit on body size, capping file size to %d B", module.name, parser_body_limit);
f1923bf329a3 mod_http_upload: Warn if upload size limit set higher than body size limit in http parser (applies to 0.10+)
Kim Alvefur <zash@zash.se>
parents: 2231
diff changeset
32 file_size_limit = parser_body_limit;
f1923bf329a3 mod_http_upload: Warn if upload size limit set higher than body size limit in http parser (applies to 0.10+)
Kim Alvefur <zash@zash.se>
parents: 2231
diff changeset
33 end
f1923bf329a3 mod_http_upload: Warn if upload size limit set higher than body size limit in http parser (applies to 0.10+)
Kim Alvefur <zash@zash.se>
parents: 2231
diff changeset
34
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 -- depends
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 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
37 module:depends("disco");
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38
2641
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
39 -- namespaces
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
40 local namespace = "urn:xmpp:http:upload:0";
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
41 local legacy_namespace = "urn:xmpp:http:upload";
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42
2287
e1a8c2324937 mod_http_upload: Add a comment
Kim Alvefur <zash@zash.se>
parents: 2286
diff changeset
43 -- identity and feature advertising
2444
3e9f9cef9c0e mod_http_upload: Add missing semicolon
Kim Alvefur <zash@zash.se>
parents: 2443
diff changeset
44 module:add_identity("store", "file", module:get_option_string("name", "HTTP File Upload"));
2641
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
45 module:add_feature(namespace);
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
46 module:add_feature(legacy_namespace);
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47
2066
cb74e4ab13f0 mod_http_upload: Advertise maximum file size in disco#info (Per XEP-0363 v0.2)
Kim Alvefur <zash@zash.se>
parents: 2065
diff changeset
48 module:add_extension(dataform {
2641
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
49 { name = "FORM_TYPE", type = "hidden", value = namespace },
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
50 { name = "max-file-size", type = "text-single" },
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
51 }:form({ ["max-file-size"] = tostring(file_size_limit) }, "result"));
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
52
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
53 module:add_extension(dataform {
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
54 { name = "FORM_TYPE", type = "hidden", value = legacy_namespace },
2066
cb74e4ab13f0 mod_http_upload: Advertise maximum file size in disco#info (Per XEP-0363 v0.2)
Kim Alvefur <zash@zash.se>
parents: 2065
diff changeset
55 { name = "max-file-size", type = "text-single" },
cb74e4ab13f0 mod_http_upload: Advertise maximum file size in disco#info (Per XEP-0363 v0.2)
Kim Alvefur <zash@zash.se>
parents: 2065
diff changeset
56 }:form({ ["max-file-size"] = tostring(file_size_limit) }, "result"));
cb74e4ab13f0 mod_http_upload: Advertise maximum file size in disco#info (Per XEP-0363 v0.2)
Kim Alvefur <zash@zash.se>
parents: 2065
diff changeset
57
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 -- state
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 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
60
1967
2ce2b194d501 mod_http_upload: Make file system path configurable
Kim Alvefur <zash@zash.se>
parents: 1966
diff changeset
61 local storage_path = module:get_option_string(module.name .. "_path", join_path(prosody.paths.data, module.name));
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 lfs.mkdir(storage_path);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63
2641
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
64 local function handle_request(origin, stanza, xmlns, filename, filesize)
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 -- local clients only
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 if origin.type ~= "c2s" then
2207
c45ad4b7aaa3 mod_http_upload: Add additional debug logging
Kim Alvefur <zash@zash.se>
parents: 2193
diff changeset
67 module:log("debug", "Request for upload slot from a %s", origin.type);
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 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
69 return true;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 -- validate
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 if not filename or filename:find("/") then
2207
c45ad4b7aaa3 mod_http_upload: Add additional debug logging
Kim Alvefur <zash@zash.se>
parents: 2193
diff changeset
73 module:log("debug", "Filename %q not allowed", filename or "");
1850
e3a0ebe671cc mod_http_upload: Include failure reason in error response
Kim Alvefur <zash@zash.se>
parents: 1849
diff changeset
74 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
75 return true;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 end
1851
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
77 if not filesize then
2207
c45ad4b7aaa3 mod_http_upload: Add additional debug logging
Kim Alvefur <zash@zash.se>
parents: 2193
diff changeset
78 module:log("debug", "Missing file size");
1851
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
79 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing or invalid file size"));
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
80 return true;
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
81 elseif filesize > file_size_limit then
2208
e654d6e1fb50 mod_http_upload: Log if file size exceeds limit
Kim Alvefur <zash@zash.se>
parents: 2207
diff changeset
82 module:log("debug", "File too large (%d > %d)", filesize, file_size_limit);
2445
e822900c87d4 mod_http_upload: Correctly attach extended error information
Kim Alvefur <zash@zash.se>
parents: 2444
diff changeset
83 origin.send(st.error_reply(stanza, "modify", "not-acceptable", "File too large")
2641
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
84 :tag("file-too-large", {xmlns=xmlns})
2445
e822900c87d4 mod_http_upload: Correctly attach extended error information
Kim Alvefur <zash@zash.se>
parents: 2444
diff changeset
85 :tag("max-file-size"):text(tostring(file_size_limit)));
1851
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
86 return true;
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
87 end
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 local reply = st.reply(stanza);
2641
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
89 reply:tag("slot", { xmlns = xmlns });
2468
3bff2848af12 mod_http_upload: Create random directory name when assigning slot
Kim Alvefur <zash@zash.se>
parents: 2467
diff changeset
90
3bff2848af12 mod_http_upload: Create random directory name when assigning slot
Kim Alvefur <zash@zash.se>
parents: 2467
diff changeset
91 local random;
2479
a440f0514c2a Backed out changeset f48e9e25aec4, util.uuid.get_nibbles is unavailable in 0.9
Kim Alvefur <zash@zash.se>
parents: 2478
diff changeset
92 repeat random = uuid();
2470
9ff809591fbc mod_http_upload: Accept already existing directory name if there is no conflicting file in it
Kim Alvefur <zash@zash.se>
parents: 2469
diff changeset
93 until lfs.mkdir(join_path(storage_path, random)) or not lfs.attributes(join_path(storage_path, random, filename))
2468
3bff2848af12 mod_http_upload: Create random directory name when assigning slot
Kim Alvefur <zash@zash.se>
parents: 2467
diff changeset
94
2477
c32ca5ad2539 mod_http_upload: Fix typo
Kim Alvefur <zash@zash.se>
parents: 2476
diff changeset
95 datamanager.list_append(origin.username, origin.host, module.name, { filename = join_path(storage_path, random, filename), size = filesize, time = os.time() });
2489
9d154c929319 mod_http_upload: Log the directory and filename joined
Kim Alvefur <zash@zash.se>
parents: 2479
diff changeset
96 local slot = random.."/"..filename;
9d154c929319 mod_http_upload: Log the directory and filename joined
Kim Alvefur <zash@zash.se>
parents: 2479
diff changeset
97 pending_slots[slot] = origin.full_jid;
2286
0a3f526779a1 mod_http_upload: Construct the upload slot URL using the LuaSocket URL library (fixes #717)
Kim Alvefur <zash@zash.se>
parents: 2285
diff changeset
98 local base_url = module:http_url();
0a3f526779a1 mod_http_upload: Construct the upload slot URL using the LuaSocket URL library (fixes #717)
Kim Alvefur <zash@zash.se>
parents: 2285
diff changeset
99 local slot_url = url.parse(base_url);
2288
827f01cbf6ba mod_http_upload: Handle case of non-existant path
Kim Alvefur <zash@zash.se>
parents: 2287
diff changeset
100 slot_url.path = url.parse_path(slot_url.path or "/");
2286
0a3f526779a1 mod_http_upload: Construct the upload slot URL using the LuaSocket URL library (fixes #717)
Kim Alvefur <zash@zash.se>
parents: 2285
diff changeset
101 t_insert(slot_url.path, random);
0a3f526779a1 mod_http_upload: Construct the upload slot URL using the LuaSocket URL library (fixes #717)
Kim Alvefur <zash@zash.se>
parents: 2285
diff changeset
102 t_insert(slot_url.path, filename);
0a3f526779a1 mod_http_upload: Construct the upload slot URL using the LuaSocket URL library (fixes #717)
Kim Alvefur <zash@zash.se>
parents: 2285
diff changeset
103 slot_url.path.is_directory = false;
0a3f526779a1 mod_http_upload: Construct the upload slot URL using the LuaSocket URL library (fixes #717)
Kim Alvefur <zash@zash.se>
parents: 2285
diff changeset
104 slot_url.path = url.build_path(slot_url.path);
0a3f526779a1 mod_http_upload: Construct the upload slot URL using the LuaSocket URL library (fixes #717)
Kim Alvefur <zash@zash.se>
parents: 2285
diff changeset
105 slot_url = url.build(slot_url);
0a3f526779a1 mod_http_upload: Construct the upload slot URL using the LuaSocket URL library (fixes #717)
Kim Alvefur <zash@zash.se>
parents: 2285
diff changeset
106 reply:tag("get"):text(slot_url):up();
0a3f526779a1 mod_http_upload: Construct the upload slot URL using the LuaSocket URL library (fixes #717)
Kim Alvefur <zash@zash.se>
parents: 2285
diff changeset
107 reply:tag("put"):text(slot_url):up();
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 origin.send(reply);
2489
9d154c929319 mod_http_upload: Log the directory and filename joined
Kim Alvefur <zash@zash.se>
parents: 2479
diff changeset
109 origin.log("debug", "Given upload slot %q", slot);
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 return true;
2641
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
111 end
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
112
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
113 -- hooks
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
114 module:hook("iq/host/"..namespace..":request", function (event)
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
115 local stanza, origin = event.stanza, event.origin;
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
116 local request = stanza.tags[1];
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
117 local filename = request.attr.filename;
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
118 local filesize = tonumber(request.attr.size);
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
119 handle_request(origin, stanza, namespace, filename, filesize);
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
120 end);
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
121
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
122 module:hook("iq/host/"..legacy_namespace..":request", function (event)
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
123 local stanza, origin = event.stanza, event.origin;
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
124 local request = stanza.tags[1];
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
125 local filename = request:get_child_text("filename");
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
126 local filesize = tonumber(request:get_child_text("size"));
2d83e6c8160b mod_http_upload: add support for XEP-0363 version 0.3
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 2607
diff changeset
127 handle_request(origin, stanza, legacy_namespace, filename, filesize);
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 end);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 -- http service
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 local function upload_data(event, path)
2467
290fef768a81 mod_http_upload: Forget upload slot under some error conditions
Kim Alvefur <zash@zash.se>
parents: 2445
diff changeset
132 local uploader = pending_slots[path];
290fef768a81 mod_http_upload: Forget upload slot under some error conditions
Kim Alvefur <zash@zash.se>
parents: 2445
diff changeset
133 if not uploader then
2191
e47046abf568 mod_http_upload: Add more logging
Kim Alvefur <zash@zash.se>
parents: 2066
diff changeset
134 module:log("warn", "Attempt to upload to unknown slot %q", path);
2193
40824a38d505 mod_http_upload: Return nil if no upload slot is found (should prevent conflicts between multiple instances on the same path)
Kim Alvefur <zash@zash.se>
parents: 2192
diff changeset
135 return; -- 404
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 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
138 if not random then
2207
c45ad4b7aaa3 mod_http_upload: Add additional debug logging
Kim Alvefur <zash@zash.se>
parents: 2193
diff changeset
139 module:log("warn", "Invalid file path %q", path);
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 return 400;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 end
1851
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
142 if #event.request.body > file_size_limit then
2192
bb8f7785aed7 mod_http_upload: Demote some errors to warnings
Kim Alvefur <zash@zash.se>
parents: 2191
diff changeset
143 module:log("warn", "Uploaded file too large %d bytes", #event.request.body);
1851
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
144 return 400;
03c5639882a7 mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents: 1850
diff changeset
145 end
2467
290fef768a81 mod_http_upload: Forget upload slot under some error conditions
Kim Alvefur <zash@zash.se>
parents: 2445
diff changeset
146 pending_slots[path] = nil;
2478
1fae2a0a4092 mod_http_upload: Fix to include base storage path
Kim Alvefur <zash@zash.se>
parents: 2477
diff changeset
147 local full_filename = join_path(storage_path, random, filename);
2469
43f7637f0143 mod_http_upload: Make sure that target file does not exist prior to upload
Kim Alvefur <zash@zash.se>
parents: 2468
diff changeset
148 if lfs.attributes(full_filename) then
43f7637f0143 mod_http_upload: Make sure that target file does not exist prior to upload
Kim Alvefur <zash@zash.se>
parents: 2468
diff changeset
149 module:log("warn", "File %s exists already, not replacing it", full_filename);
43f7637f0143 mod_http_upload: Make sure that target file does not exist prior to upload
Kim Alvefur <zash@zash.se>
parents: 2468
diff changeset
150 return 409;
43f7637f0143 mod_http_upload: Make sure that target file does not exist prior to upload
Kim Alvefur <zash@zash.se>
parents: 2468
diff changeset
151 end
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 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
153 if not fh then
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 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
155 return 500;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 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
158 if not ok then
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 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
160 os.remove(full_filename);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 return 500;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 ok, err = fh:close();
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 if not ok then
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 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
166 os.remove(full_filename);
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 return 500;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 end
2467
290fef768a81 mod_http_upload: Forget upload slot under some error conditions
Kim Alvefur <zash@zash.se>
parents: 2445
diff changeset
169 module:log("info", "File uploaded by %s to slot %s", uploader, random);
2607
a7ef9b765891 mod_http_upload: Return 201 on successful PUT (as per XEP-0363 and RFC 2616) (Thanks Flow)
Matthew Wild <mwild1@gmail.com>
parents: 2489
diff changeset
170 return 201;
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172
1912
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
173 -- FIXME Duplicated from net.http.server
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
174
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
175 local codes = require "net.http.codes";
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
176 local headerfix = setmetatable({}, {
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
177 __index = function(t, k)
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
178 local v = "\r\n"..k:gsub("_", "-"):gsub("%f[%w].", s_upper)..": ";
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
179 t[k] = v;
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
180 return v;
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
181 end
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
182 });
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183
1912
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
184 local function send_response_sans_body(response, body)
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
185 if response.finished then return; end
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
186 response.finished = true;
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
187 response.conn._http_open_response = nil;
2065
624e3fed6f92 mod_http_upload: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 2053
diff changeset
188
1912
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
189 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]);
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
190 local headers = response.headers;
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
191 body = body or response.body or "";
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
192 headers.content_length = #body;
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
193
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
194 local output = { status_line };
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
195 for k,v in pairs(headers) do
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
196 t_insert(output, headerfix[k]..v);
1905
43fac0c2c772 mod_http_upload: Fix HEAD requests
Kim Alvefur <zash@zash.se>
parents: 1874
diff changeset
197 end
1912
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
198 t_insert(output, "\r\n\r\n");
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
199 -- Here we *don't* add the body to the output
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
200
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
201 response.conn:write(t_concat(output));
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
202 if response.on_destroy then
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
203 response:on_destroy();
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
204 response.on_destroy = nil;
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
205 end
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
206 if response.persistent then
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
207 response:finish_cb();
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
208 else
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
209 response.conn:close();
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
210 end
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212
1912
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
213 local serve_uploaded_files = module:depends("http_files").serve(storage_path);
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
214
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 local function serve_head(event, path)
1912
24c22cbb86e4 mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
Kim Alvefur <zash@zash.se>
parents: 1906
diff changeset
216 event.response.send = send_response_sans_body;
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 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
218 end
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219
1942
ff95d983940c mod_http_upload: Say Hello to anyone opening the "bare" HTTP URL (helpful to show that module is loaded correctly)
Kim Alvefur <zash@zash.se>
parents: 1914
diff changeset
220 local function serve_hello(event)
ff95d983940c mod_http_upload: Say Hello to anyone opening the "bare" HTTP URL (helpful to show that module is loaded correctly)
Kim Alvefur <zash@zash.se>
parents: 1914
diff changeset
221 event.response.headers.content_type = "text/html;charset=utf-8"
ff95d983940c mod_http_upload: Say Hello to anyone opening the "bare" HTTP URL (helpful to show that module is loaded correctly)
Kim Alvefur <zash@zash.se>
parents: 1914
diff changeset
222 return "<!DOCTYPE html>\n<h1>Hello from mod_"..module.name.."!</h1>\n";
ff95d983940c mod_http_upload: Say Hello to anyone opening the "bare" HTTP URL (helpful to show that module is loaded correctly)
Kim Alvefur <zash@zash.se>
parents: 1914
diff changeset
223 end
ff95d983940c mod_http_upload: Say Hello to anyone opening the "bare" HTTP URL (helpful to show that module is loaded correctly)
Kim Alvefur <zash@zash.se>
parents: 1914
diff changeset
224
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 module:provides("http", {
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 route = {
1942
ff95d983940c mod_http_upload: Say Hello to anyone opening the "bare" HTTP URL (helpful to show that module is loaded correctly)
Kim Alvefur <zash@zash.se>
parents: 1914
diff changeset
227 ["GET"] = serve_hello;
ff95d983940c mod_http_upload: Say Hello to anyone opening the "bare" HTTP URL (helpful to show that module is loaded correctly)
Kim Alvefur <zash@zash.se>
parents: 1914
diff changeset
228 ["GET /"] = serve_hello;
1772
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 ["GET /*"] = serve_uploaded_files;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 ["HEAD /*"] = serve_head;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 ["PUT /*"] = upload_data;
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 };
45f7e3c2557f mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 });
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
234
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
235 module:log("info", "URL: <%s>; Storage path: %s", module:http_url(), storage_path);