|
@@ -1,51 +1,36 @@
|
|
|
package imagedata
|
|
|
|
|
|
import (
|
|
|
- "compress/gzip"
|
|
|
"context"
|
|
|
- "io"
|
|
|
"net/http"
|
|
|
- "net/http/cookiejar"
|
|
|
- "regexp"
|
|
|
- "strconv"
|
|
|
- "strings"
|
|
|
- "time"
|
|
|
+ "slices"
|
|
|
|
|
|
"github.com/imgproxy/imgproxy/v3/config"
|
|
|
"github.com/imgproxy/imgproxy/v3/ierrors"
|
|
|
+ "github.com/imgproxy/imgproxy/v3/imagefetcher"
|
|
|
"github.com/imgproxy/imgproxy/v3/security"
|
|
|
-
|
|
|
- defaultTransport "github.com/imgproxy/imgproxy/v3/transport"
|
|
|
- azureTransport "github.com/imgproxy/imgproxy/v3/transport/azure"
|
|
|
- transportCommon "github.com/imgproxy/imgproxy/v3/transport/common"
|
|
|
- fsTransport "github.com/imgproxy/imgproxy/v3/transport/fs"
|
|
|
- gcsTransport "github.com/imgproxy/imgproxy/v3/transport/gcs"
|
|
|
- s3Transport "github.com/imgproxy/imgproxy/v3/transport/s3"
|
|
|
- swiftTransport "github.com/imgproxy/imgproxy/v3/transport/swift"
|
|
|
+ "github.com/imgproxy/imgproxy/v3/transport"
|
|
|
+ "go.withmatt.com/httpheaders"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- downloadClient *http.Client
|
|
|
-
|
|
|
- enabledSchemes = map[string]struct{}{
|
|
|
- "http": {},
|
|
|
- "https": {},
|
|
|
- }
|
|
|
-
|
|
|
- imageHeadersToStore = []string{
|
|
|
- "Cache-Control",
|
|
|
- "Expires",
|
|
|
- "ETag",
|
|
|
- "Last-Modified",
|
|
|
- }
|
|
|
-
|
|
|
- contentRangeRe = regexp.MustCompile(`^bytes ((\d+)-(\d+)|\*)/(\d+|\*)$`)
|
|
|
+ Fetcher *imagefetcher.Fetcher
|
|
|
|
|
|
// For tests
|
|
|
redirectAllRequestsTo string
|
|
|
-)
|
|
|
|
|
|
-const msgSourceImageIsUnreachable = "Source image is unreachable"
|
|
|
+ // keepResponseHeaders is a list of HTTP headers that should be preserved in the response
|
|
|
+ keepResponseHeaders = []string{
|
|
|
+ httpheaders.CacheControl,
|
|
|
+ httpheaders.Expires,
|
|
|
+ httpheaders.LastModified,
|
|
|
+ // NOTE:
|
|
|
+ // httpheaders.Etag == "Etag".
|
|
|
+ // Http header names are case-insensitive, but we rely on the case in most cases.
|
|
|
+ // We must migrate to http.Headers and the subsequent methods everywhere.
|
|
|
+ httpheaders.Etag,
|
|
|
+ }
|
|
|
+)
|
|
|
|
|
|
type DownloadOptions struct {
|
|
|
Header http.Header
|
|
@@ -53,224 +38,40 @@ type DownloadOptions struct {
|
|
|
}
|
|
|
|
|
|
func initDownloading() error {
|
|
|
- transport, err := defaultTransport.New(true)
|
|
|
+ ts, err := transport.NewTransport()
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- registerProtocol := func(scheme string, rt http.RoundTripper) {
|
|
|
- transport.RegisterProtocol(scheme, rt)
|
|
|
- enabledSchemes[scheme] = struct{}{}
|
|
|
- }
|
|
|
-
|
|
|
- if config.LocalFileSystemRoot != "" {
|
|
|
- registerProtocol("local", fsTransport.New())
|
|
|
- }
|
|
|
-
|
|
|
- if config.S3Enabled {
|
|
|
- if t, err := s3Transport.New(); err != nil {
|
|
|
- return err
|
|
|
- } else {
|
|
|
- registerProtocol("s3", t)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if config.GCSEnabled {
|
|
|
- if t, err := gcsTransport.New(); err != nil {
|
|
|
- return err
|
|
|
- } else {
|
|
|
- registerProtocol("gs", t)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if config.ABSEnabled {
|
|
|
- if t, err := azureTransport.New(); err != nil {
|
|
|
- return err
|
|
|
- } else {
|
|
|
- registerProtocol("abs", t)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if config.SwiftEnabled {
|
|
|
- if t, err := swiftTransport.New(); err != nil {
|
|
|
- return err
|
|
|
- } else {
|
|
|
- registerProtocol("swift", t)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- downloadClient = &http.Client{
|
|
|
- Transport: transport,
|
|
|
- CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
- redirects := len(via)
|
|
|
- if redirects >= config.MaxRedirects {
|
|
|
- return newImageTooManyRedirectsError(redirects)
|
|
|
- }
|
|
|
- return nil
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func headersToStore(res *http.Response) map[string]string {
|
|
|
- m := make(map[string]string)
|
|
|
-
|
|
|
- for _, h := range imageHeadersToStore {
|
|
|
- if val := res.Header.Get(h); len(val) != 0 {
|
|
|
- m[h] = val
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return m
|
|
|
-}
|
|
|
-
|
|
|
-func BuildImageRequest(ctx context.Context, imageURL string, header http.Header, jar http.CookieJar) (*http.Request, context.CancelFunc, error) {
|
|
|
- reqCtx, reqCancel := context.WithTimeout(ctx, time.Duration(config.DownloadTimeout)*time.Second)
|
|
|
-
|
|
|
- imageURL = transportCommon.EscapeURL(imageURL)
|
|
|
-
|
|
|
- req, err := http.NewRequestWithContext(reqCtx, "GET", imageURL, nil)
|
|
|
+ Fetcher, err = imagefetcher.NewFetcher(ts, config.MaxRedirects)
|
|
|
if err != nil {
|
|
|
- reqCancel()
|
|
|
- return nil, func() {}, newImageRequestError(err)
|
|
|
- }
|
|
|
-
|
|
|
- if _, ok := enabledSchemes[req.URL.Scheme]; !ok {
|
|
|
- reqCancel()
|
|
|
- return nil, func() {}, newImageRequstSchemeError(req.URL.Scheme)
|
|
|
- }
|
|
|
-
|
|
|
- if jar != nil {
|
|
|
- for _, cookie := range jar.Cookies(req.URL) {
|
|
|
- req.AddCookie(cookie)
|
|
|
- }
|
|
|
+ return ierrors.Wrap(err, 0, ierrors.WithPrefix("can't create image fetcher"))
|
|
|
}
|
|
|
|
|
|
- req.Header.Set("User-Agent", config.UserAgent)
|
|
|
-
|
|
|
- for k, v := range header {
|
|
|
- if len(v) > 0 {
|
|
|
- req.Header.Set(k, v[0])
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return req, reqCancel, nil
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
-func SendRequest(req *http.Request) (*http.Response, error) {
|
|
|
- var client *http.Client
|
|
|
- if req.URL.Scheme == "http" || req.URL.Scheme == "https" {
|
|
|
- clientCopy := *downloadClient
|
|
|
-
|
|
|
- jar, err := cookiejar.New(nil)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- clientCopy.Jar = jar
|
|
|
- client = &clientCopy
|
|
|
- } else {
|
|
|
- client = downloadClient
|
|
|
- }
|
|
|
-
|
|
|
- for {
|
|
|
- res, err := client.Do(req)
|
|
|
- if err == nil {
|
|
|
- return res, nil
|
|
|
- }
|
|
|
-
|
|
|
- if res != nil && res.Body != nil {
|
|
|
- res.Body.Close()
|
|
|
- }
|
|
|
-
|
|
|
- if strings.Contains(err.Error(), "client connection lost") {
|
|
|
- select {
|
|
|
- case <-req.Context().Done():
|
|
|
- return nil, err
|
|
|
- case <-time.After(100 * time.Microsecond):
|
|
|
- continue
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return nil, wrapError(err)
|
|
|
+func download(ctx context.Context, imageURL string, opts DownloadOptions, secopts security.Options) (*ImageData, error) {
|
|
|
+ // We use this for testing
|
|
|
+ if len(redirectAllRequestsTo) > 0 {
|
|
|
+ imageURL = redirectAllRequestsTo
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-func requestImage(ctx context.Context, imageURL string, opts DownloadOptions) (*http.Response, context.CancelFunc, error) {
|
|
|
- req, reqCancel, err := BuildImageRequest(ctx, imageURL, opts.Header, opts.CookieJar)
|
|
|
+ req, err := Fetcher.BuildRequest(ctx, imageURL, opts.Header, opts.CookieJar)
|
|
|
if err != nil {
|
|
|
- reqCancel()
|
|
|
- return nil, func() {}, err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
+ defer req.Cancel()
|
|
|
|
|
|
- res, err := SendRequest(req)
|
|
|
+ res, err := req.FetchImage()
|
|
|
if err != nil {
|
|
|
- reqCancel()
|
|
|
- return nil, func() {}, err
|
|
|
- }
|
|
|
-
|
|
|
- if res.StatusCode == http.StatusNotModified {
|
|
|
- res.Body.Close()
|
|
|
- reqCancel()
|
|
|
- return nil, func() {}, newNotModifiedError(headersToStore(res))
|
|
|
- }
|
|
|
-
|
|
|
- // If the source responds with 206, check if the response contains entire image.
|
|
|
- // If not, return an error.
|
|
|
- if res.StatusCode == http.StatusPartialContent {
|
|
|
- contentRange := res.Header.Get("Content-Range")
|
|
|
- rangeParts := contentRangeRe.FindStringSubmatch(contentRange)
|
|
|
- if len(rangeParts) == 0 {
|
|
|
- res.Body.Close()
|
|
|
- reqCancel()
|
|
|
- return nil, func() {}, newImagePartialResponseError("Partial response with invalid Content-Range header")
|
|
|
- }
|
|
|
-
|
|
|
- if rangeParts[1] == "*" || rangeParts[2] != "0" {
|
|
|
- res.Body.Close()
|
|
|
- reqCancel()
|
|
|
- return nil, func() {}, newImagePartialResponseError("Partial response with incomplete content")
|
|
|
- }
|
|
|
-
|
|
|
- contentLengthStr := rangeParts[4]
|
|
|
- if contentLengthStr == "*" {
|
|
|
- contentLengthStr = res.Header.Get("Content-Length")
|
|
|
- }
|
|
|
-
|
|
|
- contentLength, _ := strconv.Atoi(contentLengthStr)
|
|
|
- rangeEnd, _ := strconv.Atoi(rangeParts[3])
|
|
|
-
|
|
|
- if contentLength <= 0 || rangeEnd != contentLength-1 {
|
|
|
+ if res != nil {
|
|
|
res.Body.Close()
|
|
|
- reqCancel()
|
|
|
- return nil, func() {}, newImagePartialResponseError("Partial response with incomplete content")
|
|
|
}
|
|
|
- } else if res.StatusCode != http.StatusOK {
|
|
|
- var body string
|
|
|
-
|
|
|
- if strings.HasPrefix(res.Header.Get("Content-Type"), "text/") {
|
|
|
- bbody, _ := io.ReadAll(io.LimitReader(res.Body, 1024))
|
|
|
- body = string(bbody)
|
|
|
- }
|
|
|
-
|
|
|
- res.Body.Close()
|
|
|
- reqCancel()
|
|
|
-
|
|
|
- return nil, func() {}, newImageResponseStatusError(res.StatusCode, body)
|
|
|
- }
|
|
|
-
|
|
|
- return res, reqCancel, nil
|
|
|
-}
|
|
|
-
|
|
|
-func download(ctx context.Context, imageURL string, opts DownloadOptions, secopts security.Options) (*ImageData, error) {
|
|
|
- // We use this for testing
|
|
|
- if len(redirectAllRequestsTo) > 0 {
|
|
|
- imageURL = redirectAllRequestsTo
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- res, reqCancel, err := requestImage(ctx, imageURL, opts)
|
|
|
- defer reqCancel()
|
|
|
-
|
|
|
+ res, err = security.LimitResponseSize(res, secopts)
|
|
|
if res != nil {
|
|
|
defer res.Body.Close()
|
|
|
}
|
|
@@ -278,27 +79,26 @@ func download(ctx context.Context, imageURL string, opts DownloadOptions, secopt
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- body := res.Body
|
|
|
- contentLength := int(res.ContentLength)
|
|
|
+ imgdata, err := readAndCheckImage(res.Body, int(res.ContentLength), secopts)
|
|
|
+ if err != nil {
|
|
|
+ return nil, ierrors.Wrap(err, 0)
|
|
|
+ }
|
|
|
|
|
|
- if res.Header.Get("Content-Encoding") == "gzip" {
|
|
|
- gzipBody, errGzip := gzip.NewReader(res.Body)
|
|
|
- if gzipBody != nil {
|
|
|
- defer gzipBody.Close()
|
|
|
+ h := make(map[string]string)
|
|
|
+ for k := range res.Header {
|
|
|
+ if !slices.Contains(keepResponseHeaders, k) {
|
|
|
+ continue
|
|
|
}
|
|
|
- if errGzip != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- body = gzipBody
|
|
|
- contentLength = 0
|
|
|
- }
|
|
|
|
|
|
- imgdata, err := readAndCheckImage(body, contentLength, secopts)
|
|
|
- if err != nil {
|
|
|
- return nil, ierrors.Wrap(err, 0)
|
|
|
+ // TODO: Fix Etag/ETag inconsistency
|
|
|
+ if k == "Etag" {
|
|
|
+ h["ETag"] = res.Header.Get(k)
|
|
|
+ } else {
|
|
|
+ h[k] = res.Header.Get(k)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- imgdata.Headers = headersToStore(res)
|
|
|
+ imgdata.Headers = h
|
|
|
|
|
|
return imgdata, nil
|
|
|
}
|