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"
|
2018-10-27 03:21:02 +00:00
|
|
|
)
|
|
|
|
|
2018-05-27 09:36:35 +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-04-19 10:39:17 +00:00
|
|
|
var slen1 = len("</static/>; rel=preload; as=script,")
|
|
|
|
var slen2 = len("</static/>; rel=preload; as=style,")
|
|
|
|
|
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-04-02 07:43:11 +00:00
|
|
|
var 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-04-19 10:39:17 +00:00
|
|
|
sb.WriteString("</static/")
|
|
|
|
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-04-19 10:39:17 +00:00
|
|
|
sb.WriteString("</static/")
|
|
|
|
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")
|
|
|
|
|
|
|
|
var push = func(in []string) {
|
|
|
|
for _, path := range in {
|
|
|
|
//fmt.Println("pushing /static/" + path)
|
2019-04-20 04:55:22 +00:00
|
|
|
// TODO: Avoid concatenating here
|
2019-04-02 07:43:11 +00:00
|
|
|
err := pusher.Push("/static/"+path, nil)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderTemplate2(tmplName string, hookName string, w http.ResponseWriter, r *http.Request, header *c.Header, pi interface{}) c.RouteError {
|
|
|
|
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-02-24 08:02:00 +00:00
|
|
|
// TODO: Expand this to non-HTTPS requests too
|
2019-04-19 06:36:26 +00:00
|
|
|
if !header.LooseCSP && c.Site.EnableSsl {
|
2019-03-21 22:59:41 +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")
|
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)
|
|
|
|
if lastAgent == "chrome" || lastAgent == "firefox" {
|
|
|
|
doPush(w, header)
|
|
|
|
}
|
2019-04-20 04:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func renderTemplate3(tmplName string, hookName string, w http.ResponseWriter, r *http.Request, header *c.Header, pi interface{}) error {
|
|
|
|
c.PrepResources(&header.CurrentUser, header, header.Theme)
|
|
|
|
if header.CurrentUser.Loggedin {
|
|
|
|
header.MetaDesc = ""
|
|
|
|
header.OGDesc = ""
|
|
|
|
} else if header.MetaDesc != "" && header.OGDesc == "" {
|
|
|
|
header.OGDesc = header.MetaDesc
|
|
|
|
}
|
|
|
|
header.AddScript("global.js")
|
2019-04-25 06:22:29 +00:00
|
|
|
if header.CurrentUser.Loggedin {
|
|
|
|
header.AddScriptAsync("member.js")
|
|
|
|
}
|
2019-04-02 07:43:11 +00:00
|
|
|
|
2019-04-20 04:55:22 +00:00
|
|
|
FootHeaders(w, header)
|
2018-12-08 00:45:27 +00:00
|
|
|
if header.CurrentUser.IsAdmin {
|
|
|
|
header.Elapsed1 = time.Since(header.StartedAt).String()
|
|
|
|
}
|
2019-04-19 09:34:16 +00:00
|
|
|
if c.RunPreRenderHook("pre_render_"+hookName, w, r, &header.CurrentUser, pi) {
|
2018-10-27 03:21:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-19 09:34:16 +00:00
|
|
|
return header.Theme.RunTmpl(tmplName, pi, w)
|
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
|
|
|
|
var RenderTemplate = renderTemplate3
|