Software /
code /
prosody-modules
Comparison
mod_http_upload/mod_http_upload.lua @ 1912:24c22cbb86e4
mod_http_upload: Duplicate code from net.http.server in order send proper HEAD responses
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 18 Oct 2015 17:25:14 +0200 |
parent | 1906:5849d70bcd37 |
child | 1914:b01d60dfa405 |
comparison
equal
deleted
inserted
replaced
1911:0225f2a551d0 | 1912:24c22cbb86e4 |
---|---|
9 | 9 |
10 -- imports | 10 -- imports |
11 local st = require"util.stanza"; | 11 local st = require"util.stanza"; |
12 local lfs = require"lfs"; | 12 local lfs = require"lfs"; |
13 local uuid = require"util.uuid".generate; | 13 local uuid = require"util.uuid".generate; |
14 local t_concat = table.concat; | |
15 local t_insert = table.insert; | |
14 | 16 |
15 local function join_path(a, b) | 17 local function join_path(a, b) |
16 return a .. package.config:sub(1,1) .. b; | 18 return a .. package.config:sub(1,1) .. b; |
17 end | 19 end |
18 | 20 |
109 module:log("info", "File uploaded by %s to slot %s", pending_slots[path], random); | 111 module:log("info", "File uploaded by %s to slot %s", pending_slots[path], random); |
110 pending_slots[path] = nil; | 112 pending_slots[path] = nil; |
111 return 200; | 113 return 200; |
112 end | 114 end |
113 | 115 |
114 local serve_uploaded_files = module:depends("http_files").serve(storage_path); | 116 -- FIXME Duplicated from net.http.server |
115 local http_server = require"net.http.server"; | |
116 | 117 |
117 local function size_only(response, body) | 118 local codes = require "net.http.codes"; |
118 if body then | 119 local headerfix = setmetatable({}, { |
119 response.headers.content_length = #body; | 120 __index = function(t, k) |
121 local v = "\r\n"..k:gsub("_", "-"):gsub("%f[%w].", s_upper)..": "; | |
122 t[k] = v; | |
123 return v; | |
120 end | 124 end |
121 return http_server.send_response(response); | 125 }); |
126 | |
127 local function send_response_sans_body(response, body) | |
128 if response.finished then return; end | |
129 response.finished = true; | |
130 response.conn._http_open_response = nil; | |
131 | |
132 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); | |
133 local headers = response.headers; | |
134 body = body or response.body or ""; | |
135 headers.content_length = #body; | |
136 | |
137 local output = { status_line }; | |
138 for k,v in pairs(headers) do | |
139 t_insert(output, headerfix[k]..v); | |
140 end | |
141 t_insert(output, "\r\n\r\n"); | |
142 -- Here we *don't* add the body to the output | |
143 | |
144 response.conn:write(t_concat(output)); | |
145 if response.on_destroy then | |
146 response:on_destroy(); | |
147 response.on_destroy = nil; | |
148 end | |
149 if response.persistent then | |
150 response:finish_cb(); | |
151 else | |
152 response.conn:close(); | |
153 end | |
122 end | 154 end |
123 | 155 |
156 local serve_uploaded_files = module:depends("http_files").serve(storage_path); | |
157 | |
124 local function serve_head(event, path) | 158 local function serve_head(event, path) |
125 event.response.send = size_only; | 159 event.response.send = send_response_sans_body; |
126 return serve_uploaded_files(event, path); | 160 return serve_uploaded_files(event, path); |
127 end | 161 end |
128 | 162 |
129 module:provides("http", { | 163 module:provides("http", { |
130 route = { | 164 route = { |