1
0

download.go 6.9 KB

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