experiment with range header in StaticFile
optimise StaticFile with StrLength add things i forgot yesterday
This commit is contained in:
parent
1a17190a2d
commit
133c240350
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue