Support international domain names

This internally converts the configured host name into its Punycode ASCII
representation, while showing users the correct Unicode domain name.
This commit is contained in:
Matt Baer 2021-04-26 11:18:51 -04:00
parent c0fdd8af49
commit 967ee9679c
4 changed files with 48 additions and 5 deletions

View File

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

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2018-2020 A Bunch Tell LLC.
* Copyright © 2018-2021 A Bunch Tell LLC.
*
* This file is part of WriteFreely.
*
@ -12,8 +12,11 @@
package config
import (
"net/url"
"strings"
"github.com/writeas/web-core/log"
"golang.org/x/net/idna"
"gopkg.in/ini.v1"
)
@ -257,6 +260,23 @@ func Load(fname string) (*Config, error) {
if err != nil {
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
}

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2018 A Bunch Tell LLC.
* Copyright © 2018, 2020-2021 A Bunch Tell LLC.
*
* This file is part of WriteFreely.
*
@ -11,14 +11,34 @@
package config
import (
"github.com/writeas/web-core/log"
"golang.org/x/net/idna"
"net/http"
"net/url"
"strings"
"time"
)
// FriendlyHost returns the app's Host sans any schema
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 {

2
go.mod
View File

@ -42,7 +42,7 @@ require (
github.com/writeas/web-core v1.3.1-0.20210330164422-95a3a717ed8f
github.com/writefreely/go-nodeinfo v1.2.0
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
)