2018-05-27 09:36:35 +00:00
|
|
|
package routes
|
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
import (
|
2019-04-02 07:43:11 +00:00
|
|
|
//"fmt"
|
2018-10-27 03:21:02 +00:00
|
|
|
"net/http"
|
2018-11-12 09:23:36 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-12-08 00:45:27 +00:00
|
|
|
"time"
|
2018-10-27 03:21:02 +00:00
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2020-02-23 09:08:47 +00:00
|
|
|
co "github.com/Azareal/Gosora/common/counters"
|
2020-02-29 01:10:39 +00:00
|
|
|
"github.com/Azareal/Gosora/uutils"
|
2018-10-27 03:21:02 +00:00
|
|
|
)
|
|
|
|
|
2019-11-04 10:11:07 +00:00
|
|
|
var successJSONBytes = []byte(`{"success":1}`)
|
2018-10-27 03:21:02 +00:00
|
|
|
|
2018-11-12 09:23:36 +00:00
|
|
|
func ParseSEOURL(urlBit string) (slug string, id int, err error) {
|
|
|
|
halves := strings.Split(urlBit, ".")
|
|
|
|
if len(halves) < 2 {
|
|
|
|
halves = append(halves, halves[0])
|
|
|
|
}
|
|
|
|
tid, err := strconv.Atoi(halves[1])
|
|
|
|
return halves[0], tid, err
|
|
|
|
}
|
|
|
|
|
2019-08-14 10:39:04 +00:00
|
|
|
var slen1 = len("</s/>; rel=preload; as=script,")
|
|
|
|
var slen2 = len("</s/>; rel=preload; as=style,")
|
2019-04-19 10:39:17 +00:00
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
func doPush(w http.ResponseWriter, header *c.Header) {
|
2019-04-02 07:43:11 +00:00
|
|
|
//fmt.Println("in doPush")
|
2019-04-19 06:36:26 +00:00
|
|
|
if c.Config.EnableCDNPush {
|
2019-04-19 10:39:17 +00:00
|
|
|
// TODO: Cache these in a sync.Pool?
|
|
|
|
var sb strings.Builder
|
2019-11-04 10:11:07 +00:00
|
|
|
push := func(in []string) {
|
2019-04-19 10:39:17 +00:00
|
|
|
sb.Grow((slen1 + 5) * len(in))
|
2019-04-02 07:43:11 +00:00
|
|
|
for _, path := range in {
|
2019-08-14 10:39:04 +00:00
|
|
|
sb.WriteString("</s/")
|
2019-04-19 10:39:17 +00:00
|
|
|
sb.WriteString(path)
|
|
|
|
sb.WriteString(">; rel=preload; as=script,")
|
2019-04-02 07:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
push(header.Scripts)
|
|
|
|
//push(header.PreScriptsAsync)
|
|
|
|
push(header.ScriptsAsync)
|
|
|
|
|
|
|
|
if len(header.Stylesheets) > 0 {
|
2019-04-19 10:39:17 +00:00
|
|
|
sb.Grow((slen2 + 6) * len(header.Stylesheets))
|
2019-04-02 07:43:11 +00:00
|
|
|
for _, path := range header.Stylesheets {
|
2019-08-14 10:39:04 +00:00
|
|
|
sb.WriteString("</s/")
|
2019-04-19 10:39:17 +00:00
|
|
|
sb.WriteString(path)
|
|
|
|
sb.WriteString(">; rel=preload; as=style,")
|
2019-04-02 07:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Push avatars?
|
|
|
|
|
2019-04-19 10:39:17 +00:00
|
|
|
if sb.Len() > 0 {
|
|
|
|
sbuf := sb.String()
|
|
|
|
w.Header().Set("Link", sbuf[:len(sbuf)-1])
|
2019-04-02 07:43:11 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
} else if !c.Config.DisableServerPush {
|
2019-04-02 07:43:11 +00:00
|
|
|
//fmt.Println("push enabled")
|
2019-04-19 06:36:26 +00:00
|
|
|
gzw, ok := w.(c.GzipResponseWriter)
|
2019-04-02 07:43:11 +00:00
|
|
|
if ok {
|
|
|
|
w = gzw.ResponseWriter
|
|
|
|
}
|
|
|
|
pusher, ok := w.(http.Pusher)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//fmt.Println("has pusher")
|
|
|
|
|
2019-11-04 10:11:07 +00:00
|
|
|
push := func(in []string) {
|
2019-04-02 07:43:11 +00:00
|
|
|
for _, path := range in {
|
2019-08-14 10:39:04 +00:00
|
|
|
//fmt.Println("pushing /s/" + path)
|
2019-04-20 04:55:22 +00:00
|
|
|
// TODO: Avoid concatenating here
|
2019-08-14 10:39:04 +00:00
|
|
|
err := pusher.Push("/s/"+path, nil)
|
2019-04-02 07:43:11 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
push(header.Scripts)
|
|
|
|
//push(header.PreScriptsAsync)
|
|
|
|
push(header.ScriptsAsync)
|
|
|
|
push(header.Stylesheets)
|
|
|
|
// TODO: Push avatars?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
func renderTemplate(tmplName string, w http.ResponseWriter, r *http.Request, header *c.Header, pi interface{}) c.RouteError {
|
2019-04-19 09:34:16 +00:00
|
|
|
return renderTemplate2(tmplName, tmplName, w, r, header, pi)
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
func renderTemplate2(tmplName, hookName string, w http.ResponseWriter, r *http.Request, header *c.Header, pi interface{}) c.RouteError {
|
2019-04-19 09:34:16 +00:00
|
|
|
err := renderTemplate3(tmplName, tmplName, w, r, header, pi)
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
func FootHeaders(w http.ResponseWriter, header *c.Header) {
|
2019-11-04 10:11:07 +00:00
|
|
|
if !header.LooseCSP {
|
2019-11-04 11:55:52 +00:00
|
|
|
if c.Config.SslSchema {
|
2019-11-04 10:11:07 +00:00
|
|
|
w.Header().Set("Content-Security-Policy", "default-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-eval' 'unsafe-inline'; img-src * data: 'unsafe-eval' 'unsafe-inline'; connect-src * 'unsafe-eval' 'unsafe-inline'; frame-src 'self' www.youtube-nocookie.com;upgrade-insecure-requests")
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Security-Policy", "default-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-eval' 'unsafe-inline'; img-src * data: 'unsafe-eval' 'unsafe-inline'; connect-src * 'unsafe-eval' 'unsafe-inline'; frame-src 'self' www.youtube-nocookie.com")
|
|
|
|
}
|
2019-02-24 08:02:00 +00:00
|
|
|
}
|
2019-04-02 07:43:11 +00:00
|
|
|
|
|
|
|
// Server pushes can backfire on certain browsers, so we want to make sure it's only triggered for ones where it'll help
|
|
|
|
lastAgent := header.CurrentUser.LastAgent
|
|
|
|
//fmt.Println("lastAgent:", lastAgent)
|
2020-03-08 04:15:27 +00:00
|
|
|
if lastAgent == c.Chrome || lastAgent == c.Firefox {
|
2019-04-02 07:43:11 +00:00
|
|
|
doPush(w, header)
|
|
|
|
}
|
2019-04-20 04:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
func renderTemplate3(tmplName, hookName string, w http.ResponseWriter, r *http.Request, h *c.Header, pi interface{}) error {
|
2019-06-04 15:33:44 +00:00
|
|
|
s := h.Stylesheets
|
|
|
|
h.Stylesheets = nil
|
2019-06-01 12:31:48 +00:00
|
|
|
c.PrepResources(&h.CurrentUser, h, h.Theme)
|
2019-06-04 15:33:44 +00:00
|
|
|
for _, ss := range s {
|
2019-08-14 10:39:04 +00:00
|
|
|
h.Stylesheets = append(h.Stylesheets, ss)
|
2019-06-04 15:33:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
if h.CurrentUser.Loggedin {
|
|
|
|
h.MetaDesc = ""
|
|
|
|
h.OGDesc = ""
|
|
|
|
} else if h.MetaDesc != "" && h.OGDesc == "" {
|
|
|
|
h.OGDesc = h.MetaDesc
|
2019-04-20 04:55:22 +00:00
|
|
|
}
|
2019-06-01 12:31:48 +00:00
|
|
|
h.AddScript("global.js")
|
|
|
|
if h.CurrentUser.Loggedin {
|
|
|
|
h.AddScriptAsync("member.js")
|
2019-04-25 06:22:29 +00:00
|
|
|
}
|
2019-04-02 07:43:11 +00:00
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
FootHeaders(w, h)
|
2020-02-28 05:06:16 +00:00
|
|
|
if h.Zone != "error" {
|
2020-02-29 23:15:48 +00:00
|
|
|
since := time.Duration(uutils.Nanotime() - h.StartedAt)
|
|
|
|
if h.CurrentUser.IsAdmin {
|
|
|
|
h.Elapsed1 = since.String()
|
|
|
|
}
|
|
|
|
co.PerfCounter.Push(since /*, false*/)
|
2020-02-28 05:06:16 +00:00
|
|
|
}
|
2019-06-01 12:31:48 +00:00
|
|
|
if c.RunPreRenderHook("pre_render_"+hookName, w, r, &h.CurrentUser, pi) {
|
2018-10-27 03:21:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-02-24 01:18:54 +00:00
|
|
|
err := h.Theme.RunTmpl(tmplName, pi, w)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2018-10-27 03:21:02 +00:00
|
|
|
}
|
2019-04-19 09:34:16 +00:00
|
|
|
|
|
|
|
// TODO: Rename renderTemplate to RenderTemplate instead of using this hack to avoid breaking things
|
2019-08-14 10:39:04 +00:00
|
|
|
var RenderTemplate = renderTemplate3
|