2021-04-26 13:52:50 +02:00
|
|
|
package imagedata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
2023-03-21 19:58:16 +02:00
|
|
|
"context"
|
2021-04-26 13:52:50 +02:00
|
|
|
"fmt"
|
2022-10-28 17:15:13 +02:00
|
|
|
"io"
|
2021-04-26 13:52:50 +02:00
|
|
|
"net/http"
|
2021-11-09 13:14:05 +02:00
|
|
|
"net/http/cookiejar"
|
2023-04-16 19:58:34 +02:00
|
|
|
"strings"
|
2021-04-26 13:52:50 +02:00
|
|
|
"time"
|
|
|
|
|
2021-09-30 16:23:30 +02:00
|
|
|
"github.com/imgproxy/imgproxy/v3/config"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/ierrors"
|
2023-02-23 20:11:44 +02:00
|
|
|
"github.com/imgproxy/imgproxy/v3/security"
|
2021-04-26 13:52:50 +02:00
|
|
|
|
2023-04-13 22:05:25 +02:00
|
|
|
defaultTransport "github.com/imgproxy/imgproxy/v3/transport"
|
2021-09-30 16:23:30 +02:00
|
|
|
azureTransport "github.com/imgproxy/imgproxy/v3/transport/azure"
|
|
|
|
fsTransport "github.com/imgproxy/imgproxy/v3/transport/fs"
|
|
|
|
gcsTransport "github.com/imgproxy/imgproxy/v3/transport/gcs"
|
|
|
|
s3Transport "github.com/imgproxy/imgproxy/v3/transport/s3"
|
2022-04-06 13:00:19 +02:00
|
|
|
swiftTransport "github.com/imgproxy/imgproxy/v3/transport/swift"
|
2021-04-26 13:52:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
downloadClient *http.Client
|
|
|
|
|
2021-11-01 14:13:33 +02:00
|
|
|
enabledSchemes = map[string]struct{}{
|
|
|
|
"http": {},
|
|
|
|
"https": {},
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
imageHeadersToStore = []string{
|
|
|
|
"Cache-Control",
|
|
|
|
"Expires",
|
2021-09-29 12:23:54 +02:00
|
|
|
"ETag",
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
2021-09-07 15:04:33 +02:00
|
|
|
|
|
|
|
// For tests
|
|
|
|
redirectAllRequestsTo string
|
2021-04-26 13:52:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const msgSourceImageIsUnreachable = "Source image is unreachable"
|
|
|
|
|
2023-02-23 20:39:52 +02:00
|
|
|
type DownloadOptions struct {
|
|
|
|
Header http.Header
|
|
|
|
CookieJar *cookiejar.Jar
|
|
|
|
}
|
|
|
|
|
2021-10-13 13:59:46 +02:00
|
|
|
type ErrorNotModified struct {
|
|
|
|
Message string
|
|
|
|
Headers map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrorNotModified) Error() string {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
func initDownloading() error {
|
2023-04-13 22:05:25 +02:00
|
|
|
transport, err := defaultTransport.New(true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 14:13:33 +02:00
|
|
|
registerProtocol := func(scheme string, rt http.RoundTripper) {
|
|
|
|
transport.RegisterProtocol(scheme, rt)
|
|
|
|
enabledSchemes[scheme] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
if config.LocalFileSystemRoot != "" {
|
2021-11-01 14:13:33 +02:00
|
|
|
registerProtocol("local", fsTransport.New())
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.S3Enabled {
|
|
|
|
if t, err := s3Transport.New(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2021-11-01 14:13:33 +02:00
|
|
|
registerProtocol("s3", t)
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.GCSEnabled {
|
|
|
|
if t, err := gcsTransport.New(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2021-11-01 14:13:33 +02:00
|
|
|
registerProtocol("gs", t)
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ABSEnabled {
|
|
|
|
if t, err := azureTransport.New(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2021-11-01 14:13:33 +02:00
|
|
|
registerProtocol("abs", t)
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:00:19 +02:00
|
|
|
if config.SwiftEnabled {
|
|
|
|
if t, err := swiftTransport.New(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
registerProtocol("swift", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
downloadClient = &http.Client{
|
|
|
|
Transport: transport,
|
2022-02-16 11:42:09 +02:00
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
redirects := len(via)
|
|
|
|
if redirects >= config.MaxRedirects {
|
|
|
|
return fmt.Errorf("stopped after %d redirects", redirects)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-13 13:59:46 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-21 19:58:16 +02:00
|
|
|
func BuildImageRequest(ctx context.Context, imageURL string, header http.Header, jar *cookiejar.Jar) (*http.Request, context.CancelFunc, error) {
|
|
|
|
reqCtx, reqCancel := context.WithTimeout(ctx, time.Duration(config.DownloadTimeout)*time.Second)
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(reqCtx, "GET", imageURL, nil)
|
2021-04-26 13:52:50 +02:00
|
|
|
if err != nil {
|
2023-03-21 19:58:16 +02:00
|
|
|
reqCancel()
|
|
|
|
return nil, func() {}, ierrors.New(404, err.Error(), msgSourceImageIsUnreachable)
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 14:13:33 +02:00
|
|
|
if _, ok := enabledSchemes[req.URL.Scheme]; !ok {
|
2023-03-21 19:58:16 +02:00
|
|
|
reqCancel()
|
|
|
|
return nil, func() {}, ierrors.New(
|
2021-11-01 14:13:33 +02:00
|
|
|
404,
|
2023-01-23 17:50:13 +02:00
|
|
|
fmt.Sprintf("Unknown scheme: %s", req.URL.Scheme),
|
2021-11-01 14:13:33 +02:00
|
|
|
msgSourceImageIsUnreachable,
|
2021-11-01 14:29:26 +02:00
|
|
|
)
|
2021-11-01 14:13:33 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 13:14:05 +02:00
|
|
|
if jar != nil {
|
|
|
|
for _, cookie := range jar.Cookies(req.URL) {
|
|
|
|
req.AddCookie(cookie)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
req.Header.Set("User-Agent", config.UserAgent)
|
|
|
|
|
2021-09-29 12:23:54 +02:00
|
|
|
for k, v := range header {
|
|
|
|
if len(v) > 0 {
|
|
|
|
req.Header.Set(k, v[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 19:58:16 +02:00
|
|
|
return req, reqCancel, nil
|
2022-09-07 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func SendRequest(req *http.Request) (*http.Response, error) {
|
2023-04-16 19:58:34 +02:00
|
|
|
for {
|
|
|
|
res, err := downloadClient.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 19:58:16 +02:00
|
|
|
return nil, wrapError(err)
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
2022-09-07 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
2023-03-21 19:58:16 +02:00
|
|
|
func requestImage(ctx context.Context, imageURL string, opts DownloadOptions) (*http.Response, context.CancelFunc, error) {
|
|
|
|
req, reqCancel, err := BuildImageRequest(ctx, imageURL, opts.Header, opts.CookieJar)
|
2022-09-07 12:50:21 +02:00
|
|
|
if err != nil {
|
2023-03-21 19:58:16 +02:00
|
|
|
reqCancel()
|
|
|
|
return nil, func() {}, err
|
2022-09-07 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := SendRequest(req)
|
|
|
|
if err != nil {
|
2023-03-21 19:58:16 +02:00
|
|
|
reqCancel()
|
|
|
|
return nil, func() {}, err
|
2022-09-07 12:50:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-29 12:23:54 +02:00
|
|
|
if res.StatusCode == http.StatusNotModified {
|
2022-09-07 12:50:21 +02:00
|
|
|
res.Body.Close()
|
2023-03-21 19:58:16 +02:00
|
|
|
reqCancel()
|
|
|
|
return nil, func() {}, &ErrorNotModified{Message: "Not Modified", Headers: headersToStore(res)}
|
2021-09-29 12:23:54 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
if res.StatusCode != 200 {
|
2022-10-28 17:15:13 +02:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2021-04-26 13:52:50 +02:00
|
|
|
res.Body.Close()
|
2023-03-21 19:58:16 +02:00
|
|
|
reqCancel()
|
2021-04-26 13:52:50 +02:00
|
|
|
|
2021-11-01 14:13:33 +02:00
|
|
|
status := 404
|
|
|
|
if res.StatusCode >= 500 {
|
|
|
|
status = 500
|
|
|
|
}
|
|
|
|
|
2021-09-29 12:23:54 +02:00
|
|
|
msg := fmt.Sprintf("Status: %d; %s", res.StatusCode, string(body))
|
2023-03-21 19:58:16 +02:00
|
|
|
return nil, func() {}, ierrors.New(status, msg, msgSourceImageIsUnreachable)
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
|
|
|
|
2023-03-21 19:58:16 +02:00
|
|
|
return res, reqCancel, nil
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
|
|
|
|
2023-03-21 19:58:16 +02:00
|
|
|
func download(ctx context.Context, imageURL string, opts DownloadOptions, secopts security.Options) (*ImageData, error) {
|
2021-09-07 15:04:33 +02:00
|
|
|
// We use this for testing
|
|
|
|
if len(redirectAllRequestsTo) > 0 {
|
|
|
|
imageURL = redirectAllRequestsTo
|
|
|
|
}
|
|
|
|
|
2023-03-21 19:58:16 +02:00
|
|
|
res, reqCancel, err := requestImage(ctx, imageURL, opts)
|
|
|
|
defer reqCancel()
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
if res != nil {
|
|
|
|
defer res.Body.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
body := res.Body
|
|
|
|
contentLength := int(res.ContentLength)
|
|
|
|
|
|
|
|
if res.Header.Get("Content-Encoding") == "gzip" {
|
|
|
|
gzipBody, errGzip := gzip.NewReader(res.Body)
|
|
|
|
if gzipBody != nil {
|
|
|
|
defer gzipBody.Close()
|
|
|
|
}
|
|
|
|
if errGzip != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
body = gzipBody
|
|
|
|
contentLength = 0
|
|
|
|
}
|
|
|
|
|
2023-02-23 20:11:44 +02:00
|
|
|
imgdata, err := readAndCheckImage(body, contentLength, secopts)
|
2021-04-26 13:52:50 +02:00
|
|
|
if err != nil {
|
2021-11-01 14:29:26 +02:00
|
|
|
return nil, ierrors.Wrap(err, 0)
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 13:59:46 +02:00
|
|
|
imgdata.Headers = headersToStore(res)
|
2021-04-26 13:52:50 +02:00
|
|
|
|
|
|
|
return imgdata, nil
|
|
|
|
}
|
2021-09-07 15:04:33 +02:00
|
|
|
|
|
|
|
func RedirectAllRequestsTo(u string) {
|
|
|
|
redirectAllRequestsTo = u
|
|
|
|
}
|
|
|
|
|
|
|
|
func StopRedirectingRequests() {
|
|
|
|
redirectAllRequestsTo = ""
|
|
|
|
}
|