summaryrefslogtreecommitdiffstats
path: root/bitbake
diff options
context:
space:
mode:
authorStefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>2025-02-07 13:46:53 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2025-02-11 21:18:39 +0000
commit2935d76bb468e576ab6219fa352968f2835f4ab8 (patch)
tree0c529b6c91186be3c35de8a3156c6db7762d2c66 /bitbake
parent851b24cf818e1786efe15f4ca3f3d7e0718c0004 (diff)
downloadpoky-2935d76bb468e576ab6219fa352968f2835f4ab8.tar.gz
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>
Diffstat (limited to 'bitbake')
-rw-r--r--bitbake/lib/bb/fetch2/__init__.py66
1 files changed, 14 insertions, 52 deletions
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):
353 user, password, parameters). 353 user, password, parameters).
354 """ 354 """
355 355
356 m = re.compile('(?P<type>[^:]*)://((?P<user>[^/;]+)@)?(?P<location>[^;]+)(;(?P<parm>.*))?').match(url) 356 uri = URI(url)
357 if not m: 357 path = uri.path if uri.path else "/"
358 raise MalformedUrl(url) 358 return uri.scheme, uri.hostport, path, uri.username, uri.password, uri.params
359
360 type = m.group('type')
361 location = m.group('location')
362 if not location:
363 raise MalformedUrl(url)
364 user = m.group('user')
365 parm = m.group('parm')
366
367 locidx = location.find('/')
368 if locidx != -1 and type.lower() != 'file':
369 host = location[:locidx]
370 path = location[locidx:]
371 elif type.lower() == 'file':
372 host = ""
373 path = location
374 if user:
375 path = user + '@' + path
376 user = ""
377 else:
378 host = location
379 path = "/"
380 if user:
381 m = re.compile('(?P<user>[^:]+)(:?(?P<pswd>.*))').match(user)
382 if m:
383 user = m.group('user')
384 pswd = m.group('pswd')
385 else:
386 user = ''
387 pswd = ''
388
389 p = collections.OrderedDict()
390 if parm:
391 for s in parm.split(';'):
392 if s:
393 if not '=' in s:
394 raise MalformedUrl(url, "The URL: '%s' is invalid: parameter %s does not specify a value (missing '=')" % (url, s))
395 s1, s2 = s.split('=', 1)
396 p[s1] = s2
397
398 return type, host, urllib.parse.unquote(path), user, pswd, p
399 359
400def encodeurl(decoded): 360def encodeurl(decoded):
401 """Encodes a URL from tokens (scheme, network location, path, 361 """Encodes a URL from tokens (scheme, network location, path,
@@ -406,24 +366,26 @@ def encodeurl(decoded):
406 366
407 if not type: 367 if not type:
408 raise MissingParameterError('type', "encoded from the data %s" % str(decoded)) 368 raise MissingParameterError('type', "encoded from the data %s" % str(decoded))
409 url = ['%s://' % type] 369 uri = URI()
370 uri.scheme = type
410 if user and type != "file": 371 if user and type != "file":
411 url.append("%s" % user) 372 uri.username = user
412 if pswd: 373 if pswd:
413 url.append(":%s" % pswd) 374 uri.password = pswd
414 url.append("@")
415 if host and type != "file": 375 if host and type != "file":
416 url.append("%s" % host) 376 uri.hostname = host
417 if path: 377 if path:
418 # Standardise path to ensure comparisons work 378 # Standardise path to ensure comparisons work
419 while '//' in path: 379 while '//' in path:
420 path = path.replace("//", "/") 380 path = path.replace("//", "/")
421 url.append("%s" % urllib.parse.quote(path)) 381 uri.path = path
382 if type == "file":
383 # Use old not IETF compliant style
384 uri.relative = False
422 if p: 385 if p:
423 for parm in p: 386 uri.params = p
424 url.append(";%s=%s" % (parm, p[parm]))
425 387
426 return "".join(url) 388 return str(uri)
427 389
428def uri_replace(ud, uri_find, uri_replace, replacements, d, mirrortarball=None): 390def uri_replace(ud, uri_find, uri_replace, replacements, d, mirrortarball=None):
429 if not ud.url or not uri_find or not uri_replace: 391 if not ud.url or not uri_find or not uri_replace: