From 61a97e94db6590c6a05d97050f4931584001cf7c Mon Sep 17 00:00:00 2001 From: Ben McClelland Date: Thu, 1 Aug 2024 08:32:31 -0700 Subject: [PATCH] fix: set default content type to binary/octet-stream AWS uses binary/octet-stream for the default content type if the client doesn't specify the content type. Change the default for the gateway to match this behavior. Fixes #697 --- s3api/controllers/base.go | 24 +++++++++++++++++------- tests/integration/tests.go | 8 ++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/s3api/controllers/base.go b/s3api/controllers/base.go index 184e0b82..bae057d1 100644 --- a/s3api/controllers/base.go +++ b/s3api/controllers/base.go @@ -50,7 +50,8 @@ type S3ApiController struct { } const ( - iso8601Format = "20060102T150405Z" + iso8601Format = "20060102T150405Z" + defaultContentType = "binary/octet-stream" ) func New(be backend.Backend, iam auth.IAMService, logger s3log.AuditLogger, evs s3event.S3EventSender, mm *metrics.Manager, debug bool, readonly bool) S3ApiController { @@ -419,10 +420,15 @@ func (c S3ApiController) GetActions(ctx *fiber.Ctx) error { lastmod = res.LastModified.Format(timefmt) } + contentType := getstring(res.ContentType) + if contentType == "" { + contentType = defaultContentType + } + utils.SetResponseHeaders(ctx, []utils.CustomHeader{ { Key: "Content-Type", - Value: getstring(res.ContentType), + Value: contentType, }, { Key: "Content-Encoding", @@ -2693,12 +2699,16 @@ func (c S3ApiController) HeadObject(ctx *fiber.Ctx) error { Value: getstring(res.ContentEncoding), }) } - if res.ContentType != nil { - headers = append(headers, utils.CustomHeader{ - Key: "Content-Type", - Value: getstring(res.ContentType), - }) + + contentType := getstring(res.ContentType) + if contentType == "" { + contentType = defaultContentType } + headers = append(headers, utils.CustomHeader{ + Key: "Content-Type", + Value: contentType, + }) + utils.SetResponseHeaders(ctx, headers) return SendResponse(ctx, nil, diff --git a/tests/integration/tests.go b/tests/integration/tests.go index 611f9061..ccf1eff4 100644 --- a/tests/integration/tests.go +++ b/tests/integration/tests.go @@ -2954,6 +2954,8 @@ func HeadObject_mp_success(s *S3Conf) error { }) } +const defaultContentType = "binary/octet-stream" + func HeadObject_success(s *S3Conf) error { testName := "HeadObject_success" return actionHandler(s, testName, func(s3client *s3.Client, bucket string) error { @@ -2992,6 +2994,9 @@ func HeadObject_success(s *S3Conf) error { if contentLength != dataLen { return fmt.Errorf("expected data length %v, instead got %v", dataLen, contentLength) } + if *out.ContentType != defaultContentType { + return fmt.Errorf("expected content type %v, instead got %v", defaultContentType, *out.ContentType) + } return nil }) @@ -3376,6 +3381,9 @@ func GetObject_success(s *S3Conf) error { if *out.ContentLength != dataLength { return fmt.Errorf("expected content-length %v, instead got %v", dataLength, out.ContentLength) } + if *out.ContentType != defaultContentType { + return fmt.Errorf("expected content type %v, instead got %v", defaultContentType, *out.ContentType) + } bdy, err := io.ReadAll(out.Body) if err != nil {