logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Third-party Modules View previous topic :: View next topic
Reply to topic   Topic: How can I set response headers for a specific MIME type?
Author
jgo655uk



Joined: 12 Sep 2014
Posts: 6
Location: UK

PostPosted: Wed 17 Dec '14 15:59    Post subject: How can I set response headers for a specific MIME type? Reply with quote

Hi,

I'm looking for some advice on how to achieve something in Apache.

I have an instance of Apache 2.2.15 configured as a simple reverse proxy to a 2nd Apache server (same version), using mod_proxy.

The 2nd Apache server simply serves up eBook EPUB files (we're a library) from some attached storage.

Each epub file is stored named with a system identifier and a meaningless file extension, e.g. 23423423454.file. This is part of a bigger strategic system, so not something we can just change.

The 2nd Apache server uses the magic module to correctly set the MIME type in the response headers when any epub file is requested.

So far, so good.

The problem/challenge I am facing is that for various reasons, we need Apache (on either of the 2 servers in the response chain) to serve up the requested file to the client with a '.epub' file extension, but only for epubs.

The users request the epub files by a generic identifier that doesn't reflect anything about the underlying content - the same format is used for other files, e.g. PDFs for example. If there was an identifier specific to epubs, we've worked out we could do something like this using mod_headers (works with Firefox):

SetEnvIf Request_URI ^/start_of_epub_specific_identifier force-epub
Header set Content-Disposition "inline; filename=genericname.epub" env=force-epub
Header set Content-Type application/epub-zip env=force-epub


However, we don't have the luxury of knowing when the request is first made that that it will eventually resolve to an epub file. We only know that once the file is being sent back in the response body and the MIME type has been set in the response headers, but the above approach ONLY works with request headers.


From what I've read, I might be able to do what I need using mod_perl and checking the response headers for the epub MIME type, and if found, then add/edit the 2 headers listed above.

I can't believe that I'm the first person to have this requirement though - has anyone else ever done anything similar, and if so could you explain what/how you did it?

Any suggestions gratefully received!


Thanks
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Thu 18 Dec '14 12:36    Post subject: Reply with quote

if it is from a specific url or directory I would use a match to that and use ForceType.

Or use a regex

RewriteRule someRegEx$ - [T=application/epub-zip]
Back to top
jgo655uk



Joined: 12 Sep 2014
Posts: 6
Location: UK

PostPosted: Thu 18 Dec '14 18:20    Post subject: Reply with quote

Unfortunately it's same base URL/identifier syntax for every file type we serve, so we cannot set anything specific to epubs in the initial request, which is part of our problem.

The MIME type is also not a problem - this is set by the server at the bottom of the stack, so the response does have the approriate headers saying that epub files are MIME type 'application/epub+zip' (from memory)

In a nutshell, although the user might request http://server1/id123, we want their browser to download and save the served file as id123.epub, rather than just id123

So - can something like mod_perl be used on a proxy between the client and the source server, to detect when a proxied response has the MIME type 'application/epub+zip' and perform some action on that response, in this case probably setting or editing another header value?

Thank you for any suggestions Smile
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Thu 18 Dec '14 18:50    Post subject: Reply with quote

I don't know perl... only PHP Wink
What ever language you use. You could rewrite any request for any file that does not exist on the server through your script that does the magic.

Code:

RewriteEngine on
# is not a file
RewriteCond %{SCRIPT_FILENAME} !-f
#is not a directory
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ your.pl/$1
Back to top
jgo655uk



Joined: 12 Sep 2014
Posts: 6
Location: UK

PostPosted: Wed 07 Jan '15 21:44    Post subject: Reply with quote

Just to give an update on this - I finally stumbled across a solution in the form of an apparently little known module called mod_setenvifplus

http://opensource.adnovum.ch/mod_setenvifplus/

Apparently this is derived from the standard Apache modules mod_setenvif and mod_headers.

Crucially, it all allows an equivalent of SetEnvIf (ResponseSetEnvIfPlus) to be used against the RESPONSE headers, which is exactly what I wanted!
Back to top


Reply to topic   Topic: How can I set response headers for a specific MIME type? View previous topic :: View next topic
Post new topic   Forum Index -> Third-party Modules