Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maunium
synapse
Commits
68125098
Unverified
Commit
68125098
authored
4 years ago
by
Patrick Cloke
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Implement handling of HTTP HEAD requests. (#7999)
parent
2a89ce8c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
changelog.d/7999.bugfix
+1
-0
1 addition, 0 deletions
changelog.d/7999.bugfix
synapse/http/server.py
+11
-5
11 additions, 5 deletions
synapse/http/server.py
tests/test_server.py
+42
-3
42 additions, 3 deletions
tests/test_server.py
with
54 additions
and
8 deletions
changelog.d/7999.bugfix
0 → 100644
+
1
−
0
View file @
68125098
Fix a long standing bug where HTTP HEAD requests resulted in a 400 error.
This diff is collapsed.
Click to expand it.
synapse/http/server.py
+
11
−
5
View file @
68125098
...
...
@@ -242,10 +242,12 @@ class _AsyncResource(resource.Resource, metaclass=abc.ABCMeta):
no appropriate method exists. Can be overriden in sub classes for
different routing.
"""
# Treat HEAD requests as GET requests.
request_method
=
request
.
method
.
decode
(
"
ascii
"
)
if
request_method
==
"
HEAD
"
:
request_method
=
"
GET
"
method_handler
=
getattr
(
self
,
"
_async_render_%s
"
%
(
request
.
method
.
decode
(
"
ascii
"
),),
None
)
method_handler
=
getattr
(
self
,
"
_async_render_%s
"
%
(
request_method
,),
None
)
if
method_handler
:
raw_callback_return
=
method_handler
(
request
)
...
...
@@ -362,11 +364,15 @@ class JsonResource(DirectServeJsonResource):
A tuple of the callback to use, the name of the servlet, and the
key word arguments to pass to the callback
"""
# Treat HEAD requests as GET requests.
request_path
=
request
.
path
.
decode
(
"
ascii
"
)
request_method
=
request
.
method
if
request_method
==
b
"
HEAD
"
:
request_method
=
b
"
GET
"
# Loop through all the registered callbacks to check if the method
# and path regex match
for
path_entry
in
self
.
path_regexs
.
get
(
request
.
method
,
[]):
for
path_entry
in
self
.
path_regexs
.
get
(
request
_
method
,
[]):
m
=
path_entry
.
pattern
.
match
(
request_path
)
if
m
:
# We found a match!
...
...
@@ -579,7 +585,7 @@ def set_cors_headers(request: Request):
"""
request
.
setHeader
(
b
"
Access-Control-Allow-Origin
"
,
b
"
*
"
)
request
.
setHeader
(
b
"
Access-Control-Allow-Methods
"
,
b
"
GET, POST, PUT, DELETE, OPTIONS
"
b
"
Access-Control-Allow-Methods
"
,
b
"
GET,
HEAD,
POST, PUT, DELETE, OPTIONS
"
)
request
.
setHeader
(
b
"
Access-Control-Allow-Headers
"
,
...
...
This diff is collapsed.
Click to expand it.
tests/test_server.py
+
42
−
3
View file @
68125098
...
...
@@ -157,6 +157,29 @@ class JsonResourceTests(unittest.TestCase):
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 @@ class WrapHtmlRequestHandlerTests(unittest.TestCase):
self
.
reactor
=
ThreadedMemoryReactorClock
()
def
test_good_response
(
self
):
def
callback
(
request
):
async
def
callback
(
request
):
request
.
write
(
b
"
response
"
)
request
.
finish
()
...
...
@@ -275,7 +298,7 @@ class WrapHtmlRequestHandlerTests(unittest.TestCase):
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 @@ class WrapHtmlRequestHandlerTests(unittest.TestCase):
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 @@ class WrapHtmlRequestHandlerTests(unittest.TestCase):
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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment