From abc9e06ed10fc0f73ae2f6b7876c16204be03d09 Mon Sep 17 00:00:00 2001 From: shove Date: Sat, 13 Jan 2024 04:51:48 +0800 Subject: [PATCH] net.urllib: fix parsing url error, when querypath is '//' (fix #20476) (#20504) --- vlib/net/urllib/urllib.v | 3 ++- vlib/net/urllib/urllib_test.v | 6 ++++++ vlib/vweb/tests/vweb_test.v | 13 +++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/vlib/net/urllib/urllib.v b/vlib/net/urllib/urllib.v index 90b27fec37cb0d..11aa93680a1fe6 100644 --- a/vlib/net/urllib/urllib.v +++ b/vlib/net/urllib/urllib.v @@ -512,7 +512,8 @@ fn parse_url(rawurl string, via_request bool) !URL { '')) } } - if ((url.scheme != '' || !via_request) && !rest.starts_with('///')) && rest.starts_with('//') { + if ((url.scheme != '' || !via_request) && !rest.starts_with('///')) && rest.starts_with('//') + && rest.len > 2 { authority, r := split(rest[2..], `/`, false) rest = r a := parse_authority(authority)! diff --git a/vlib/net/urllib/urllib_test.v b/vlib/net/urllib/urllib_test.v index 679169a0585363..939839df5e912b 100644 --- a/vlib/net/urllib/urllib_test.v +++ b/vlib/net/urllib/urllib_test.v @@ -122,3 +122,9 @@ fn test_parse() { } } } + +fn test_parse_slashes() { + assert urllib.parse('/')!.str() == '/' + assert urllib.parse('//')!.str() == '//' + assert urllib.parse('///')!.str() == '///' +} diff --git a/vlib/vweb/tests/vweb_test.v b/vlib/vweb/tests/vweb_test.v index 4d82c7fb1262ce..e222cc2e299f62 100644 --- a/vlib/vweb/tests/vweb_test.v +++ b/vlib/vweb/tests/vweb_test.v @@ -339,3 +339,16 @@ ${config.content}' } return read.bytestr() } + +// for issue 20476 +// phenomenon: parsing url error when querypath is `//` +fn test_empty_querypath() { + mut x := http.get('http://${localserver}') or { panic(err) } + assert x.body == 'Welcome to VWeb' + x = http.get('http://${localserver}/') or { panic(err) } + assert x.body == 'Welcome to VWeb' + x = http.get('http://${localserver}//') or { panic(err) } + assert x.body == 'Welcome to VWeb' + x = http.get('http://${localserver}///') or { panic(err) } + assert x.body == 'Welcome to VWeb' +}