diff --git a/img/loader/http.go b/img/loader/http.go index 511017d..ded6d8d 100644 --- a/img/loader/http.go +++ b/img/loader/http.go @@ -66,6 +66,7 @@ func (r *Http) Load(url string, ctx context.Context) (*img.Image, error) { } contentType := resp.Header.Get("Content-Type") + contentEncoding := resp.Header.Get("Content-Encoding") result, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -73,8 +74,9 @@ func (r *Http) Load(url string, ctx context.Context) (*img.Image, error) { } return &img.Image{ - Id: url, - Data: result, - MimeType: contentType, + Id: url, + Data: result, + MimeType: contentType, + ContentEncoding: contentEncoding, }, nil } diff --git a/img/service.go b/img/service.go index 7c175dd..0a86dab 100644 --- a/img/service.go +++ b/img/service.go @@ -193,7 +193,6 @@ func (r *Service) AsIs(resp http.ResponseWriter, req *http.Request) { Log.Printf("Requested image %s as is\n", imgUrl) var proxyHeaders = make(http.Header) - accept := req.Header.Get("Accept") if len(accept) > 0 { proxyHeaders.Add("Accept", accept) @@ -202,6 +201,7 @@ func (r *Service) AsIs(resp http.ResponseWriter, req *http.Request) { if len(acceptEncoding) > 0 { proxyHeaders.Add("Accept-Encoding", acceptEncoding) } + result, err := r.Loader.Load(imgUrl, NewContextWithHeaders(req.Context(), &proxyHeaders)) if err != nil { @@ -209,10 +209,6 @@ func (r *Service) AsIs(resp http.ResponseWriter, req *http.Request) { return } - if len(result.MimeType) > 0 { - resp.Header().Add("Content-Type", result.MimeType) - } - r.execOp(&Command{ Config: &TransformationConfig{ Src: &Image{ @@ -249,11 +245,15 @@ func (r *Service) getQueue() *Queue { // Adds Content-Length and Cache-Control headers func addHeaders(resp http.ResponseWriter, image *Image) { + headers := resp.Header() if len(image.MimeType) != 0 { - resp.Header().Add("Content-Type", image.MimeType) + headers.Add("Content-Type", image.MimeType) + } + if len(image.ContentEncoding) != 0 { + headers.Add("Content-Encoding", image.ContentEncoding) } - resp.Header().Add("Content-Length", strconv.Itoa(len(image.Data))) - resp.Header().Add("Cache-Control", fmt.Sprintf("public, max-age=%d", CacheTTL)) + headers.Add("Content-Length", strconv.Itoa(len(image.Data))) + headers.Add("Cache-Control", fmt.Sprintf("public, max-age=%d", CacheTTL)) } func getQueryParam(url *url.URL, name string) (string, bool) { diff --git a/img/service_test.go b/img/service_test.go index afba6c4..b7d6b73 100644 --- a/img/service_test.go +++ b/img/service_test.go @@ -143,9 +143,10 @@ func (l *loaderMock) Load(url string, ctx context.Context) (*img.Image, error) { case "http://site.com/img.svg": if acceptEncoding, ok := img.HeaderFromContext(ctx); ok && acceptEncoding.Get("Accept-Encoding") == "gzip" { return &img.Image{ - Data: []byte(ImgSrc), - MimeType: "svg/gzip", - Id: url, + Data: []byte(ImgSrc), + MimeType: "svg/xml", + ContentEncoding: "gzip", + Id: url, }, nil } } @@ -606,7 +607,8 @@ func TestService_AsIs(t *testing.T) { test.Error(t, test.Equal("public, max-age=86400", w.Header().Get("Cache-Control"), "Cache-Control header"), test.Equal("3", w.Header().Get("Content-Length"), "Content-Length header"), - test.Equal("svg/gzip", w.Header().Get("Content-Type"), "Content-Type header"), + test.Equal("svg/xml", w.Header().Get("Content-Type"), "Content-Type header"), + test.Equal("gzip", w.Header().Get("Content-Encoding"), "Content-Encoding header"), test.Equal("", w.Header().Get("Vary"), "No Vary header"), ) }, @@ -695,6 +697,16 @@ func FuzzService_ResizeUrl(f *testing.F) { }) } +func TestHeaderFromContext(t *testing.T) { + header, ok := img.HeaderFromContext(context.Background()) + if header != nil { + t.Errorf("expected nil header") + } + if ok { + t.Errorf("expected ok to be false") + } +} + func createService(t *testing.T) *img.Service { img.CacheTTL = 86400 s, err := img.NewService(&loaderMock{}, &resizerMock{}, 1) diff --git a/img/types.go b/img/types.go index da376ff..8c7e240 100644 --- a/img/types.go +++ b/img/types.go @@ -6,6 +6,9 @@ type Image struct { Id string Data []byte MimeType string + // Content encoding is a literally Content-Encoding header from the response + // because of the #32 (https://github.com/Pixboost/transformimgs/issues/32) + ContentEncoding string } // Info holds basic information about an image. diff --git a/run.sh b/run.sh index 0763c2b..2053400 100644 --- a/run.sh +++ b/run.sh @@ -2,11 +2,4 @@ set -e -# Fetching and installing all dependencies -# and running server on port 8080. Using this -# script to run the application inside docker -# container. - -cd cmd/ -echo 'Running Application' -go run main.go -imConvert=/usr/local/bin/convert -imIdentify=/usr/local/bin/identify $@ \ No newline at end of file +go run cmd/main.go -imConvert=/usr/local/bin/convert -imIdentify=/usr/local/bin/identify $@ diff --git a/test.sh b/test.sh index bf88069..4e498be 100755 --- a/test.sh +++ b/test.sh @@ -10,6 +10,7 @@ go test $(go list -buildvcs=false ./... | grep -v '/vendor/') -v -bench . -bench go test -fuzz=FuzzCalculateTargetSizeForResize -fuzztime 30s ./img/processor/internal/ go test -fuzz=FuzzCalculateTargetSizeForFit -fuzztime 30s ./img/processor/internal/ go test -fuzz=FuzzHttp_LoadImg -fuzztime 30s ./img/loader/ +go test -fuzz=FuzzHttp_LoadCustomGlobalHeaders -fuzztime 30s ./img/loader/ go test -fuzz=FuzzService_ResizeUrl -fuzztime 30s ./img/ echo 'Running go vet'