Skip to content

Commit

Permalink
fix: set default content type to binary/octet-stream
Browse files Browse the repository at this point in the history
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
  • Loading branch information
benmcclelland committed Aug 2, 2024
1 parent 18a8813 commit 61a97e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
24 changes: 17 additions & 7 deletions s3api/controllers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 61a97e9

Please sign in to comment.