changeset 8762:2fac3c55f9bc

vcs: slight clean up of _check_url Make static void function static and without return value.
author Mads Kiilerich <mads@kiilerich.com>
date Sat, 14 Nov 2020 17:55:36 +0100
parents 7b7afdbe57af
children 29d8bdab74dc
files kallithea/lib/vcs/backends/git/repository.py kallithea/lib/vcs/backends/hg/repository.py
diffstat 2 files changed, 16 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/vcs/backends/git/repository.py	Thu Dec 03 01:13:44 2020 +0100
+++ b/kallithea/lib/vcs/backends/git/repository.py	Sat Nov 14 17:55:36 2020 +0100
@@ -147,20 +147,17 @@
         stdout, _stderr = self._run_git_command(cmd, cwd=cwd)
         return safe_str(stdout)
 
-    @classmethod
-    def _check_url(cls, url):
+    @staticmethod
+    def _check_url(url):
         """
-        Function will check given url and try to verify if it's a valid
-        link. Sometimes it may happened that git will issue basic
-        auth request that can cause whole API to hang when used from python
-        or other external calls.
+        Raise URLError if url doesn't seem like a valid safe Git URL. We
+        only allow http, https, git, and ssh URLs.
 
-        On failures it'll raise urllib2.HTTPError, exception is also thrown
-        when the return code is non 200
+        For http and https URLs, make a connection and probe to see if it is valid.
         """
         # check first if it's not an local url
         if os.path.isabs(url) and os.path.isdir(url):
-            return True
+            return
 
         if url.startswith('git://'):
             try:
@@ -175,7 +172,7 @@
                     raise urllib.error.URLError("Invalid escape character in path: '%s'" % c)
                 if c.isspace() and c != ' ':
                     raise urllib.error.URLError("Invalid whitespace character in path: %r" % c)
-            return True
+            return
 
         if not url.startswith('http://') and not url.startswith('https://'):
             raise urllib.error.URLError("Unsupported protocol in URL %s" % url)
@@ -211,8 +208,6 @@
             raise urllib.error.URLError(
                 "url [%s] does not look like an git" % cleaned_uri)
 
-        return True
-
     def _get_repo(self, create, src_url=None, update_after_clone=False,
                   bare=False):
         if create and os.path.exists(self.path):
--- a/kallithea/lib/vcs/backends/hg/repository.py	Thu Dec 03 01:13:44 2020 +0100
+++ b/kallithea/lib/vcs/backends/hg/repository.py	Sat Nov 14 17:55:36 2020 +0100
@@ -281,13 +281,13 @@
                                         ignorews=ignore_whitespace,
                                         context=context)))
 
-    @classmethod
-    def _check_url(cls, url, repoui=None):
-        """
-        Function will check given url and try to verify if it's a valid
-        link. Sometimes it may happened that mercurial will issue basic
-        auth request that can cause whole API to hang when used from python
-        or other external calls.
+    @staticmethod
+    def _check_url(url, repoui=None):
+        r"""
+        Raise URLError if url doesn't seem like a valid safe Hg URL. We
+        only allow http, https, ssh, and hg-git URLs.
+
+        For http, https and git URLs, make a connection and probe to see if it is valid.
 
         On failures it'll raise urllib2.HTTPError, exception is also thrown
         when the return code is non 200
@@ -295,13 +295,13 @@
         # check first if it's not an local url
         url = safe_bytes(url)
         if os.path.isdir(url) or url.startswith(b'file:'):
-            return True
+            return
 
         if url.startswith(b'ssh:'):
             # in case of invalid uri or authentication issues, sshpeer will
             # throw an exception.
             mercurial.sshpeer.instance(repoui or mercurial.ui.ui(), url, False).lookup(b'tip')
-            return True
+            return
 
         url_prefix = None
         if b'+' in url[:url.find(b'://')]:
@@ -343,8 +343,6 @@
                     "url [%s] does not look like an hg repo org_exc: %s"
                     % (cleaned_uri, e))
 
-        return True
-
     def _get_repo(self, create, src_url=None, update_after_clone=False):
         """
         Function will check for mercurial repository in given path and return