diff options
| author | Sebastiaan van Stijn <github@gone.nl> | 2022-04-05 11:31:27 +0200 |
|---|---|---|
| committer | Sebastiaan van Stijn <github@gone.nl> | 2022-04-11 21:37:00 +0200 |
| commit | ef490cae4520ff1c8aa765ec9107885417a2583b (patch) | |
| tree | 9e36b6d0f9608095b44b9c89f9d18ea7547f1490 /api/server | |
| parent | c6872980bb0b32d2f30f70c6761501a9cd31a94f (diff) | |
| download | docker-ef490cae4520ff1c8aa765ec9107885417a2583b.tar.gz | |
api/server/httputils: matchesContentType(): return error instead of logging
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Diffstat (limited to 'api/server')
| -rw-r--r-- | api/server/httputils/httputils.go | 21 | ||||
| -rw-r--r-- | api/server/httputils/httputils_test.go | 22 |
2 files changed, 25 insertions, 18 deletions
diff --git a/api/server/httputils/httputils.go b/api/server/httputils/httputils.go index da407a80ef..1d671693f6 100644 --- a/api/server/httputils/httputils.go +++ b/api/server/httputils/httputils.go @@ -9,7 +9,6 @@ import ( "github.com/docker/docker/errdefs" "github.com/pkg/errors" - "github.com/sirupsen/logrus" ) // APIVersionKey is the client's requested API version. @@ -49,17 +48,12 @@ func CheckForJSON(r *http.Request) error { ct := r.Header.Get("Content-Type") // No Content-Type header is ok as long as there's no Body - if ct == "" { - if r.Body == nil || r.ContentLength == 0 { - return nil - } + if ct == "" && (r.Body == nil || r.ContentLength == 0) { + return nil } // Otherwise it better be json - if matchesContentType(ct, "application/json") { - return nil - } - return errdefs.InvalidParameter(errors.Errorf("Content-Type specified (%s) must be 'application/json'", ct)) + return matchesContentType(ct, "application/json") } // ParseForm ensures the request form is parsed even with invalid content types. @@ -89,10 +83,13 @@ func VersionFromContext(ctx context.Context) string { } // matchesContentType validates the content type against the expected one -func matchesContentType(contentType, expectedType string) bool { +func matchesContentType(contentType, expectedType string) error { mimetype, _, err := mime.ParseMediaType(contentType) if err != nil { - logrus.Errorf("Error parsing media type: %s error: %v", contentType, err) + return errdefs.InvalidParameter(errors.Wrapf(err, "malformed Content-Type header (%s)", contentType)) + } + if mimetype != expectedType { + return errdefs.InvalidParameter(errors.Errorf("unsupported Content-Type header (%s): must be '%s'", contentType, expectedType)) } - return err == nil && mimetype == expectedType + return nil } diff --git a/api/server/httputils/httputils_test.go b/api/server/httputils/httputils_test.go index 97f83188d1..81602c0836 100644 --- a/api/server/httputils/httputils_test.go +++ b/api/server/httputils/httputils_test.go @@ -4,15 +4,25 @@ import "testing" // matchesContentType func TestJsonContentType(t *testing.T) { - if !matchesContentType("application/json", "application/json") { - t.Fail() + err := matchesContentType("application/json", "application/json") + if err != nil { + t.Error(err) } - if !matchesContentType("application/json; charset=utf-8", "application/json") { - t.Fail() + err = matchesContentType("application/json; charset=utf-8", "application/json") + if err != nil { + t.Error(err) } - if matchesContentType("dockerapplication/json", "application/json") { - t.Fail() + expected := "unsupported Content-Type header (dockerapplication/json): must be 'application/json'" + err = matchesContentType("dockerapplication/json", "application/json") + if err == nil || err.Error() != expected { + t.Errorf(`expected "%s", got "%v"`, expected, err) + } + + expected = "malformed Content-Type header (foo;;;bar): mime: invalid media parameter" + err = matchesContentType("foo;;;bar", "application/json") + if err == nil || err.Error() != expected { + t.Errorf(`expected "%s", got "%v"`, expected, err) } } |
