Skip to content

router.add_static is greedy #5250

@redsquirrel

Description

@redsquirrel

🐞 Describe the bug

The first argument for add_static matches on any path that starts with that string. For instance, if you specify "h", then add_static will match on "h" or "help" or "hoover".

💡 To Reproduce

Run this code:

from aiohttp import web

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

app = web.Application()

router = app.router

router.add_static("/h", "./")

router.add_routes([web.get('/', handle),
                web.get('/{name}', handle)])

if __name__ == '__main__':
    web.run_app(app)

Add a text file in the current working directory named test.txt and put some text in there.

Then navigate to the following URLs to demonstrate the issue:

The last link unexpectedly returns a 404.

💡 Expected behavior

I would have expected http://0.0.0.0:8080/hoover to have similar behavior to http://0.0.0.0:8080/groot, and render "Hello, hoover".

📋 Your version of the Python

3.8

📋 Your version of the aiohttp/yarl/multidict distributions

aiohttp 3.7.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions