From e50366ce6051f9db287c5e3f9e2298b4c82ff9b9 Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 27 Apr 2019 20:22:39 +1000 Subject: [PATCH] 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. --- common/files.go | 8 ++--- common/theme.go | 2 +- experimental/plugin_hyperdrive.go | 52 ++++++++++++++----------------- gen_router.go | 13 ++++---- router_gen/main.go | 13 ++++---- 5 files changed, 43 insertions(+), 45 deletions(-) diff --git a/common/files.go b/common/files.go index 82a08002..75b13e8b 100644 --- a/common/files.go +++ b/common/files.go @@ -233,7 +233,7 @@ func (list SFileList) JSTmplInit() error { path = tmplName + ".js" DebugLog("js path: ", path) var ext = filepath.Ext("/tmpl_client/" + path) - gzipData, err := compressBytesGzip(data) + gzipData, err := CompressBytesGzip(data) if err != nil { return err } @@ -273,7 +273,7 @@ func (list SFileList) Init() error { // Avoid double-compressing images var gzipData []byte if mimetype != "image/jpeg" && mimetype != "image/png" && mimetype != "image/gif" { - gzipData, err = compressBytesGzip(data) + gzipData, err = CompressBytesGzip(data) if err != nil { return err } @@ -311,7 +311,7 @@ func (list SFileList) Add(path string, prefix string) error { var ext = filepath.Ext(path) path = strings.TrimPrefix(path, prefix) - gzipData, err := compressBytesGzip(data) + gzipData, err := CompressBytesGzip(data) if err != nil { return err } @@ -340,7 +340,7 @@ func (list SFileList) Set(name string, data SFile) { list[name] = data } -func compressBytesGzip(in []byte) ([]byte, error) { +func CompressBytesGzip(in []byte) ([]byte, error) { var buff bytes.Buffer gz, err := gzip.NewWriterLevel(&buff, gzip.BestCompression) if err != nil { diff --git a/common/theme.go b/common/theme.go index 0402b654..4e72b436 100644 --- a/common/theme.go +++ b/common/theme.go @@ -155,7 +155,7 @@ func (theme *Theme) AddThemeStaticFiles() error { } path = strings.TrimPrefix(path, "themes/"+theme.Name+"/public") - gzipData, err := compressBytesGzip(data) + gzipData, err := CompressBytesGzip(data) if err != nil { return err } diff --git a/experimental/plugin_hyperdrive.go b/experimental/plugin_hyperdrive.go index 1e9eda76..f2d720ce 100644 --- a/experimental/plugin_hyperdrive.go +++ b/experimental/plugin_hyperdrive.go @@ -49,52 +49,42 @@ func tickHdriveWol(args ...interface{}) (skip bool, rerr c.RouteError) { 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)) user := c.GuestUser - head, err := c.UserCheck(w, req, &user) - if err != nil { - c.LogWarning(err) + head, rerr := c.UserCheck(w, req, &user) + if rerr != nil { return true, rerr } rerr = routes.TopicList(w, req, user, head) if rerr != nil { - c.LogWarning(err) return true, rerr } if w.Code != 200 { - c.LogWarning(err) + c.LogWarning(errors.New("not 200 for topic list in hyperdrive")) 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.ReadFrom(w.Result().Body) hyperspace.topicList.Store(buf.Bytes()) - w = httptest.NewRecorder() - w.Header().Set("Content-Encoding", "gzip") - w.Header().Set("Content-Type", "text/html; charset=utf-8") - gz := gzip.NewWriter(w) - 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() + gbuf, err := c.CompressBytesGzip(buf.Bytes()) + if err != nil { + c.LogWarning(err) + return false, nil } + hyperspace.gzipTopicList.Store(gbuf) return false, nil } @@ -127,12 +117,18 @@ func jumpHdrive(args ...interface{}) (skip bool, rerr c.RouteError) { if r.URL.RawQuery != "" { return false, nil } + if r.FormValue("js") == "1" { + return false, nil + } //c.DebugLog c.DebugLog("Successful jump") header := args[3].(*c.Header) routes.FootHeaders(w, header) w.Write(tList) - + if ok { + w.Header().Set("X-I","1") + } + return true, nil } \ No newline at end of file diff --git a/gen_router.go b/gen_router.go index 38515f1e..31d80c6c 100644 --- a/gen_router.go +++ b/gen_router.go @@ -992,15 +992,16 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { // Disable Gzip when SSL is disabled for security reasons? if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { - w.Header().Set("Content-Encoding", "gzip") - w.Header().Set("Content-Type", "text/html; charset=utf-8") - gz := gzip.NewWriter(w) + h := w.Header() + h.Set("Content-Encoding", "gzip") + h.Set("Content-Type", "text/html; charset=utf-8") + gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w} defer func() { - if w.Header().Get("Content-Encoding") == "gzip" { - gz.Close() + if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" { + 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) diff --git a/router_gen/main.go b/router_gen/main.go index ba3e7f7e..feaede4b 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -774,15 +774,16 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) { // Disable Gzip when SSL is disabled for security reasons? if prefix != "/ws" && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { - w.Header().Set("Content-Encoding", "gzip") - w.Header().Set("Content-Type", "text/html; charset=utf-8") - gz := gzip.NewWriter(w) + h := w.Header() + h.Set("Content-Encoding", "gzip") + h.Set("Content-Type", "text/html; charset=utf-8") + gzw := c.GzipResponseWriter{Writer: gzip.NewWriter(w), ResponseWriter: w} defer func() { - if w.Header().Get("Content-Encoding") == "gzip" { - gz.Close() + if h.Get("Content-Encoding") == "gzip" && h.Get("X-I") == "" { + 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)