使用过去工作的URL,我知道从中收到解析错误net/url.它出什么问题了?
parse postgres://user:abc{DEf1=ghi@example.com:5432/db?sslmode=require: net/url: invalid userinfo
Run Code Online (Sandbox Code Playgroud)
请参阅https://play.golang.com/p/mQZaN5JN3_q以运行.
package main
import (
"fmt"
"net/url"
)
func main() {
dsn := "postgres://user:abc{DEf1=ghi@example.com:5432/db?sslmode=require"
u, err := url.Parse(dsn)
fmt.Println(u, err)
}
Run Code Online (Sandbox Code Playgroud)
事实证明,直到 Go v1.9.3net/url在解析 url 时都没有验证用户信息。如果用户名或密码包含特殊字符,则在使用 v1.9.4 编译时可能会破坏现有应用程序。
现在,它期望用户信息是百分比编码的字符串,以便处理特殊字符。新行为在 中引入ba1018b。
package main
import (
"fmt"
"net/url"
)
func main() {
dsn1 := "postgres://user:abc{DEf1=ghi@example.com:5432/db?sslmode=require" // this works up until 1.9.3 but no longer in 1.9.4
dsn2 := "postgres://user:abc%7BDEf1=ghi@example.com:5432/db?sslmode=require" // this works everywhere, note { is now %7B
u, err := url.Parse(dsn1)
fmt.Println("1st url:\t", u, err)
u, err = url.Parse(dsn2)
fmt.Println("2nd url:\t", u, err)
}
Run Code Online (Sandbox Code Playgroud)
在https://play.golang.com/p/jGIQgbiKZwz上运行代码。
那么,你可以
url.QueryEscape("your#$%^&*(proper$#$%%^(password")
Run Code Online (Sandbox Code Playgroud)
并使用这个来解析您的网址。