fetch2: Revert decodeurl() to not use the URI class
PATCH 1/2 fetch2: Revert decodeurl() to not use the URI class
This reverts the recent change of decodeurl() to use the URI class to parse the URL instead of doing it itself. While reusing code is generally a good idea, using urllib.parse.urlparse() (which the URI class does) to parse the regular expression "URLs" that are used in PREMIRRORS and MIRRORS does not work. A regular expression URL containing https?://... would be silently ignored, while a URL using a negative lookahead such as git://(?!internal\.git\.server).*/.* would result in a cryptic error:
Exception: re.error: missing ), unterminated subpattern at position 0
The problem is that urllib.parse.urlparse() treats the ? as the start of URL parameters and thus stops parsing whatever part of the URL it was parsing.
--- bitbake/lib/bb/fetch2/__init__.py | 46 +++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-)
bitbake/lib/bb/fetch2/__init__.py +43 -3
@@ -353,9 +353,49 @@ def decodeurl(url): user, password, parameters). """ - uri = URI(url) - path = uri.path if uri.path else "/" - return uri.scheme, uri.hostport, path, uri.username, uri.password, uri.params + 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 def encodeurl(decoded): """Encodes a URL from tokens (scheme, network location, path,
PATCH 2/2 tests/fetch: Verify regular expression "URLs" that contain a ?
A regular expression "URL" as used in PREMIRRORS and MIRRORS may contain a ? as part of the regular expression. Make sure this does not cause problems when parsed by the fetcher.
--- bitbake/lib/bb/tests/fetch.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
bitbake/lib/bb/tests/fetch.py +8 -4
@@ -525,8 +525,7 @@ class MirrorUriTest(FetcherTest): mirrorvar = "http://.*/.* file:///somepath/downloads/ " \ "git://someserver.org/bitbake git://git.openembedded.org/bitbake " \ - "https://.*/.* file:///someotherpath/downloads/ " \ - "http://.*/.* file:///someotherpath/downloads/ " \ + "https?://.*/.* file:///someotherpath/downloads/ " \ "svn://svn.server1.com/ svn://svn.server2.com/" def test_urireplace(self):
@@ -1415,12 +1414,17 @@ class URLHandle(unittest.TestCase): "https://somesite.com/somerepo.git;user=anyUser:idtoken=1234" : ('https', 'somesite.com', '/somerepo.git', '', '', {'user': 'anyUser:idtoken=1234'}), 'git://s.o-me_ONE:%[email protected]/bitbake;branch=main;protocol=https' % password: ('git', 'git.openembedded.org', '/bitbake', 's.o-me_ONE', password, {'branch': 'main', 'protocol' : 'https'}), } - # we require a pathname to encodeurl but users can still pass such urls to - # decodeurl and we need to handle them + # We require a pathname to encodeurl but users can still pass URLs without + # a path to decodeurl and we need to handle them. decodeurl is also used to + # split the regular expression "URLs" that are used in PREMIRRORS and + # MIRRORS. decodedata = datatable.copy() decodedata.update({ "http://somesite.net;someparam=1": ('http', 'somesite.net', '/', '', '', {'someparam': '1'}), "npmsw://some.registry.url;package=@pkg;version=latest": ('npmsw', 'some.registry.url', '/', '', '', {'package': '@pkg', 'version': 'latest'}), + r"git://.*/.*": ("git", ".*", "/.*", "", "", {}), + r"https?://.*/.*": ("https?", ".*", "/.*", "", "", {}), + r"git://(?!internal\.git\.server).*/.*": ("git", r"(?!internal\.git\.server).*", "/.*", "", "", {}), }) def test_decodeurl(self):
Quoted 11 lines · Peter Kjellerstedt via lists.openembedded.org
This reverts the recent change of
decodeurl()to use the URI class to parse the URL instead of doing it itself. While reusing code is generally a good idea, usingurllib.parse.urlparse()(which the URI class does) to parse the regular expression "URLs" that are used in PREMIRRORS and MIRRORS does not work. A regular expression URL containing https?://... would be silently ignored, while a URL using a negative lookahead such asgit://(?!internal\.git\.server).*/.*would result in a cryptic error:Exception: re.error: missing ), unterminated subpattern at position 0
Quoted 8 lines
The problem is that
urllib.parse.urlparse()treats the ? as the start of URL parameters and thus stops parsing whatever part of the URL it was parsing.Signed-off-by Peter Kjellerstedt <[email protected]>---
bitbake/lib/bb/fetch2/__init__.py | 46 +++++++++++++++++++++++++++++--1 file changed, 43 insertions(+), 3 deletions(-)
I'm forever coming under pressure to take a patch like the one this reverts, it is the first thing most people new to the fetcher dislike. I can understand why.
I wonder if we should restore the function but only use it for parsing mirror urls, and comment it accordingly?
Cheers,
Richard