在GO中使用标准包验证URL

the*_*uke 28 validation go

GO中的标准包中是否有任何功能可以验证URL?

我在初始搜索时没有找到任何内容,我宁愿不采用正则表达式检查.

Not*_*fer 55

是的,url.ParseRequestURI如果URL无效则返回错误,不是绝对URL等等.url.Parse几乎任何东西都返回有效...

import "net/url"

...


u, err := url.ParseRequestURI("http://google.com/")
if err != nil {
   panic(err)
}
Run Code Online (Sandbox Code Playgroud)

以上示例不会失败,但这些将:

u, err := url.ParseRequestURI("http//google.com")

u, err := url.ParseRequestURI("google.com")

u, err := url.ParseRequestURI("/foo/bar")
Run Code Online (Sandbox Code Playgroud)

  • 做了什么改变?相对URL现在通过.https://play.golang.org/p/7v5YF24DUP (9认同)
  • `u, err := url.ParseRequestURI("/foo/bar")` 这在 Go 1.15 中通过了。 (5认同)
  • 看[在RFC 3986规范](https://www.ietf.org/rfc/rfc3986.txt),你是对的@Not_a_Golfer (3认同)
  • 不知何故,它给了我“javascript:alert(0)”是一个有效的网址...... (2认同)

jch*_*nes 8

接受的答案允许使用空白http://和相对网址/foo/bar。如果您想进行更严格的检查,则将拒绝以下内容:

import "net/url"

func IsUrl(str string) bool {
    u, err := url.Parse(str)
    return err == nil && u.Scheme != "" && u.Host != ""
}
Run Code Online (Sandbox Code Playgroud)

例如:https//play.golang.org/p/JngFarWPF2-

来自这个答案:https : //stackoverflow.com/a/25747925/744298


小智 6

这帮助我了解标准库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()
    }
}
Run Code Online (Sandbox Code Playgroud)

结果

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
Run Code Online (Sandbox Code Playgroud)