Create isValidURN and isValidURI and use isValidURI everywhere where it is needed

This commit is contained in:
2024-10-16 16:48:44 +02:00
parent 14696371e2
commit c200d5bf73
4 changed files with 20 additions and 4 deletions

14
atom.go
View File

@@ -3,6 +3,7 @@ package atomfeed
import (
"mime"
"net/url"
"regexp"
"strings"
)
@@ -13,11 +14,20 @@ type (
URI string
)
func isValidURL(testURL URI) bool {
_, err := url.ParseRequestURI(string(testURL))
func isValidURL(uri URI) bool {
_, err := url.ParseRequestURI(string(uri))
return err == nil
}
func isValidURN(uri URI) bool {
pattern := `\A(?i:urn:(?!urn:)(?<nid>[a-z0-9][a-z0-9-]{1,31}):(?<nss>(?:[-a-z0-9()+,.:=@;$_!*'&~\/]|%[0-9a-f]{2})+)(?:\?\+(?<rcomponent>.*?))?(?:\?=(?<qcomponent>.*?))?(?:#(?<fcomponent>.*?))?)\z`
return regexp.MustCompile(pattern).MatchString(string(uri))
}
func isValidURI(uri URI) bool {
return isValidURL(uri) || isValidURN(uri)
}
func isCompositeMediaType(mediaType string) bool {
mediaType, _, err := mime.ParseMediaType(mediaType)
if err != nil {