From 45aaf27bcb676e245371bf46b710ab498cdcfcb5 Mon Sep 17 00:00:00 2001 From: knotteye Date: Sun, 30 May 2021 21:58:16 -0500 Subject: Better accept header parsing --- accept.go | 19 +++++++++++++++++++ main.go | 7 +++---- 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 accept.go diff --git a/accept.go b/accept.go new file mode 100644 index 0000000..1263ab8 --- /dev/null +++ b/accept.go @@ -0,0 +1,19 @@ +package main + +import "strings" +import "regexp" + +// False positive for application/ld+ld, application/activity+ld, application/json+json +var activityRegexp = regexp.MustCompile("application\\/(ld|json|activity)((\\+(ld|json))|$)") + +func acceptActivity(header string) bool { + accept := false + if strings.Contains(header, ";") { + split := strings.Split(header, ";") + accept = accept || activityRegexp.MatchString(split[0]) + accept = accept || strings.Contains(split[len(split)-1], "profile=\"https://www.w3.org/ns/activitystreams\"") + } else { + accept = accept || activityRegexp.MatchString(header) + } + return accept +} diff --git a/main.go b/main.go index bd88c41..35b9fe3 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,6 @@ var SiteEmailPassword = GetConfigValue("emailpass") var SiteEmailServer = GetConfigValue("emailserver") //mail.fchan.xyz var SiteEmailPort = GetConfigValue("emailport") //587 -var ldjson = "application/ld+json" var activitystreams = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" func main() { @@ -137,7 +136,7 @@ func main() { } if mainActor { - if accept == activitystreams || accept == ldjson { + if acceptActivity(accept) { GetActorInfo(w, db, Domain) return } @@ -180,7 +179,7 @@ func main() { } if actorMain || actorMainPage { - if accept == activitystreams || accept == ldjson { + if acceptActivity(accept) { GetActorInfo(w, db, actor.Id) return } @@ -267,7 +266,7 @@ func main() { //catch all if actorPost { - if accept == activitystreams || accept == ldjson { + if acceptActivity(accept) { GetActorPost(w, db, path) return } -- cgit v1.2.3