1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-23 11:14:48 +02:00

Respond with 404 when the source image can not be found in OpenStack Swift (#903)

This commit is contained in:
Joe Cai 2022-06-24 20:50:08 +10:00 committed by GitHub
parent 7a5074187e
commit 6f292443ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 2 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)