Selaa lähdekoodia

Return an empty body for OPTIONS requests. (#7886)

tags/v1.18.0rc1
Patrick Cloke 3 vuotta sitten
committed by GitHub
vanhempi
commit
6a080ea184
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 muutettua tiedostoa jossa 12 lisäystä ja 25 poistoa
  1. +1
    -0
      changelog.d/7886.misc
  2. +5
    -19
      synapse/http/server.py
  3. +6
    -6
      tests/test_server.py

+ 1
- 0
changelog.d/7886.misc Näytä tiedosto

@@ -0,0 +1 @@
Return an empty body for OPTIONS requests.

+ 5
- 19
synapse/http/server.py Näytä tiedosto

@@ -442,21 +442,6 @@ class StaticResource(File):
return super().render_GET(request)


def _options_handler(request):
"""Request handler for OPTIONS requests

This is a request handler suitable for return from
_get_handler_for_request. It returns a 200 and an empty body.

Args:
request (twisted.web.http.Request):

Returns:
Tuple[int, dict]: http code, response body.
"""
return 200, {}


def _unrecognised_request_handler(request):
"""Request handler for unrecognised requests

@@ -490,11 +475,12 @@ class OptionsResource(resource.Resource):
"""Responds to OPTION requests for itself and all children."""

def render_OPTIONS(self, request):
code, response_json_object = _options_handler(request)
request.setResponseCode(204)
request.setHeader(b"Content-Length", b"0")

return respond_with_json(
request, code, response_json_object, send_cors=True, canonical_json=False,
)
set_cors_headers(request)
return b""

def getChildWithDefault(self, path, request):
if request.method == b"OPTIONS":


+ 6
- 6
tests/test_server.py Näytä tiedosto

@@ -193,10 +193,10 @@ class OptionsResourceTests(unittest.TestCase):
return channel

def test_unknown_options_request(self):
"""An OPTIONS requests to an unknown URL still returns 200 OK."""
"""An OPTIONS requests to an unknown URL still returns 204 No Content."""
channel = self._make_request(b"OPTIONS", b"/foo/")
self.assertEqual(channel.result["code"], b"200")
self.assertEqual(channel.result["body"], b"{}")
self.assertEqual(channel.result["code"], b"204")
self.assertNotIn("body", channel.result)

# Ensure the correct CORS headers have been added
self.assertTrue(
@@ -213,10 +213,10 @@ class OptionsResourceTests(unittest.TestCase):
)

def test_known_options_request(self):
"""An OPTIONS requests to an known URL still returns 200 OK."""
"""An OPTIONS requests to an known URL still returns 204 No Content."""
channel = self._make_request(b"OPTIONS", b"/res/")
self.assertEqual(channel.result["code"], b"200")
self.assertEqual(channel.result["body"], b"{}")
self.assertEqual(channel.result["code"], b"204")
self.assertNotIn("body", channel.result)

# Ensure the correct CORS headers have been added
self.assertTrue(


Ladataan…
Peruuta
Tallenna