stream.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "Last-Modified",
  36. }
  37. streamBufPool = sync.Pool{
  38. New: func() interface{} {
  39. buf := make([]byte, 4096)
  40. return &buf
  41. },
  42. }
  43. )
  44. func streamOriginImage(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter, po *options.ProcessingOptions, imageURL string) {
  45. stats.IncImagesInProgress()
  46. defer stats.DecImagesInProgress()
  47. defer metrics.StartStreamingSegment(ctx)()
  48. var (
  49. cookieJar *cookiejar.Jar
  50. err error
  51. )
  52. imgRequestHeader := make(http.Header)
  53. for _, k := range streamReqHeaders {
  54. if v := r.Header.Get(k); len(v) != 0 {
  55. imgRequestHeader.Set(k, v)
  56. }
  57. }
  58. if config.CookiePassthrough {
  59. cookieJar, err = cookies.JarFromRequest(r)
  60. checkErr(ctx, "streaming", err)
  61. }
  62. req, reqCancel, err := imagedata.BuildImageRequest(r.Context(), imageURL, imgRequestHeader, cookieJar)
  63. defer reqCancel()
  64. checkErr(ctx, "streaming", err)
  65. res, err := imagedata.SendRequest(req)
  66. if res != nil {
  67. defer res.Body.Close()
  68. }
  69. checkErr(ctx, "streaming", err)
  70. for _, k := range streamRespHeaders {
  71. vv := res.Header.Values(k)
  72. for _, v := range vv {
  73. rw.Header().Set(k, v)
  74. }
  75. }
  76. if res.ContentLength >= 0 {
  77. rw.Header().Set("Content-Length", strconv.Itoa(int(res.ContentLength)))
  78. }
  79. if res.StatusCode < 300 {
  80. var filename, ext, mimetype string
  81. _, filename = filepath.Split(req.URL.Path)
  82. ext = filepath.Ext(filename)
  83. if len(po.Filename) > 0 {
  84. filename = po.Filename
  85. } else {
  86. filename = filename[:len(filename)-len(ext)]
  87. }
  88. mimetype = rw.Header().Get("Content-Type")
  89. if len(ext) == 0 && len(mimetype) > 0 {
  90. if exts, err := mime.ExtensionsByType(mimetype); err == nil && len(exts) != 0 {
  91. ext = exts[0]
  92. }
  93. }
  94. rw.Header().Set("Content-Disposition", imagetype.ContentDisposition(filename, ext, po.ReturnAttachment))
  95. }
  96. setCacheControl(rw, po.Expires, map[string]string{
  97. "Cache-Control": rw.Header().Get("Cache-Control"),
  98. "Expires": rw.Header().Get("Expires"),
  99. })
  100. setCanonical(rw, imageURL)
  101. rw.Header().Set("Content-Security-Policy", "script-src 'none'")
  102. rw.WriteHeader(res.StatusCode)
  103. buf := streamBufPool.Get().(*[]byte)
  104. defer streamBufPool.Put(buf)
  105. _, copyerr := io.CopyBuffer(rw, res.Body, *buf)
  106. router.LogResponse(
  107. reqID, r, res.StatusCode, nil,
  108. log.Fields{
  109. "image_url": imageURL,
  110. "processing_options": po,
  111. },
  112. )
  113. if copyerr != nil {
  114. panic(http.ErrAbortHandler)
  115. }
  116. }