Skip to content

Commit

Permalink
fix(utils): recognize content types with whitespace (#181)
Browse files Browse the repository at this point in the history
The RFC for content-type header format allows optional whitespace before the
semicolon. Our regular expressions for recognizing various mime types do not
account for this, so valid mime types may not be recognized. This commit
adjusts the expression to close that gap.

Signed-off-by: Dustin Popp <dpopp07@gmail.com>
  • Loading branch information
dpopp07 committed Feb 28, 2023
1 parent 6974692 commit 09fe5cf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func init() {
}

const (
jsonMimePattern = "(?i)^application\\/((json)|(merge\\-patch\\+json)|(vnd\\..*\\+json))(;.*)?$"
jsonPatchMimePattern = "(?i)^application\\/json\\-patch\\+json(;.*)?$"
jsonMimePattern = "(?i)^application\\/((json)|(merge\\-patch\\+json)|(vnd\\..*\\+json))(\\s*;.*)?$"
jsonPatchMimePattern = "(?i)^application\\/json\\-patch\\+json(\\s*;.*)?$"
)

// IsNil checks if the specified object is nil or not.
Expand Down
2 changes: 2 additions & 0 deletions core/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestIsJSONMimeType(t *testing.T) {
assert.True(t, IsJSONMimeType("application/json"))
assert.True(t, IsJSONMimeType("APPlication/json"))
assert.True(t, IsJSONMimeType("application/json;blah"))
assert.True(t, IsJSONMimeType("application/json ; blah"))
assert.True(t, IsJSONMimeType("application/vnd.docker.distribution.manifest.v2+json"))
assert.True(t, IsJSONMimeType("application/vnd.anothervendor.custom.semantics+json"))
assert.True(t, IsJSONMimeType("application/vnd.yet.another.vendor.with.custom.semantics.blah.v3+json;charset=UTF8"))
Expand All @@ -43,6 +44,7 @@ func TestIsJSONPatchMimeType(t *testing.T) {
assert.True(t, IsJSONPatchMimeType("application/json-patch+json"))
assert.True(t, IsJSONPatchMimeType("APPlication/json-PATCH+json"))
assert.True(t, IsJSONPatchMimeType("application/json-patch+json;charset=UTF8"))
assert.True(t, IsJSONPatchMimeType("application/json-patch+json ; charset=UTF8"))

assert.False(t, IsJSONPatchMimeType("application/json"))
assert.False(t, IsJSONPatchMimeType("YOapplication/json-patch+jsonYO"))
Expand Down

0 comments on commit 09fe5cf

Please sign in to comment.