From 14696371e2ecbe708eb96f3d0ecafbd61cd59bb8 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Wed, 16 Oct 2024 16:14:34 +0200 Subject: [PATCH] Create and use isValidURL --- atom.go | 6 ++++++ outOfLineContent.go | 3 +-- person.go | 3 +-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/atom.go b/atom.go index 643acc0..e9af0ac 100644 --- a/atom.go +++ b/atom.go @@ -2,6 +2,7 @@ package atomfeed import ( "mime" + "net/url" "strings" ) @@ -12,6 +13,11 @@ type ( URI string ) +func isValidURL(testURL URI) bool { + _, err := url.ParseRequestURI(string(testURL)) + return err == nil +} + func isCompositeMediaType(mediaType string) bool { mediaType, _, err := mime.ParseMediaType(mediaType) if err != nil { diff --git a/outOfLineContent.go b/outOfLineContent.go index bd11531..1a87068 100644 --- a/outOfLineContent.go +++ b/outOfLineContent.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "mime" - "net/url" "reflect" ) @@ -23,7 +22,7 @@ func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, erro return nil, fmt.Errorf("content type %T incompatible with out of line content", content) } - if _, err := url.ParseRequestURI(content.(string)); err != nil { + if !isValidURL(content.(URI)) { return nil, errors.New("content not a valid uri") } diff --git a/person.go b/person.go index 1e8fe8c..7314a7e 100644 --- a/person.go +++ b/person.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "net/mail" - "net/url" ) type Person struct { @@ -30,7 +29,7 @@ func (p *Person) Check() error { } if p.URI != "" { - if _, err := url.ParseRequestURI(string(p.URI)); err != nil { + if !isValidURL(p.URI) { return fmt.Errorf("uri element of person %v not correctly formatted", p.Name) } }