From 2935d76bb468e576ab6219fa352968f2835f4ab8 Mon Sep 17 00:00:00 2001 From: Stefan Herbrechtsmeier Date: Fri, 7 Feb 2025 13:46:53 +0100 Subject: bitbake: fetch2: remove duplicated code in url decode and encode Use the URI class to decode and encode an URL. Remove duplicate code and unify the behavior. (Bitbake rev: a5d569c94700f04b8193c6bccae5af619931b00f) Signed-off-by: Stefan Herbrechtsmeier Signed-off-by: Richard Purdie --- bitbake/lib/bb/fetch2/__init__.py | 66 +++++++++------------------------------ 1 file changed, 14 insertions(+), 52 deletions(-) (limited to 'bitbake/lib') diff --git a/bitbake/lib/bb/fetch2/__init__.py b/bitbake/lib/bb/fetch2/__init__.py index ab992b7ea7..2a60e94ed0 100644 --- a/bitbake/lib/bb/fetch2/__init__.py +++ b/bitbake/lib/bb/fetch2/__init__.py @@ -353,49 +353,9 @@ def decodeurl(url): user, password, parameters). """ - m = re.compile('(?P[^:]*)://((?P[^/;]+)@)?(?P[^;]+)(;(?P.*))?').match(url) - if not m: - raise MalformedUrl(url) - - type = m.group('type') - location = m.group('location') - if not location: - raise MalformedUrl(url) - user = m.group('user') - parm = m.group('parm') - - locidx = location.find('/') - if locidx != -1 and type.lower() != 'file': - host = location[:locidx] - path = location[locidx:] - elif type.lower() == 'file': - host = "" - path = location - if user: - path = user + '@' + path - user = "" - else: - host = location - path = "/" - if user: - m = re.compile('(?P[^:]+)(:?(?P.*))').match(user) - if m: - user = m.group('user') - pswd = m.group('pswd') - else: - user = '' - pswd = '' - - p = collections.OrderedDict() - if parm: - for s in parm.split(';'): - if s: - if not '=' in s: - raise MalformedUrl(url, "The URL: '%s' is invalid: parameter %s does not specify a value (missing '=')" % (url, s)) - s1, s2 = s.split('=', 1) - p[s1] = s2 - - return type, host, urllib.parse.unquote(path), user, pswd, p + uri = URI(url) + path = uri.path if uri.path else "/" + return uri.scheme, uri.hostport, path, uri.username, uri.password, uri.params def encodeurl(decoded): """Encodes a URL from tokens (scheme, network location, path, @@ -406,24 +366,26 @@ def encodeurl(decoded): if not type: raise MissingParameterError('type', "encoded from the data %s" % str(decoded)) - url = ['%s://' % type] + uri = URI() + uri.scheme = type if user and type != "file": - url.append("%s" % user) + uri.username = user if pswd: - url.append(":%s" % pswd) - url.append("@") + uri.password = pswd if host and type != "file": - url.append("%s" % host) + uri.hostname = host if path: # Standardise path to ensure comparisons work while '//' in path: path = path.replace("//", "/") - url.append("%s" % urllib.parse.quote(path)) + uri.path = path + if type == "file": + # Use old not IETF compliant style + uri.relative = False if p: - for parm in p: - url.append(";%s=%s" % (parm, p[parm])) + uri.params = p - return "".join(url) + return str(uri) def uri_replace(ud, uri_find, uri_replace, replacements, d, mirrortarball=None): if not ud.url or not uri_find or not uri_replace: -- cgit v1.2.3-54-g00ecf