Python Bottle: Utf8 Path String Invalid When Using App.mount()
Trying to use special chars in an URL path fails when using app.mount: http://127.0.0.1:8080/test/äöü results in: Error: 400 Bad Request Invalid path string. Expected UTF-8 te
Solution 1:
Yes, as it seems this is a bug in bottle.
The problem is in the _handle
method:
def_handle(self, environ):
path = environ['bottle.raw_path'] = environ['PATH_INFO']
if py3k:
try:
environ['PATH_INFO'] = path.encode('latin1').decode('utf8')
except UnicodeError:
return HTTPError(400, 'Invalid path string. Expected UTF-8')
Here environ['PATH_INFO']
is converted to utf8, so when the same method is called again for the mounted app, the contents will already be utf8, therefore the conversion will fail.
A very quick fix would be to change that code to skip the conversion if it was already done:
def_handle(self, environ):
converted = 'bottle.raw_path'in environ
path = environ['bottle.raw_path'] = environ['PATH_INFO']
if py3k andnot converted:
try:
environ['PATH_INFO'] = path.encode('latin1').decode('utf8')
except UnicodeError:
return HTTPError(400, 'Invalid path string. Expected UTF-8')
And it would probably be good to file a bug report against bottle.
Post a Comment for "Python Bottle: Utf8 Path String Invalid When Using App.mount()"