sanic: Routing is incorrect with some special characters (e.g. dot)

I want to use something like this:

@app.route('/path/to/file.<ext>')
async def generate_some_file(request, ext):
    ...

It works, but the dot is interpreted as a special regex character that represents ANY character, so there urls are working:

  • /path/to/file.txt
  • /path/to/fileQtxt
  • /path/to/file txt
  • /path/to/file%3Ftxt etc.

When I try to escape the dot:

@app.route(r'/path/to/file\.<ext>')
async def generate_some_file(request, ext):
    ...

then this route works as expected, but app.url_for('generate_some_file', ext=ext) returns broken url:

/path/to/file\.txt

Is also affects other regex characters e.g. ^, $, [, ] etc. For example,

@app.route('/path/to/fi[abcl]e.<ext>')
async def generate_some_file(request, ext):
    return response.text(app.url_for('generate_some_file', ext=ext))

When I open GET /path/to/file.txt it returns /path/to/fi[abcl]e.txt that is totally incorrect.

Note that Flask does not interpret these characters as special and it works as expected (werkzeug/routing.py uses re.escape everywhere).

I use Sanic 18.12 (because 19.3 is not yet uploaded to PyPI)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (13 by maintainers)

Most upvoted comments

@harshanarayana it works but is too complicated for me 😃 /path/to/file.<ext> looks nicer

Well, we can make it much simpler by turning it into @app.route(uri="/path/to/<file:file(.*)\.(.*)>") 😆