[mod_python] setting content_type intelligently

Gregory (Grisha) Trubetskoy grisha at modpython.org
Thu Feb 5 06:50:28 EST 2004



On Tue, 13 Jan 2004, Daniel J. Popowich wrote:

> In most of my mod_python apps if a request comes in for a file that I
> don't want to handle, let's say an image file, foo.gif, I raise
> apache.SERVER_RETURN with a value of apache.DECLINED.  Apache then
> handles the request and sends in the header:
>
>   Content-Type: image/gif
>
> However, I now have an app that wants to live in a <Location ...>
> directive so returning apache.DECLINED doesn't work: apache has no
> other means of handling the request so it returns a 404 error.

I am not sure I understand why that is... - what's the difference between
a request in <Location ...> and any other request?

> My only means of recourse seems to be something like this:
>
>     if NOT_HANDLING_PAGE:
>         req.write(open(filename).read())

As a sidenote, if you're using 3.1.2b, you should check out
req.sendfile().

>
> but content_type is not set properly (it appears to default to
> text/plain), so binary files, like image files, appear as gibberish in
> a browser.
>
> Two questions:
>
>    1.  What does mod_python/apache do if content_type is not set by a
>        handler?  Does it default to text/plain?

Nothing, IIRC.

>    2.  I'm looking for an intelligent means of setting the
>        content_type.  I know I can use mimetypes.guess_type(), but
>        that is based only on the file's extension.  Apache does more
>        than that, eg, if 'foo.gif' was named 'foo' apache still
>        determines the content type correctly as image/gif.  Is there a
>        way to tap into apache's content_type determination?

None that I know of.

Grisha


More information about the Mod_python mailing list