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

Fixed #24720 -- Moved append slash checking in CommonMiddleware to process_response() #4574

Closed
wants to merge 3 commits into from

Conversation

electricjay
Copy link
Contributor

Addresses issue #24720 - using middleware.common to append slashes causes extra overhead to all requests that do not end in a slash

https://code.djangoproject.com/ticket/24720

To avoid resolving the url twice for all urls that do not end in a
slash - only check if we should add the slash if we receive a 404
response in process_response (if we are prepending www it still does
the check in process_request to avoid multiple redirects)

Avoiding this double url resolution speeds up the affected requests by about 5%

This pull request exposes a bug(?!?) in the TestClient (see
test_client_regress.tests.ContextTests.test_nested_requests). It
appears that returning a redirect from process_response behaves
differently than returning a redirect from process_request that leads
to extra context being added to the response.context when making
nested calls. Possibly related to the template_rendered signal (or not).

…called

To avoid resolving the url twice for all urls that do not end in a
slash - only check if we should add the slash if we receive a 404
response in process_response (if we are prepending www  it still does
the check in process_request to avoid multiple redirects)

This exposes a bug(?!?) in the TestClient (see
test_client_regress.tests.ContextTests.test_nested_requests).  It
appears that returning a redirect from process_response behaves
differently than returning a redirect from process_request that leads
to extra context being added to the response.context.
@@ -39,6 +39,8 @@ def test_append_slash_have_slash(self):
"""
request = self.rf.get('/slash/')
self.assertEqual(CommonMiddleware().process_request(request), None)
response = HttpResponseNotFound
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing parens ()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good eye

@electricjay
Copy link
Contributor Author

After some research I have determined that what I thought was a bug in the test client is just a minor issue with the test test_client_regress.tests.ContextTests.test_nested_requests. The triggering of the common middleware appears to be accidental and not an intentional part of the test.

The extra context is added to the response because a 404 response is generated using a template (but never sent to the client) when the original url lookup fails. We may want to note this in the documentation, but I am not sure of the best place for it.

The triggering of the “add slash” functionality of the common
middleware appears to be accidental and not an intentional part of the
test.
@timgraham timgraham changed the title middleware.common: Defer checking if we should append a slash until process_response is called Fixed #24720 -- Moved append slash checking in CommonMiddleware to process_response() May 5, 2015
@timgraham
Copy link
Member

This looks good. Here are some cosmetic edits: http://dpaste.com/329WCVC
A good place to note the backwards incompatible change you mentioned would be the 1.9 release notes.
Please squash commits when you update it.

@timgraham
Copy link
Member

@electricjay, are you able to apply my patch, add release notes, and squash commits?

@timgraham
Copy link
Member

merged in 434d309, thanks!

@timgraham timgraham closed this Jul 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants