request_methods.go 7.6 KB

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