add basic support for ipns uris

This commit is contained in:
Azareal 2021-04-05 17:56:38 +10:00
parent ba1d68eea7
commit 7b3cf6877f
2 changed files with 10 additions and 6 deletions

View File

@ -648,6 +648,8 @@ func ParseMessage2(msg string, sectionID int, sectionType string, settings *Pars
// Do nothing // Do nothing
} else if msg[i] == 'i' && fch == 'p' && msg[i+2] == 'f' && msg[i+3] == 's' { } else if msg[i] == 'i' && fch == 'p' && msg[i+2] == 'f' && msg[i+3] == 's' {
// Do nothing // Do nothing
} else if msg[i] == 'i' && fch == 'p' && msg[i+2] == 'n' && msg[i+3] == 's' {
// Do nothing
} else if fch == '/' && msg[i] == '/' { } else if fch == '/' && msg[i] == '/' {
// Do nothing // Do nothing
} else { } else {
@ -867,14 +869,14 @@ func ParseMessage2(msg string, sectionID int, sectionType string, settings *Pars
} }
// 6, 7, 8, 6, 2, 7 // 6, 7, 8, 6, 2, 7
// ftp://, http://, https://, git://, ipfs://, //, mailto: (not a URL, just here for length comparison purposes) // ftp://, http://, https://, git://, ipfs://, ipns://, //, mailto: (not a URL, just here for length comparison purposes)
// TODO: Write a test for this // TODO: Write a test for this
func validateURLString(d string) bool { func validateURLString(d string) bool {
i := 0 i := 0
if len(d) >= 6 { if len(d) >= 6 {
if d[0:6] == "ftp://" || d[0:6] == "git://" { if d[0:6] == "ftp://" || d[0:6] == "git://" {
i = 6 i = 6
} else if len(d) >= 7 && (d[0:7] == "http://" || d[0:7] == "ipfs://") { } else if len(d) >= 7 && (d[0:7] == "http://" || d[0:7] == "ipfs://" || d[0:7] == "ipns://") {
i = 7 i = 7
} else if len(d) >= 8 && d[0:8] == "https://" { } else if len(d) >= 8 && d[0:8] == "https://" {
i = 8 i = 8
@ -928,7 +930,7 @@ func PartialURLString(d string) (url []byte) {
if len(d) >= 6 { if len(d) >= 6 {
if d[0:6] == "ftp://" || d[0:6] == "git://" { if d[0:6] == "ftp://" || d[0:6] == "git://" {
i = 6 i = 6
} else if len(d) >= 7 && (d[0:7] == "http://" || d[0:7] == "ipfs://") { } else if len(d) >= 7 && (d[0:7] == "http://" || d[0:7] == "ipfs://" || d[0:7] == "ipns://") {
i = 7 i = 7
} else if len(d) >= 8 && d[0:8] == "https://" { } else if len(d) >= 8 && d[0:8] == "https://" {
i = 8 i = 8
@ -957,7 +959,7 @@ func PartialURLStringLen(d string) (int, bool) {
//log.Print(string(d[0:5])) //log.Print(string(d[0:5]))
if d[0:6] == "ftp://" || d[0:6] == "git://" { if d[0:6] == "ftp://" || d[0:6] == "git://" {
i = 6 i = 6
} else if len(d) >= 7 && (d[0:7] == "http://" || d[0:7] == "ipfs://") { } else if len(d) >= 7 && (d[0:7] == "http://" || d[0:7] == "ipfs://" || d[0:7] == "ipns://") {
i = 7 i = 7
} else if len(d) >= 8 && d[0:8] == "https://" { } else if len(d) >= 8 && d[0:8] == "https://" {
i = 8 i = 8
@ -1002,7 +1004,7 @@ func PartialURLStringLen2(d string) int {
//log.Print(string(d[0:5])) //log.Print(string(d[0:5]))
if d[0:6] == "ftp://" || d[0:6] == "git://" { if d[0:6] == "ftp://" || d[0:6] == "git://" {
i = 6 i = 6
} else if len(d) >= 7 && (d[0:7] == "http://" || d[0:7] == "ipfs://") { } else if len(d) >= 7 && (d[0:7] == "http://" || d[0:7] == "ipfs://" || d[0:7] == "ipns://") {
i = 7 i = 7
} else if len(d) >= 8 && d[0:8] == "https://" { } else if len(d) >= 8 && d[0:8] == "https://" {
i = 8 i = 8
@ -1071,7 +1073,7 @@ func parseMediaString(data string, settings *ParseSettings) (media MediaEmbed, o
//fmt.Println("host:", host) //fmt.Println("host:", host)
//log.Print("Site.URL:",Site.URL) //log.Print("Site.URL:",Site.URL)
samesite := host == "localhost" || host == "127.0.0.1" || host == "::1" || host == Site.URL samesite := (host == "localhost" || host == "127.0.0.1" || host == "::1" || host == Site.URL) && scheme != "ipns"
if samesite { if samesite {
host = strings.Split(Site.URL, ":")[0] host = 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...

View File

@ -269,6 +269,7 @@ func TestParser(t *testing.T) {
local := func(u string) { local := func(u string) {
s := "//" + c.Site.URL s := "//" + c.Site.URL
fs := "http://" + c.Site.URL fs := "http://" + c.Site.URL
ipns := "ipns://" + c.Site.URL
if c.Config.SslSchema { if c.Config.SslSchema {
s = "https:" + s s = "https:" + s
fs = "https://" + c.Site.URL fs = "https://" + c.Site.URL
@ -284,6 +285,7 @@ func TestParser(t *testing.T) {
l.Add("http://"+u, "<a href='"+fs+"'>"+c.Site.URL+"</a>") l.Add("http://"+u, "<a href='"+fs+"'>"+c.Site.URL+"</a>")
l.Add("https://"+u, "<a href='"+fs+"'>"+c.Site.URL+"</a>") l.Add("https://"+u, "<a href='"+fs+"'>"+c.Site.URL+"</a>")
l.Add("ipfs://testthis", "<a rel='ugc'href='ipfs://testthis'>ipfs://testthis</a>") l.Add("ipfs://testthis", "<a rel='ugc'href='ipfs://testthis'>ipfs://testthis</a>")
l.Add(ipns, "<a rel='ugc'href='"+ipns+"'>"+c.Site.URL+"</a>")
l.Add("//"+u+"/attachs/sha256hash.webm?sid=1&stype=forums", "<video controls src=\""+fs+"/attachs/sha256hash.webm?sid=1&amp;stype=forums\"><a class='attach'href=\""+fs+"/attachs/sha256hash.webm?sid=1&amp;stype=forums\"download>Attachment</a></video>") l.Add("//"+u+"/attachs/sha256hash.webm?sid=1&stype=forums", "<video controls src=\""+fs+"/attachs/sha256hash.webm?sid=1&amp;stype=forums\"><a class='attach'href=\""+fs+"/attachs/sha256hash.webm?sid=1&amp;stype=forums\"download>Attachment</a></video>")
l.Add("//"+u+"/attachs/sha256hash.webm", "<video controls src=\""+fs+"/attachs/sha256hash.webm?sid=1&amp;stype=forums\"><a class='attach'href=\""+fs+"/attachs/sha256hash.webm?sid=1&amp;stype=forums\"download>Attachment</a></video>") l.Add("//"+u+"/attachs/sha256hash.webm", "<video controls src=\""+fs+"/attachs/sha256hash.webm?sid=1&amp;stype=forums\"><a class='attach'href=\""+fs+"/attachs/sha256hash.webm?sid=1&amp;stype=forums\"download>Attachment</a></video>")