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"
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 {

View File

@ -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
}

View File

@ -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
}

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?
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)

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?
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)