[mod_python] setting content_type intelligently

Daniel J. Popowich dpopowich at mtrsd.k12.ma.us
Thu Feb 5 11:08:07 EST 2004

Michael C. Neel writes:
> > <Location ...> directives live in url space, completely outside from
> > the filesystem.  I have my app configured like this:
> > 
> > 
> >     <Location /site>
> >     SetHandler python-program
> >     PythonHandler ...
> >     ...
> >     </Location>
> > 
> > When requests come in, such as http://myserver/site/foo/bar, my
> > handler will use 'foo/bar' to determine what it sends back to the
> > client and, generally, use the pathinfo as a relative path from a data
> > directory configured, purposefully, outside of DocumentRoot.  So, how
> > do I set content-type correctly for content I don't generate, ie,
> > images?
> > 
> > If my app was <Directory> based, I could raise apache.SERVER_RETURN
> > with apache.DECLINED; apache would have means to find the file and "do
> > the right thing," but inside <Location> there is NO filesystem, thus
> > my 404 error.
> > 
> You can still return apache.DECLINED from Location's, I do this all the
> time for files I don't want to handle.  Since your location doesn't
> exist under document root, make sure the items you are declining do.
> ...

I'm aware I can (technically) return apache.DECLINED, it's just that
it doesn't work for me because the images are outside of DocumentRoot.

> However, a better solution imho would be to not reference images in the
> /site url, i.e. images are all in /images under doc root and therefore
> the location /site would never get called.

For cultural reasons this is not feasible for this app.  My users
demand their images be "near" their docs.  Oh, tho I've tried to press
the issue.

Side note: As per Grisha's recommendation I checked out
req.sendfile(FILE) in my app.  It sends the file to the client (and I
presume more efficiently than req.write(open(FILE).read()) ever
could), but that's all, no header processing by apache, so I still
need to come up with a way to determine content type.

Sure wish I could tap into apache's content-type determination code.
That would be ideal.  In the meantime, mimetypes.guess_type() seems
to be my only solution, or talk my users into putting all images under



More information about the Mod_python mailing list