dtyw10299 2018-07-25 09:53
浏览 136

在Go中验证国际域名的最佳方法

What is the best way to validate international domain names in Go?

https://golang.org/pkg/net/?m=all#isDomainName

https://golang.org/src/net/dnsclient.go?s=3444:3476#L109

Maybe copying this function which isn't exported from net package?

We need to validate domains like icaan.org and example.(special characters)

EDIT: IDN https://en.wikipedia.org/wiki/Internationalized_domain_name

Already using govalidator which fails to validate IDN's as it doesn't handle unicode characters https://github.com/asaskevich/govalidator/blob/master/validator.go

Here are some examples of IDN's

Just seen the reference to punycode.

Though all these punycode's are in the public suffix list https://publicsuffix.org/list/public_suffix_list.dat

:(

  • 写回答

1条回答 默认 最新

  • duanhui7329 2018-07-27 16:48
    关注

    One possible approach is to use the builtin functions url.Parse(string) and url.Hostname() along with a regular expression to match sequences of Unicode letters/numbers/marks separated by dots (as represented in the sample data set).

    For example:

    var domainNamePattern = regexp.MustCompile(`^([\p{L}\p{M}\p{N}_%+-]+\.)+[\p{L}\p{M}\p{N}]+$`)
    
    func main() {
      ss := []string{
        `https://evertpot.com/internationalized-domain-names-are-you-ready/`,
        `http://bogus!.com`,
        `https://foo1.bar2.com.gah.zip/`,
        `http://مثال.إختبار`,
        `http://例子.测试`,
        `http://例子.測試`,
        `http://παράδειγμα.δοκιμή`,
        `http://उदाहरण.परीक्षा`,
        `http://例え.テスト`,
        `http://실례.테스트`,
        `http://مثال.آزمایشی`,
        `http://пример.испытание`,
      }
    
      for _, s := range ss {
        u, err := url.Parse(s)
        if err != nil || !domainNamePattern.MatchString(u.Hostname()) {
          bogusPart := s
          if err == nil {
            bogusPart = u.Hostname()
          }
          fmt.Printf("ERROR: invalid URL or hostname %q
    ", bogusPart)
          continue
        }
        fmt.Printf("OK: hostname=%q
    ", u.Hostname())
      }
    }
    
    // OK: hostname="evertpot.com"
    // ERROR: invalid URL or hostname "bogus!.com"
    // OK: hostname="foo1.bar2.com.gah.zip"
    // OK: hostname="مثال.إختبار"
    // OK: hostname="例子.测试"
    // OK: hostname="例子.測試"
    // OK: hostname="παράδειγμα.δοκιμή"
    // OK: hostname="उदाहरण.परीक्षा"
    // OK: hostname="例え.テスト"
    // OK: hostname="실례.테스트"
    // OK: hostname="مثال.آزمایشی"
    // OK: hostname="пример.испытание"
    

    Of course, more care should be taken to build the regular expression in such a way that it conforms to any relevant specifications, but this example should be a good starting point.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog