Ticket #763 (defect)
Opened 9 months ago
Last modified 9 months ago
Exception when parsing Content-Type
Status: closed (fixed)
| Reported by: | dag@brattli.net | Assigned to: | rdelon |
|---|---|---|---|
| Priority: | lowest | Milestone: | 3.1 |
| Component: | CherryPy code | Keywords: | |
| Cc: |
CherryPy 3.0.2 and 3.1beta throws exception when parsing content types that ends with a ";" An example of a Content-Type header that gives such an error is:
Content-Type: application/x-www-form-urlencoded; charset=utf-8;
It will produce the following exception:
*** got error ***
[08/Jan/2008:08:37:50] Traceback (most recent call last):
File "/Library/Python/2.5/site-packages/CherryPy-3.1b1-py2.5.egg/cherrypy/_cprequest.py", line 90, in run
hook()
File "/Library/Python/2.5/site-packages/CherryPy-3.1b1-py2.5.egg/cherrypy/_cprequest.py", line 58, in __call__
return self.callback(**self.kwargs)
File "/Library/Python/2.5/site-packages/CherryPy-3.1b1-py2.5.egg/cherrypy/lib/encoding.py", line 10, in decode
ct = cherrypy.request.headers.elements("Content-Type")
File "/Library/Python/2.5/site-packages/CherryPy-3.1b1-py2.5.egg/cherrypy/lib/http.py", line 350, in elements
return header_elements(key, h)
File "/Library/Python/2.5/site-packages/CherryPy-3.1b1-py2.5.egg/cherrypy/lib/http.py", line 191, in header_elements
hv = HeaderElement.from_str(element)
File "/Library/Python/2.5/site-packages/CherryPy-3.1b1-py2.5.egg/cherrypy/lib/http.py", line 131, in from_str
ival, params = cls.parse(elementstr)
File "/Library/Python/2.5/site-packages/CherryPy-3.1b1-py2.5.egg/cherrypy/lib/http.py", line 120, in parse
key = atom.pop(0)
IndexError: pop from empty list
The problem with the content-type is the separator ";" at the end. One could question if it legal to end the content-type header with a ";", but CherryPy should probably not give an expection when trying to parse it. CherryPy 2.2.1 does not throw an exception when parsing such a content-type header.
A simple fix for lib/http.py is:
def parse(elementstr):
"""Transform 'token;key=val' to ('token', {'key': 'val'})."""
# Split the element into a value and parameters. The 'value' may
# be of the form, "token=token", but we don't split that here.
- atoms = [x.strip() for x in elementstr.split(";")]
+ atoms = [x.strip() for x in elementstr.split(";") if x]
initial_value = atoms.pop(0).strip()
params = {}
for atom in atoms:
atom = [x.strip() for x in atom.split("=", 1) if x.strip()]
key = atom.pop(0)
if atom:
val = atom[0]
else:
val = ""
params[key] = val
return initial_value, params
parse = staticmethod(parse)
-- Dag
Change History
01/12/08 17:13:39: Modified by fumanchu
- status changed from new to closed.
- resolution set to fixed.


It probably SHOULD raise a 400 error, but it's easier to just ignore the bad syntax. Fixed in [1836].