Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix media repo breaking #5593

Merged
merged 8 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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 changelog.d/5593.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix regression in 1.1rc1 where OPTIONS requests to the media repo would fail.
31 changes: 24 additions & 7 deletions synapse/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,33 @@ def render(self, request):
"""
Render the request, using an asynchronous render handler if it exists.
"""
render_callback_name = "_async_render_" + request.method.decode("ascii")
render_callback_name = "render_" + request.method.decode("ascii")
async_render_callback_name = "_async_" + render_callback_name

if hasattr(self, render_callback_name):
# Call the handler
callback = getattr(self, render_callback_name)
defer.ensureDeferred(callback(request))
# Try and get the async renderer
callback = getattr(self, async_render_callback_name, None)

# If it doesn't exist, try and get a regular renderer
if not callback:
callback = getattr(self, render_callback_name, None)

# No renderer for this request method. Fall back to Twisted's "not
# found" handling.
if not callback:
return super().render(request)

resp = callback(request)

# If it's a coroutine, turn it into a Deferred
if isinstance(resp, types.CoroutineType):
richvdh marked this conversation as resolved.
Show resolved Hide resolved
resp = defer.ensureDeferred(resp)

# If it's a Deferred, return NOT_DONE_YET as it doesn't have a
# value yet
if not isinstance(resp, defer.Deferred):
hawkowl marked this conversation as resolved.
Show resolved Hide resolved
return NOT_DONE_YET
else:
super().render(request)

return resp


def _options_handler(request):
Expand Down
1 change: 1 addition & 0 deletions synapse/rest/media/v1/preview_url_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def __init__(self, hs, media_repo, media_storage):
)

def render_OPTIONS(self, request):
request.setHeader(b"Allow", b"OPTIONS, GET")
Copy link
Member

Choose a reason for hiding this comment

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

feels a bit odd to fix this here and not any of the other render_OPTIONS and on_OPTIONS tbh. Don't mind, though.

return respond_with_json(request, 200, {}, send_cors=True)

@wrap_json_request_handler
Expand Down
4 changes: 4 additions & 0 deletions synapse/util/logcontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import logging
import threading
import types

from twisted.internet import defer, threads

Expand Down Expand Up @@ -544,6 +545,9 @@ def run_in_background(f, *args, **kwargs):
# by synchronous exceptions, so let's turn them into Failures.
return defer.fail()

if isinstance(res, types.CoroutineType):
hawkowl marked this conversation as resolved.
Show resolved Hide resolved
res = defer.ensureDeferred(res)

if not isinstance(res, defer.Deferred):
return res

Expand Down
12 changes: 12 additions & 0 deletions tests/rest/media/v1/test_url_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,15 @@ def test_blacklisted_ipv6_range(self):
"error": "DNS resolution failure during URL preview generation",
},
)

def test_OPTIONS(self):
"""
OPTIONS returns the OPTIONS.
"""
request, channel = self.make_request(
"OPTIONS", "url_preview?url=http://example.com", shorthand=False
)
request.render(self.preview_url)
self.pump()
self.assertEqual(channel.code, 200)
self.assertEqual(channel.json_body, {})