stream.go 3.0 KB

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