From f557acf76b165995d855b3fc0f5324519f615d27 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Sat, 13 Aug 2022 00:50:38 +1000 Subject: [PATCH] net.urllib: fix ipv6 address parsing `[ipv6]:port` closes #15309 --- vlib/net/urllib/urllib.v | 11 +++++------ vlib/net/urllib/urllib_test.v | 2 ++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/vlib/net/urllib/urllib.v b/vlib/net/urllib/urllib.v index 825ddb73a..f82e5e1c7 100644 --- a/vlib/net/urllib/urllib.v +++ b/vlib/net/urllib/urllib.v @@ -598,12 +598,11 @@ fn parse_host(host string) ?string { host3 := unescape(host[i..], .encode_host) or { return err.msg() } return host1 + host2 + host3 } - if idx := host.last_index(':') { - colon_port = host[idx..i] - if !valid_optional_port(colon_port) { - return error(error_msg('parse_host: invalid port $colon_port after host ', - '')) - } + } else if i := host.last_index(':') { + colon_port := host[i..] + if !valid_optional_port(colon_port) { + return error(error_msg('parse_host: invalid port $colon_port after host ', + '')) } } h := unescape(host, .encode_host) or { return err.msg() } diff --git a/vlib/net/urllib/urllib_test.v b/vlib/net/urllib/urllib_test.v index 920b392e1..ef4d773b4 100644 --- a/vlib/net/urllib/urllib_test.v +++ b/vlib/net/urllib/urllib_test.v @@ -111,6 +111,8 @@ fn test_parse() ? { 'telnet://192.0.2.16:80/', 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2', 'foo://example.com:8042/over/there?name=ferret#nose', + 'ftp://2001:0db8:85a3:0000:0000:8a2e:0370:7334/path/file.txt', + 'ws://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:4000', ] for url in urls { _ := urllib.parse(url) or { -- 2.30.2