当前位置: 首页>>代码示例>>Golang>>正文


Golang idna.ToASCII函数代码示例

本文整理汇总了Golang中golang.org/x/net/idna.ToASCII函数的典型用法代码示例。如果您正苦于以下问题:Golang ToASCII函数的具体用法?Golang ToASCII怎么用?Golang ToASCII使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了ToASCII函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: writeRegex

func writeRegex(tlds []string) error {
	allTldsSet := make(map[string]struct{})
	add := func(tld string) {
		if _, e := allTldsSet[tld]; e {
			log.Fatalf("Duplicate TLD: %s", tld)
		}
		allTldsSet[tld] = struct{}{}
	}
	for _, tldlist := range [...][]string{tlds, xurls.PseudoTLDs} {
		for _, tld := range tldlist {
			add(tld)
			asciiTld, err := idna.ToASCII(tld)
			if err != nil {
				return err
			}
			if asciiTld != tld {
				add(asciiTld)
			}
		}
	}
	var allTlds []string
	for tld := range allTldsSet {
		allTlds = append(allTlds, tld)
	}
	sort.Strings(allTlds)
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()
	return regexTmpl.Execute(f, map[string]string{
		"gtld       ": `(?i)(` + strings.Join(allTlds, `|`) + `)(?-i)`,
		"otherScheme": `(?i)(` + strings.Join(xurls.SchemesNoAuthority, `|`) + `)(?-i):`,
	})
}
开发者ID:Clarifai,项目名称:kubernetes,代码行数:35,代码来源:main.go

示例2: Fuzz

func Fuzz(data []byte) int {
	for _, v := range data {
		if v <= 0x20 || v >= 0x80 {
			return 0
		}
	}
	dec, err := idna.ToUnicode(string(data))
	if err != nil {
		return 0
	}
	enc, err := idna.ToASCII(dec)
	if err != nil {
		fmt.Printf("data: %q\n", data)
		fmt.Printf("dec : %q\n", dec)
		panic(err)
	}
	dec1, err := idna.ToUnicode(enc)
	if err != nil {
		fmt.Printf("data: %q\n", data)
		fmt.Printf("dec : %q\n", dec)
		fmt.Printf("enc : %q\n", enc)
		panic(err)
	}
	if dec != dec1 {
		fmt.Printf("data: %q\n", data)
		fmt.Printf("dec : %q\n", dec)
		fmt.Printf("enc : %q\n", enc)
		fmt.Printf("dec1: %q\n", dec1)
		panic("not equal")
	}
	return 1
}
开发者ID:sjn1978,项目名称:go-fuzz,代码行数:32,代码来源:main.go

示例3: query

func query(name, qtype string) {
	start := time.Now()
	qname, err := idna.ToASCII(name)
	if err != nil {
		color.Fprintf(os.Stderr, "Invalid IDN domain name: %s\n", name)
		os.Exit(1)
	}

	rrs, err := resolver.ResolveErr(qname, qtype)

	color.Printf("\n")
	if len(rrs) > 0 {
		color.Printf("@{g};; RESULTS:\n")
	}
	for _, rr := range rrs {
		color.Printf("@{g}%s\n", rr.String())
	}

	if err != nil {
		color.Printf("@{r};; %s\t%s\t%s\n", err, name, qtype)
	} else if rrs == nil {
		color.Printf("@{y};; NIL\t%s\t%s\n", name, qtype)
	} else if len(rrs) > 0 {
		color.Printf("@{g};; TRUE\t%s\t%s\n", name, qtype)
	} else {
		color.Printf("@{r};; FALSE\t%s\t%s\n", name, qtype)
	}

	color.Printf("@{.w};; Elapsed: %s\n", time.Since(start).String())
}
开发者ID:pages-alex-alex2006hw,项目名称:dnsr,代码行数:30,代码来源:main.go

示例4: StandardizeURL

// StandardizeURL standardizes the url by making sure it has a schema and converting IDNA domains into ASCII.
func StandardizeURL(url string) string {
	link := url
	var schema, domain, path string

	// Try to get the schema
	slice := strings.SplitN(url, "://", 2)
	if len(slice) == 2 && len(slice[0]) < 10 { // schema exists
		schema = slice[0] + "://"
		link = slice[1]
	} else {
		schema = "http://"
	}

	// Get the domain
	slice = strings.SplitN(link, "/", 2)
	if len(slice) == 2 {
		domain = slice[0]
		path = "/" + slice[1]
	} else {
		domain = slice[0]
		path = "/"
	}

	domain, _ = idna.ToASCII(domain)
	link = schema + domain + path

	return link
}
开发者ID:pawelszydlo,项目名称:papa-bot,代码行数:29,代码来源:utils.go

示例5: CheckPublicSuffix

func CheckPublicSuffix(zones map[string]*Zone) {
	color.Fprintf(os.Stderr, "@{.}Checking against the Public Suffix List for %d zones...\n", len(zones))
	mapZones(zones, func(z *Zone) {
		host, err := idna.ToASCII(pfx + z.Domain)
		if err != nil {
			LogWarning(err)
			return
		}
		s, _ := publicsuffix.PublicSuffix(host)
		s = Normalize(s)
		switch {
		// ZoneDB and PSL agree
		case s == z.Domain:
			return

		// PSL wildcard
		case strings.HasPrefix(s, pfx) && len(z.Subdomains) != 0:
			return

		// ZoneDB and PSL disagree
		default:
			color.Fprintf(os.Stderr, "@{y}Public Suffix List: @{y!}%[email protected]{y} for @{y!}%s\n", s, z.Domain)
		}
	})
}
开发者ID:digideskio,项目名称:zonedb,代码行数:25,代码来源:publicsuffix.go

示例6: verifyWhois

func verifyWhois(host string) error {
	host, err := idna.ToASCII(Normalize(host))
	if err != nil {
		return err
	}
	err = CanDial("tcp", host+":43")
	return err
}
开发者ID:digideskio,项目名称:zonedb,代码行数:8,代码来源:whois.go

示例7: verifyNS

func verifyNS(host string) error {
	host, err := idna.ToASCII(Normalize(host))
	if err != nil {
		return err
	}
	err = CanDial("udp", host+":53")
	return err
}
开发者ID:digideskio,项目名称:zonedb,代码行数:8,代码来源:dns.go

示例8: IsValid

func IsValid(email string) bool {

	at := strings.LastIndex(email, "@")

	if at == -1 {
		return false
	}

	local, _ := idna.ToASCII(email[0:at])
	domain, _ := idna.ToASCII(email[at+1:])

	localLen := len(local)
	if localLen < 1 || localLen > 64 {
		return false
	}

	domainLen := len(domain)
	if domainLen < 1 || domainLen > 255 {
		return false
	}

	if local[0] == '.' || local[localLen-1] == '.' {
		return false
	}

	if domain[0] == '.' || domain[domainLen-1] == '.' {
		return false
	}

	if dotsCheck.MatchString(local) || dotsCheck.MatchString(domain) {
		return false
	}

	if !domainCheck.MatchString(domain) {
		return false
	}

	local = strings.Replace(local, "\\\\", "", -1)
	if !localCheck.MatchString(local) && !quotedCheck.MatchString(local) {
		return false
	}

	return true
}
开发者ID:jitinl,项目名称:go-emailvalidator,代码行数:44,代码来源:email.go

示例9: NewRuleUnicode

// NewRuleUnicode is like NewRule, but expects the content to be encoded in Unicode (U-labels).
func NewRuleUnicode(content string) (*Rule, error) {
	var err error

	content, err = idna.ToASCII(content)
	if err != nil {
		return nil, err
	}

	return NewRule(content)
}
开发者ID:jfrazelle,项目名称:boulder,代码行数:11,代码来源:publicsuffix.go

示例10: NormalizeHostname

// Normalizes the hostname given. If the hostname is not valid, returns "" and
// an error.
func NormalizeHostname(name string) (string, error) {
	name = strings.TrimSuffix(strings.ToLower(name), ".")

	name, err := idna.ToASCII(name)
	if err != nil {
		return "", fmt.Errorf("IDN error: %#v: %v", name, err)
	}

	if !reHostname.MatchString(name) {
		return "", fmt.Errorf("invalid hostname: %#v", name)
	}

	return name, nil
}
开发者ID:hlandau,项目名称:acme,代码行数:16,代码来源:hostname.go

示例11: validateHost

func validateHost(host string) (string, error) {
	lower := strings.ToLower(host)
	if domainRegexp.MatchString(lower) || lower == "localhost" ||
		net.ParseIP(lower) != nil {
		return lower, nil
	}
	// The URL will be used by net/http, where IDNA is not supported.
	if punycode, err := idna.ToASCII(host); err != nil {
		return "", err
	} else if domainRegexp.MatchString(punycode) {
		return punycode, nil
	}
	return "", errors.New("not valid domain name or IP address")
}
开发者ID:fanyang01,项目名称:crawler,代码行数:14,代码来源:normalize.go

示例12: normalizeNames

func normalizeNames(names []string) error {
	for i := range names {
		n := strings.TrimSuffix(strings.ToLower(names[i]), ".")

		n, err := idna.ToASCII(n)
		if err != nil {
			return fmt.Errorf("IDN error: %v", err)
		}

		if !validHostname(n) {
			return fmt.Errorf("invalid hostname: %q", n)
		}

		names[i] = n
	}

	return nil
}
开发者ID:joneskoo,项目名称:acmetool-kapsi,代码行数:18,代码来源:util.go

示例13: NormalizeURLString

// NormalizeURLString returns the normalized string, or an error if it can't be parsed into an URL object.
// It takes an URL string as input, as well as the normalization flags.
func NormalizeURLString(u string, f NormalizationFlags) (string, error) {
	if parsed, e := url.Parse(u); e != nil {
		return "", e
	} else {
		options := make([]precis.Option, 1, 3)
		options[0] = precis.IgnoreCase
		if f&FlagLowercaseHost == FlagLowercaseHost {
			options = append(options, precis.FoldCase())
		}
		options = append(options, precis.Norm(norm.NFC))
		profile := precis.NewFreeform(options...)
		if parsed.Host, e = idna.ToASCII(profile.NewTransformer().String(parsed.Host)); e != nil {
			return "", e
		}
		return NormalizeURL(parsed, f), nil
	}
	panic("Unreachable code.")
}
开发者ID:kubernetes,项目名称:heapster,代码行数:20,代码来源:purell.go

示例14: encodeIDNAHost

func encodeIDNAHost(u *url.URL) {
	h := u.Host
	p := ""
	var err error
	if strings.Index(h, ":") > 0 {
		h, p, err = net.SplitHostPort(h)
		if err != nil {
			return
		}
	}
	if h, err = idna.ToASCII(h); err == nil {
		if len(p) > 0 {
			u.Host = fmt.Sprintf("%s:%s", h, p)
		} else {
			u.Host = h
		}
	}
}
开发者ID:Flipboard,项目名称:purell,代码行数:18,代码来源:purell.go

示例15: query

// Handler for DNS queries
func query(w http.ResponseWriter, r *http.Request) {
	server := r.URL.Query().Get(":server")
	domain := dns.Fqdn(r.URL.Query().Get(":domain"))
	querytype := r.URL.Query().Get(":querytype")

	if domain, err := idna.ToASCII(domain); err == nil { // Valid domain name (ASCII or IDN)
		if _, isDomain := dns.IsDomainName(domain); isDomain { // Well-formed domain name
			if querytype, ok := dns.StringToType[strings.ToUpper(querytype)]; ok { // Valid DNS query type
				resolve(w, r, server, domain, querytype)
			} else {
				error(w, 400, 404, "Invalid DNS query type")
			}
		} else {
			error(w, 400, 402, "Input string is not a well-formed domain name")
		}
	} else {
		error(w, 400, 401, "Input string could not be parsed")
	}
}
开发者ID:Soullivaneuh,项目名称:rrda,代码行数:20,代码来源:rrda.go


注:本文中的golang.org/x/net/idna.ToASCII函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。