don't fallback to english to prevent cache layer DoS
This commit is contained in:
parent
644f8da3f4
commit
b4de45569e
@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type I18N struct {
|
||||
@ -22,6 +23,7 @@ type I18N struct {
|
||||
}
|
||||
|
||||
var I18nProvider *I18N = nil
|
||||
var ErrNoLang = errors.New("no such language")
|
||||
|
||||
//Read all .ini files in dir, where the filenames are BCP 47 tags
|
||||
//Use the language matcher to get the best match for the locale preference
|
||||
@ -41,17 +43,23 @@ func NewI18n(locale, dir string) (*I18N, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
found:= false
|
||||
serverLangs := make([]language.Tag, 1)
|
||||
serverLangs[0] = language.AmericanEnglish // en-US fallback
|
||||
// serverLangs[0] = language.AmericanEnglish // en-US fallback
|
||||
for _, file := range files {
|
||||
if filepath.Ext(file.Name()) == ".ini" {
|
||||
name := strings.TrimSuffix(file.Name(), ".ini")
|
||||
tag, err := language.Parse(name)
|
||||
if err == nil {
|
||||
serverLangs = append(serverLangs, tag)
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return nil, ErrNoLang
|
||||
}
|
||||
|
||||
matcher := language.NewMatcher(serverLangs)
|
||||
tag, _, _ := matcher.Match(pref)
|
||||
|
||||
|
@ -51,6 +51,7 @@ func (self *nullHandler) GetI18N(r *http.Request) *I18N {
|
||||
i, err = NewI18n(lang, self.translations)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil
|
||||
}
|
||||
if i != nil {
|
||||
self.i18n[lang] = i
|
||||
@ -64,7 +65,10 @@ func (self *nullHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
sfw := strings.Count(r.URL.RawQuery, "sfw=1") > 0
|
||||
i18n := self.GetI18N(r)
|
||||
|
||||
if i18n == nil {
|
||||
http.Redirect(w, r, r.URL.Path, http.StatusFound)
|
||||
return
|
||||
}
|
||||
path := r.URL.Path
|
||||
_, file := filepath.Split(path)
|
||||
|
||||
|
@ -544,3 +544,10 @@ background-repeat: repeat;
|
||||
:target {
|
||||
background-color: #493769;
|
||||
}
|
||||
|
||||
body {
|
||||
display: inline-flex;
|
||||
flex-direction: column;
|
||||
flex-wrap: wrap;
|
||||
flex-flow: wrap;
|
||||
}
|
||||
|
Reference in New Issue
Block a user