Skip to content

Commit

Permalink
fix: content type parsing in httpx (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl authored Feb 24, 2025
1 parent 04d5c5b commit 1e9352e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.61.0
version: v1.64.5
args: --timeout 5m
- name: Install cockroach DB
run: |
Expand Down
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ linters:
- govet
disable-all: true

run:
skip-files:
issues:
exclude-files:
- ".+_test.go"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ licenses: .bin/licenses node_modules # checks open-source licenses
GOBIN=$(shell pwd)/.bin go install golang.org/x/tools/cmd/goimports@latest

.bin/golangci-lint: Makefile
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b .bin v1.61.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b .bin v1.64.5

.bin/licenses: Makefile
curl https://raw.githubusercontent.com/ory/ci/master/licenses/install | sh
Expand Down
19 changes: 8 additions & 11 deletions httpx/content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ package httpx
import (
"mime"
"net/http"
"slices"
"strings"

"github.com/ory/x/stringslice"
)

// HasContentType determines whether the request `content-type` includes a
Expand All @@ -18,17 +17,15 @@ import (
func HasContentType(r *http.Request, mimetypes ...string) bool {
contentType := r.Header.Get("Content-Type")
if contentType == "" {
return stringslice.Has(mimetypes, "application/octet-stream")
return slices.Contains(mimetypes, "application/octet-stream")
}

for _, v := range strings.Split(contentType, ",") {
t, _, err := mime.ParseMediaType(strings.TrimSpace(v))
if err != nil {
break
}
if stringslice.Has(mimetypes, t) {
return true
}
mediaType, _, err := mime.ParseMediaType(strings.TrimSpace(contentType))
if err != nil {
return false
}
if slices.Contains(mimetypes, mediaType) {
return true
}
return false
}
6 changes: 4 additions & 2 deletions httpx/content_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ func TestHasContentType(t *testing.T) {
assert.True(t, HasContentType(&http.Request{Header: map[string][]string{}}, "application/octet-stream"))
assert.False(t, HasContentType(&http.Request{Header: map[string][]string{}}, "not-application/octet-stream"))
assert.True(t, HasContentType(&http.Request{Header: map[string][]string{"Content-Type": {"application/octet-stream"}}}, "application/octet-stream"))
assert.True(t, HasContentType(&http.Request{Header: map[string][]string{"Content-Type": {"application/octet-stream, not-application/application"}}}, "not-application/application"))
assert.True(t, HasContentType(&http.Request{Header: map[string][]string{"Content-Type": {"application/octet-stream,not-application/application"}}}, "not-application/application"))

// Invalid conent types
assert.False(t, HasContentType(&http.Request{Header: map[string][]string{"Content-Type": {"application/octet-stream, not-application/application"}}}, "not-application/application"))
assert.False(t, HasContentType(&http.Request{Header: map[string][]string{"Content-Type": {"application/octet-stream,not-application/application"}}}, "not-application/application"))
assert.False(t, HasContentType(&http.Request{Header: map[string][]string{"Content-Type": {"application/octet-stream, application/not-application"}}}, "not-application/not-octet-stream"))
assert.False(t, HasContentType(&http.Request{Header: map[string][]string{"Content-Type": {"a"}}}, "not-application/not-octet-stream"))
}

0 comments on commit 1e9352e

Please sign in to comment.