GO中的标准包中是否有允许验证URL的功能?
我的初始搜索没有找到任何内容, 我宁愿不采用正则表达式检查。
答案 0 :(得分:44)
是的,url.ParseRequestURI
如果网址无效,则返回错误,不是绝对网址等。url.Parse
几乎可以在任何内容上返回有效内容......
import "net/url"
...
u, err := url.ParseRequestURI("http://google.com/")
if err != nil {
panic(err)
}
以上示例不会失败,但这些将会:
u, err := url.ParseRequestURI("http//google.com")
u, err := url.ParseRequestURI("google.com")
u, err := url.ParseRequestURI("/foo/bar")
答案 1 :(得分:3)
接受的答案允许空白http://
和相对的网址,例如/foo/bar
。如果您想进行更严格的检查,则将拒绝以下内容:
import "net/url"
func IsUrl(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}
答案 2 :(得分:1)
我认为标准包装不明智。我最终使用的是https://github.com/asaskevich/govalidator
答案 3 :(得分:0)
这帮助我了解了标准库url.Parse方法的工作原理,希望对您有所帮助。请注意,所有这些值都不会引发错误。
package main
import (
"fmt"
"net/url"
)
func main() {
urls := []string{
"https",
"https://",
"",
"http://www",
"http://www.dumpsters.com",
"https://www.dumpsters.com:443",
"/testing-path",
"testing-path",
"alskjff#?asf//dfas",
}
for _, u := range urls {
val, err := url.Parse(u)
scheme := val.Scheme
host := val.Host
hostname := val.Hostname()
path := val.Path
fmt.Println("val : "+u+" : ", val)
fmt.Println("error : "+u+" : ", err)
fmt.Println("scheme : "+u+" : ", scheme)
fmt.Println("host : "+u+" : ", host)
fmt.Println("hostname : "+u+" : ", hostname)
fmt.Println("path : "+u+" : ", path)
fmt.Println()
}
}
结果
val : https : https
error : https : <nil>
scheme : https :
host : https :
hostname : https :
path : https : https
val : https:// : https:
error : https:// : <nil>
scheme : https:// : https
host : https:// :
hostname : https:// :
path : https:// :
val : :
error : : <nil>
scheme : :
host : :
hostname : :
path : :
val : http://www : http://www
error : http://www : <nil>
scheme : http://www : http
host : http://www : www
hostname : http://www : www
path : http://www :
val : http://www.dumpsters.com : http://www.dumpsters.com
error : http://www.dumpsters.com : <nil>
scheme : http://www.dumpsters.com : http
host : http://www.dumpsters.com : www.dumpsters.com
hostname : http://www.dumpsters.com : www.dumpsters.com
path : http://www.dumpsters.com :
val : https://www.dumpsters.com:443 : https://www.dumpsters.com:443
error : https://www.dumpsters.com:443 : <nil>
scheme : https://www.dumpsters.com:443 : https
host : https://www.dumpsters.com:443 : www.dumpsters.com:443
hostname : https://www.dumpsters.com:443 : www.dumpsters.com
path : https://www.dumpsters.com:443 :
val : /testing-path : /testing-path
error : /testing-path : <nil>
scheme : /testing-path :
host : /testing-path :
hostname : /testing-path :
path : /testing-path : /testing-path
val : testing-path : testing-path
error : testing-path : <nil>
scheme : testing-path :
host : testing-path :
hostname : testing-path :
path : testing-path : testing-path
val : alskjff#?asf//dfas : alskjff#?asf//dfas
error : alskjff#?asf//dfas : <nil>
scheme : alskjff#?asf//dfas :
host : alskjff#?asf//dfas :
hostname : alskjff#?asf//dfas :
path : alskjff#?asf//dfas : alskjff
答案 4 :(得分:0)
func IsUrl(str string) bool {
url, err := url.ParseRequestURI(str)
if err != nil {
log.Info(err.Error())
return false
}
address := net.ParseIP(url.Host)
log.Infow("url-info", "host", address)
if address == nil {
log.Infow("url-info", "host", url.Host)
return strings.Contains(url.Host, ".")
}
return true
}
func Test_IsAllowUrl(t *testing.T) {
assert.True(t, IsUrl("http://google.com"))
assert.True(t, IsUrl("http://w.com/cn"))
assert.True(t, IsUrl("http://192.158.0.1:90"))
assert.False(t, IsUrl("http://w"))
assert.False(t, IsUrl("fsw"))
assert.False(t, IsUrl("http://192.158.1/1"))
}
最后一个“ http:/192.158.1/1”未通过。可能应该匹配所有允许的域,但将变得复杂 https://www.iana.org/domains/root/db