download.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package imagedata
  2. import (
  3. "compress/gzip"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/http/cookiejar"
  9. "strings"
  10. "time"
  11. "github.com/imgproxy/imgproxy/v3/config"
  12. "github.com/imgproxy/imgproxy/v3/ierrors"
  13. "github.com/imgproxy/imgproxy/v3/security"
  14. defaultTransport "github.com/imgproxy/imgproxy/v3/transport"
  15. azureTransport "github.com/imgproxy/imgproxy/v3/transport/azure"
  16. fsTransport "github.com/imgproxy/imgproxy/v3/transport/fs"
  17. gcsTransport "github.com/imgproxy/imgproxy/v3/transport/gcs"
  18. s3Transport "github.com/imgproxy/imgproxy/v3/transport/s3"
  19. swiftTransport "github.com/imgproxy/imgproxy/v3/transport/swift"
  20. )
  21. var (
  22. downloadClient *http.Client
  23. enabledSchemes = map[string]struct{}{
  24. "http": {},
  25. "https": {},
  26. }
  27. imageHeadersToStore = []string{
  28. "Cache-Control",
  29. "Expires",
  30. "ETag",
  31. }
  32. // For tests
  33. redirectAllRequestsTo string
  34. )
  35. const msgSourceImageIsUnreachable = "Source image is unreachable"
  36. type DownloadOptions struct {
  37. Header http.Header
  38. CookieJar *cookiejar.Jar
  39. }
  40. type ErrorNotModified struct {
  41. Message string
  42. Headers map[string]string
  43. }
  44. func (e *ErrorNotModified) Error() string {
  45. return e.Message
  46. }
  47. func initDownloading() error {
  48. transport, err := defaultTransport.New(true)
  49. if err != nil {
  50. return err
  51. }
  52. registerProtocol := func(scheme string, rt http.RoundTripper) {
  53. transport.RegisterProtocol(scheme, rt)
  54. enabledSchemes[scheme] = struct{}{}
  55. }
  56. if config.LocalFileSystemRoot != "" {
  57. registerProtocol("local", fsTransport.New())
  58. }
  59. if config.S3Enabled {
  60. if t, err := s3Transport.New(); err != nil {
  61. return err
  62. } else {
  63. registerProtocol("s3", t)
  64. }
  65. }
  66. if config.GCSEnabled {
  67. if t, err := gcsTransport.New(); err != nil {
  68. return err
  69. } else {
  70. registerProtocol("gs", t)
  71. }
  72. }
  73. if config.ABSEnabled {
  74. if t, err := azureTransport.New(); err != nil {
  75. return err
  76. } else {
  77. registerProtocol("abs", t)
  78. }
  79. }
  80. if config.SwiftEnabled {
  81. if t, err := swiftTransport.New(); err != nil {
  82. return err
  83. } else {
  84. registerProtocol("swift", t)
  85. }
  86. }
  87. downloadClient = &http.Client{
  88. Transport: transport,
  89. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  90. redirects := len(via)
  91. if redirects >= config.MaxRedirects {
  92. return fmt.Errorf("stopped after %d redirects", redirects)
  93. }
  94. return nil
  95. },
  96. }
  97. return nil
  98. }
  99. func headersToStore(res *http.Response) map[string]string {
  100. m := make(map[string]string)
  101. for _, h := range imageHeadersToStore {
  102. if val := res.Header.Get(h); len(val) != 0 {
  103. m[h] = val
  104. }
  105. }
  106. return m
  107. }
  108. func BuildImageRequest(ctx context.Context, imageURL string, header http.Header, jar *cookiejar.Jar) (*http.Request, context.CancelFunc, error) {
  109. reqCtx, reqCancel := context.WithTimeout(ctx, time.Duration(config.DownloadTimeout)*time.Second)
  110. req, err := http.NewRequestWithContext(reqCtx, "GET", imageURL, nil)
  111. if err != nil {
  112. reqCancel()
  113. return nil, func() {}, ierrors.New(404, err.Error(), msgSourceImageIsUnreachable)
  114. }
  115. if _, ok := enabledSchemes[req.URL.Scheme]; !ok {
  116. reqCancel()
  117. return nil, func() {}, ierrors.New(
  118. 404,
  119. fmt.Sprintf("Unknown scheme: %s", req.URL.Scheme),
  120. msgSourceImageIsUnreachable,
  121. )
  122. }
  123. if jar != nil {
  124. for _, cookie := range jar.Cookies(req.URL) {
  125. req.AddCookie(cookie)
  126. }
  127. }
  128. req.Header.Set("User-Agent", config.UserAgent)
  129. for k, v := range header {
  130. if len(v) > 0 {
  131. req.Header.Set(k, v[0])
  132. }
  133. }
  134. return req, reqCancel, nil
  135. }
  136. func SendRequest(req *http.Request) (*http.Response, error) {
  137. for {
  138. res, err := downloadClient.Do(req)
  139. if err == nil {
  140. return res, nil
  141. }
  142. if res != nil && res.Body != nil {
  143. res.Body.Close()
  144. }
  145. if strings.Contains(err.Error(), "client connection lost") {
  146. select {
  147. case <-req.Context().Done():
  148. return nil, err
  149. case <-time.After(100 * time.Microsecond):
  150. continue
  151. }
  152. }
  153. return nil, wrapError(err)
  154. }
  155. }
  156. func requestImage(ctx context.Context, imageURL string, opts DownloadOptions) (*http.Response, context.CancelFunc, error) {
  157. req, reqCancel, err := BuildImageRequest(ctx, imageURL, opts.Header, opts.CookieJar)
  158. if err != nil {
  159. reqCancel()
  160. return nil, func() {}, err
  161. }
  162. res, err := SendRequest(req)
  163. if err != nil {
  164. reqCancel()
  165. return nil, func() {}, err
  166. }
  167. if res.StatusCode == http.StatusNotModified {
  168. res.Body.Close()
  169. reqCancel()
  170. return nil, func() {}, &ErrorNotModified{Message: "Not Modified", Headers: headersToStore(res)}
  171. }
  172. if res.StatusCode != 200 {
  173. body, _ := io.ReadAll(res.Body)
  174. res.Body.Close()
  175. reqCancel()
  176. status := 404
  177. if res.StatusCode >= 500 {
  178. status = 500
  179. }
  180. msg := fmt.Sprintf("Status: %d; %s", res.StatusCode, string(body))
  181. return nil, func() {}, ierrors.New(status, msg, msgSourceImageIsUnreachable)
  182. }
  183. return res, reqCancel, nil
  184. }
  185. func download(ctx context.Context, imageURL string, opts DownloadOptions, secopts security.Options) (*ImageData, error) {
  186. // We use this for testing
  187. if len(redirectAllRequestsTo) > 0 {
  188. imageURL = redirectAllRequestsTo
  189. }
  190. res, reqCancel, err := requestImage(ctx, imageURL, opts)
  191. defer reqCancel()
  192. if res != nil {
  193. defer res.Body.Close()
  194. }
  195. if err != nil {
  196. return nil, err
  197. }
  198. body := res.Body
  199. contentLength := int(res.ContentLength)
  200. if res.Header.Get("Content-Encoding") == "gzip" {
  201. gzipBody, errGzip := gzip.NewReader(res.Body)
  202. if gzipBody != nil {
  203. defer gzipBody.Close()
  204. }
  205. if errGzip != nil {
  206. return nil, err
  207. }
  208. body = gzipBody
  209. contentLength = 0
  210. }
  211. imgdata, err := readAndCheckImage(body, contentLength, secopts)
  212. if err != nil {
  213. return nil, ierrors.Wrap(err, 0)
  214. }
  215. imgdata.Headers = headersToStore(res)
  216. return imgdata, nil
  217. }
  218. func RedirectAllRequestsTo(u string) {
  219. redirectAllRequestsTo = u
  220. }
  221. func StopRedirectingRequests() {
  222. redirectAllRequestsTo = ""
  223. }