Save a few allocations in the parser.

Pave the way for rel=ugc support.
This commit is contained in:
Azareal 2019-10-06 10:55:03 +10:00
parent 592f5614ea
commit 11d1286b56
1 changed files with 24 additions and 6 deletions

View File

@ -23,6 +23,7 @@ var InvalidProfile = []byte("<red>[Invalid Profile]</red>")
var InvalidForum = []byte("<red>[Invalid Forum]</red>") var InvalidForum = []byte("<red>[Invalid Forum]</red>")
var unknownMedia = []byte("<red>[Unknown Media]</red>") var unknownMedia = []byte("<red>[Unknown Media]</red>")
var URLOpen = []byte("<a href='") var URLOpen = []byte("<a href='")
var URLOpenUser = []byte("<a rel='ugc' href='")
var URLOpen2 = []byte("'>") var URLOpen2 = []byte("'>")
var bytesSinglequote = []byte("'") var bytesSinglequote = []byte("'")
var bytesGreaterthan = []byte(">") var bytesGreaterthan = []byte(">")
@ -33,6 +34,8 @@ var imageOpen2 = []byte("\"><img src='")
var imageClose = []byte("' class='postImage' /></a>") var imageClose = []byte("' class='postImage' /></a>")
var attachOpen = []byte("<a download class='attach' href=\"") var attachOpen = []byte("<a download class='attach' href=\"")
var attachClose = []byte("\">Attachment</a>") var attachClose = []byte("\">Attachment</a>")
var sidParam = []byte("?sid=")
var stypeParam = []byte("&amp;stype=")
var urlPattern = `(?s)([ {1}])((http|https|ftp|mailto)*)(:{??)\/\/([\.a-zA-Z\/]+)([ {1}])` var urlPattern = `(?s)([ {1}])((http|https|ftp|mailto)*)(:{??)\/\/([\.a-zA-Z\/]+)([ {1}])`
var urlReg *regexp.Regexp var urlReg *regexp.Regexp
@ -598,7 +601,8 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/)
} }
//fmt.Println("p2") //fmt.Println("p2")
var addImage = func(url string) { addImage := func(url string) {
sb.Grow(len(imageOpen) + len(url) + len(url) + len(imageOpen2) + len(imageClose))
sb.Write(imageOpen) sb.Write(imageOpen)
sb.WriteString(url) sb.WriteString(url)
sb.Write(imageOpen2) sb.Write(imageOpen2)
@ -609,6 +613,7 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/)
} }
// TODO: Reduce the amount of code duplication // TODO: Reduce the amount of code duplication
// TODO: Avoid allocating a string for media.Type?
if media.Type == "attach" { if media.Type == "attach" {
addImage(media.URL + "?sid=" + strconv.Itoa(sectionID) + "&amp;stype=" + sectionType) addImage(media.URL + "?sid=" + strconv.Itoa(sectionID) + "&amp;stype=" + sectionType)
continue continue
@ -617,7 +622,11 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/)
continue continue
} else if media.Type == "aother" { } else if media.Type == "aother" {
sb.Write(attachOpen) sb.Write(attachOpen)
sb.WriteString(media.URL + "?sid=" + strconv.Itoa(sectionID) + "&amp;stype=" + sectionType) sb.WriteString(media.URL)
sb.Write(sidParam)
sb.WriteString(strconv.Itoa(sectionID))
sb.Write(stypeParam)
sb.WriteString(sectionType)
sb.Write(attachClose) sb.Write(attachClose)
i += urlLen i += urlLen
lastItem = i lastItem = i
@ -634,7 +643,13 @@ func ParseMessage(msg string, sectionID int, sectionType string /*, user User*/)
} }
//fmt.Println("p3") //fmt.Println("p3")
// TODO: Add support for rel="ugc"
sb.Grow(len(URLOpen) + (len(msg[i : i+urlLen]) * 2) + len(URLOpen2) + len(URLClose))
if media.Trusted {
sb.Write(URLOpen) sb.Write(URLOpen)
} else {
sb.Write(URLOpenUser)
}
sb.WriteString(msg[i : i+urlLen]) sb.WriteString(msg[i : i+urlLen])
sb.Write(URLOpen2) sb.Write(URLOpen2)
sb.WriteString(msg[i : i+urlLen]) sb.WriteString(msg[i : i+urlLen])
@ -828,6 +843,8 @@ type MediaEmbed struct {
Type string //image Type string //image
URL string URL string
Body string Body string
Trusted bool // samesite urls
} }
// TODO: Write a test for this // TODO: Write a test for this
@ -846,7 +863,7 @@ func parseMediaString(data string) (media MediaEmbed, ok bool) {
query := url.Query() query := url.Query()
// TODO: Treat 127.0.0.1 and [::1] as localhost too // TODO: Treat 127.0.0.1 and [::1] as localhost too
var samesite = hostname == "localhost" || hostname == Site.URL samesite := hostname == "localhost" || hostname == Site.URL
if samesite { if samesite {
hostname = strings.Split(Site.URL, ":")[0] hostname = strings.Split(Site.URL, ":")[0]
// ?- Test this as I'm not sure it'll do what it should. If someone's running SSL on port 80 or non-SSL on port 443 then... Well... They're in far worse trouble than this... // ?- Test this as I'm not sure it'll do what it should. If someone's running SSL on port 80 or non-SSL on port 443 then... Well... They're in far worse trouble than this...
@ -858,6 +875,7 @@ func parseMediaString(data string) (media MediaEmbed, ok bool) {
if scheme == "" { if scheme == "" {
scheme = "http" scheme = "http"
} }
media.Trusted = samesite
path := url.EscapedPath() path := url.EscapedPath()
pathFrags := strings.Split(path, "/") pathFrags := strings.Split(path, "/")
@ -869,12 +887,12 @@ func parseMediaString(data string) (media MediaEmbed, ok bool) {
sport = ":" + port sport = ":" + port
} }
media.URL = scheme + "://" + hostname + sport + path media.URL = scheme + "://" + hostname + sport + path
var extarr = strings.Split(path, ".") extarr := strings.Split(path, ".")
if len(extarr) == 0 { if len(extarr) == 0 {
// TODO: Write a unit test for this // TODO: Write a unit test for this
return media, false return media, false
} }
var ext = extarr[len(extarr)-1] ext := extarr[len(extarr)-1]
if ImageFileExts.Contains(ext) { if ImageFileExts.Contains(ext) {
media.Type = "attach" media.Type = "attach"
} else { } else {