atom/atom.go

60 lines
1.3 KiB
Go
Raw Normal View History

2024-10-13 17:19:40 +02:00
package atomfeed
import (
"mime"
2024-10-16 16:14:34 +02:00
"net/url"
"regexp"
"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
func isValidURL(uri URI) bool {
_, err := url.ParseRequestURI(string(uri))
2024-10-16 16:14:34 +02:00
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 isCorrectlyEscaped(text string) bool {
relevantEntities := []string{"&amp;", "&lt;", "&gt;", "&quot;", "&apos;"}
for _, entity := range relevantEntities {
if strings.Contains(text, entity) {
return false
}
}
return true
}
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 {
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")
}