From 6f292443eafb2e39f9252175b61faa6b38105a7c Mon Sep 17 00:00:00 2001 From: Joe Cai Date: Fri, 24 Jun 2022 20:50:08 +1000 Subject: [PATCH] Respond with 404 when the source image can not be found in OpenStack Swift (#903) --- CHANGELOG.md | 1 + transport/swift/swift.go | 19 +++++++++++++++++-- transport/swift/swift_test.go | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03ca54be..3484b52b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ### Fix - Fix trimming of CMYK images. +- Respond with 404 when the source image can not be found in OpenStack Object Storage. ## [3.6.0] - 2022-06-13 ### Add diff --git a/transport/swift/swift.go b/transport/swift/swift.go index 01547028..9e700f4f 100644 --- a/transport/swift/swift.go +++ b/transport/swift/swift.go @@ -2,7 +2,9 @@ package swift import ( "context" + "errors" "fmt" + "io" "net/http" "strings" "time" @@ -46,12 +48,25 @@ func (t transport) RoundTrip(req *http.Request) (resp *http.Response, err error) object, objectHeaders, err := t.con.ObjectOpen(req.Context(), container, objectName, false, make(swift.Headers)) + header := make(http.Header) + if err != nil { + if errors.Is(err, swift.ObjectNotFound) || errors.Is(err, swift.ContainerNotFound) { + return &http.Response{ + StatusCode: http.StatusNotFound, + Proto: "HTTP/1.0", + ProtoMajor: 1, + ProtoMinor: 0, + Header: header, + Body: io.NopCloser(strings.NewReader(err.Error())), + Close: false, + Request: req, + }, nil + } + return nil, fmt.Errorf("error opening object: %v", err) } - header := make(http.Header) - if config.ETagEnabled { if etag, ok := objectHeaders["Etag"]; ok { header.Set("ETag", etag) diff --git a/transport/swift/swift_test.go b/transport/swift/swift_test.go index 1efd7d0a..182774f0 100644 --- a/transport/swift/swift_test.go +++ b/transport/swift/swift_test.go @@ -90,6 +90,24 @@ func (s *SwiftTestSuite) TestRoundTripWithETagDisabledReturns200() { require.Equal(s.T(), 200, response.StatusCode) } +func (s *SwiftTestSuite) TestRoundTripReturns404WhenObjectNotFound() { + config.ETagEnabled = true + request, _ := http.NewRequest("GET", "swift://test/foo/not-here.png", nil) + + response, err := s.transport.RoundTrip(request) + require.Nil(s.T(), err) + require.Equal(s.T(), 404, response.StatusCode) +} + +func (s *SwiftTestSuite) TestRoundTripReturns404WhenContainerNotFound() { + config.ETagEnabled = true + request, _ := http.NewRequest("GET", "swift://invalid/foo/test.png", nil) + + response, err := s.transport.RoundTrip(request) + require.Nil(s.T(), err) + require.Equal(s.T(), 404, response.StatusCode) +} + func (s *SwiftTestSuite) TestRoundTripWithETagEnabled() { config.ETagEnabled = true request, _ := http.NewRequest("GET", "swift://test/foo/test.png", nil)