net.urllib: don't crash on str() if url is missing host (#10313)

pull/10323/head
Ryan Roden-Corrent 2021-06-02 09:12:27 -04:00 committed by GitHub
parent f6bb4d9a4a
commit a368800b26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -374,7 +374,7 @@ pub:
} }
fn (u &Userinfo) empty() bool { fn (u &Userinfo) empty() bool {
return u.username == '' && u.password == '' return isnil(u) || (u.username == '' && u.password == '')
} }
// string returns the encoded userinfo information in the standard form // string returns the encoded userinfo information in the standard form
@ -737,7 +737,7 @@ pub fn (u URL) str() string {
if u.opaque != '' { if u.opaque != '' {
buf.write_string(u.opaque) buf.write_string(u.opaque)
} else { } else {
if u.scheme != '' || u.host != '' || (u.user != 0 && !u.user.empty()) { if u.scheme != '' || u.host != '' || !u.user.empty() {
if u.host != '' || u.path != '' || !u.user.empty() { if u.host != '' || u.path != '' || !u.user.empty() {
buf.write_string('//') buf.write_string('//')
} }

View File

@ -43,3 +43,9 @@ fn test_parse_query() ? {
assert q1.data['format'].data == ['"%l: %c %t"'] assert q1.data['format'].data == ['"%l: %c %t"']
assert q2.data['format'].data == ['"%l: %c %t"'] assert q2.data['format'].data == ['"%l: %c %t"']
} }
fn test_parse_missing_host() ? {
// issue #10311
url := urllib.parse('http:///') ?
assert url.str() == 'http://///'
}