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"
|
2020-04-21 00:08:54 +00:00
|
|
|
"sync"
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-27 04:15:02 +00:00
|
|
|
const slen1 = len("</s/>;rel=preload;as=script,") + 6
|
|
|
|
const slen2 = len("</s/>;rel=preload;as=style,") + 7
|
2020-04-21 00:08:54 +00:00
|
|
|
|
2020-05-27 04:15:02 +00:00
|
|
|
var pushStrPool = sync.Pool{}
|
2019-04-19 10:39:17 +00:00
|
|
|
|
2020-05-27 04:15:02 +00:00
|
|
|
func doPush(w http.ResponseWriter, h *c.Header) {
|
2019-04-02 07:43:11 +00:00
|
|
|
//fmt.Println("in doPush")
|
2020-05-27 04:15:02 +00:00
|
|
|
if len(h.Scripts) == 0 && len(h.ScriptsAsync) == 0 && len(h.Stylesheets) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
if c.Config.EnableCDNPush {
|
2020-05-27 04:15:02 +00:00
|
|
|
var sb *strings.Builder = &strings.Builder{}
|
|
|
|
/*ii := pushStrPool.Get()
|
2020-04-21 00:08:54 +00:00
|
|
|
if ii == nil {
|
|
|
|
sb = &strings.Builder{}
|
|
|
|
} else {
|
|
|
|
sb = ii.(*strings.Builder)
|
|
|
|
sb.Reset()
|
2020-05-27 04:15:02 +00:00
|
|
|
}*/
|
|
|
|
sb.Grow((slen1 * (len(h.Scripts) + len(h.ScriptsAsync))) + ((slen2 + 7) * len(h.Stylesheets)))
|
2019-11-04 10:11:07 +00:00
|
|
|
push := func(in []string) {
|
2020-05-27 04:15:02 +00:00
|
|
|
for i, path := range in {
|
|
|
|
if i != 0 {
|
|
|
|
sb.WriteString(",</s/")
|
|
|
|
} else {
|
|
|
|
sb.WriteString("</s/")
|
|
|
|
}
|
2019-04-19 10:39:17 +00:00
|
|
|
sb.WriteString(path)
|
2020-05-27 04:15:02 +00:00
|
|
|
sb.WriteString(">;rel=preload;as=script")
|
2019-04-02 07:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-27 04:15:02 +00:00
|
|
|
push(h.Scripts)
|
|
|
|
//push(h.PreScriptsAsync)
|
|
|
|
push(h.ScriptsAsync)
|
|
|
|
|
|
|
|
if len(h.Stylesheets) > 0 {
|
|
|
|
for i, path := range h.Stylesheets {
|
|
|
|
if i != 0 {
|
|
|
|
sb.WriteString(",</s/")
|
|
|
|
} else {
|
|
|
|
sb.WriteString("</s/")
|
|
|
|
}
|
2019-04-19 10:39:17 +00:00
|
|
|
sb.WriteString(path)
|
2020-05-27 04:15:02 +00:00
|
|
|
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()
|
2020-05-27 04:15:02 +00:00
|
|
|
w.Header().Set("Link", sbuf)
|
|
|
|
//pushStrPool.Put(sb)
|
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")
|
2020-05-27 04:15:02 +00:00
|
|
|
/*if bzw, ok := w.(c.BrResponseWriter); ok {
|
|
|
|
w = bzw.ResponseWriter
|
|
|
|
} else */if gzw, ok := w.(c.GzipResponseWriter); ok {
|
2019-04-02 07:43:11 +00:00
|
|
|
w = gzw.ResponseWriter
|
|
|
|
}
|
|
|
|
pusher, ok := w.(http.Pusher)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2020-03-27 12:00:29 +00:00
|
|
|
//panic("has pusher")
|
2019-04-02 07:43:11 +00:00
|
|
|
//fmt.Println("has pusher")
|
|
|
|
|
2020-05-27 04:15:02 +00:00
|
|
|
var sb *strings.Builder = &strings.Builder{}
|
|
|
|
/*ii := pushStrPool.Get()
|
|
|
|
if ii == nil {
|
|
|
|
sb = &strings.Builder{}
|
|
|
|
} else {
|
|
|
|
sb = ii.(*strings.Builder)
|
|
|
|
sb.Reset()
|
|
|
|
}*/
|
|
|
|
sb.Grow(6 * (len(h.Scripts) + len(h.ScriptsAsync) + len(h.Stylesheets)))
|
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)
|
2020-05-27 04:15:02 +00:00
|
|
|
sb.WriteString("/s/")
|
|
|
|
sb.WriteString(path)
|
|
|
|
err := pusher.Push(sb.String(), nil)
|
2019-04-02 07:43:11 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2020-05-27 04:15:02 +00:00
|
|
|
sb.Reset()
|
2019-04-02 07:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-27 04:15:02 +00:00
|
|
|
push(h.Scripts)
|
|
|
|
//push(h.PreScriptsAsync)
|
|
|
|
push(h.ScriptsAsync)
|
|
|
|
push(h.Stylesheets)
|
2019-04-02 07:43:11 +00:00
|
|
|
// TODO: Push avatars?
|
2020-05-27 04:15:02 +00:00
|
|
|
//pushStrPool.Put(sb)
|
2019-04-02 07:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-18 09:45:36 +00:00
|
|
|
func FootHeaders(w http.ResponseWriter, h *c.Header) {
|
2020-06-08 12:19:32 +00:00
|
|
|
// TODO: Only set video domain when there is a video on the page
|
2020-03-18 09:45:36 +00:00
|
|
|
if !h.LooseCSP {
|
2020-06-09 11:52:59 +00:00
|
|
|
he := w.Header()
|
2019-11-04 11:55:52 +00:00
|
|
|
if c.Config.SslSchema {
|
2020-06-09 11:52:59 +00:00
|
|
|
if h.ExternalMedia {
|
|
|
|
he.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 embed.nicovideo.jp;upgrade-insecure-requests")
|
|
|
|
} else {
|
|
|
|
he.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';upgrade-insecure-requests")
|
|
|
|
}
|
2019-11-04 10:11:07 +00:00
|
|
|
} else {
|
2020-06-09 11:52:59 +00:00
|
|
|
if h.ExternalMedia {
|
|
|
|
he.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 embed.nicovideo.jp")
|
|
|
|
} else {
|
|
|
|
he.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'")
|
|
|
|
}
|
2019-11-04 10:11:07 +00:00
|
|
|
}
|
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
|
2020-03-18 09:45:36 +00:00
|
|
|
lastAgent := h.CurrentUser.LastAgent
|
2019-04-02 07:43:11 +00:00
|
|
|
//fmt.Println("lastAgent:", lastAgent)
|
2020-03-08 04:15:27 +00:00
|
|
|
if lastAgent == c.Chrome || lastAgent == c.Firefox {
|
2020-03-18 09:45:36 +00:00
|
|
|
doPush(w, h)
|
2019-04-02 07:43:11 +00:00
|
|
|
}
|
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
|
2020-03-22 04:37:45 +00:00
|
|
|
noDescSimpleBot := h.CurrentUser.LastAgent == c.SimpleBots[0] || h.CurrentUser.LastAgent == c.SimpleBots[1]
|
|
|
|
var simpleBot bool
|
|
|
|
for _, agent := range c.SimpleBots {
|
|
|
|
if h.CurrentUser.LastAgent == agent {
|
|
|
|
simpleBot = true
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 23:50:57 +00:00
|
|
|
inner := r.FormValue("i") == "1"
|
|
|
|
if !inner && !simpleBot {
|
2020-03-18 09:21:34 +00:00
|
|
|
c.PrepResources(h.CurrentUser, h, h.Theme)
|
2020-03-13 06:37:18 +00:00
|
|
|
for _, ss := range s {
|
|
|
|
h.Stylesheets = append(h.Stylesheets, ss)
|
|
|
|
}
|
|
|
|
h.AddScript("global.js")
|
|
|
|
if h.CurrentUser.Loggedin {
|
|
|
|
h.AddScriptAsync("member.js")
|
|
|
|
}
|
2020-03-18 23:50:57 +00:00
|
|
|
} else {
|
2020-03-13 09:02:28 +00:00
|
|
|
h.CurrentUser.LastAgent = 0
|
2019-06-04 15:33:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 04:37:45 +00:00
|
|
|
if h.CurrentUser.Loggedin || inner || noDescSimpleBot {
|
2019-06-01 12:31:48 +00:00
|
|
|
h.MetaDesc = ""
|
|
|
|
h.OGDesc = ""
|
|
|
|
} else if h.MetaDesc != "" && h.OGDesc == "" {
|
|
|
|
h.OGDesc = h.MetaDesc
|
2019-04-20 04:55:22 +00:00
|
|
|
}
|
2019-04-02 07:43:11 +00:00
|
|
|
|
2020-03-18 21:21:06 +00:00
|
|
|
if !simpleBot {
|
2020-03-18 09:45:36 +00:00
|
|
|
FootHeaders(w, h)
|
2020-03-22 04:37:45 +00:00
|
|
|
} else {
|
|
|
|
h.GoogSiteVerify = ""
|
2020-03-18 09:45:36 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-03-18 09:21:34 +00:00
|
|
|
if c.RunPreRenderHook("pre_render_"+hookName, w, r, h.CurrentUser, pi) {
|
2018-10-27 03:21:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-27 04:15:02 +00:00
|
|
|
/*defer func() {
|
|
|
|
c.StrSlicePool.Put(h.Scripts)
|
|
|
|
c.StrSlicePool.Put(h.PreScriptsAsync)
|
|
|
|
}()*/
|
2020-03-27 12:00:29 +00:00
|
|
|
return h.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
|
2019-08-14 10:39:04 +00:00
|
|
|
var RenderTemplate = renderTemplate3
|
2020-05-27 04:15:02 +00:00
|
|
|
|
|
|
|
func actionSuccess(w http.ResponseWriter, r *http.Request, dest string, js bool) c.RouteError {
|
|
|
|
if !js {
|
|
|
|
http.Redirect(w, r, dest, http.StatusSeeOther)
|
|
|
|
} else {
|
|
|
|
_, _ = w.Write(successJSONBytes)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|