Support international domain names

This internally converts the configured host name into its Punycode ASCII
representation, while showing users the correct Unicode domain name.
pull/457/head
Matt Baer 4 years ago
parent c0fdd8af49
commit 967ee9679c
  1. 5
      collections.go
  2. 22
      config/config.go
  3. 24
      config/funcs.go
  4. 2
      go.mod

@ -33,6 +33,7 @@ import (
"github.com/writefreely/writefreely/author" "github.com/writefreely/writefreely/author"
"github.com/writefreely/writefreely/config" "github.com/writefreely/writefreely/config"
"github.com/writefreely/writefreely/page" "github.com/writefreely/writefreely/page"
"golang.org/x/net/idna"
) )
type ( type (
@ -236,7 +237,9 @@ func (c *Collection) DisplayCanonicalURL() string {
if p == "/" { if p == "/" {
p = "" p = ""
} }
return u.Hostname() + p d := u.Hostname()
d, _ = idna.ToUnicode(d)
return d + p
} }
func (c *Collection) RedirectingCanonicalURL(isRedir bool) string { func (c *Collection) RedirectingCanonicalURL(isRedir bool) string {

@ -1,5 +1,5 @@
/* /*
* Copyright © 2018-2020 A Bunch Tell LLC. * Copyright © 2018-2021 A Bunch Tell LLC.
* *
* This file is part of WriteFreely. * This file is part of WriteFreely.
* *
@ -12,8 +12,11 @@
package config package config
import ( import (
"net/url"
"strings" "strings"
"github.com/writeas/web-core/log"
"golang.org/x/net/idna"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
@ -257,6 +260,23 @@ func Load(fname string) (*Config, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Do any transformations
u, err := url.Parse(uc.App.Host)
if err != nil {
return nil, err
}
d, err := idna.ToASCII(u.Hostname())
log.Error("Host: %s", uc.App.Host)
if err != nil {
log.Error("ToASCII: %s", err)
return nil, err
}
uc.App.Host = u.Scheme + "://" + d
if u.Port() != "" {
uc.App.Host += ":" + u.Port()
}
return uc, nil return uc, nil
} }

@ -1,5 +1,5 @@
/* /*
* Copyright © 2018 A Bunch Tell LLC. * Copyright © 2018, 2020-2021 A Bunch Tell LLC.
* *
* This file is part of WriteFreely. * This file is part of WriteFreely.
* *
@ -11,14 +11,34 @@
package config package config
import ( import (
"github.com/writeas/web-core/log"
"golang.org/x/net/idna"
"net/http" "net/http"
"net/url"
"strings" "strings"
"time" "time"
) )
// FriendlyHost returns the app's Host sans any schema // FriendlyHost returns the app's Host sans any schema
func (ac AppCfg) FriendlyHost() string { func (ac AppCfg) FriendlyHost() string {
return ac.Host[strings.Index(ac.Host, "://")+len("://"):] rawHost := ac.Host[strings.Index(ac.Host, "://")+len("://"):]
u, err := url.Parse(ac.Host)
if err != nil {
log.Error("url.Parse failed on %s: %s", ac.Host, err)
return rawHost
}
d, err := idna.ToUnicode(u.Hostname())
if err != nil {
log.Error("idna.ToUnicode failed on %s: %s", ac.Host, err)
return rawHost
}
res := d
if u.Port() != "" {
res += ":" + u.Port()
}
return res
} }
func (ac AppCfg) CanCreateBlogs(currentlyUsed uint64) bool { func (ac AppCfg) CanCreateBlogs(currentlyUsed uint64) bool {

@ -42,7 +42,7 @@ require (
github.com/writeas/web-core v1.3.1-0.20210330164422-95a3a717ed8f github.com/writeas/web-core v1.3.1-0.20210330164422-95a3a717ed8f
github.com/writefreely/go-nodeinfo v1.2.0 github.com/writefreely/go-nodeinfo v1.2.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect golang.org/x/net v0.0.0-20200707034311-ab3426394381
gopkg.in/ini.v1 v1.62.0 gopkg.in/ini.v1 v1.62.0
) )

Loading…
Cancel
Save