Ticket #2433 (closed defect: fixed)
paste.util.mimeparse.best_match compatibility with '*' mime type
| Reported by: | amol | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | 2.1rc1 |
| Component: | TurboGears | Version: | 2.0.3 |
| Severity: | normal | Keywords: | |
| Cc: | dave@… |
Description
Copying here the content of email I sent to the TG2 ML as it has been asked to me to open a ticket about this issue.
As RFC 2616 media range in HTTP_ACCEPT should be specified as
media-range = ( "*/*"
| ( type "/" "*" ) | ( type "/" subtype ) ) *( ";" parameter )
so when "all" is meant "*/*" should be passed, but some user agents, like the facebook external link retriever pass '*' instead of '*/*' this makes paste.util.mimeparse.best_match crash on paste.util.mimeparse.parse_mime_type
paste.util.mimeprase.best_match is used by Turbogears to detect which template to render, and so each TG2 application crashes when contacted by facebook.
A solution is to change paste.util.mimeparse.parse_mime_type to support the '*' syntax by adding
if parts[0].strip() == '*':
parts[0] = '*/*'
just before
(type, subtype) = parts[0].split("/")
this fixes the problem and I have already patched all my deployed TG2 apps, but I think that a more general fix might be good and also people at paste and mimeparse might be interested in having the patch. (This happens with Paste-1.7.2 at least)
The working version of the parse_mime_type method is the following one:
def parse_mime_type(mime_type):
"""Carves up a mime_type and returns a tuple of the
(type, subtype, params) where 'params' is a dictionary of all the parameters for the media range. For example, the media range 'application/xhtml;q=0.5' would get parsed into:
('application', 'xhtml', {'q', '0.5'}) """
parts = mime_type.split(";") params = dict([tuple([s.strip() for s in param.split("=")])\
for param in parts[1:] ])
if parts[0].strip() == '*':
parts[0] = '*/*'
(type, subtype) = parts[0].split("/") return (type.strip(), subtype.strip(), params)
Change History
comment:2 Changed 22 months ago by brondsem
I'm encountering this exception fairly frequently. And #2487 describes the same problem, I think.
It looks like Paste 1.7.3 was release recently, and includes some mimeparse fixes. http://pythonpaste.org/news.html#id1
As a bit of a side note: very frequently our controllers only have one @expose type, so in lookup_template_engine, if len(self.engines) == 1, that content_type could be used and best_match does not have to be called at all.
comment:4 Changed 22 months ago by chrisz
Does this only affect TG 2.0.x? If not, the milestone should be set to 2.1.
comment:5 Changed 22 months ago by brondsem
- Milestone changed from 2.0.* bugfix to 2.1
It effects 2.1b1 also.
The paste guys have an issue in their trac for this, and a fix in the paste trunk as of 13 months ago — and it still hasn't been released.
Guess I'll monkeypatch for now. Sigh.