This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement handling of HEAD requests #7999
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c9c5f17
Servlets should respond to HEAD requests.
clokep c4e8839
Handle HEAD requests for direct serve resources.
clokep 445ae8d
Add changelog.
clokep 2f62a4c
Lint.
clokep 343801e
Fix typo.
clokep 2d751cc
Convert HEAD -> GET in async_render.
clokep f1c709b
Simplify the servlet handling.
clokep 09588c2
Clarify changelog.
clokep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a long standing bug where HTTP HEAD requests resulted in a 400 error. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,29 @@ def _callback(request, **kwargs): | |
self.assertEqual(channel.json_body["error"], "Unrecognized request") | ||
self.assertEqual(channel.json_body["errcode"], "M_UNRECOGNIZED") | ||
|
||
def test_head_request(self): | ||
""" | ||
JsonResource.handler_for_request gives correctly decoded URL args to | ||
the callback, while Twisted will give the raw bytes of URL query | ||
arguments. | ||
""" | ||
|
||
def _callback(request, **kwargs): | ||
return 200, {"result": True} | ||
|
||
res = JsonResource(self.homeserver) | ||
res.register_paths( | ||
"GET", [re.compile("^/_matrix/foo$")], _callback, "test_servlet", | ||
) | ||
|
||
# The path was registered as GET, but this is a HEAD request. | ||
request, channel = make_request(self.reactor, b"HEAD", b"/_matrix/foo") | ||
render(request, res, self.reactor) | ||
|
||
self.assertEqual(channel.result["code"], b"200") | ||
self.assertNotIn("body", channel.result) | ||
self.assertEqual(channel.headers.getRawHeaders(b"Content-Length"), [b"15"]) | ||
|
||
|
||
class OptionsResourceTests(unittest.TestCase): | ||
def setUp(self): | ||
|
@@ -255,7 +278,7 @@ def setUp(self): | |
self.reactor = ThreadedMemoryReactorClock() | ||
|
||
def test_good_response(self): | ||
def callback(request): | ||
async def callback(request): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somewhat unrelated, but the |
||
request.write(b"response") | ||
request.finish() | ||
|
||
|
@@ -275,7 +298,7 @@ def test_redirect_exception(self): | |
with the right location. | ||
""" | ||
|
||
def callback(request, **kwargs): | ||
async def callback(request, **kwargs): | ||
raise RedirectException(b"/look/an/eagle", 301) | ||
|
||
res = WrapHtmlRequestHandlerTests.TestResource() | ||
|
@@ -295,7 +318,7 @@ def test_redirect_exception_with_cookie(self): | |
returned too | ||
""" | ||
|
||
def callback(request, **kwargs): | ||
async def callback(request, **kwargs): | ||
e = RedirectException(b"/no/over/there", 304) | ||
e.cookies.append(b"session=yespls") | ||
raise e | ||
|
@@ -312,3 +335,19 @@ def callback(request, **kwargs): | |
self.assertEqual(location_headers, [b"/no/over/there"]) | ||
cookies_headers = [v for k, v in headers if k == b"Set-Cookie"] | ||
self.assertEqual(cookies_headers, [b"session=yespls"]) | ||
|
||
def test_head_request(self): | ||
"""A head request should work by being turned into a GET request.""" | ||
|
||
async def callback(request): | ||
request.write(b"response") | ||
request.finish() | ||
|
||
res = WrapHtmlRequestHandlerTests.TestResource() | ||
res.callback = callback | ||
|
||
request, channel = make_request(self.reactor, b"HEAD", b"/path") | ||
render(request, res, self.reactor) | ||
|
||
self.assertEqual(channel.result["code"], b"200") | ||
self.assertNotIn("body", channel.result) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, ideally we'd only do this if the class didn't have an
_async_render_HEAD
, but given we don't have any such classes, it doesn't really matterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I debated on checking for
_async_render_HEAD
, but it seemed silly to add processing for something we know is always going to be false! If we need this in the future we can add this check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems fair!