download.go 6.1 KB

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