From 7b3cf6877f2daf3bd1a5106284d9ef5f4d9eb6ff Mon Sep 17 00:00:00 2001 From: Azareal Date: Mon, 5 Apr 2021 17:56:38 +1000 Subject: [PATCH] add basic support for ipns uris --- common/parser.go | 14 ++++++++------ parser_test.go | 2 ++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/common/parser.go b/common/parser.go index 8d65580e..72bec0e6 100644 --- a/common/parser.go +++ b/common/parser.go @@ -648,6 +648,8 @@ func ParseMessage2(msg string, sectionID int, sectionType string, settings *Pars // Do nothing } else if msg[i] == 'i' && fch == 'p' && msg[i+2] == 'f' && msg[i+3] == 's' { // Do nothing + } else if msg[i] == 'i' && fch == 'p' && msg[i+2] == 'n' && msg[i+3] == 's' { + // Do nothing } else if fch == '/' && msg[i] == '/' { // Do nothing } else { @@ -867,14 +869,14 @@ func ParseMessage2(msg string, sectionID int, sectionType string, settings *Pars } // 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 func validateURLString(d string) bool { i := 0 if len(d) >= 6 { if d[0:6] == "ftp://" || d[0:6] == "git://" { 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 } else if len(d) >= 8 && d[0:8] == "https://" { i = 8 @@ -928,7 +930,7 @@ func PartialURLString(d string) (url []byte) { if len(d) >= 6 { if d[0:6] == "ftp://" || d[0:6] == "git://" { 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 } else if len(d) >= 8 && d[0:8] == "https://" { i = 8 @@ -957,7 +959,7 @@ func PartialURLStringLen(d string) (int, bool) { //log.Print(string(d[0:5])) if d[0:6] == "ftp://" || d[0:6] == "git://" { 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 } else if len(d) >= 8 && d[0:8] == "https://" { i = 8 @@ -1002,7 +1004,7 @@ func PartialURLStringLen2(d string) int { //log.Print(string(d[0:5])) if d[0:6] == "ftp://" || d[0:6] == "git://" { 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 } else if len(d) >= 8 && d[0:8] == "https://" { i = 8 @@ -1071,7 +1073,7 @@ func parseMediaString(data string, settings *ParseSettings) (media MediaEmbed, o //fmt.Println("host:", host) //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 { 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... diff --git a/parser_test.go b/parser_test.go index 4b7e885a..8102d3a1 100644 --- a/parser_test.go +++ b/parser_test.go @@ -269,6 +269,7 @@ func TestParser(t *testing.T) { local := func(u string) { s := "//" + c.Site.URL fs := "http://" + c.Site.URL + ipns := "ipns://" + c.Site.URL if c.Config.SslSchema { s = "https:" + s fs = "https://" + c.Site.URL @@ -284,6 +285,7 @@ func TestParser(t *testing.T) { l.Add("http://"+u, ""+c.Site.URL+"") l.Add("https://"+u, ""+c.Site.URL+"") l.Add("ipfs://testthis", "ipfs://testthis") + l.Add(ipns, ""+c.Site.URL+"") l.Add("//"+u+"/attachs/sha256hash.webm?sid=1&stype=forums", "") l.Add("//"+u+"/attachs/sha256hash.webm", "")