views:

32

answers:

2

Hi,

I am trying to write a web application that will serve uploaded files, and let the uploader specify the content type. For this to work, I need to be able to control the content-type when I serve the file. With most web-servers I'm aware of (e.g. Apache), the best I can do in that respect is set an appropriate suffix and hope that nobody uploads a file with an "unsupported" content-type. What I would like to do instead is set the content-type in my code processing the request -- e.g. have a database record describing the file and pull it from there. But then, it seems I would need to serve the whole file from my application, as if it were dynamic, and pay a severe performance penalty (among other things, losing the ability to serve it directly from CDNs such as Amazon S3).

So the question is --

  1. Is there a way for me to tell a web browser "Take the file from that URL, but use the content-type supplied in the redirect response"?

  2. Is there a less-optimal solution, which would still allow me to use a static-content-server for the file while using a dynamic application for the content-type?

Thanks, Shai.

+1  A: 

Yes. You can write an Apache module (in your language of choice, including C, Perl, Python, etc.) that filters a request by replacing the content-type header with one fetched from your data store. For example, using mod_python, you can create a PythonTypeHandler that does exactly what you describe, but lets Apache do the heavy lifting of serving the static content.

Jonathan Feinberg
And with most CDN's, you specify the CDN url for the file in your web page. The browser requests the file from the CDN. If the CDN doesn't have the file yet it pulls it from the real URL (your mod_perl/mod_python/whatever code) and serves the file to the browser using most of the same headers that your code sent out.
kbosak
Does a typical CDN respect your content-type, or does it do its own lookup?
Jonathan Feinberg
Yes, a CDN should leave those headers intact. Normally all or most of your headers will be left intact unless you configure the CDN not to pass them along.
Hans Lawrenz
A: 

You might also want to look into mod_mime_magic to take care of this. It will peek into the file to determine what type it is.

http://httpd.apache.org/docs/2.2/mod/mod%5Fmime%5Fmagic.html

Also, on the upload side you could check that the file extension matches the actual file type using whatever magic module is available for your preferred language.

Hans Lawrenz