Mercurial > prosody-modules
annotate mod_http_upload/mod_http_upload.lua @ 2207:c45ad4b7aaa3
mod_http_upload: Add additional debug logging
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 12 Jun 2016 02:51:08 +0200 |
parents | 40824a38d505 |
children | e654d6e1fb50 |
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"; |
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; |
1966
3b748666ff97
mod_http_upload: URL-encode filename in case it contains characters that need escaping (thanks Thilo)
Kim Alvefur <zash@zash.se>
parents:
1942
diff
changeset
|
14 local urlencode = require"util.http".urlencode; |
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; |
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
|
16 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
|
17 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
|
18 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
|
19 |
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
|
20 local function join_path(a, b) |
1817
1971ff719e72
mod_http_upload: Fix typo (thanks mo)
Kim Alvefur <zash@zash.se>
parents:
1815
diff
changeset
|
21 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
|
22 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
|
23 |
1851
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
24 -- config |
2053
40056a27f394
mod_http_upload: Lower default size limit to 1MB
Kim Alvefur <zash@zash.se>
parents:
1967
diff
changeset
|
25 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
|
26 |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 -- depends |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 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
|
29 module:depends("disco"); |
1772
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 -- namespace |
1874
8ef500508c59
mod_http_upload.lua: use official namespace from XEP-0363
Michael Töglhofer <michael@toeglhofer.net>
parents:
1851
diff
changeset
|
32 local xmlns_http_upload = "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
|
33 |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 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
|
35 |
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
|
36 module:add_extension(dataform { |
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
|
37 { name = "FORM_TYPE", type = "hidden", value = xmlns_http_upload }, |
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
|
38 { 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
|
39 }: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
|
40 |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 -- state |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 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
|
43 |
1967
2ce2b194d501
mod_http_upload: Make file system path configurable
Kim Alvefur <zash@zash.se>
parents:
1966
diff
changeset
|
44 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
|
45 lfs.mkdir(storage_path); |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 -- hooks |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 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
|
49 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
|
50 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
|
51 -- local clients only |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 if origin.type ~= "c2s" then |
2207
c45ad4b7aaa3
mod_http_upload: Add additional debug logging
Kim Alvefur <zash@zash.se>
parents:
2193
diff
changeset
|
53 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
|
54 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
|
55 return true; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 end |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 -- validate |
1848
e5243fa16210
mod_http_upload: Cache first-level child <request> in local variable
Kim Alvefur <zash@zash.se>
parents:
1817
diff
changeset
|
58 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
|
59 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
|
60 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
|
61 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
|
62 return true; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end |
1851
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
64 local filesize = tonumber(request:get_child_text("size")); |
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
65 if not filesize then |
2207
c45ad4b7aaa3
mod_http_upload: Add additional debug logging
Kim Alvefur <zash@zash.se>
parents:
2193
diff
changeset
|
66 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
|
67 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
|
68 return true; |
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
69 elseif filesize > file_size_limit then |
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
70 origin.send(st.error_reply(stanza, "modify", "not-acceptable", "File too large", |
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
71 st.stanza("file-too-large", {xmlns=xmlns_http_upload}) |
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
72 :tag("max-size"):text(tostring(file_size_limit)))); |
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
73 return true; |
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
74 end |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 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
|
76 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
|
77 local random = uuid(); |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 pending_slots[random.."/"..filename] = origin.full_jid; |
1966
3b748666ff97
mod_http_upload: URL-encode filename in case it contains characters that need escaping (thanks Thilo)
Kim Alvefur <zash@zash.se>
parents:
1942
diff
changeset
|
79 local url = module:http_url() .. "/" .. random .. "/" .. urlencode(filename); |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 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
|
81 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
|
82 origin.send(reply); |
2207
c45ad4b7aaa3
mod_http_upload: Add additional debug logging
Kim Alvefur <zash@zash.se>
parents:
2193
diff
changeset
|
83 origin.log("debug", "Given upload slot %q", random); |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 return true; |
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 |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 -- http service |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 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
|
89 if not pending_slots[path] then |
2191
e47046abf568
mod_http_upload: Add more logging
Kim Alvefur <zash@zash.se>
parents:
2066
diff
changeset
|
90 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
|
91 return; -- 404 |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 end |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 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
|
94 if not random then |
2207
c45ad4b7aaa3
mod_http_upload: Add additional debug logging
Kim Alvefur <zash@zash.se>
parents:
2193
diff
changeset
|
95 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
|
96 return 400; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 end |
1851
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
98 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
|
99 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
|
100 return 400; |
03c5639882a7
mod_http_upload: Add support for a file size limit
Kim Alvefur <zash@zash.se>
parents:
1850
diff
changeset
|
101 end |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 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
|
103 if not lfs.mkdir(dirname) then |
2192
bb8f7785aed7
mod_http_upload: Demote some errors to warnings
Kim Alvefur <zash@zash.se>
parents:
2191
diff
changeset
|
104 module:log("warn", "Could not create directory %s for upload", dirname); |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 return 500; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 end |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 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
|
108 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
|
109 if not fh then |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 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
|
111 return 500; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 end |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 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
|
114 if not ok then |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 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
|
116 os.remove(full_filename); |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 return 500; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 end |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 ok, err = fh:close(); |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 if not ok then |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 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
|
122 os.remove(full_filename); |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 return 500; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 end |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 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
|
126 pending_slots[path] = nil; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 return 200; |
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 |
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
|
130 -- 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
|
131 |
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
|
132 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
|
133 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
|
134 __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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 }); |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 |
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
|
141 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
|
142 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
|
143 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
|
144 response.conn._http_open_response = nil; |
2065
624e3fed6f92
mod_http_upload: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents:
2053
diff
changeset
|
145 |
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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 |
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
|
151 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
|
152 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
|
153 t_insert(output, headerfix[k]..v); |
1905
43fac0c2c772
mod_http_upload: Fix HEAD requests
Kim Alvefur <zash@zash.se>
parents:
1874
diff
changeset
|
154 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
|
155 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
|
156 -- 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
|
157 |
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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 end |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 end |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 |
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
|
170 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
|
171 |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 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
|
173 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
|
174 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
|
175 end |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 |
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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 module:provides("http", { |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 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
|
184 ["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
|
185 ["GET /"] = serve_hello; |
1772
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 ["GET /*"] = serve_uploaded_files; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 ["HEAD /*"] = serve_head; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 ["PUT /*"] = upload_data; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 }; |
45f7e3c2557f
mod_http_upload: Implementation of Conversations HTTP upload file transfer mode
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 }); |
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
|
191 |
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
|
192 module:log("info", "URL: <%s>; Storage path: %s", module:http_url(), storage_path); |