processing_handler.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. var (
  11. processingSem chan struct{}
  12. headerVaryValue string
  13. fallbackImage *imageData
  14. )
  15. const (
  16. fallbackImageUsedCtxKey = ctxKey("fallbackImageUsed")
  17. )
  18. func initProcessingHandler() error {
  19. var err error
  20. processingSem = make(chan struct{}, conf.Concurrency)
  21. vary := make([]string, 0)
  22. if conf.EnableWebpDetection || conf.EnforceWebp {
  23. vary = append(vary, "Accept")
  24. }
  25. if conf.EnableClientHints {
  26. vary = append(vary, "DPR", "Viewport-Width", "Width")
  27. }
  28. headerVaryValue = strings.Join(vary, ", ")
  29. if fallbackImage, err = getFallbackImageData(); err != nil {
  30. return err
  31. }
  32. return nil
  33. }
  34. func respondWithImage(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter, data []byte) {
  35. po := getProcessingOptions(ctx)
  36. imgdata := getImageData(ctx)
  37. var contentDisposition string
  38. if len(po.Filename) > 0 {
  39. contentDisposition = po.Format.ContentDisposition(po.Filename)
  40. } else {
  41. contentDisposition = po.Format.ContentDispositionFromURL(getImageURL(ctx))
  42. }
  43. rw.Header().Set("Content-Type", po.Format.Mime())
  44. rw.Header().Set("Content-Disposition", contentDisposition)
  45. if conf.SetCanonicalHeader {
  46. origin := getImageURL(ctx)
  47. if strings.HasPrefix(origin, "https://") || strings.HasPrefix(origin, "http://") {
  48. linkHeader := fmt.Sprintf(`<%s>; rel="canonical"`, origin)
  49. rw.Header().Set("Link", linkHeader)
  50. }
  51. }
  52. var cacheControl, expires string
  53. if conf.CacheControlPassthrough && imgdata.Headers != nil {
  54. if val, ok := imgdata.Headers["Cache-Control"]; ok {
  55. cacheControl = val
  56. }
  57. if val, ok := imgdata.Headers["Expires"]; ok {
  58. expires = val
  59. }
  60. }
  61. if len(cacheControl) == 0 && len(expires) == 0 {
  62. cacheControl = fmt.Sprintf("max-age=%d, public", conf.TTL)
  63. expires = time.Now().Add(time.Second * time.Duration(conf.TTL)).Format(http.TimeFormat)
  64. }
  65. if len(cacheControl) > 0 {
  66. rw.Header().Set("Cache-Control", cacheControl)
  67. }
  68. if len(expires) > 0 {
  69. rw.Header().Set("Expires", expires)
  70. }
  71. if len(headerVaryValue) > 0 {
  72. rw.Header().Set("Vary", headerVaryValue)
  73. }
  74. if conf.EnableDebugHeaders {
  75. rw.Header().Set("X-Origin-Content-Length", strconv.Itoa(len(imgdata.Data)))
  76. }
  77. rw.Header().Set("Content-Length", strconv.Itoa(len(data)))
  78. statusCode := 200
  79. if getFallbackImageUsed(ctx) {
  80. statusCode = conf.FallbackImageHTTPCode
  81. }
  82. rw.WriteHeader(statusCode)
  83. rw.Write(data)
  84. imageURL := getImageURL(ctx)
  85. logResponse(reqID, r, statusCode, nil, &imageURL, po)
  86. }
  87. func respondWithNotModified(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter) {
  88. rw.WriteHeader(304)
  89. imageURL := getImageURL(ctx)
  90. logResponse(reqID, r, 304, nil, &imageURL, getProcessingOptions(ctx))
  91. }
  92. func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
  93. ctx := r.Context()
  94. if newRelicEnabled {
  95. var newRelicCancel context.CancelFunc
  96. ctx, newRelicCancel, rw = startNewRelicTransaction(ctx, rw, r)
  97. defer newRelicCancel()
  98. }
  99. if prometheusEnabled {
  100. prometheusRequestsTotal.Inc()
  101. defer startPrometheusDuration(prometheusRequestDuration)()
  102. }
  103. select {
  104. case processingSem <- struct{}{}:
  105. case <-ctx.Done():
  106. panic(newError(499, "Request was cancelled before processing", "Cancelled"))
  107. }
  108. defer func() { <-processingSem }()
  109. ctx, timeoutCancel := context.WithTimeout(ctx, time.Duration(conf.WriteTimeout)*time.Second)
  110. defer timeoutCancel()
  111. ctx, err := parsePath(ctx, r)
  112. if err != nil {
  113. panic(err)
  114. }
  115. ctx, downloadcancel, err := downloadImageCtx(ctx)
  116. defer downloadcancel()
  117. if err != nil {
  118. if newRelicEnabled {
  119. sendErrorToNewRelic(ctx, err)
  120. }
  121. if prometheusEnabled {
  122. incrementPrometheusErrorsTotal("download")
  123. }
  124. if fallbackImage == nil {
  125. panic(err)
  126. }
  127. if ierr, ok := err.(*imgproxyError); !ok || ierr.Unexpected {
  128. reportError(err, r)
  129. }
  130. logWarning("Could not load image. Using fallback image: %s", err.Error())
  131. ctx = setFallbackImageUsedCtx(ctx)
  132. ctx = context.WithValue(ctx, imageDataCtxKey, fallbackImage)
  133. }
  134. checkTimeout(ctx)
  135. if conf.ETagEnabled && !getFallbackImageUsed(ctx) {
  136. eTag := calcETag(ctx)
  137. rw.Header().Set("ETag", eTag)
  138. if eTag == r.Header.Get("If-None-Match") {
  139. respondWithNotModified(ctx, reqID, r, rw)
  140. return
  141. }
  142. }
  143. checkTimeout(ctx)
  144. if len(conf.SkipProcessingFormats) > 0 {
  145. imgdata := getImageData(ctx)
  146. po := getProcessingOptions(ctx)
  147. if imgdata.Type == po.Format || po.Format == imageTypeUnknown {
  148. for _, f := range conf.SkipProcessingFormats {
  149. if f == imgdata.Type {
  150. po.Format = imgdata.Type
  151. respondWithImage(ctx, reqID, r, rw, imgdata.Data)
  152. return
  153. }
  154. }
  155. }
  156. }
  157. imageData, processcancel, err := processImage(ctx)
  158. defer processcancel()
  159. if err != nil {
  160. if newRelicEnabled {
  161. sendErrorToNewRelic(ctx, err)
  162. }
  163. if prometheusEnabled {
  164. incrementPrometheusErrorsTotal("processing")
  165. }
  166. panic(err)
  167. }
  168. checkTimeout(ctx)
  169. respondWithImage(ctx, reqID, r, rw, imageData)
  170. }
  171. func setFallbackImageUsedCtx(ctx context.Context) context.Context {
  172. return context.WithValue(ctx, fallbackImageUsedCtxKey, true)
  173. }
  174. func getFallbackImageUsed(ctx context.Context) bool {
  175. result, _ := ctx.Value(fallbackImageUsedCtxKey).(bool)
  176. return result
  177. }