Hyperdrive should handle Gzip properly now...

Fixed a potential bug where Hyperdrive might cache already cached content.
Skip jumpHdrive if requested via JavaScript for now.

Export CompressBytesGzip so Hyperdrive can use it.
This commit is contained in:
Azareal 2019-04-27 20:22:39 +10:00
parent 27a4a74840
commit e50366ce60
5 changed files with 43 additions and 45 deletions

View File

@ -233,7 +233,7 @@ func (list SFileList) JSTmplInit() error {
path = tmplName + ".js" path = tmplName + ".js"
DebugLog("js path: ", path) DebugLog("js path: ", path)
var ext = filepath.Ext("/tmpl_client/" + path) var ext = filepath.Ext("/tmpl_client/" + path)
gzipData, err := compressBytesGzip(data) gzipData, err := CompressBytesGzip(data)
if err != nil { if err != nil {
return err return err
} }
@ -273,7 +273,7 @@ func (list SFileList) Init() error {
// Avoid double-compressing images // Avoid double-compressing images
var gzipData []byte var gzipData []byte
if mimetype != "image/jpeg" && mimetype != "image/png" && mimetype != "image/gif" { if mimetype != "image/jpeg" && mimetype != "image/png" && mimetype != "image/gif" {
gzipData, err = compressBytesGzip(data) gzipData, err = CompressBytesGzip(data)
if err != nil { if err != nil {
return err return err
} }
@ -311,7 +311,7 @@ func (list SFileList) Add(path string, prefix string) error {
var ext = filepath.Ext(path) var ext = filepath.Ext(path)
path = strings.TrimPrefix(path, prefix) path = strings.TrimPrefix(path, prefix)
gzipData, err := compressBytesGzip(data) gzipData, err := CompressBytesGzip(data)
if err != nil { if err != nil {
return err return err
} }
@ -340,7 +340,7 @@ func (list SFileList) Set(name string, data SFile) {
list[name] = data list[name] = data
} }
func compressBytesGzip(in []byte) ([]byte, error) { func CompressBytesGzip(in []byte) ([]byte, error) {
var buff bytes.Buffer var buff bytes.Buffer
gz, err := gzip.NewWriterLevel(&buff, gzip.BestCompression) gz, err := gzip.NewWriterLevel(&buff, gzip.BestCompression)
if err != nil { if err != nil {

View File

@ -155,7 +155,7 @@ func (theme *Theme) AddThemeStaticFiles() error {
} }
path = strings.TrimPrefix(path, "themes/"+theme.Name+"/public") path = strings.TrimPrefix(path, "themes/"+theme.Name+"/public")
gzipData, err := compressBytesGzip(data) gzipData, err := CompressBytesGzip(data)
if err != nil { if err != nil {
return err return err
} }

View File

@ -49,52 +49,42 @@ func tickHdriveWol(args ...interface{}) (skip bool, rerr c.RouteError) {
return tickHdrive(args) return tickHdrive(args)
} }
func dummyReqHdrive() http.ResponseWriter { // TODO: Find a better way of doing this
func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
c.DebugLog("Refueling...")
// Avoid accidentally caching already cached content
hyperspace.topicList.Store([]byte(""))
hyperspace.gzipTopicList.Store([]byte(""))
w := httptest.NewRecorder()
req := httptest.NewRequest("get", "/topics/", bytes.NewReader(nil)) req := httptest.NewRequest("get", "/topics/", bytes.NewReader(nil))
user := c.GuestUser user := c.GuestUser
head, err := c.UserCheck(w, req, &user) head, rerr := c.UserCheck(w, req, &user)
if err != nil { if rerr != nil {
c.LogWarning(err)
return true, rerr return true, rerr
} }
rerr = routes.TopicList(w, req, user, head) rerr = routes.TopicList(w, req, user, head)
if rerr != nil { if rerr != nil {
c.LogWarning(err)
return true, rerr return true, rerr
} }
if w.Code != 200 { if w.Code != 200 {
c.LogWarning(err) c.LogWarning(errors.New("not 200 for topic list in hyperdrive"))
return false, nil return false, nil
} }
return w
}
// TODO: Find a better way of doing this
func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
c.DebugLog("Refueling...")
w := httptest.NewRecorder()
dummyReqHdrive(w)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
buf.ReadFrom(w.Result().Body) buf.ReadFrom(w.Result().Body)
hyperspace.topicList.Store(buf.Bytes()) hyperspace.topicList.Store(buf.Bytes())
w = httptest.NewRecorder() gbuf, err := c.CompressBytesGzip(buf.Bytes())
w.Header().Set("Content-Encoding", "gzip") if err != nil {
w.Header().Set("Content-Type", "text/html; charset=utf-8") c.LogWarning(err)
gz := gzip.NewWriter(w) return false, nil
w = c.GzipResponseWriter{Writer: gz, ResponseWriter: w}
dummyReqHdrive(w)
buf = new(bytes.Buffer)
buf.ReadFrom(w.Result().Body)
hyperspace.gzipTopicList.Store(buf.Bytes())
if w.Header().Get("Content-Encoding") == "gzip" {
gz.Close()
} }
hyperspace.gzipTopicList.Store(gbuf)
return false, nil return false, nil
} }
@ -127,12 +117,18 @@ func jumpHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
if r.URL.RawQuery != "" { if r.URL.RawQuery != "" {
return false, nil return false, nil
} }
if r.FormValue("js") == "1" {
return false, nil
}
//c.DebugLog //c.DebugLog
c.DebugLog("Successful jump") c.DebugLog("Successful jump")
header := args[3].(*c.Header) header := args[3].(*c.Header)
routes.FootHeaders(w, header) routes.FootHeaders(w, header)
w.Write(tList) w.Write(tList)
if ok {
w.Header().Set("X-I","1")
}
return true, nil return true, nil
} }

View File

@ -992,15 +992,16 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Disable Gzip when SSL is disabled for security reasons? // Disable Gzip when SSL is disabled for security reasons?
if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip") h := w.Header()
w.Header().Set("Content-Type", "text/html; charset=utf-8") h.Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w) h.Set("Content-Type", "text/html; charset=utf-8")
gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w}
defer func() { defer func() {
if w.Header().Get("Content-Encoding") == "gzip" { if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" {
gz.Close() gzw.Writer.(*gzip.Writer).Close()
} }
}() }()
w = c.GzipResponseWriter{Writer: gz, ResponseWriter: w} w = gzw
} }
skip, ferr = hTbl.VhookSkippable("router_pre_route", w, req, user, prefix, extraData) skip, ferr = hTbl.VhookSkippable("router_pre_route", w, req, user, prefix, extraData)

View File

@ -774,15 +774,16 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Disable Gzip when SSL is disabled for security reasons? // Disable Gzip when SSL is disabled for security reasons?
if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip") h := w.Header()
w.Header().Set("Content-Type", "text/html; charset=utf-8") h.Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w) h.Set("Content-Type", "text/html; charset=utf-8")
gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w}
defer func() { defer func() {
if w.Header().Get("Content-Encoding") == "gzip" { if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" {
gz.Close() gzw.Writer.(*gzip.Writer).Close()
} }
}() }()
w = c.GzipResponseWriter{Writer: gz, ResponseWriter: w} w = gzw
} }
skip, ferr = hTbl.VhookSkippable("router_pre_route", w, req, user, prefix, extraData) skip, ferr = hTbl.VhookSkippable("router_pre_route", w, req, user, prefix, extraData)