request_methods.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package processing
  2. import (
  3. "context"
  4. "io"
  5. "log/slog"
  6. "net/http"
  7. "strconv"
  8. "github.com/imgproxy/imgproxy/v3/cookies"
  9. "github.com/imgproxy/imgproxy/v3/errorreport"
  10. "github.com/imgproxy/imgproxy/v3/handlers"
  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/options/keys"
  16. "github.com/imgproxy/imgproxy/v3/processing"
  17. "github.com/imgproxy/imgproxy/v3/server"
  18. )
  19. // makeImageRequestHeaders creates headers for the image request
  20. func (r *request) makeImageRequestHeaders() http.Header {
  21. h := make(http.Header)
  22. // If ETag is enabled, we forward If-None-Match header
  23. if r.config.ETagEnabled {
  24. h.Set(httpheaders.IfNoneMatch, r.req.Header.Get(httpheaders.IfNoneMatch))
  25. }
  26. // If LastModified is enabled, we forward If-Modified-Since header
  27. if r.config.LastModifiedEnabled {
  28. h.Set(httpheaders.IfModifiedSince, r.req.Header.Get(httpheaders.IfModifiedSince))
  29. }
  30. return h
  31. }
  32. // acquireWorker acquires the processing worker
  33. func (r *request) acquireWorker(ctx context.Context) (context.CancelFunc, error) {
  34. defer monitoring.StartQueueSegment(ctx)()
  35. fn, err := r.Workers().Acquire(ctx)
  36. if err != nil {
  37. // We don't actually need to check timeout here,
  38. // but it's an easy way to check if this is an actual timeout
  39. // or the request was canceled
  40. if terr := server.CheckTimeout(ctx); terr != nil {
  41. return nil, ierrors.Wrap(terr, 0, ierrors.WithCategory(handlers.CategoryTimeout))
  42. }
  43. // We should never reach this line as err could be only ctx.Err()
  44. // and we've already checked for it. But beter safe than sorry
  45. return nil, ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategoryQueue))
  46. }
  47. return fn, nil
  48. }
  49. // makeDownloadOptions creates a new default download options
  50. func (r *request) makeDownloadOptions(ctx context.Context, h http.Header) imagedata.DownloadOptions {
  51. downloadFinished := monitoring.StartDownloadingSegment(ctx, r.monitoringMeta.Filter(
  52. monitoring.MetaSourceImageURL,
  53. monitoring.MetaSourceImageOrigin,
  54. ))
  55. return imagedata.DownloadOptions{
  56. Header: h,
  57. MaxSrcFileSize: r.secops.MaxSrcFileSize,
  58. DownloadFinished: downloadFinished,
  59. }
  60. }
  61. // fetchImage downloads the source image asynchronously
  62. func (r *request) fetchImage(ctx context.Context, do imagedata.DownloadOptions) (imagedata.ImageData, http.Header, error) {
  63. var err error
  64. if r.config.CookiePassthrough {
  65. do.CookieJar, err = cookies.JarFromRequest(r.req)
  66. if err != nil {
  67. return nil, nil, ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategoryDownload))
  68. }
  69. }
  70. return r.ImageDataFactory().DownloadAsync(ctx, r.imageURL, "source image", do)
  71. }
  72. // handleDownloadError replaces the image data with fallback image if needed
  73. func (r *request) handleDownloadError(
  74. ctx context.Context,
  75. originalErr error,
  76. ) (imagedata.ImageData, int, error) {
  77. err := r.wrapDownloadingErr(originalErr)
  78. // If there is no fallback image configured, just return the error
  79. data, headers := r.getFallbackImage(ctx)
  80. if data == nil {
  81. return nil, 0, err
  82. }
  83. // Just send error
  84. monitoring.SendError(ctx, handlers.CategoryDownload, err)
  85. // We didn't return, so we have to report error
  86. if err.ShouldReport() {
  87. errorreport.Report(err, r.req)
  88. }
  89. slog.Warn(
  90. "Could not load image. Using fallback image",
  91. "request_id", r.reqID,
  92. "image_url", r.imageURL,
  93. "error", err.Error(),
  94. )
  95. var statusCode int
  96. // Set status code if needed
  97. if r.config.FallbackImageHTTPCode > 0 {
  98. statusCode = r.config.FallbackImageHTTPCode
  99. } else {
  100. statusCode = err.StatusCode()
  101. }
  102. // Fallback image should have exact FallbackImageTTL lifetime
  103. headers.Del(httpheaders.Expires)
  104. headers.Del(httpheaders.LastModified)
  105. r.rw.SetOriginHeaders(headers)
  106. r.rw.SetIsFallbackImage()
  107. return data, statusCode, nil
  108. }
  109. // getFallbackImage returns fallback image if any
  110. func (r *request) getFallbackImage(ctx context.Context) (imagedata.ImageData, http.Header) {
  111. fbi := r.FallbackImage()
  112. if fbi == nil {
  113. return nil, nil
  114. }
  115. data, h, err := fbi.Get(ctx, r.opts)
  116. if err != nil {
  117. slog.Warn(err.Error())
  118. if ierr := r.wrapDownloadingErr(err); ierr.ShouldReport() {
  119. errorreport.Report(ierr, r.req)
  120. }
  121. return nil, nil
  122. }
  123. return data, h
  124. }
  125. // processImage calls actual image processing
  126. func (r *request) processImage(ctx context.Context, originData imagedata.ImageData) (*processing.Result, error) {
  127. defer monitoring.StartProcessingSegment(ctx, r.monitoringMeta.Filter(monitoring.MetaOptions))()
  128. return r.Processor().ProcessImage(ctx, originData, r.opts, r.secops)
  129. }
  130. // writeDebugHeaders writes debug headers (X-Origin-*, X-Result-*) to the response
  131. func (r *request) writeDebugHeaders(result *processing.Result, originData imagedata.ImageData) error {
  132. if !r.config.EnableDebugHeaders {
  133. return nil
  134. }
  135. if result != nil {
  136. r.rw.Header().Set(httpheaders.XOriginWidth, strconv.Itoa(result.OriginWidth))
  137. r.rw.Header().Set(httpheaders.XOriginHeight, strconv.Itoa(result.OriginHeight))
  138. r.rw.Header().Set(httpheaders.XResultWidth, strconv.Itoa(result.ResultWidth))
  139. r.rw.Header().Set(httpheaders.XResultHeight, strconv.Itoa(result.ResultHeight))
  140. }
  141. // Try to read origin image size
  142. size, err := originData.Size()
  143. if err != nil {
  144. return ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategoryImageDataSize))
  145. }
  146. r.rw.Header().Set(httpheaders.XOriginContentLength, strconv.Itoa(size))
  147. return nil
  148. }
  149. // respondWithNotModified writes not-modified response
  150. func (r *request) respondWithNotModified() error {
  151. r.rw.SetExpires(r.opts.GetTime(keys.Expires))
  152. r.rw.SetVary()
  153. if r.config.LastModifiedEnabled {
  154. r.rw.Passthrough(httpheaders.LastModified)
  155. }
  156. if r.config.ETagEnabled {
  157. r.rw.Passthrough(httpheaders.Etag)
  158. }
  159. r.rw.WriteHeader(http.StatusNotModified)
  160. server.LogResponse(
  161. r.reqID, r.req, http.StatusNotModified, nil,
  162. slog.String("image_url", r.imageURL),
  163. slog.Any("processing_options", r.opts),
  164. )
  165. return nil
  166. }
  167. func (r *request) respondWithImage(statusCode int, resultData imagedata.ImageData) error {
  168. // We read the size of the image data here, so we can set Content-Length header.
  169. // This indireclty ensures that the image data is fully read from the source, no
  170. // errors happened.
  171. resultSize, err := resultData.Size()
  172. if err != nil {
  173. return ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategoryImageDataSize))
  174. }
  175. r.rw.SetContentType(resultData.Format().Mime())
  176. r.rw.SetContentLength(resultSize)
  177. r.rw.SetContentDisposition(
  178. r.imageURL,
  179. r.opts.GetString(keys.Filename, ""),
  180. resultData.Format().Ext(),
  181. "",
  182. r.opts.GetBool(keys.ReturnAttachment, false),
  183. )
  184. r.rw.SetExpires(r.opts.GetTime(keys.Expires))
  185. r.rw.SetVary()
  186. r.rw.SetCanonical(r.imageURL)
  187. if r.config.LastModifiedEnabled {
  188. r.rw.Passthrough(httpheaders.LastModified)
  189. }
  190. if r.config.ETagEnabled {
  191. r.rw.Passthrough(httpheaders.Etag)
  192. }
  193. r.rw.WriteHeader(statusCode)
  194. _, err = io.Copy(r.rw, resultData.Reader())
  195. var ierr *ierrors.Error
  196. if err != nil {
  197. ierr = handlers.NewResponseWriteError(err)
  198. if r.config.ReportIOErrors {
  199. return ierrors.Wrap(ierr, 0, ierrors.WithCategory(handlers.CategoryIO), ierrors.WithShouldReport(true))
  200. }
  201. }
  202. server.LogResponse(
  203. r.reqID, r.req, statusCode, ierr,
  204. slog.String("image_url", r.imageURL),
  205. slog.Any("processing_options", r.opts),
  206. )
  207. return nil
  208. }
  209. // wrapDownloadingErr wraps original error to download error
  210. func (r *request) wrapDownloadingErr(originalErr error) *ierrors.Error {
  211. err := ierrors.Wrap(originalErr, 0, ierrors.WithCategory(handlers.CategoryDownload))
  212. // we report this error only if enabled
  213. if r.config.ReportDownloadingErrors {
  214. err = ierrors.Wrap(err, 0, ierrors.WithShouldReport(true))
  215. }
  216. return err
  217. }