2018-09-19 06:09:03 +00:00
|
|
|
// Highly experimental plugin for caching rendered pages for guests
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-04-20 12:49:48 +00:00
|
|
|
//"log"
|
2019-04-20 04:55:22 +00:00
|
|
|
"bytes"
|
2019-04-28 03:34:59 +00:00
|
|
|
"errors"
|
2019-04-28 10:08:05 +00:00
|
|
|
"strings"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2018-09-19 06:09:03 +00:00
|
|
|
"sync/atomic"
|
2019-04-20 04:55:22 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-09-19 06:09:03 +00:00
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
|
|
|
"github.com/Azareal/Gosora/routes"
|
2018-09-19 06:09:03 +00:00
|
|
|
)
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
var hyperspace *Hyperspace
|
2018-09-19 06:09:03 +00:00
|
|
|
|
|
|
|
func init() {
|
2019-04-20 04:55:22 +00:00
|
|
|
c.Plugins.Add(&c.Plugin{UName: "hyperdrive", Name: "Hyperdrive", Author: "Azareal", Init: initHdrive, Deactivate: deactivateHdrive})
|
2018-09-19 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
func initHdrive(plugin *c.Plugin) error {
|
|
|
|
hyperspace = newHyperspace()
|
|
|
|
plugin.AddHook("tasks_tick_topic_list",tickHdrive)
|
2019-04-20 12:49:48 +00:00
|
|
|
plugin.AddHook("tasks_tick_widget_wol",tickHdriveWol)
|
2019-04-28 10:50:45 +00:00
|
|
|
plugin.AddHook("route_topic_list_start",jumpHdriveTopicList)
|
|
|
|
plugin.AddHook("route_forum_list_start",jumpHdriveForumList)
|
2019-04-28 11:38:49 +00:00
|
|
|
tickHdrive()
|
2018-09-19 06:09:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
func deactivateHdrive(plugin *c.Plugin) {
|
|
|
|
plugin.RemoveHook("tasks_tick_topic_list",tickHdrive)
|
2019-04-20 12:49:48 +00:00
|
|
|
plugin.RemoveHook("tasks_tick_widget_wol",tickHdriveWol)
|
2019-04-28 10:50:45 +00:00
|
|
|
plugin.RemoveHook("route_topic_list_start",jumpHdriveTopicList)
|
|
|
|
plugin.RemoveHook("route_forum_list_start",jumpHdriveForumList)
|
2019-04-20 04:55:22 +00:00
|
|
|
hyperspace = nil
|
2018-09-19 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
type Hyperspace struct {
|
2018-09-19 06:09:03 +00:00
|
|
|
topicList atomic.Value
|
2019-04-27 06:32:26 +00:00
|
|
|
gzipTopicList atomic.Value
|
2019-04-28 10:50:45 +00:00
|
|
|
forumList atomic.Value
|
|
|
|
gzipForumList atomic.Value
|
2019-04-28 10:08:05 +00:00
|
|
|
lastTopicListUpdate atomic.Value
|
2018-09-19 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
func newHyperspace() *Hyperspace {
|
|
|
|
pageCache := new(Hyperspace)
|
2018-09-19 06:09:03 +00:00
|
|
|
pageCache.topicList.Store([]byte(""))
|
2019-04-28 10:08:05 +00:00
|
|
|
pageCache.gzipTopicList.Store([]byte(""))
|
2019-04-28 10:50:45 +00:00
|
|
|
pageCache.forumList.Store([]byte(""))
|
|
|
|
pageCache.gzipForumList.Store([]byte(""))
|
2019-04-28 10:08:05 +00:00
|
|
|
pageCache.lastTopicListUpdate.Store(int64(0))
|
2018-09-19 06:09:03 +00:00
|
|
|
return pageCache
|
|
|
|
}
|
2019-04-20 04:55:22 +00:00
|
|
|
|
2019-04-20 12:49:48 +00:00
|
|
|
func tickHdriveWol(args ...interface{}) (skip bool, rerr c.RouteError) {
|
|
|
|
c.DebugLog("docking at wol")
|
|
|
|
return tickHdrive(args)
|
|
|
|
}
|
|
|
|
|
2019-04-27 10:22:39 +00:00
|
|
|
// 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(""))
|
2019-04-28 10:50:45 +00:00
|
|
|
hyperspace.forumList.Store([]byte(""))
|
|
|
|
hyperspace.gzipForumList.Store([]byte(""))
|
2019-04-27 10:22:39 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2019-04-20 04:55:22 +00:00
|
|
|
req := httptest.NewRequest("get", "/topics/", bytes.NewReader(nil))
|
|
|
|
user := c.GuestUser
|
|
|
|
|
2019-04-27 10:22:39 +00:00
|
|
|
head, rerr := c.UserCheck(w, req, &user)
|
|
|
|
if rerr != nil {
|
2019-04-20 04:55:22 +00:00
|
|
|
return true, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
rerr = routes.TopicList(w, req, user, head)
|
|
|
|
if rerr != nil {
|
|
|
|
return true, rerr
|
|
|
|
}
|
|
|
|
if w.Code != 200 {
|
2019-04-27 10:22:39 +00:00
|
|
|
c.LogWarning(errors.New("not 200 for topic list in hyperdrive"))
|
2019-04-27 06:32:26 +00:00
|
|
|
return false, nil
|
2019-04-20 04:55:22 +00:00
|
|
|
}
|
2019-04-27 06:32:26 +00:00
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(w.Result().Body)
|
|
|
|
hyperspace.topicList.Store(buf.Bytes())
|
|
|
|
|
2019-04-27 10:22:39 +00:00
|
|
|
gbuf, err := c.CompressBytesGzip(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
c.LogWarning(err)
|
|
|
|
return false, nil
|
2019-04-27 06:32:26 +00:00
|
|
|
}
|
2019-04-27 10:22:39 +00:00
|
|
|
hyperspace.gzipTopicList.Store(gbuf)
|
2019-04-28 10:50:45 +00:00
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req = httptest.NewRequest("get", "/forums/", bytes.NewReader(nil))
|
|
|
|
user = c.GuestUser
|
|
|
|
|
|
|
|
head, rerr = c.UserCheck(w, req, &user)
|
|
|
|
if rerr != nil {
|
|
|
|
return true, rerr
|
|
|
|
}
|
|
|
|
|
|
|
|
rerr = routes.ForumList(w, req, user, head)
|
|
|
|
if rerr != nil {
|
|
|
|
return true, rerr
|
|
|
|
}
|
|
|
|
if w.Code != 200 {
|
|
|
|
c.LogWarning(errors.New("not 200 for forum list in hyperdrive"))
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(w.Result().Body)
|
|
|
|
hyperspace.forumList.Store(buf.Bytes())
|
|
|
|
|
|
|
|
gbuf, err = c.CompressBytesGzip(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
c.LogWarning(err)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
hyperspace.gzipForumList.Store(gbuf)
|
2019-04-28 10:08:05 +00:00
|
|
|
hyperspace.lastTopicListUpdate.Store(time.Now().Unix())
|
2019-04-27 06:32:26 +00:00
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-04-28 10:50:45 +00:00
|
|
|
func jumpHdriveTopicList(args ...interface{}) (skip bool, rerr c.RouteError) {
|
|
|
|
return jumpHdrive(hyperspace.gzipTopicList.Load().([]byte), hyperspace.topicList.Load().([]byte), args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func jumpHdriveForumList(args ...interface{}) (skip bool, rerr c.RouteError) {
|
|
|
|
return jumpHdrive(hyperspace.gzipForumList.Load().([]byte), hyperspace.forumList.Load().([]byte), args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func jumpHdrive(pg []byte, p []byte, args []interface{}) (skip bool, rerr c.RouteError) {
|
2019-04-27 06:32:26 +00:00
|
|
|
var tList []byte
|
|
|
|
w := args[0].(http.ResponseWriter)
|
2019-04-28 03:34:59 +00:00
|
|
|
var iw http.ResponseWriter
|
|
|
|
gzw, ok := w.(c.GzipResponseWriter)
|
2019-04-27 06:32:26 +00:00
|
|
|
if ok {
|
2019-04-28 10:50:45 +00:00
|
|
|
tList = pg
|
2019-04-28 03:34:59 +00:00
|
|
|
iw = gzw.ResponseWriter
|
2019-04-27 06:32:26 +00:00
|
|
|
} else {
|
2019-04-28 10:50:45 +00:00
|
|
|
tList = p
|
2019-04-28 03:34:59 +00:00
|
|
|
iw = w
|
2019-04-27 06:32:26 +00:00
|
|
|
}
|
2019-04-20 04:55:22 +00:00
|
|
|
if len(tList) == 0 {
|
2019-04-28 10:50:45 +00:00
|
|
|
c.DebugLog("no itemlist in hyperspace")
|
2019-04-20 04:55:22 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2019-04-28 03:34:59 +00:00
|
|
|
//c.DebugLog("tList: ", tList)
|
2019-04-20 04:55:22 +00:00
|
|
|
|
|
|
|
// Avoid intercepting user requests as we only have guests in cache right now
|
|
|
|
user := args[2].(*c.User)
|
|
|
|
if user.ID != 0 {
|
2019-04-20 12:49:48 +00:00
|
|
|
c.DebugLog("not guest")
|
2019-04-20 04:55:22 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid intercepting search requests and filters as we don't have those in cache
|
|
|
|
r := args[1].(*http.Request)
|
2019-04-20 12:49:48 +00:00
|
|
|
//c.DebugLog("r.URL.Path:",r.URL.Path)
|
|
|
|
//c.DebugLog("r.URL.RawQuery:",r.URL.RawQuery)
|
2019-04-20 04:55:22 +00:00
|
|
|
if r.URL.RawQuery != "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-04-27 10:22:39 +00:00
|
|
|
if r.FormValue("js") == "1" {
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-04-20 12:49:48 +00:00
|
|
|
//c.DebugLog
|
|
|
|
c.DebugLog("Successful jump")
|
2019-04-20 04:55:22 +00:00
|
|
|
|
2019-04-28 10:08:05 +00:00
|
|
|
var etag string
|
|
|
|
lastUpdate := hyperspace.lastTopicListUpdate.Load().(int64)
|
|
|
|
c.DebugLog("lastUpdate:",lastUpdate)
|
|
|
|
if ok {
|
|
|
|
iw.Header().Set("X-I","1")
|
|
|
|
etag = "\""+strconv.FormatInt(lastUpdate, 10)+"-g\""
|
|
|
|
} else {
|
|
|
|
etag = "\""+strconv.FormatInt(lastUpdate, 10)+"\""
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastUpdate != 0 {
|
|
|
|
iw.Header().Set("ETag", etag)
|
|
|
|
if match := r.Header.Get("If-None-Match"); match != "" {
|
|
|
|
if strings.Contains(match, etag) {
|
|
|
|
iw.WriteHeader(http.StatusNotModified)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
header := args[3].(*c.Header)
|
|
|
|
routes.FootHeaders(w, header)
|
2019-04-28 03:34:59 +00:00
|
|
|
iw.Write(tList)
|
2019-04-27 10:22:39 +00:00
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
return true, nil
|
|
|
|
}
|