2024-10-13 17:19:40 +02:00
|
|
|
package atomfeed
|
|
|
|
|
2024-10-15 19:46:26 +02:00
|
|
|
import (
|
|
|
|
"mime"
|
2024-10-16 16:14:34 +02:00
|
|
|
"net/url"
|
2024-10-16 16:48:44 +02:00
|
|
|
"regexp"
|
2024-10-15 19:46:26 +02:00
|
|
|
"strings"
|
|
|
|
)
|
2024-10-15 19:40:13 +02:00
|
|
|
|
2024-10-13 17:19:40 +02:00
|
|
|
type (
|
|
|
|
EmailAddress string
|
|
|
|
LanguageTag string
|
|
|
|
MediaType string
|
|
|
|
URI string
|
|
|
|
)
|
2024-10-15 19:40:13 +02:00
|
|
|
|
2024-10-16 16:48:44 +02:00
|
|
|
func isValidURL(uri URI) bool {
|
|
|
|
_, err := url.ParseRequestURI(string(uri))
|
2024-10-16 16:14:34 +02:00
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2024-10-16 16:48:44 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-10-15 19:46:45 +02:00
|
|
|
func isCompositeMediaType(mediaType string) bool {
|
|
|
|
mediaType, _, err := mime.ParseMediaType(mediaType)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.HasPrefix(mediaType, "multipart/") || strings.HasPrefix(mediaType, "message/")
|
|
|
|
}
|
|
|
|
|
2024-10-15 19:40:13 +02:00
|
|
|
func isXMLMediaType(mediaType string) bool {
|
2024-10-15 19:46:26 +02:00
|
|
|
mediaType, _, err := mime.ParseMediaType(mediaType)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-10-15 19:40:13 +02:00
|
|
|
return strings.HasSuffix(mediaType, "/xml") || strings.HasSuffix(mediaType, "+xml")
|
|
|
|
}
|