Juan Hernandez has uploaded a new change for review.

Change subject: cli: Simplify URL check
......................................................................

cli: Simplify URL check

Currently we check the URL using regular expressions. These regular
expressions are complicated, and they don't provide much benefit. In
addition they don't really work in some situations, for example, when
the host name doesn't have a dot. This patch simplifies the URL checking
to avoid this issue.

Change-Id: Idcbc45e6904a700c153725716e6d3b8bf8793d38
Bug-Url: https://bugzilla.redhat.com/1186365
Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com>
---
M src/ovirtcli/command/connect.py
1 file changed, 10 insertions(+), 32 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/79/37379/1

diff --git a/src/ovirtcli/command/connect.py b/src/ovirtcli/command/connect.py
index ad509f4..c2d16bf 100644
--- a/src/ovirtcli/command/connect.py
+++ b/src/ovirtcli/command/connect.py
@@ -15,7 +15,7 @@
 #
 
 
-import re
+import urlparse
 
 from ovirtcli.command.command import OvirtCommand
 from ovirtsdk.api import API
@@ -181,38 +181,16 @@
     def is_valid_url(self, url):
         if url is None:
             return False;
-        regex = re.compile(
-            r'^(?:http)s?://'  # http:// or https://
-            
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'
  # domain...
-            r'localhost|'  # localhost...
-            r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|'  # ...or ipv4
-            r'\[?[A-F0-9]*:[A-F0-9:]+\]?)'  # ...or ipv6
-            r'(?::\d+)?'  # optional port
-           , re.IGNORECASE)
-        if not regex.search(url):
+        parsed = urlparse.urlparse(url)
+        scheme = getattr(parsed, 'scheme')
+        if scheme is None:
             return False
-        hostname = self.get_hostname(url)
-        if not self.hostname_is_ip(hostname):
-            return True
-        return self.is_valid_ip(hostname)
-
-    def get_hostname(self, url):
-        url_obj = urlparse(url)
-        return getattr(url_obj, 'hostname')
-
-    def hostname_is_ip(self, hostname):
-        regex = re.compile(
-                   r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',
-                   re.IGNORECASE
-        )
-        return regex.search(hostname)
-
-    def is_valid_ip(self, hostip):
-        regex = re.compile(
-                   
r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$',
-                   re.IGNORECASE
-        )
-        return regex.search(hostip)
+        if not scheme.lower() in ['http', 'https']:
+            return False
+        hostname = getattr(parsed, 'hostname')
+        if hostname is None:
+            return False
+        return True
 
     def __normalize_typeerror(self, exception):
             err = str(exception)


-- 
To view, visit http://gerrit.ovirt.org/37379
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idcbc45e6904a700c153725716e6d3b8bf8793d38
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-cli
Gerrit-Branch: master
Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to