request_methods.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package processing
  2. import (
  3. "context"
  4. "io"
  5. "net/http"
  6. "strconv"
  7. "github.com/imgproxy/imgproxy/v3/cookies"
  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"
  15. "github.com/imgproxy/imgproxy/v3/processing"
  16. "github.com/imgproxy/imgproxy/v3/server"
  17. log "github.com/sirupsen/logrus"
  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.po.SecurityOptions.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, r.po)
  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. log.
  90. WithField("request_id", r.reqID).
  91. Warningf("Could not load image %s. Using fallback image. %s", r.imageURL, err.Error())
  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(
  108. ctx context.Context,
  109. po *options.ProcessingOptions,
  110. ) (imagedata.ImageData, http.Header) {
  111. fbi := r.FallbackImage()
  112. if fbi == nil {
  113. return nil, nil
  114. }
  115. data, h, err := fbi.Get(ctx, po)
  116. if err != nil {
  117. log.Warning(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.MetaProcessingOptions))()
  128. return processing.ProcessImage(ctx, originData, r.po, r.WatermarkImage())
  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.po.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. log.Fields{
  163. "image_url": r.imageURL,
  164. "processing_options": r.po,
  165. },
  166. )
  167. return nil
  168. }
  169. func (r *request) respondWithImage(statusCode int, resultData imagedata.ImageData) error {
  170. // We read the size of the image data here, so we can set Content-Length header.
  171. // This indireclty ensures that the image data is fully read from the source, no
  172. // errors happened.
  173. resultSize, err := resultData.Size()
  174. if err != nil {
  175. return ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategoryImageDataSize))
  176. }
  177. r.rw.SetContentType(resultData.Format().Mime())
  178. r.rw.SetContentLength(resultSize)
  179. r.rw.SetContentDisposition(
  180. r.imageURL,
  181. r.po.Filename,
  182. resultData.Format().Ext(),
  183. "",
  184. r.po.ReturnAttachment,
  185. )
  186. r.rw.SetExpires(r.po.Expires)
  187. r.rw.SetVary()
  188. r.rw.SetCanonical(r.imageURL)
  189. if r.config.LastModifiedEnabled {
  190. r.rw.Passthrough(httpheaders.LastModified)
  191. }
  192. if r.config.ETagEnabled {
  193. r.rw.Passthrough(httpheaders.Etag)
  194. }
  195. r.rw.WriteHeader(statusCode)
  196. _, err = io.Copy(r.rw, resultData.Reader())
  197. var ierr *ierrors.Error
  198. if err != nil {
  199. ierr = handlers.NewResponseWriteError(err)
  200. if r.config.ReportIOErrors {
  201. return ierrors.Wrap(ierr, 0, ierrors.WithCategory(handlers.CategoryIO), ierrors.WithShouldReport(true))
  202. }
  203. }
  204. server.LogResponse(
  205. r.reqID, r.req, statusCode, ierr,
  206. log.Fields{
  207. "image_url": r.imageURL,
  208. "processing_options": r.po,
  209. },
  210. )
  211. return nil
  212. }
  213. // wrapDownloadingErr wraps original error to download error
  214. func (r *request) wrapDownloadingErr(originalErr error) *ierrors.Error {
  215. err := ierrors.Wrap(originalErr, 0, ierrors.WithCategory(handlers.CategoryDownload))
  216. // we report this error only if enabled
  217. if r.config.ReportDownloadingErrors {
  218. err = ierrors.Wrap(err, 0, ierrors.WithShouldReport(true))
  219. }
  220. return err
  221. }