dynamic Content-Disposition header

Added by kf over 13 years ago

Operating System: Debian
Version of Lighttpd: 1.4.26

I am trying to achieve the following: a HTTP-GET to "http://<host>/file.mp3?filename=newfilename.mp3" will return the http://&lt;host&gt;/file.mp3 with the "Content-Disposition" header set to "attachment; filename=newfilename.mp3".

lighttpd.conf can easily set a static header like:

$HTTP["url"] =~ "\.(dcf|m4a|mp3|mp4|wma|wmv|ogg|zip)$" {
        setenv.add-response-header = ("Content-Disposition" => "attachment")

... but is there a way to grab the 'filename' parameter from $HTTP["querystring"] and append the value to my header?

Replies (3)

RE: dynamic Content-Disposition header - Added by darix over 13 years ago

I would use a small mod_magnet scriptlet for that.

RE: dynamic Content-Disposition header - Added by kf over 13 years ago

Thanks for the tip. I wrote this script:

-- match for pattern, return match
function findpattern(text, pattern, start)
  return string.sub(text, string.find(text, pattern, start))

-- if a match for 'filename=%w*.%w*' is found, then return the new header
function parseFileName(full_query)
  if full_query and string.len(full_query) then
    local the_query = findpattern(full_query,'filename=%w*.%w*')
    if the_query and string.len(the_query) then
      local filename = the_query:sub(10,string.len(the_query))
      if filename and string.len(filename) then
        return "attachment; filename="..filename
  return nil

-- maybe change the Content-Disposition header
local new_disposition_header = parseFileName(lighty.env["uri.query"])
if new_disposition_header and string.len(new_disposition_header) then
  lighty.header["Content-Disposition"] = new_disposition_header

RE: dynamic Content-Disposition header - Added by nitrox over 13 years ago

You might want to add it with a small description to our absoLUAtion collection.