processing_handler.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. log "github.com/sirupsen/logrus"
  10. "github.com/imgproxy/imgproxy/v3/config"
  11. "github.com/imgproxy/imgproxy/v3/cookies"
  12. "github.com/imgproxy/imgproxy/v3/errorreport"
  13. "github.com/imgproxy/imgproxy/v3/etag"
  14. "github.com/imgproxy/imgproxy/v3/ierrors"
  15. "github.com/imgproxy/imgproxy/v3/imagedata"
  16. "github.com/imgproxy/imgproxy/v3/imagetype"
  17. "github.com/imgproxy/imgproxy/v3/metrics"
  18. "github.com/imgproxy/imgproxy/v3/metrics/stats"
  19. "github.com/imgproxy/imgproxy/v3/options"
  20. "github.com/imgproxy/imgproxy/v3/processing"
  21. "github.com/imgproxy/imgproxy/v3/router"
  22. "github.com/imgproxy/imgproxy/v3/security"
  23. "github.com/imgproxy/imgproxy/v3/semaphore"
  24. "github.com/imgproxy/imgproxy/v3/svg"
  25. "github.com/imgproxy/imgproxy/v3/vips"
  26. )
  27. var (
  28. queueSem *semaphore.Semaphore
  29. processingSem *semaphore.Semaphore
  30. headerVaryValue string
  31. )
  32. func initProcessingHandler() {
  33. if config.RequestsQueueSize > 0 {
  34. queueSem = semaphore.New(config.RequestsQueueSize + config.Concurrency)
  35. }
  36. processingSem = semaphore.New(config.Concurrency)
  37. vary := make([]string, 0)
  38. if config.EnableWebpDetection || config.EnforceWebp || config.EnableAvifDetection || config.EnforceAvif {
  39. vary = append(vary, "Accept")
  40. }
  41. if config.EnableClientHints {
  42. vary = append(vary, "Sec-CH-DPR", "DPR", "Sec-CH-Width", "Width")
  43. }
  44. headerVaryValue = strings.Join(vary, ", ")
  45. }
  46. func setCacheControl(rw http.ResponseWriter, force *time.Time, originHeaders map[string]string) {
  47. var cacheControl, expires string
  48. var ttl int
  49. if force != nil {
  50. rw.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", int(time.Until(*force).Seconds())))
  51. rw.Header().Set("Expires", force.Format(http.TimeFormat))
  52. return
  53. }
  54. if config.CacheControlPassthrough && originHeaders != nil {
  55. if val, ok := originHeaders["Cache-Control"]; ok && len(val) > 0 {
  56. cacheControl = val
  57. }
  58. if val, ok := originHeaders["Expires"]; ok && len(val) > 0 {
  59. expires = val
  60. }
  61. }
  62. if len(cacheControl) == 0 && len(expires) == 0 {
  63. ttl = config.TTL
  64. if _, ok := originHeaders["Fallback-Image"]; ok && config.FallbackImageTTL > 0 {
  65. ttl = config.FallbackImageTTL
  66. }
  67. cacheControl = fmt.Sprintf("max-age=%d, public", ttl)
  68. expires = time.Now().Add(time.Second * time.Duration(ttl)).Format(http.TimeFormat)
  69. }
  70. if len(cacheControl) > 0 {
  71. rw.Header().Set("Cache-Control", cacheControl)
  72. }
  73. if len(expires) > 0 {
  74. rw.Header().Set("Expires", expires)
  75. }
  76. }
  77. func setLastModified(rw http.ResponseWriter, originHeaders map[string]string) {
  78. if config.LastModifiedEnabled {
  79. if val, ok := originHeaders["Last-Modified"]; ok && len(val) != 0 {
  80. rw.Header().Set("Last-Modified", val)
  81. }
  82. }
  83. }
  84. func setVary(rw http.ResponseWriter) {
  85. if len(headerVaryValue) > 0 {
  86. rw.Header().Set("Vary", headerVaryValue)
  87. }
  88. }
  89. func setCanonical(rw http.ResponseWriter, originURL string) {
  90. if config.SetCanonicalHeader {
  91. if strings.HasPrefix(originURL, "https://") || strings.HasPrefix(originURL, "http://") {
  92. linkHeader := fmt.Sprintf(`<%s>; rel="canonical"`, originURL)
  93. rw.Header().Set("Link", linkHeader)
  94. }
  95. }
  96. }
  97. func respondWithImage(reqID string, r *http.Request, rw http.ResponseWriter, statusCode int, resultData *imagedata.ImageData, po *options.ProcessingOptions, originURL string, originData *imagedata.ImageData) {
  98. var contentDisposition string
  99. if len(po.Filename) > 0 {
  100. contentDisposition = resultData.Type.ContentDisposition(po.Filename, po.ReturnAttachment)
  101. } else {
  102. contentDisposition = resultData.Type.ContentDispositionFromURL(originURL, po.ReturnAttachment)
  103. }
  104. rw.Header().Set("Content-Type", resultData.Type.Mime())
  105. rw.Header().Set("Content-Disposition", contentDisposition)
  106. setCacheControl(rw, po.Expires, originData.Headers)
  107. setLastModified(rw, originData.Headers)
  108. setVary(rw)
  109. setCanonical(rw, originURL)
  110. if config.EnableDebugHeaders {
  111. rw.Header().Set("X-Origin-Content-Length", strconv.Itoa(len(originData.Data)))
  112. rw.Header().Set("X-Origin-Width", resultData.Headers["X-Origin-Width"])
  113. rw.Header().Set("X-Origin-Height", resultData.Headers["X-Origin-Height"])
  114. rw.Header().Set("X-Result-Width", resultData.Headers["X-Result-Width"])
  115. rw.Header().Set("X-Result-Height", resultData.Headers["X-Result-Height"])
  116. }
  117. rw.Header().Set("Content-Security-Policy", "script-src 'none'")
  118. rw.Header().Set("Content-Length", strconv.Itoa(len(resultData.Data)))
  119. rw.WriteHeader(statusCode)
  120. rw.Write(resultData.Data)
  121. router.LogResponse(
  122. reqID, r, statusCode, nil,
  123. log.Fields{
  124. "image_url": originURL,
  125. "processing_options": po,
  126. },
  127. )
  128. }
  129. func respondWithNotModified(reqID string, r *http.Request, rw http.ResponseWriter, po *options.ProcessingOptions, originURL string, originHeaders map[string]string) {
  130. setCacheControl(rw, po.Expires, originHeaders)
  131. setVary(rw)
  132. rw.WriteHeader(304)
  133. router.LogResponse(
  134. reqID, r, 304, nil,
  135. log.Fields{
  136. "image_url": originURL,
  137. "processing_options": po,
  138. },
  139. )
  140. }
  141. func sendErrAndPanic(ctx context.Context, errType string, err error) {
  142. send := true
  143. if ierr, ok := err.(*ierrors.Error); ok {
  144. switch ierr.StatusCode {
  145. case http.StatusServiceUnavailable:
  146. errType = "timeout"
  147. case 499:
  148. // Don't need to send a "request cancelled" error
  149. send = false
  150. }
  151. }
  152. if send {
  153. metrics.SendError(ctx, errType, err)
  154. }
  155. panic(err)
  156. }
  157. func checkErr(ctx context.Context, errType string, err error) {
  158. if err == nil {
  159. return
  160. }
  161. sendErrAndPanic(ctx, errType, err)
  162. }
  163. func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
  164. stats.IncRequestsInProgress()
  165. defer stats.DecRequestsInProgress()
  166. ctx := r.Context()
  167. if queueSem != nil {
  168. token, aquired := queueSem.TryAquire()
  169. if !aquired {
  170. panic(ierrors.New(429, "Too many requests", "Too many requests"))
  171. }
  172. defer token.Release()
  173. }
  174. path := r.RequestURI
  175. if queryStart := strings.IndexByte(path, '?'); queryStart >= 0 {
  176. path = path[:queryStart]
  177. }
  178. if len(config.PathPrefix) > 0 {
  179. path = strings.TrimPrefix(path, config.PathPrefix)
  180. }
  181. path = strings.TrimPrefix(path, "/")
  182. signature := ""
  183. if signatureEnd := strings.IndexByte(path, '/'); signatureEnd > 0 {
  184. signature = path[:signatureEnd]
  185. path = path[signatureEnd:]
  186. } else {
  187. sendErrAndPanic(ctx, "path_parsing", ierrors.New(
  188. 404, fmt.Sprintf("Invalid path: %s", path), "Invalid URL",
  189. ))
  190. }
  191. path = fixPath(path)
  192. if err := security.VerifySignature(signature, path); err != nil {
  193. sendErrAndPanic(ctx, "security", ierrors.New(403, err.Error(), "Forbidden"))
  194. }
  195. po, imageURL, err := options.ParsePath(path, r.Header)
  196. checkErr(ctx, "path_parsing", err)
  197. err = security.VerifySourceURL(imageURL)
  198. checkErr(ctx, "security", err)
  199. if po.Raw {
  200. streamOriginImage(ctx, reqID, r, rw, po, imageURL)
  201. return
  202. }
  203. // SVG is a special case. Though saving to svg is not supported, SVG->SVG is.
  204. if !vips.SupportsSave(po.Format) && po.Format != imagetype.Unknown && po.Format != imagetype.SVG {
  205. sendErrAndPanic(ctx, "path_parsing", ierrors.New(
  206. 422,
  207. fmt.Sprintf("Resulting image format is not supported: %s", po.Format),
  208. "Invalid URL",
  209. ))
  210. }
  211. imgRequestHeader := make(http.Header)
  212. var etagHandler etag.Handler
  213. if config.ETagEnabled {
  214. etagHandler.ParseExpectedETag(r.Header.Get("If-None-Match"))
  215. if etagHandler.SetActualProcessingOptions(po) {
  216. if imgEtag := etagHandler.ImageEtagExpected(); len(imgEtag) != 0 {
  217. imgRequestHeader.Set("If-None-Match", imgEtag)
  218. }
  219. }
  220. }
  221. if config.LastModifiedEnabled {
  222. if modifiedSince := r.Header.Get("If-Modified-Since"); len(modifiedSince) != 0 {
  223. imgRequestHeader.Set("If-Modified-Since", modifiedSince)
  224. }
  225. }
  226. // The heavy part start here, so we need to restrict concurrency
  227. var processingSemToken *semaphore.Token
  228. func() {
  229. defer metrics.StartQueueSegment(ctx)()
  230. var aquired bool
  231. processingSemToken, aquired = processingSem.Aquire(ctx)
  232. if !aquired {
  233. // We don't actually need to check timeout here,
  234. // but it's an easy way to check if this is an actual timeout
  235. // or the request was cancelled
  236. checkErr(ctx, "queue", router.CheckTimeout(ctx))
  237. }
  238. }()
  239. defer processingSemToken.Release()
  240. stats.IncImagesInProgress()
  241. defer stats.DecImagesInProgress()
  242. statusCode := http.StatusOK
  243. originData, err := func() (*imagedata.ImageData, error) {
  244. defer metrics.StartDownloadingSegment(ctx)()
  245. downloadOpts := imagedata.DownloadOptions{
  246. Header: imgRequestHeader,
  247. CookieJar: nil,
  248. }
  249. if config.CookiePassthrough {
  250. downloadOpts.CookieJar, err = cookies.JarFromRequest(r)
  251. checkErr(ctx, "download", err)
  252. }
  253. return imagedata.Download(ctx, imageURL, "source image", downloadOpts, po.SecurityOptions)
  254. }()
  255. if err == nil {
  256. defer originData.Close()
  257. } else if nmErr, ok := err.(*imagedata.ErrorNotModified); ok {
  258. if config.ETagEnabled && len(etagHandler.ImageEtagExpected()) != 0 {
  259. rw.Header().Set("ETag", etagHandler.GenerateExpectedETag())
  260. }
  261. respondWithNotModified(reqID, r, rw, po, imageURL, nmErr.Headers)
  262. return
  263. } else {
  264. ierr, ierrok := err.(*ierrors.Error)
  265. if ierrok {
  266. statusCode = ierr.StatusCode
  267. }
  268. if config.ReportDownloadingErrors && (!ierrok || ierr.Unexpected) {
  269. errorreport.Report(err, r)
  270. }
  271. metrics.SendError(ctx, "download", err)
  272. if imagedata.FallbackImage == nil {
  273. panic(err)
  274. }
  275. log.Warningf("Could not load image %s. Using fallback image. %s", imageURL, err.Error())
  276. if config.FallbackImageHTTPCode > 0 {
  277. statusCode = config.FallbackImageHTTPCode
  278. }
  279. originData = imagedata.FallbackImage
  280. }
  281. checkErr(ctx, "timeout", router.CheckTimeout(ctx))
  282. if config.ETagEnabled && statusCode == http.StatusOK {
  283. imgDataMatch := etagHandler.SetActualImageData(originData)
  284. rw.Header().Set("ETag", etagHandler.GenerateActualETag())
  285. if imgDataMatch && etagHandler.ProcessingOptionsMatch() {
  286. respondWithNotModified(reqID, r, rw, po, imageURL, originData.Headers)
  287. return
  288. }
  289. }
  290. checkErr(ctx, "timeout", router.CheckTimeout(ctx))
  291. if originData.Type == po.Format || po.Format == imagetype.Unknown {
  292. // Don't process SVG
  293. if originData.Type == imagetype.SVG {
  294. if config.SanitizeSvg {
  295. sanitized, svgErr := svg.Satitize(originData)
  296. checkErr(ctx, "svg_processing", svgErr)
  297. // Since we'll replace origin data, it's better to close it to return
  298. // it's buffer to the pool
  299. originData.Close()
  300. originData = sanitized
  301. }
  302. respondWithImage(reqID, r, rw, statusCode, originData, po, imageURL, originData)
  303. return
  304. }
  305. if len(po.SkipProcessingFormats) > 0 {
  306. for _, f := range po.SkipProcessingFormats {
  307. if f == originData.Type {
  308. respondWithImage(reqID, r, rw, statusCode, originData, po, imageURL, originData)
  309. return
  310. }
  311. }
  312. }
  313. }
  314. if !vips.SupportsLoad(originData.Type) {
  315. sendErrAndPanic(ctx, "processing", ierrors.New(
  316. 422,
  317. fmt.Sprintf("Source image format is not supported: %s", originData.Type),
  318. "Invalid URL",
  319. ))
  320. }
  321. // At this point we can't allow requested format to be SVG as we can't save SVGs
  322. if po.Format == imagetype.SVG {
  323. sendErrAndPanic(ctx, "processing", ierrors.New(
  324. 422, "Resulting image format is not supported: svg", "Invalid URL",
  325. ))
  326. }
  327. // We're going to rasterize SVG. Since librsvg lacks the support of some SVG
  328. // features, we're going to replace them to minimize rendering error
  329. if originData.Type == imagetype.SVG && config.SvgFixUnsupported {
  330. fixed, changed, svgErr := svg.FixUnsupported(originData)
  331. checkErr(ctx, "svg_processing", svgErr)
  332. if changed {
  333. // Since we'll replace origin data, it's better to close it to return
  334. // it's buffer to the pool
  335. originData.Close()
  336. originData = fixed
  337. }
  338. }
  339. resultData, err := func() (*imagedata.ImageData, error) {
  340. defer metrics.StartProcessingSegment(ctx)()
  341. return processing.ProcessImage(ctx, originData, po)
  342. }()
  343. checkErr(ctx, "processing", err)
  344. defer resultData.Close()
  345. checkErr(ctx, "timeout", router.CheckTimeout(ctx))
  346. respondWithImage(reqID, r, rw, statusCode, resultData, po, imageURL, originData)
  347. }