Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

test.py:

#!/usr/bin/python
import bottle
import testapp
bottle.debug(True)
app = bottle.Bottle()
app.mount('/test',testapp.app)
app.run(reloader=True, host='0.0.0.0', port=8080)
run(host="localhost",port=8080)

testapp.py:

import bottle
app = bottle.Bottle()
@app.route("/:category", method=["GET","POST"])
def admin(category):
    try:
        return category
    except Exception(e):
        print ("e:"+str(e))

Where as the same code works well when not using app.mount:

test_working.py:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import bottle
import testapp
bottle.debug(True)
app = bottle.Bottle()
@app.route("/test/:category", method=["GET","POST"])
def admin(category):
    try:
        return category
    except Exception(e):
        print ("e:"+str(e))
app.run(reloader=True, host='0.0.0.0', port=8080)
run(host="localhost",port=8080)

This looks like a bug or am I missing something here? :/

like image 326
onny Avatar asked Mar 07 '26 03:03

onny


1 Answers

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 and not 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.

like image 51
mata Avatar answered Mar 09 '26 16:03

mata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!