serve brotli in plugin hyperdrive
bypass on inner in plugin hyperdrive
This commit is contained in:
parent
26590c4d80
commit
c8d021d70b
|
@ -42,19 +42,15 @@ func deactivateHdrive(pl *c.Plugin) {
|
||||||
|
|
||||||
type Hyperspace struct {
|
type Hyperspace struct {
|
||||||
topicList atomic.Value
|
topicList atomic.Value
|
||||||
gzipTopicList atomic.Value
|
|
||||||
forumList atomic.Value
|
forumList atomic.Value
|
||||||
gzipForumList atomic.Value
|
|
||||||
lastTopicListUpdate atomic.Value
|
lastTopicListUpdate atomic.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHyperspace() *Hyperspace {
|
func newHyperspace() *Hyperspace {
|
||||||
pageCache := new(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.topicList.Store(blank)
|
||||||
pageCache.gzipTopicList.Store(blank)
|
|
||||||
pageCache.forumList.Store(blank)
|
pageCache.forumList.Store(blank)
|
||||||
pageCache.gzipForumList.Store(blank)
|
|
||||||
pageCache.lastTopicListUpdate.Store(int64(0))
|
pageCache.lastTopicListUpdate.Store(int64(0))
|
||||||
return pageCache
|
return pageCache
|
||||||
}
|
}
|
||||||
|
@ -69,16 +65,12 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
|
||||||
c.DebugLog("Refueling...")
|
c.DebugLog("Refueling...")
|
||||||
|
|
||||||
// Avoid accidentally caching already cached content
|
// 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.topicList.Store(blank)
|
||||||
hyperspace.gzipTopicList.Store(blank)
|
|
||||||
hyperspace.forumList.Store(blank)
|
hyperspace.forumList.Store(blank)
|
||||||
hyperspace.gzipForumList.Store(blank)
|
|
||||||
|
|
||||||
tListMap := make(map[string][]byte)
|
tListMap := make(map[string][3][]byte)
|
||||||
gtListMap := make(map[string][]byte)
|
fListMap := make(map[string][3][]byte)
|
||||||
fListMap := make(map[string][]byte)
|
|
||||||
gfListMap := make(map[string][]byte)
|
|
||||||
|
|
||||||
cacheTheme := func(tname string) (skip, fail bool, rerr c.RouteError) {
|
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 {
|
if rerr != nil {
|
||||||
return true, true, rerr
|
return true, true, rerr
|
||||||
}
|
}
|
||||||
|
|
||||||
rerr = routes.TopicList(w, req, &user, head)
|
rerr = routes.TopicList(w, req, &user, head)
|
||||||
if rerr != nil {
|
if rerr != nil {
|
||||||
return true, true, rerr
|
return true, true, rerr
|
||||||
|
@ -105,14 +96,19 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(w.Result().Body)
|
buf.ReadFrom(w.Result().Body)
|
||||||
tListMap[tname] = buf.Bytes()
|
|
||||||
|
|
||||||
gbuf, err := c.CompressBytesGzip(buf.Bytes())
|
gbuf, err := c.CompressBytesGzip(buf.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.LogWarning(err)
|
c.LogWarning(err)
|
||||||
return false, true, nil
|
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()
|
w = httptest.NewRecorder()
|
||||||
req = httptest.NewRequest("get", "/forums/", bytes.NewReader(nil))
|
req = httptest.NewRequest("get", "/forums/", bytes.NewReader(nil))
|
||||||
|
@ -122,7 +118,6 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
|
||||||
if rerr != nil {
|
if rerr != nil {
|
||||||
return true, true, rerr
|
return true, true, rerr
|
||||||
}
|
}
|
||||||
|
|
||||||
rerr = routes.ForumList(w, req, &user, head)
|
rerr = routes.ForumList(w, req, &user, head)
|
||||||
if rerr != nil {
|
if rerr != nil {
|
||||||
return true, true, rerr
|
return true, true, rerr
|
||||||
|
@ -134,14 +129,19 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
|
||||||
|
|
||||||
buf = new(bytes.Buffer)
|
buf = new(bytes.Buffer)
|
||||||
buf.ReadFrom(w.Result().Body)
|
buf.ReadFrom(w.Result().Body)
|
||||||
fListMap[tname] = buf.Bytes()
|
|
||||||
|
|
||||||
gbuf, err = c.CompressBytesGzip(buf.Bytes())
|
gbuf, err = c.CompressBytesGzip(buf.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.LogWarning(err)
|
c.LogWarning(err)
|
||||||
return false, true, nil
|
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
|
return false, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,9 +153,7 @@ func tickHdrive(args ...interface{}) (skip bool, rerr c.RouteError) {
|
||||||
}
|
}
|
||||||
|
|
||||||
hyperspace.topicList.Store(tListMap)
|
hyperspace.topicList.Store(tListMap)
|
||||||
hyperspace.gzipTopicList.Store(gtListMap)
|
|
||||||
hyperspace.forumList.Store(fListMap)
|
hyperspace.forumList.Store(fListMap)
|
||||||
hyperspace.gzipForumList.Store(gfListMap)
|
|
||||||
hyperspace.lastTopicListUpdate.Store(time.Now().Unix())
|
hyperspace.lastTopicListUpdate.Store(time.Now().Unix())
|
||||||
|
|
||||||
return false, nil
|
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) {
|
func jumpHdriveTopicList(args ...interface{}) (skip bool, rerr c.RouteError) {
|
||||||
theme := c.GetThemeByReq(args[1].(*http.Request))
|
theme := c.GetThemeByReq(args[1].(*http.Request))
|
||||||
p := hyperspace.topicList.Load().(map[string][]byte)
|
p := hyperspace.topicList.Load().(map[string][3][]byte)
|
||||||
pg := hyperspace.gzipTopicList.Load().(map[string][]byte)
|
return jumpHdrive(p[theme.Name], args)
|
||||||
return jumpHdrive(pg[theme.Name], p[theme.Name], args)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func jumpHdriveForumList(args ...interface{}) (skip bool, rerr c.RouteError) {
|
func jumpHdriveForumList(args ...interface{}) (skip bool, rerr c.RouteError) {
|
||||||
theme := c.GetThemeByReq(args[1].(*http.Request))
|
theme := c.GetThemeByReq(args[1].(*http.Request))
|
||||||
p := hyperspace.forumList.Load().(map[string][]byte)
|
p := hyperspace.forumList.Load().(map[string][3][]byte)
|
||||||
pg := hyperspace.gzipForumList.Load().(map[string][]byte)
|
return jumpHdrive(p[theme.Name], args)
|
||||||
return jumpHdrive(pg[theme.Name], 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
|
var tList []byte
|
||||||
w := args[0].(http.ResponseWriter)
|
w := args[0].(http.ResponseWriter)
|
||||||
|
r := args[1].(*http.Request)
|
||||||
var iw http.ResponseWriter
|
var iw http.ResponseWriter
|
||||||
gzw, ok := w.(c.GzipResponseWriter)
|
gzw, ok := w.(c.GzipResponseWriter)
|
||||||
if ok {
|
//bzw, ok2 := w.(c.BrResponseWriter)
|
||||||
tList = pg
|
// !temp until global brotli
|
||||||
|
br := strings.Contains(r.Header.Get("Accept-Encoding"), "br")
|
||||||
|
if br && ok {
|
||||||
|
tList = p[2]
|
||||||
iw = gzw.ResponseWriter
|
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 {
|
} else {
|
||||||
tList = p
|
tList = p[0]
|
||||||
iw = w
|
iw = w
|
||||||
}
|
}
|
||||||
if len(tList) == 0 {
|
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
|
// 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.Path:",r.URL.Path)
|
||||||
//c.DebugLog("r.URL.RawQuery:",r.URL.RawQuery)
|
//c.DebugLog("r.URL.RawQuery:",r.URL.RawQuery)
|
||||||
if r.URL.RawQuery != "" {
|
if r.URL.RawQuery != "" {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
if r.FormValue("js") == "1" {
|
if r.FormValue("js") == "1" || r.FormValue("i") == "1" {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
//c.DebugLog
|
|
||||||
c.DebugLog("Successful jump")
|
c.DebugLog("Successful jump")
|
||||||
|
|
||||||
var etag string
|
var etag string
|
||||||
lastUpdate := hyperspace.lastTopicListUpdate.Load().(int64)
|
lastUpdate := hyperspace.lastTopicListUpdate.Load().(int64)
|
||||||
c.DebugLog("lastUpdate:", lastUpdate)
|
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")
|
iw.Header().Set("X-I", "1")
|
||||||
etag = "\"" + strconv.FormatInt(lastUpdate, 10) + "-g\""
|
etag = "\"" + strconv.FormatInt(lastUpdate, 10) + "-g\""
|
||||||
|
/*} else if ok2 {
|
||||||
|
iw.Header().Set("X-I", "1")
|
||||||
|
etag = "\"" + strconv.FormatInt(lastUpdate, 10) + "-b\""
|
||||||
|
*/
|
||||||
} else {
|
} else {
|
||||||
etag = "\"" + strconv.FormatInt(lastUpdate, 10) + "\""
|
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)
|
header := args[3].(*c.Header)
|
||||||
if ok {
|
if br || ok /*ok2*/ {
|
||||||
gzw.Header().Set("Content-Type", "text/html;charset=utf-8")
|
iw.Header().Set("Content-Type", "text/html;charset=utf-8")
|
||||||
}
|
}
|
||||||
routes.FootHeaders(w, header)
|
routes.FootHeaders(w, header)
|
||||||
iw.Write(tList)
|
iw.Write(tList)
|
||||||
|
|
Loading…
Reference in New Issue