stream.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "context"
  4. "io"
  5. "mime"
  6. "net/http"
  7. "net/http/cookiejar"
  8. "path/filepath"
  9. "strconv"
  10. "sync"
  11. log "github.com/sirupsen/logrus"
  12. "github.com/imgproxy/imgproxy/v3/config"
  13. "github.com/imgproxy/imgproxy/v3/cookies"
  14. "github.com/imgproxy/imgproxy/v3/imagedata"
  15. "github.com/imgproxy/imgproxy/v3/imagetype"
  16. "github.com/imgproxy/imgproxy/v3/metrics"
  17. "github.com/imgproxy/imgproxy/v3/metrics/stats"
  18. "github.com/imgproxy/imgproxy/v3/options"
  19. "github.com/imgproxy/imgproxy/v3/router"
  20. )
  21. var (
  22. streamReqHeaders = []string{
  23. "If-None-Match",
  24. "Accept-Encoding",
  25. "Range",
  26. }
  27. streamRespHeaders = []string{
  28. "Cache-Control",
  29. "Expires",
  30. "ETag",
  31. "Content-Type",
  32. "Content-Encoding",
  33. "Content-Range",
  34. "Accept-Ranges",
  35. }
  36. streamBufPool = sync.Pool{
  37. New: func() interface{} {
  38. buf := make([]byte, 4096)
  39. return &buf
  40. },
  41. }
  42. )
  43. func streamOriginImage(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter, po *options.ProcessingOptions, imageURL string) {
  44. stats.IncImagesInProgress()
  45. defer stats.DecImagesInProgress()
  46. defer metrics.StartStreamingSegment(ctx)()
  47. var (
  48. cookieJar *cookiejar.Jar
  49. err error
  50. )
  51. imgRequestHeader := make(http.Header)
  52. for _, k := range streamReqHeaders {
  53. if v := r.Header.Get(k); len(v) != 0 {
  54. imgRequestHeader.Set(k, v)
  55. }
  56. }
  57. if config.CookiePassthrough {
  58. cookieJar, err = cookies.JarFromRequest(r)
  59. checkErr(ctx, "streaming", err)
  60. }
  61. req, reqCancel, err := imagedata.BuildImageRequest(r.Context(), imageURL, imgRequestHeader, cookieJar)
  62. defer reqCancel()
  63. checkErr(ctx, "streaming", err)
  64. res, err := imagedata.SendRequest(req)
  65. if res != nil {
  66. defer res.Body.Close()
  67. }
  68. checkErr(ctx, "streaming", err)
  69. for _, k := range streamRespHeaders {
  70. vv := res.Header.Values(k)
  71. for _, v := range vv {
  72. rw.Header().Set(k, v)
  73. }
  74. }
  75. if res.ContentLength >= 0 {
  76. rw.Header().Set("Content-Length", strconv.Itoa(int(res.ContentLength)))
  77. }
  78. if res.StatusCode < 300 {
  79. var filename, ext, mimetype string
  80. _, filename = filepath.Split(req.URL.Path)
  81. ext = filepath.Ext(filename)
  82. if len(po.Filename) > 0 {
  83. filename = po.Filename
  84. } else {
  85. filename = filename[:len(filename)-len(ext)]
  86. }
  87. mimetype = rw.Header().Get("Content-Type")
  88. if len(ext) == 0 && len(mimetype) > 0 {
  89. if exts, err := mime.ExtensionsByType(mimetype); err == nil && len(exts) != 0 {
  90. ext = exts[0]
  91. }
  92. }
  93. rw.Header().Set("Content-Disposition", imagetype.ContentDisposition(filename, ext, po.ReturnAttachment))
  94. }
  95. setCacheControl(rw, po.Expires, map[string]string{
  96. "Cache-Control": rw.Header().Get("Cache-Control"),
  97. "Expires": rw.Header().Get("Expires"),
  98. })
  99. setCanonical(rw, imageURL)
  100. rw.Header().Set("Content-Security-Policy", "script-src 'none'")
  101. rw.WriteHeader(res.StatusCode)
  102. buf := streamBufPool.Get().(*[]byte)
  103. defer streamBufPool.Put(buf)
  104. _, copyerr := io.CopyBuffer(rw, res.Body, *buf)
  105. router.LogResponse(
  106. reqID, r, res.StatusCode, nil,
  107. log.Fields{
  108. "image_url": imageURL,
  109. "processing_options": po,
  110. },
  111. )
  112. if copyerr != nil {
  113. panic(http.ErrAbortHandler)
  114. }
  115. }