request_methods.go 7.5 KB

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