download.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package imagedata
  2. import (
  3. "compress/gzip"
  4. "context"
  5. "io"
  6. "net/http"
  7. "net/http/cookiejar"
  8. "regexp"
  9. "strconv"
  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. transportCommon "github.com/imgproxy/imgproxy/v3/transport/common"
  18. fsTransport "github.com/imgproxy/imgproxy/v3/transport/fs"
  19. gcsTransport "github.com/imgproxy/imgproxy/v3/transport/gcs"
  20. s3Transport "github.com/imgproxy/imgproxy/v3/transport/s3"
  21. swiftTransport "github.com/imgproxy/imgproxy/v3/transport/swift"
  22. )
  23. var (
  24. downloadClient *http.Client
  25. enabledSchemes = map[string]struct{}{
  26. "http": {},
  27. "https": {},
  28. }
  29. imageHeadersToStore = []string{
  30. "Cache-Control",
  31. "Expires",
  32. "ETag",
  33. "Last-Modified",
  34. }
  35. contentRangeRe = regexp.MustCompile(`^bytes ((\d+)-(\d+)|\*)/(\d+|\*)$`)
  36. // For tests
  37. redirectAllRequestsTo string
  38. )
  39. const msgSourceImageIsUnreachable = "Source image is unreachable"
  40. type DownloadOptions struct {
  41. Header http.Header
  42. CookieJar http.CookieJar
  43. }
  44. func initDownloading() error {
  45. transport, err := defaultTransport.New(true)
  46. if err != nil {
  47. return err
  48. }
  49. registerProtocol := func(scheme string, rt http.RoundTripper) {
  50. transport.RegisterProtocol(scheme, rt)
  51. enabledSchemes[scheme] = struct{}{}
  52. }
  53. if config.LocalFileSystemRoot != "" {
  54. registerProtocol("local", fsTransport.New())
  55. }
  56. if config.S3Enabled {
  57. if t, err := s3Transport.New(); err != nil {
  58. return err
  59. } else {
  60. registerProtocol("s3", t)
  61. }
  62. }
  63. if config.GCSEnabled {
  64. if t, err := gcsTransport.New(); err != nil {
  65. return err
  66. } else {
  67. registerProtocol("gs", t)
  68. }
  69. }
  70. if config.ABSEnabled {
  71. if t, err := azureTransport.New(); err != nil {
  72. return err
  73. } else {
  74. registerProtocol("abs", t)
  75. }
  76. }
  77. if config.SwiftEnabled {
  78. if t, err := swiftTransport.New(); err != nil {
  79. return err
  80. } else {
  81. registerProtocol("swift", t)
  82. }
  83. }
  84. downloadClient = &http.Client{
  85. Transport: transport,
  86. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  87. redirects := len(via)
  88. if redirects >= config.MaxRedirects {
  89. return newImageTooManyRedirectsError(redirects)
  90. }
  91. return nil
  92. },
  93. }
  94. return nil
  95. }
  96. func headersToStore(res *http.Response) map[string]string {
  97. m := make(map[string]string)
  98. for _, h := range imageHeadersToStore {
  99. if val := res.Header.Get(h); len(val) != 0 {
  100. m[h] = val
  101. }
  102. }
  103. return m
  104. }
  105. func BuildImageRequest(ctx context.Context, imageURL string, header http.Header, jar http.CookieJar) (*http.Request, context.CancelFunc, error) {
  106. reqCtx, reqCancel := context.WithTimeout(ctx, time.Duration(config.DownloadTimeout)*time.Second)
  107. imageURL = transportCommon.EscapeURL(imageURL)
  108. req, err := http.NewRequestWithContext(reqCtx, "GET", imageURL, nil)
  109. if err != nil {
  110. reqCancel()
  111. return nil, func() {}, newImageRequestError(err)
  112. }
  113. if _, ok := enabledSchemes[req.URL.Scheme]; !ok {
  114. reqCancel()
  115. return nil, func() {}, newImageRequstSchemeError(req.URL.Scheme)
  116. }
  117. if jar != nil {
  118. for _, cookie := range jar.Cookies(req.URL) {
  119. req.AddCookie(cookie)
  120. }
  121. }
  122. req.Header.Set("User-Agent", config.UserAgent)
  123. for k, v := range header {
  124. if len(v) > 0 {
  125. req.Header.Set(k, v[0])
  126. }
  127. }
  128. return req, reqCancel, nil
  129. }
  130. func SendRequest(req *http.Request) (*http.Response, error) {
  131. var client *http.Client
  132. if req.URL.Scheme == "http" || req.URL.Scheme == "https" {
  133. clientCopy := *downloadClient
  134. jar, err := cookiejar.New(nil)
  135. if err != nil {
  136. return nil, err
  137. }
  138. clientCopy.Jar = jar
  139. client = &clientCopy
  140. } else {
  141. client = downloadClient
  142. }
  143. for {
  144. res, err := client.Do(req)
  145. if err == nil {
  146. return res, nil
  147. }
  148. if res != nil && res.Body != nil {
  149. res.Body.Close()
  150. }
  151. if strings.Contains(err.Error(), "client connection lost") {
  152. select {
  153. case <-req.Context().Done():
  154. return nil, err
  155. case <-time.After(100 * time.Microsecond):
  156. continue
  157. }
  158. }
  159. return nil, wrapError(err)
  160. }
  161. }
  162. func requestImage(ctx context.Context, imageURL string, opts DownloadOptions) (*http.Response, context.CancelFunc, error) {
  163. req, reqCancel, err := BuildImageRequest(ctx, imageURL, opts.Header, opts.CookieJar)
  164. if err != nil {
  165. reqCancel()
  166. return nil, func() {}, err
  167. }
  168. res, err := SendRequest(req)
  169. if err != nil {
  170. reqCancel()
  171. return nil, func() {}, err
  172. }
  173. if res.StatusCode == http.StatusNotModified {
  174. res.Body.Close()
  175. reqCancel()
  176. return nil, func() {}, newNotModifiedError(headersToStore(res))
  177. }
  178. // If the source responds with 206, check if the response contains entire image.
  179. // If not, return an error.
  180. if res.StatusCode == http.StatusPartialContent {
  181. contentRange := res.Header.Get("Content-Range")
  182. rangeParts := contentRangeRe.FindStringSubmatch(contentRange)
  183. if len(rangeParts) == 0 {
  184. res.Body.Close()
  185. reqCancel()
  186. return nil, func() {}, newImagePartialResponseError("Partial response with invalid Content-Range header")
  187. }
  188. if rangeParts[1] == "*" || rangeParts[2] != "0" {
  189. res.Body.Close()
  190. reqCancel()
  191. return nil, func() {}, newImagePartialResponseError("Partial response with incomplete content")
  192. }
  193. contentLengthStr := rangeParts[4]
  194. if contentLengthStr == "*" {
  195. contentLengthStr = res.Header.Get("Content-Length")
  196. }
  197. contentLength, _ := strconv.Atoi(contentLengthStr)
  198. rangeEnd, _ := strconv.Atoi(rangeParts[3])
  199. if contentLength <= 0 || rangeEnd != contentLength-1 {
  200. res.Body.Close()
  201. reqCancel()
  202. return nil, func() {}, newImagePartialResponseError("Partial response with incomplete content")
  203. }
  204. } else if res.StatusCode != http.StatusOK {
  205. var body string
  206. if strings.HasPrefix(res.Header.Get("Content-Type"), "text/") {
  207. bbody, _ := io.ReadAll(io.LimitReader(res.Body, 1024))
  208. body = string(bbody)
  209. }
  210. res.Body.Close()
  211. reqCancel()
  212. return nil, func() {}, newImageResponseStatusError(res.StatusCode, body)
  213. }
  214. return res, reqCancel, nil
  215. }
  216. func download(ctx context.Context, imageURL string, opts DownloadOptions, secopts security.Options) (*ImageData, error) {
  217. // We use this for testing
  218. if len(redirectAllRequestsTo) > 0 {
  219. imageURL = redirectAllRequestsTo
  220. }
  221. res, reqCancel, err := requestImage(ctx, imageURL, opts)
  222. defer reqCancel()
  223. if res != nil {
  224. defer res.Body.Close()
  225. }
  226. if err != nil {
  227. return nil, err
  228. }
  229. body := res.Body
  230. contentLength := int(res.ContentLength)
  231. if res.Header.Get("Content-Encoding") == "gzip" {
  232. gzipBody, errGzip := gzip.NewReader(res.Body)
  233. if gzipBody != nil {
  234. defer gzipBody.Close()
  235. }
  236. if errGzip != nil {
  237. return nil, err
  238. }
  239. body = gzipBody
  240. contentLength = 0
  241. }
  242. imgdata, err := readAndCheckImage(body, contentLength, secopts)
  243. if err != nil {
  244. return nil, ierrors.Wrap(err, 0)
  245. }
  246. imgdata.Headers = headersToStore(res)
  247. return imgdata, nil
  248. }
  249. func RedirectAllRequestsTo(u string) {
  250. redirectAllRequestsTo = u
  251. }
  252. func StopRedirectingRequests() {
  253. redirectAllRequestsTo = ""
  254. }