Skip to content

Commit

Permalink
Correct url parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerzr committed Sep 22, 2016
1 parent 613b741 commit 6e447fa
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 31 deletions.
36 changes: 35 additions & 1 deletion nxsugarpy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
#
##############################################################################

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
import re

def secondsToStr(secs):
out = ""
if secs < 1:
Expand All @@ -41,4 +47,32 @@ def secondsToStr(secs):
out = "{0}{1:.0f}m".format(out, rmins)
if rsecs > 0:
out = "{0}{1:.0f}s".format(out, rsecs)
return out
return out

def parseNexusUrl(url, user="", password=""):
if re.match(r'''[a-zA-Z0-9]*://.*''', url) == None:
url = "tcp://"+url
nxurl = urlparse(url)
if user == "" and nxurl.username != None:
user = nxurl.username
if password == "" and nxurl.password != None:
password = nxurl.password
scheme = nxurl.scheme
if not nxurl.scheme:
scheme = "tcp"
host = nxurl.hostname
if not nxurl.hostname:
host = "localhost"
port = nxurl.port
if not nxurl.port:
if scheme == "tcp":
port = 1717
elif scheme == "ssl":
port = 1718
elif scheme == "ws":
port = 80
elif scheme == "wss":
port = 443
else:
port = 1717
return scheme, user, password, host, port
29 changes: 16 additions & 13 deletions nxsugarpy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
class Server(object):
def __init__(self, url):
self.url = url
self._showurl = ""
self._connurl = ""
self.user = ""
self.password = ""
self.pulls = 1
Expand Down Expand Up @@ -91,6 +93,8 @@ def isTesting(self):

def addService(self, name, path, opts=None):
svc = Service(self.url, path, {"pulls": self.pulls, "pullTimeout": self.pullTimeout, "maxThreads": self.maxThreads, "testing": self.testing})
svc.user = self.user
svc.password = self.password
svc.name = name
svc.logLevel = self.logLevel
svc.statsPeriod = self.statsPeriod
Expand Down Expand Up @@ -121,30 +125,29 @@ def serve(self):
return errs

# Dial and login
nxurl = urlparse(self.url)
if self.user == "" and nxurl.username != None:
self.user = nxurl.username
if self.password == "" and nxurl.password != None:
self.password = nxurl.password
self.url = "{0}://{1}:{2}".format(nxurl.scheme, nxurl.hostname, nxurl.port)
scheme, user, password, host, port = parseNexusUrl(self.url, self.user, self.password)
self.user = user
self.password = password
self._showurl = "{0}://{1}:{2}".format(scheme, host, port)
self._connurl = "{0}://{1}:{2}@{3}:{4}".format(scheme, user, password, host, port)
for _, svc in self._services.items():
svc.url = self.url
svc._showurl = self._showurl
svc._connurl = self._connurl
svc.user = self.user
svc.password = self.password
svc.url = self.url
connurl = "{0}://{1}:{2}@{3}:{4}".format(nxurl.scheme, self.user, self.password, nxurl.hostname, nxurl.port)


self._setState(StateConnecting)
try:
self._nc = nxpy.Client(connurl)
self._nc = nxpy.Client(self._connurl)
except Exception as e:
errs = "can't connect to nexus server ({0}): {1}".format(connurl, str(e))
errs = "can't connect to nexus server ({0}): {1}".format(self._showurl, str(e))
logWithFields(ErrorLevel, "server", {"type": "connection_error"}, errs)
return errs
if not self._nc.is_version_compatible:
logWithFields(WarnLevel, "server", {"type": "incompatible_version"}, "connecting to an incompatible version of nexus at ({0}): client ({1}) server ({2})", self.url, nxpy.__version__, self._nc.nexus_version)
logWithFields(WarnLevel, "server", {"type": "incompatible_version"}, "connecting to an incompatible version of nexus at ({0}): client ({1}) server ({2})", self._showurl, nxpy.__version__, self._nc.nexus_version)
if not self._nc.is_logged:
errs = "can't login to nexus server ({0}) as ({1}): {2}".format(connurl, self.user, errToStr(self._nc.login_error))
errs = "can't login to nexus server ({0}) as ({1}): {2}".format(self._showurl, self.user, errToStr(self._nc.login_error))
logWithFields(ErrorLevel, "server", {"type": "login_error"}, errs)
return errs

Expand Down
29 changes: 12 additions & 17 deletions nxsugarpy/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
except ImportError:
from queue import Queue, Full

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse

StateInitializing, StateConnecting, StateServing, StateStopped = range(4)
_connStateStr = {
StateInitializing: "initializing",
Expand Down Expand Up @@ -86,6 +81,8 @@ def __init__(self, url, path, opts = {}):
self.name = "service"
self.description = ""
self.url = url
self._showurl = ""
self._connurl = ""
self.user = ""
self.password = ""
self.path = path
Expand Down Expand Up @@ -168,7 +165,7 @@ def _infoMethod(self, task):
"name": self.name,
"description": self.description,
"version": self.version,
"nxcli-version": "0.0.0",
"nxcli-version": "not implemented",
"wan-ips": info.wanIps,
"lan-ips": info.lanIps,
"user": info.user,
Expand Down Expand Up @@ -291,24 +288,22 @@ def serve(self, errQueue=None):
self._setState(StateConnecting)

# Dial and login
nxurl = urlparse(self.url)
if self.user == "" and nxurl.username != None:
self.user = nxurl.username
if self.password == "" and nxurl.password != None:
self.password = nxurl.password
self.url = "{0}://{1}:{2}".format(nxurl.scheme, nxurl.hostname, nxurl.port)
connurl = "{0}://{1}:{2}@{3}:{4}".format(nxurl.scheme, self.user, self.password, nxurl.hostname, nxurl.port)
scheme, user, password, host, port = parseNexusUrl(self.url, self.user, self.password)
self.user = user
self.password = password
self._showurl = "{0}://{1}:{2}".format(scheme, host, port)
self._connurl = "{0}://{1}:{2}@{3}:{4}".format(scheme, user, password, host, port)

try:
self._nc = nxpy.Client(connurl)
self._nc = nxpy.Client(self._connurl)
except Exception as e:
errs = "can't connect to nexus server ({0}): {1}".format(connurl, str(e))
errs = "can't connect to nexus server ({0}): {1}".format(self._showurl, str(e))
logWithFields(ErrorLevel, "server", {"type": "connection_error"}, errs)
return errs
if not self._nc.is_version_compatible:
logWithFields(WarnLevel, "server", {"type": "incompatible_version"}, "connecting to an incompatible version of nexus at ({0}): client ({1}) server ({2})", self.url, nxpy.__version__, self._nc.nexus_version)
logWithFields(WarnLevel, "server", {"type": "incompatible_version"}, "connecting to an incompatible version of nexus at ({0}): client ({1}) server ({2})", self._showurl, nxpy.__version__, self._nc.nexus_version)
if not self._nc.is_logged:
errs = "can't login to nexus server ({0}) as ({1}): {2}".format(connurl, self.user, errToStr(self._nc.login_error))
errs = "can't login to nexus server ({0}) as ({1}): {2}".format(self._showurl, self.user, errToStr(self._nc.login_error))
logWithFields(ErrorLevel, "server", {"type": "login_error"}, errs)
return errs
self._connid = self._nc.connid
Expand Down

0 comments on commit 6e447fa

Please sign in to comment.