Python bottle: UTF8 path string invalid when using app.mount()

跟風遠走 提交于 2019-12-10 19:55:17

问题


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? :/


回答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 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.



来源:https://stackoverflow.com/questions/23168292/python-bottle-utf8-path-string-invalid-when-using-app-mount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!