本文整理汇总了Golang中golang.org/x/net/idna.ToASCII函数的典型用法代码示例。如果您正苦于以下问题:Golang ToASCII函数的具体用法?Golang ToASCII怎么用?Golang ToASCII使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ToASCII函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: 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) {
parsed, err := url.Parse(u)
if err != nil {
return "", err
}
if f&FlagLowercaseHost == FlagLowercaseHost {
parsed.Host = strings.ToLower(parsed.Host)
}
// The idna package doesn't fully conform to RFC 5895
// (https://tools.ietf.org/html/rfc5895), so we do it here.
// Taken from Go 1.8 cycle source, courtesy of bradfitz.
// TODO: Remove when (if?) idna package conforms to RFC 5895.
parsed.Host = width.Fold.String(parsed.Host)
parsed.Host = norm.NFC.String(parsed.Host)
if parsed.Host, err = idna.ToASCII(parsed.Host); err != nil {
return "", err
}
return NormalizeURL(parsed, f), nil
}
开发者ID:intelsdi-x,项目名称:rkt,代码行数:24,代码来源:purell.go
示例17: checkHost
func checkHost(host string) error {
if host == "" {
return &url.Error{"host", host, errors.New("empty host")}
}
host = strings.ToLower(host)
if domainRegexp.MatchString(host) || host == "localhost" {
return nil
}
if punycode, err := idna.ToASCII(host); err != nil {
return err
} else if domainRegexp.MatchString(punycode) {
return nil
}
// IPv4 and IPv6.
if ipv4Regexp.MatchString(host) || ipv6Regexp.MatchString(host) {
return nil
}
return &url.Error{"host", host, errors.New("invalid host")}
}
开发者ID:palaiyacw,项目名称:imgry,代码行数:23,代码来源:urlx.go
示例18: PunycodeHostPort
// PunycodeHostPort returns the IDNA Punycode version
// of the provided "host" or "host:port" string.
func PunycodeHostPort(v string) (string, error) {
if isASCII(v) {
return v, nil
}
host, port, err := net.SplitHostPort(v)
if err != nil {
// The input 'v' argument was just a "host" argument,
// without a port. This error should not be returned
// to the caller.
host = v
port = ""
}
host, err = idna.ToASCII(host)
if err != nil {
// Non-UTF-8? Not representable in Punycode, in any
// case.
return "", err
}
if port == "" {
return host, nil
}
return net.JoinHostPort(host, port), nil
}
开发者ID:Rudloff,项目名称:platform,代码行数:26,代码来源:httplex.go
示例19: findURLs
// Search for URLs in a string
func findURLs(message string) (urls []*url.URL) {
// Source of the regular expression:
// http://daringfireball.net/2010/07/improved_regex_for_matching_urls
re := regexp.MustCompile("(?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’])")
urlCandidates := re.FindAllString(message, maxUrlsCount)
for _, candidate := range urlCandidates {
url, err := url.Parse(candidate)
if err != nil {
break
}
// Scheme is required to query a URL
if url.Scheme == "" {
url.Scheme = "http"
}
// Conversion to ASCII is needed for Unicode hostnames
asciiHost, err := idna.ToASCII(url.Host)
if err == nil {
url.Host = asciiHost
}
urls = append(urls, url)
}
return
}
开发者ID:romainletendart,项目名称:goxxx,代码行数:25,代码来源:titlefinder.go
示例20: Send
// Send email
func (m *MailData) Send() error {
// trim space
m.To_email = strings.TrimSpace(m.To_email)
// punycode convert
splitEmail := strings.Split(m.To_email, "@")
if len(splitEmail) != 2 {
return errors.New(fmt.Sprintf("Bad email"))
}
domain, err := idna.ToASCII(splitEmail[1])
if err != nil {
return errors.New(fmt.Sprintf("Domain name failed: %v", err))
}
m.To_email = strings.Split(m.To_email, "@")[0] + "@" + domain
if m.Iface == "" {
// default interface
m.netConn = net.Dialer{}
} else {
if m.Iface[0:8] == "socks://" {
m.Iface = m.Iface[8:]
var err error
m.socksConn, err = proxy.SOCKS5("tcp", m.Iface, nil, proxy.FromEnvironment())
if err != nil {
return err
}
} else {
connectAddr := net.ParseIP(m.Iface)
tcpAddr := &net.TCPAddr{
IP: connectAddr,
}
m.netConn = net.Dialer{LocalAddr: tcpAddr}
}
}
start := time.Now()
//record, err := net.LookupMX(c.domain)
record, err := models.DomainGetMX(domain)
if err != nil {
return err
}
lookupTime := time.Since(start)
start = time.Now()
var serverMx string
for i := range record {
smx := net.JoinHostPort(record[i].Host, "25")
if m.socksConn != nil {
m.conn, err = m.socksConn.Dial("tcp", smx)
} else {
m.conn, err = m.netConn.Dial("tcp", smx)
}
if err == nil {
serverMx = record[i].Host
connTime := time.Since(start)
fmt.Printf("Connect time to %s %s. Lookup time %s.\n\r", domain, connTime, lookupTime)
break
}
}
if err != nil {
return err
}
defer m.conn.Close()
// 5 minute by RFC
m.conn.SetDeadline(time.Now().Add(5 * time.Minute))
c, err := smtp.NewClient(m.conn, serverMx)
if err != nil {
return errors.New(fmt.Sprintf("%v (NewClient)", err))
}
if err := c.Hello(m.Host); err != nil {
return errors.New(fmt.Sprintf("%v (Hello)", err))
}
// Set the sender and recipient first
if err := c.Mail(m.From_email); err != nil {
return errors.New(fmt.Sprintf("%v (Mail)", err))
}
if err := c.Rcpt(m.To_email); err != nil {
return errors.New(fmt.Sprintf("%v (Rcpt)", err))
}
w, err := c.Data()
if err != nil {
return errors.New(fmt.Sprintf("%v (Data)", err))
}
_, err = fmt.Fprint(w, m.Data())
if err != nil {
return errors.New(fmt.Sprintf("%v (SendData)", err))
}
err = w.Close()
if err != nil {
return errors.New(fmt.Sprintf("%v (Close)", err))
}
return c.Quit()
//.........这里部分代码省略.........
开发者ID:Supme,项目名称:gonder,代码行数:101,代码来源:mailer.go
注:本文中的golang.org/x/net/idna.ToASCII函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论