From 133c240350611950ab1cfc4b52aafe4ea8ed3e25 Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 7 Mar 2020 07:52:58 +1000 Subject: [PATCH] experiment with range header in StaticFile optimise StaticFile with StrLength add things i forgot yesterday --- common/files.go | 10 ++++++---- common/templates/templates.go | 2 +- routes/misc.go | 29 +++++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/common/files.go b/common/files.go index 31a2f721..85e6f06c 100644 --- a/common/files.go +++ b/common/files.go @@ -31,6 +31,7 @@ type SFile struct { OName string Pos int64 Length int64 + StrLength string GzipLength int64 StrGzipLength string Mimetype string @@ -184,7 +185,8 @@ func (list SFileList) JSTmplInit() error { data = replace(data, " c.", "") data = replace(data, "phrases.", "") data = replace(data, ", 10;", "") - data = replace(data, "var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "const plist = tmplPhrases[\""+tmplName+"\"];") + //data = replace(data, "var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "const plist = tmplPhrases[\""+tmplName+"\"];") + data = replace(data, "//var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "const "+shortName+"_phrase_arr = tmplPhrases[\""+tmplName+"\"];") data = replace(data, "var cached_var_", "let cached_var_") data = replace(data, `tmpl_vars, ok := tmpl_i.`, `/*`) data = replace(data, "[]byte(", "") @@ -242,7 +244,7 @@ func (list SFileList) JSTmplInit() error { hasher.Write(data) checksum := hex.EncodeToString(hasher.Sum(nil)) - list.Set("/s/"+path, SFile{data, gzipData, checksum, path + "?h=" + checksum, 0, int64(len(data)), int64(len(gzipData)), strconv.Itoa(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)}) + list.Set("/s/"+path, SFile{data, gzipData, checksum, path + "?h=" + checksum, 0, int64(len(data)), strconv.Itoa(len(data)), int64(len(gzipData)), strconv.Itoa(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)}) DebugLogf("Added the '%s' static file.", path) return nil @@ -287,7 +289,7 @@ func (list SFileList) Init() error { } } - list.Set("/s/"+path, SFile{data, gzipData, checksum, path + "?h=" + checksum, 0, int64(len(data)), int64(len(gzipData)), strconv.Itoa(len(gzipData)), mimetype, f, f.ModTime().UTC().Format(http.TimeFormat)}) + list.Set("/s/"+path, SFile{data, gzipData, checksum, path + "?h=" + checksum, 0, int64(len(data)), strconv.Itoa(len(data)), int64(len(gzipData)), strconv.Itoa(len(gzipData)), mimetype, f, f.ModTime().UTC().Format(http.TimeFormat)}) DebugLogf("Added the '%s' static file.", path) return nil @@ -320,7 +322,7 @@ func (list SFileList) Add(path, prefix string) error { hasher.Write(data) checksum := hex.EncodeToString(hasher.Sum(nil)) - list.Set("/s"+path, SFile{data, gzipData, checksum, path + "?h=" + checksum, 0, int64(len(data)), int64(len(gzipData)), strconv.Itoa(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)}) + list.Set("/s"+path, SFile{data, gzipData, checksum, path + "?h=" + checksum, 0, int64(len(data)), strconv.Itoa(len(data)), int64(len(gzipData)), strconv.Itoa(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)}) DebugLogf("Added the '%s' static file", path) return nil diff --git a/common/templates/templates.go b/common/templates/templates.go index 74a936f5..2c2bb006 100644 --- a/common/templates/templates.go +++ b/common/templates/templates.go @@ -519,7 +519,7 @@ if !ok { } if len(c.langIndexToName) > 0 { - //fout += "var plist = phrases.GetTmplPhrasesBytes(" + fname + "_tmpl_phrase_id)\n" + fout += "//var plist = phrases.GetTmplPhrasesBytes(" + fname + "_tmpl_phrase_id)\n" //fout += "if len(plist) > 0 {\n_ = plist[len(plist)-1]\n}\n" //fout += "var plist = " + fname + "_phrase_arr\n" } diff --git a/routes/misc.go b/routes/misc.go index 04ef0673..8e4e1bf7 100644 --- a/routes/misc.go +++ b/routes/misc.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" "time" + //"fmt" c "github.com/Azareal/Gosora/common" @@ -27,6 +28,30 @@ func StaticFile(w http.ResponseWriter, r *http.Request) { } h := w.Header() + if file.Length > 300 { + rangeHead := h.Get("Range") + if rangeHead != "" { + if file.GzipLength > 300 && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + if len(file.Sha256) != 0 { + h.Set("Cache-Control", cacheControlMaxAgeWeek) + } else { + h.Set("Cache-Control", cacheControlMaxAge) //Cache-Control: max-age=31536000 + } + h.Set("Content-Encoding", "gzip") + h.Set("Content-Length", file.StrGzipLength) + http.ServeContent(w, r, r.URL.Path, file.Info.ModTime(), bytes.NewReader(file.GzipData)) + } else if file.GzipLength == 0 { + if len(file.Sha256) != 0 { + h.Set("Cache-Control", cacheControlMaxAgeWeek) + } else { + h.Set("Cache-Control", cacheControlMaxAge) //Cache-Control: max-age=31536000 + } + h.Set("Content-Length", file.StrLength) + http.ServeContent(w, r, r.URL.Path, file.Info.ModTime(), bytes.NewReader(file.Data)) + } + } + } + // Surely, there's a more efficient way of doing this? t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")) if err == nil && file.Info.ModTime().Before(t.Add(1*time.Second)) { @@ -42,12 +67,12 @@ func StaticFile(w http.ResponseWriter, r *http.Request) { } h.Set("Vary", "Accept-Encoding") - if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && file.GzipLength > 0 { + if file.GzipLength > 0 && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { h.Set("Content-Encoding", "gzip") h.Set("Content-Length", file.StrGzipLength) io.Copy(w, bytes.NewReader(file.GzipData)) // Use w.Write instead? } else { - h.Set("Content-Length", strconv.FormatInt(file.Length, 10)) // Avoid doing a type conversion every time? + h.Set("Content-Length", file.StrLength) io.Copy(w, bytes.NewReader(file.Data)) } // Other options instead of io.Copy: io.CopyN(), w.Write(), http.ServeContent()