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 <stefan.herbrechtsmeier@weidmueller.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Stefan Herbrechtsmeier
2025-02-07 13:46:53 +01:00
committed by Richard Purdie
parent 851b24cf81
commit 2935d76bb4

View File

@@ -353,49 +353,9 @@ def decodeurl(url):
user, password, parameters).
"""
m = re.compile('(?P<type>[^:]*)://((?P<user>[^/;]+)@)?(?P<location>[^;]+)(;(?P<parm>.*))?').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<user>[^:]+)(:?(?P<pswd>.*))').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: