本文整理汇总了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):`,
})
}
示例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
}
示例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())
}
示例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
}
示例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)
}
})
}
示例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
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例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")
}
示例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
}
示例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.")
}
示例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
}
}
}
示例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")
}
}