Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BackingImage does not download URL correctly in some situation #182

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions pkg/sync/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -39,7 +40,7 @@ func (h *HTTPHandler) GetSizeFromURL(url string) (size int64, err error) {
return 0, err
}

client := http.Client{}
client := NewDownloadHttpClient()
resp, err := client.Do(rr)
if err != nil {
return 0, err
Expand Down Expand Up @@ -72,7 +73,7 @@ func (h *HTTPHandler) DownloadFromURL(ctx context.Context, url, filePath string,
return 0, err
}

client := http.Client{}
client := NewDownloadHttpClient()
resp, err := client.Do(rr)
if err != nil {
return 0, err
Expand Down Expand Up @@ -176,6 +177,27 @@ func IdleTimeoutCopy(ctx context.Context, cancel context.CancelFunc, src io.Read
return copied, err
}

func removeReferer(req *http.Request) {
for k := range req.Header {
if strings.ToLower(k) == "referer" {
delete(req.Header, k)
}
}
}

func NewDownloadHttpClient() http.Client {
return http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// Remove the "Referer" header to enable downloads of files
// that are delivered via CDN and therefore may be redirected
// several times. This is the same behaviour of curl or wget
// in their default configuration.
removeReferer(req)
return nil
},
}
}

const (
MockFileSize = 4096
)
Expand Down
20 changes: 20 additions & 0 deletions pkg/sync/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sync

import (
. "gopkg.in/check.v1"
"net/http"
)

type TestSuite struct{}

var _ = Suite(&TestSuite{})

func (s *TestSuite) TestRemoveReferer(c *C) {
req, err := http.NewRequest("HEAD", "https://foo.bar", nil)
c.Assert(err, IsNil)
req.Header.Set("Referer", "https://foo.bar")
req.Header.Set("Foo", "foo")
removeReferer(req)
c.Assert(req.Referer(), Equals, "")
c.Assert(req.Header, HasLen, 1)
}