Skip to content

Commit

Permalink
Proxying Accept-Encoding and Accept headers for /asis
Browse files Browse the repository at this point in the history
  • Loading branch information
dooman87 committed Jan 31, 2025
1 parent 837ff1b commit 92921a1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
8 changes: 5 additions & 3 deletions img/loader/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ 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 {
return nil, err
}

return &img.Image{
Id: url,
Data: result,
MimeType: contentType,
Id: url,
Data: result,
MimeType: contentType,
ContentEncoding: contentEncoding,
}, nil
}
16 changes: 8 additions & 8 deletions img/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -202,17 +201,14 @@ 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 {
sendError(resp, err)
return
}

if len(result.MimeType) > 0 {
resp.Header().Add("Content-Type", result.MimeType)
}

r.execOp(&Command{
Config: &TransformationConfig{
Src: &Image{
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 16 additions & 4 deletions img/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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"),
)
},
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions img/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 1 addition & 8 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@
go run cmd/main.go -imConvert=/usr/local/bin/convert -imIdentify=/usr/local/bin/identify $@
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 92921a1

Please sign in to comment.