[mod_python] Enhancements for better content negotiation

Deron Meranda deron.meranda at gmail.com
Thu Feb 2 17:30:52 EST 2006

I have a bunch of code I was thinking of contributing to mod_python,
but would like some opinions before doing so (because I don't
know if this is the best place)...

Basically I wrote some utility functions which can be used to
assist with content negotiation; such as parsing the various
Accept-* headers *correctly*.  I am in fact using these in production
systems now because I do quite a bit of content negotiation.

I often see a bunch of example code (or even production code)
which does something like,

  if req.headers_in['Accept'].find('image/xyz') >= 0:

That is really broken per RFC 2616 (HTTP 1.1)!   It totally ignores the
quality factors and other rules (see sections 14.1 - 14.4).  For example
if the Accept header sent was "image/xyz;q=0", then in fact this
is the user agent saying to NEVER send image/xyz!

I think this sloppy way of testing is because the syntax and rules
for accept headers is actually fairly complex.  So this is a perfect
opportunity for reusable functions which assist with this (much like
the apache.parse_qs function).

Essentially I have functions that:

 1. Parse and sort any Accept-* header according to the RFC's BNF
 2. Negotiate the best content type
 3. Negotiate the best language
 4. Negotiate the best encoding
 5. Negotiate the best charset

It also knows how to handle wildcards, hierarchical language tags,
charset name aliases, etc.  And you can tell it to ignore the "super"
wildcards like */* too.

As a quick example you can do something like

  acc = req.headers_in['Accept']
  ct = acceptable_content_type( acc, ['text/html','application/xhtml+xml'] )

and it will tell you which of the two formats the browser supports/prefers,
if either, according to all the complex rules in the RFC.

I have some preliminary questions:

* Is this something that seems useful to others?

* Is mod_python util a preferable place to consider putting these,
or maybe this should perhaps go to something larger like
WSGI or some other Web-SIG project?
Deron Meranda

More information about the Mod_python mailing list