mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
net.urllib: Update with latest changes from go lib
This commit is contained in:
parent
2d87fea074
commit
3db50f724b
@ -8,7 +8,7 @@
|
|||||||
// it deviates for compatibility reasons.
|
// it deviates for compatibility reasons.
|
||||||
|
|
||||||
// Based off: https://github.com/golang/go/blob/master/src/net/url/url.go
|
// Based off: https://github.com/golang/go/blob/master/src/net/url/url.go
|
||||||
// Last commit: https://github.com/golang/go/commit/a326bc6df27309815e4a2ae005adef233cfb9ea9
|
// Last commit: https://github.com/golang/go/commit/61bb56ad63992a3199acc55b2537c8355ef887b6
|
||||||
// Copyright 2009 The Go Authors. All rights reserved.
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
@ -596,13 +596,13 @@ fn parse_host(host string) ?string {
|
|||||||
if host.starts_with('[') {
|
if host.starts_with('[') {
|
||||||
// parse an IP-Literal in RFC 3986 and RFC 6874.
|
// parse an IP-Literal in RFC 3986 and RFC 6874.
|
||||||
// E.g., '[fe80::1]', '[fe80::1%25en0]', '[fe80::1]:80'.
|
// E.g., '[fe80::1]', '[fe80::1%25en0]', '[fe80::1]:80'.
|
||||||
i := host.last_index(']')
|
mut i := host.last_index(']')
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
return error(error_msg('missing \']\' in host', ''))
|
return error(error_msg('missing \']\' in host', ''))
|
||||||
}
|
}
|
||||||
colonport := host.right(i+1)
|
mut colon_port := host.right(i+1)
|
||||||
if !valid_optional_port(colonport) {
|
if !valid_optional_port(colon_port) {
|
||||||
return error(error_msg('invalid port $colonport after host ', ''))
|
return error(error_msg('invalid port $colon_port after host ', ''))
|
||||||
}
|
}
|
||||||
|
|
||||||
// RFC 6874 defines that %25 (%-encoded percent) introduces
|
// RFC 6874 defines that %25 (%-encoded percent) introduces
|
||||||
@ -623,6 +623,14 @@ fn parse_host(host string) ?string {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return host1 + host2 + host3
|
return host1 + host2 + host3
|
||||||
|
} else {
|
||||||
|
i = host.last_index(':')
|
||||||
|
if i != -1 {
|
||||||
|
colon_port = host.right(i)
|
||||||
|
if !valid_optional_port(colon_port) {
|
||||||
|
return error(error_msg('invalid port $colon_port after host ', ''))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1007,46 +1015,40 @@ pub fn (u &URL) request_uri() string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// hostname returns u.host, without any port number.
|
// hostname returns u.host, stripping any valid port number if present.
|
||||||
//
|
//
|
||||||
// If host is an IPv6 literal with a port number, hostname returns the
|
// If the result is enclosed in square brackets, as literal IPv6 addresses are,
|
||||||
// IPv6 literal without the square brackets. IPv6 literals may include
|
// the square brackets are removed from the result.
|
||||||
// a zone identifier.
|
|
||||||
pub fn (u &URL) hostname() string {
|
pub fn (u &URL) hostname() string {
|
||||||
return strip_port(u.host)
|
host_port := split_host_port(u.host)
|
||||||
|
return host_port[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// port returns the port part of u.host, without the leading colon.
|
// port returns the port part of u.host, without the leading colon.
|
||||||
// If u.host doesn't contain a port, port returns an empty string.
|
// If u.host doesn't contain a port, port returns an empty string.
|
||||||
pub fn (u &URL) port() string {
|
pub fn (u &URL) port() string {
|
||||||
return port_only(u.host)
|
host_port := split_host_port(u.host)
|
||||||
|
return host_port[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn strip_port(hostport string) string {
|
// split_host_port separates host and port. If the port is not valid, it returns
|
||||||
colon := hostport.index(':')
|
// the entire input as host, and it doesn't check the validity of the host.
|
||||||
if colon == -1 {
|
// Per RFC 3986, it requires ports to be numeric.
|
||||||
return hostport
|
fn split_host_port(hostport string) []string {
|
||||||
|
mut host := hostport
|
||||||
|
mut port := ''
|
||||||
|
|
||||||
|
colon := host.last_index(':')
|
||||||
|
if colon != -1 && valid_optional_port(host.right(colon)) {
|
||||||
|
port = host.right(colon+1)
|
||||||
|
host = host.left(colon)
|
||||||
}
|
}
|
||||||
i := hostport.index(']')
|
|
||||||
if i != -1 {
|
|
||||||
return hostport.left(i).trim_left('[')
|
|
||||||
}
|
|
||||||
return hostport.left(colon)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn port_only(hostport string) string {
|
if host.starts_with('[') && host.ends_with(']') {
|
||||||
colon := hostport.index(':')
|
host = host.substr(1, host.len-1)
|
||||||
if colon == -1 {
|
|
||||||
return ''
|
|
||||||
}
|
}
|
||||||
i := hostport.index(']:')
|
|
||||||
if i != -1 {
|
return [host, port]
|
||||||
return hostport.right(i+(']:'.len))
|
|
||||||
}
|
|
||||||
if hostport.contains(']') {
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
return hostport.right(colon+(':'.len))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// valid_userinfo reports whether s is a valid userinfo string per RFC 3986
|
// valid_userinfo reports whether s is a valid userinfo string per RFC 3986
|
||||||
|
Loading…
Reference in New Issue
Block a user