Score:1

lighttpd 1.4.46+ serve specific file based on $HTTP["useragent"]

za flag

I have a lighttpd server version 1.4.46+ on Linux which serves file1.xml and file2.xml from htdocs directory as is. There are clients that request these files, each of those send a User-Agent header to the server, which have format MyApp 12345 where 12345 is the client version.

What I need to do is to parse the version number 12345 and serve either file1.xml or file2.xml, depending on client version number. file1.xml needs to be served to clients with version number below 11255 and file2.xml needs to be served to clients with version number 11255 and above, and reverse.

What is the easiest way to do this? Preferably to avoid redirects, and preferably to avoid shell or Perl scripts involved, if possible. Avoiding redirects is at highest priority, while using a script is at lower priority.

Any lighttpd gurus around to help me out please?

I have tried Perl style regular expression match (=~) on $HTTP["useragent"] and setting conditional server.document-root to server_root + "/htdocs/file1" or "/htdocs/file2" that include both files, but failed.

I wanted to try include_shell, but not sure how to pass arguments to the script that could handle the version number and possibly write an include configuration file based on results. I could possibly add if-conditions for version numbers 1-11254 to serve file1.xml but that list of conditions would become very huge, which looks wrong.

Gilles Quenot avatar
cn flag
GG Better here ;)
FearDC avatar
za flag
@GillesQuénot thank you sir! :)
Score:0
cn flag

You can use lighttpd mod_magnet and a few lines of custom lua code.

local r = lighty.r
if r.req_attr["physical.path"] == "/path/to/htdocs/file.xml" then
  local ver = string.match(r.req_header["User-Agent"], "MyApp (%d+)")
  local path = string.sub(r.req_attr["physical.path"], 1, -4)   -- remove '.xml'
  if ver and ver > 12345 then
    r.req_attr["physical.path] = path .. "2.xml"
  else
    r.req_attr["physical.path] = path .. "1.xml"
  end
  r.resp_header["Vary"] = "User-Agent"
end
return 0

The above is untested and for lighttpd 1.4.60 or later. The current version of lighttpd is lighttpd 1.4.69. If you're running lighttpd 1.4.46, it is likely that your systems are poorly maintained and very out-of-date, but you can find info on the older lighttpd mod_magnet interfaces on the same doc: lighttpd mod_magnet

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.