serve brotli in plugin hyperdrive

bypass on inner in plugin hyperdrive
This commit is contained in:
Azareal 2020-04-14 07:09:51 +10:00
parent 26590c4d80
commit c8d021d70b

View File

@ -42,19 +42,15 @@ func deactivateHdrive(pl *c.Plugin) {
type Hyperspace struct {
topicList atomic.Value
gzipTopicList atomic.Value
forumList atomic.Value
gzipForumList atomic.Value
lastTopicListUpdate atomic.Value
}
func newHyperspace() *Hyperspace {
pageCache := new(Hyperspace)
blank := make(map[string][]byte, len(c.Themes))
blank := make(map[string][3][]byte, len(c.Themes))
pageCache.topicList.Store(blank)
pageCache.gzipTopicList.Store(blank)
pageCache.forumList.Store(blank)
pageCache.gzipForumList.Store(blank)
pageCache.lastTopicListUpdate.Store(int64(0))
return pageCache
}
@ -69,16 +65,12 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
c.DebugLog("Refueling...")
// Avoid accidentally caching already cached content
blank := make(map[string][]byte, len(c.Themes))
blank := make(map[string][3][]byte, len(c.Themes))
hyperspace.topicList.Store(blank)
hyperspace.gzipTopicList.Store(blank)
hyperspace.forumList.Store(blank)
hyperspace.gzipForumList.Store(blank)
tListMap := make(map[string][]byte)
gtListMap := make(map[string][]byte)
fListMap := make(map[string][]byte)
gfListMap := make(map[string][]byte)
tListMap := make(map[string][3][]byte)
fListMap := make(map[string][3][]byte)
cacheTheme := func(tname string) (skip, fail bool, rerr c.RouteError) {
@ -93,7 +85,6 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
if rerr != nil {
return true, true, rerr
}
rerr = routes.TopicList(w, req, &user, head)
if rerr != nil {
return true, true, rerr
@ -105,14 +96,19 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
buf := new(bytes.Buffer)
buf.ReadFrom(w.Result().Body)
tListMap[tname] = buf.Bytes()
gbuf, err := c.CompressBytesGzip(buf.Bytes())
if err != nil {
c.LogWarning(err)
return false, true, nil
}
gtListMap[tname] = gbuf
bbuf, err := c.CompressBytesBrotli(buf.Bytes())
if err != nil {
c.LogWarning(err)
return false, true, nil
}
tListMap[tname] = [3][]byte{buf.Bytes(), gbuf, bbuf}
w = httptest.NewRecorder()
req = httptest.NewRequest("get", "/forums/", bytes.NewReader(nil))
@ -122,7 +118,6 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
if rerr != nil {
return true, true, rerr
}
rerr = routes.ForumList(w, req, &user, head)
if rerr != nil {
return true, true, rerr
@ -134,14 +129,19 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
buf = new(bytes.Buffer)
buf.ReadFrom(w.Result().Body)
fListMap[tname] = buf.Bytes()
gbuf, err = c.CompressBytesGzip(buf.Bytes())
if err != nil {
c.LogWarning(err)
return false, true, nil
}
gfListMap[tname] = gbuf
bbuf, err = c.CompressBytesBrotli(buf.Bytes())
if err != nil {
c.LogWarning(err)
return false, true, nil
}
fListMap[tname] = [3][]byte{buf.Bytes(), gbuf, bbuf}
return false, false, nil
}
@ -153,9 +153,7 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
}
hyperspace.topicList.Store(tListMap)
hyperspace.gzipTopicList.Store(gtListMap)
hyperspace.forumList.Store(fListMap)
hyperspace.gzipForumList.Store(gfListMap)
hyperspace.lastTopicListUpdate.Store(time.Now().Unix())
return false, nil
@ -163,28 +161,40 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
func jumpHdriveTopicList(args ...interface{}) (skip bool, rerr c.RouteError) {
theme := c.GetThemeByReq(args[1].(*http.Request))
p := hyperspace.topicList.Load().(map[string][]byte)
pg := hyperspace.gzipTopicList.Load().(map[string][]byte)
return jumpHdrive(pg[theme.Name], p[theme.Name], args)
p := hyperspace.topicList.Load().(map[string][3][]byte)
return jumpHdrive(p[theme.Name], args)
}
func jumpHdriveForumList(args ...interface{}) (skip bool, rerr c.RouteError) {
theme := c.GetThemeByReq(args[1].(*http.Request))
p := hyperspace.forumList.Load().(map[string][]byte)
pg := hyperspace.gzipForumList.Load().(map[string][]byte)
return jumpHdrive(pg[theme.Name], p[theme.Name], args)
p := hyperspace.forumList.Load().(map[string][3][]byte)
return jumpHdrive(p[theme.Name], args)
}
func jumpHdrive(pg, p []byte, args []interface{}) (skip bool, rerr c.RouteError) {
func jumpHdrive( /*pg, */ p [3][]byte, args []interface{}) (skip bool, rerr c.RouteError) {
var tList []byte
w := args[0].(http.ResponseWriter)
r := args[1].(*http.Request)
var iw http.ResponseWriter
gzw, ok := w.(c.GzipResponseWriter)
if ok {
tList = pg
//bzw, ok2 := w.(c.BrResponseWriter)
// !temp until global brotli
br := strings.Contains(r.Header.Get("Accept-Encoding"), "br")
if br && ok {
tList = p[2]
iw = gzw.ResponseWriter
} else if br {
tList = p[2]
iw = w
} else if ok {
tList = p[1]
iw = gzw.ResponseWriter
/*} else if ok2 {
tList = p[2]
iw = bzw.ResponseWriter
*/
} else {
tList = p
tList = p[0]
iw = w
}
if len(tList) == 0 {
@ -201,24 +211,31 @@ func jumpHdrive(pg, p []byte, args []interface{}) (skip bool, rerr c.RouteError)
}
// Avoid intercepting search requests and filters as we don't have those in cache
r := args[1].(*http.Request)
//c.DebugLog("r.URL.Path:",r.URL.Path)
//c.DebugLog("r.URL.RawQuery:",r.URL.RawQuery)
if r.URL.RawQuery != "" {
return false, nil
}
if r.FormValue("js") == "1" {
if r.FormValue("js") == "1" || r.FormValue("i") == "1" {
return false, nil
}
//c.DebugLog
c.DebugLog("Successful jump")
var etag string
lastUpdate := hyperspace.lastTopicListUpdate.Load().(int64)
c.DebugLog("lastUpdate:", lastUpdate)
if ok {
if br {
h := iw.Header()
h.Set("X-I", "1")
h.Set("Content-Encoding", "br")
etag = "\"" + strconv.FormatInt(lastUpdate, 10) + "-b\""
} else if ok {
iw.Header().Set("X-I", "1")
etag = "\"" + strconv.FormatInt(lastUpdate, 10) + "-g\""
/*} else if ok2 {
iw.Header().Set("X-I", "1")
etag = "\"" + strconv.FormatInt(lastUpdate, 10) + "-b\""
*/
} else {
etag = "\"" + strconv.FormatInt(lastUpdate, 10) + "\""
}
@ -234,8 +251,8 @@ func jumpHdrive(pg, p []byte, args []interface{}) (skip bool, rerr c.RouteError)
}
header := args[3].(*c.Header)
if ok {
gzw.Header().Set("Content-Type", "text/html;charset=utf-8")
if br || ok /*ok2*/ {
iw.Header().Set("Content-Type", "text/html;charset=utf-8")
}
routes.FootHeaders(w, header)
iw.Write(tList)