pallets / flask

The Python micro framework for building web applications.
https://flask.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
68.09k stars 16.22k forks source link

Cannot route non-ascii URLs #553

Closed louiz closed 12 years ago

louiz commented 12 years ago

Try this simple routing function:

@app.route(u'/♥/') def heart(): return "It works"

Or @app.route('/%E2%99%A5/') def heart(): return "It works"

Trying to GET /%E2%99%A5/ HTTP/1.1 (which is what a browser does when trying to reach http://example.com/♥/)

You get a 404 not found in return.

I could not found anything in the doc talking about that, so I assume it’s a bug.

kailashbuki commented 12 years ago

Hi, i can see that you are messing up with your encodings here. If you are using non-ascii URLs, make sure to decode those characters using a suitable encoding. For example, your example works with a small change(using utf-8 encoding);

@app.route("/%s/" % "♥".decode('utf-8'))
def heart():
    return 'it works'

I am really not sure if this is something flask must incorporate or not.

mitsuhiko commented 12 years ago

If you use unicode strings properly it works out of the box:

@app.route(u"/♥")
def heart():
    return "It works!"
louiz commented 12 years ago

Hm, actually you’re right, it works perfectly as is. I don’t know what I missed when I first tested this. Sorry about the noise.

xyproto commented 11 years ago

Looking forward to a release of flask for Python 3.

ghost commented 11 years ago

@xyproto Surprise! Flask is already available on Python3. Just pip install flask

xyproto commented 11 years ago

@gioi Perfect, I'll package it for Arch Linux then. (I could see no hints of the Python 3 version being released on the flask webpage).

xyproto commented 11 years ago

Ok, moved. "python-flask" is now the official Python 3 package for Arch Linux. The "setup.py test" checks fails for werkzeug for python3, though, but I'm sure you had that in mind when you made the recommendation. Thanks, gioi!

ghost commented 11 years ago

Thank you, @xyproto. Would you please check that mitsuhiko/werkzeug#426 corresponds to the failing test?

xyproto commented 11 years ago

Sure. It seems to be a different test that fails:

test_shared_data_middleware (werkzeug.testsuite.wsgi.WSGIUtilsTestCase) ... ERROR
test_shareddatamiddleware_get_file_loader (werkzeug.testsuite.wsgi.WSGIUtilsTestCase) ... ok

======================================================================
ERROR: test_shared_data_middleware (werkzeug.testsuite.wsgi.WSGIUtilsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/build/python-werkzeug/src/python-werkzeug-0.9.1/werkzeug/testsuite/wsgi.py", line 36, in test_shared_data_middleware
    with open(path.join(test_dir, to_native(u'\xe4\xf6\xfc', 'utf-8')), 'w') as test_file:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 15-17: ordinal not in range(128)

----------------------------------------------------------------------
Ran 317 tests in 1.662s

FAILED (errors=1)
ghost commented 11 years ago

Argh, horrible news. You should report it as a bug, in the meantime I'll try to reproduce this.

DarkRedman commented 11 years ago

@gioi I've already tried to install Flask for Python but it never seems available, and it does install flask for python2, it seems pip is for python2. I'm on Arch and Ubuntu I also checked in repositories but python3-flask doesn't exist.

xyproto commented 11 years ago

@gio Will do.

@DarkRedman: if you're on Arch, just install "python-flask" (not "python3-flask", python 3 is the default python on Arch and thus only called "python")

ghost commented 11 years ago

@DarkRedman http://stackoverflow.com/questions/10763440/how-to-install-python3-version-of-package-via-pip

ghost commented 10 years ago

I just tried this in Python 3 and it gave me a 404 for a unicode route; is this a bug or am I just doing something wrong?