Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't create a new resource with allow_head=True #2585

Merged
merged 2 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/2585.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not create a new resource on `router.add_get(..., allow_head=True)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's improvement, not removal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's both optimization and breaking change: in aiohttp 2.x the call router.add_get('/', handler, name='name') adds two named resources: name for GET and name-head for HEAD.
But both resources are identical.

After PR merging I want add generic optimization for sequential add_route() calls if created resource is the same.

router.add_get('/', ...)
router.add_post('/', ...)

should create the single resource if path and name are the same.

10 changes: 3 additions & 7 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,14 +879,10 @@ def add_get(self, path, handler, *, name=None, allow_head=True, **kwargs):
Shortcut for add_route with method GET, if allow_head is true another
route is added allowing head requests to the same endpoint
"""
resource = self.add_resource(path, name=name)
if allow_head:
# it name is not None append -head to avoid it conflicting with
# the GET route below
head_name = name and '{}-head'.format(name)
self.add_route(hdrs.METH_HEAD, path, handler,
name=head_name, **kwargs)
return self.add_route(hdrs.METH_GET, path, handler, name=name,
**kwargs)
resource.add_route(hdrs.METH_HEAD, handler, **kwargs)
return resource.add_route(hdrs.METH_GET, handler, **kwargs)

def add_post(self, path, handler, **kwargs):
"""
Expand Down