Skip to content

Commit

Permalink
Improve media type detection for ActivityPub types
Browse files Browse the repository at this point in the history
  • Loading branch information
cjslep committed Aug 1, 2018
1 parent 1ed4785 commit b0c125a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 288 deletions.
241 changes: 0 additions & 241 deletions pub/headers.go

This file was deleted.

22 changes: 0 additions & 22 deletions pub/headers_test.go

This file was deleted.

44 changes: 19 additions & 25 deletions pub/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,18 @@ const (
digestDelimiter = "="
)

var alternatives = []string{
"application/activity+json",
"application/ld+json; profile=https://www.w3.org/ns/activitystreams",
var mediaTypes []string

func init() {
mediaTypes = []string{
"application/activity+json",
}
jsonLdType := "application/ld+json"
for _, semi := range []string{";", " ;", " ; ", "; "} {
for _, profile := range []string{"profile=https://www.w3.org/ns/activitystreams", "profile=\"https://www.w3.org/ns/activitystreams\""} {
mediaTypes = append(mediaTypes, fmt.Sprintf("%s%s%s", jsonLdType, semi, profile))
}
}
}

func trimAll(s []string) []string {
Expand All @@ -48,36 +57,21 @@ func trimAll(s []string) []string {
return r
}

func headerContainsOneOf(header string, acceptable []string) bool {
sanitizedHeaderValues := trimAll(strings.Split(header, ";"))
sanitizedHeaderMap := make(map[string]bool, len(sanitizedHeaderValues))
for _, s := range sanitizedHeaderValues {
sanitizedHeaderMap[s] = true
}
found := false
for _, v := range acceptable {
if found {
break
}
// Remove any number of whitespace after ;'s
sanitizedAcceptableValues := trimAll(strings.Split(v, ";"))
found = true
for _, v := range sanitizedAcceptableValues {
if has, ok := sanitizedHeaderMap[v]; !has || !ok {
found = false
break
}
func headerIsActivityPubMediaType(header string) bool {
for _, mediaType := range mediaTypes {
if strings.Contains(header, mediaType) {
return true
}
}
return found
return false
}

func isActivityPubPost(r *http.Request) bool {
return r.Method == "POST" && headerContainsOneOf(r.Header.Get(contentTypeHeader), append([]string{postContentTypeHeader}, alternatives...))
return r.Method == "POST" && headerIsActivityPubMediaType(r.Header.Get(contentTypeHeader))
}

func isActivityPubGet(r *http.Request) bool {
return r.Method == "GET" && headerContainsOneOf(r.Header.Get(acceptHeader), append([]string{getAcceptHeader}, alternatives...))
return r.Method == "GET" && headerIsActivityPubMediaType(r.Header.Get(acceptHeader))
}

// isPublic determines if a target is the Public collection as defined in the
Expand Down
Loading

0 comments on commit b0c125a

Please sign in to comment.