allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [allura] 01/01: [#8334] set a global socket default timeout for oembed usage
Date Mon, 30 Sep 2019 17:31:49 GMT
This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch db/8334
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 5c4baf3b1ca0627f331854c838e4b8013fbc06dd
Author: Dave Brondsema <dave@brondsema.net>
AuthorDate: Mon Sep 30 13:31:40 2019 -0400

    [#8334] set a global socket default timeout for oembed usage
---
 Allura/allura/lib/macro.py | 13 +++++++++----
 Allura/allura/lib/utils.py | 11 +++++++++++
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/Allura/allura/lib/macro.py b/Allura/allura/lib/macro.py
index 6746a48..abb4776 100644
--- a/Allura/allura/lib/macro.py
+++ b/Allura/allura/lib/macro.py
@@ -31,6 +31,7 @@ from tg import request
 from paste.deploy.converters import asint
 from bs4 import BeautifulSoup
 
+from allura.lib.utils import socket_default_timeout
 from . import helpers as h
 from . import security
 
@@ -452,10 +453,14 @@ def embed(url=None):
                                       'http://*.youtube-nocookie.com/*', 'https://*.youtube-nocookie.com/*',
                                       ])
     consumer.addEndpoint(endpoint)
-    try:
-        html = consumer.embed(url)['html']
-    except oembed.OEmbedNoEndpoint:
-        html = None
+
+    # workaround for https://github.com/abarmat/python-oembed/pull/9 not being implemented
yet
+    with socket_default_timeout(5):
+
+        try:
+            html = consumer.embed(url)['html']
+        except oembed.OEmbedNoEndpoint:
+            html = None
 
     if html:
         # youtube has a trailing ")" at the moment
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index 0203f7a..fdf0340 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -33,6 +33,7 @@ import collections
 from urlparse import urlparse
 import urllib
 import types
+import socket
 
 import tg
 import emoji
@@ -862,3 +863,13 @@ def urlencode(params):
 
 def close_ipv4_addrs(ip1, ip2):
     return ip1.split('.')[0:3] == ip2.split('.')[0:3]
+
+
+@contextmanager
+def socket_default_timeout(timeout):
+    orig_timeout = socket.getdefaulttimeout()
+    socket.setdefaulttimeout(timeout)
+    try:
+        yield
+    finally:
+        socket.setdefaulttimeout(orig_timeout)
\ No newline at end of file


Mime
View raw message