processing_handler.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. var (
  11. responseGzipBufPool *bufPool
  12. responseGzipPool *gzipPool
  13. processingSem chan struct{}
  14. headerVaryValue string
  15. fallbackImage *imageData
  16. )
  17. func initProcessingHandler() error {
  18. var err error
  19. processingSem = make(chan struct{}, conf.Concurrency)
  20. if conf.GZipCompression > 0 {
  21. responseGzipBufPool = newBufPool("gzip", conf.Concurrency, conf.GZipBufferSize)
  22. if responseGzipPool, err = newGzipPool(conf.Concurrency); err != nil {
  23. return err
  24. }
  25. }
  26. vary := make([]string, 0)
  27. if conf.EnableWebpDetection || conf.EnforceWebp {
  28. vary = append(vary, "Accept")
  29. }
  30. if conf.GZipCompression > 0 {
  31. vary = append(vary, "Accept-Encoding")
  32. }
  33. if conf.EnableClientHints {
  34. vary = append(vary, "DPR", "Viewport-Width", "Width")
  35. }
  36. headerVaryValue = strings.Join(vary, ", ")
  37. if fallbackImage, err = getFallbackImageData(); err != nil {
  38. return err
  39. }
  40. return nil
  41. }
  42. func respondWithImage(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter, data []byte) {
  43. po := getProcessingOptions(ctx)
  44. var contentDisposition string
  45. if len(po.Filename) > 0 {
  46. contentDisposition = po.Format.ContentDisposition(po.Filename)
  47. } else {
  48. contentDisposition = po.Format.ContentDispositionFromURL(getImageURL(ctx))
  49. }
  50. rw.Header().Set("Content-Type", po.Format.Mime())
  51. rw.Header().Set("Content-Disposition", contentDisposition)
  52. var cacheControl, expires string
  53. if conf.CacheControlPassthrough {
  54. cacheControl = getCacheControlHeader(ctx)
  55. expires = getExpiresHeader(ctx)
  56. }
  57. if len(cacheControl) == 0 && len(expires) == 0 {
  58. cacheControl = fmt.Sprintf("max-age=%d, public", conf.TTL)
  59. expires = time.Now().Add(time.Second * time.Duration(conf.TTL)).Format(http.TimeFormat)
  60. }
  61. if len(cacheControl) > 0 {
  62. rw.Header().Set("Cache-Control", cacheControl)
  63. }
  64. if len(expires) > 0 {
  65. rw.Header().Set("Expires", expires)
  66. }
  67. if len(headerVaryValue) > 0 {
  68. rw.Header().Set("Vary", headerVaryValue)
  69. }
  70. if conf.GZipCompression > 0 && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  71. buf := responseGzipBufPool.Get(0)
  72. defer responseGzipBufPool.Put(buf)
  73. gz := responseGzipPool.Get(buf)
  74. defer responseGzipPool.Put(gz)
  75. gz.Write(data)
  76. gz.Close()
  77. rw.Header().Set("Content-Encoding", "gzip")
  78. rw.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
  79. rw.WriteHeader(200)
  80. rw.Write(buf.Bytes())
  81. } else {
  82. rw.Header().Set("Content-Length", strconv.Itoa(len(data)))
  83. rw.WriteHeader(200)
  84. rw.Write(data)
  85. }
  86. imageURL := getImageURL(ctx)
  87. logResponse(reqID, r, 200, nil, &imageURL, po)
  88. // logResponse(reqID, r, 200, getTimerSince(ctx), getImageURL(ctx), po))
  89. }
  90. func respondWithNotModified(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter) {
  91. rw.WriteHeader(304)
  92. imageURL := getImageURL(ctx)
  93. logResponse(reqID, r, 304, nil, &imageURL, getProcessingOptions(ctx))
  94. }
  95. func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
  96. ctx := r.Context()
  97. if newRelicEnabled {
  98. var newRelicCancel context.CancelFunc
  99. ctx, newRelicCancel = startNewRelicTransaction(ctx, rw, r)
  100. defer newRelicCancel()
  101. }
  102. if prometheusEnabled {
  103. prometheusRequestsTotal.Inc()
  104. defer startPrometheusDuration(prometheusRequestDuration)()
  105. }
  106. processingSem <- struct{}{}
  107. defer func() { <-processingSem }()
  108. ctx, timeoutCancel := context.WithTimeout(ctx, time.Duration(conf.WriteTimeout)*time.Second)
  109. defer timeoutCancel()
  110. ctx, err := parsePath(ctx, r)
  111. if err != nil {
  112. panic(err)
  113. }
  114. ctx, downloadcancel, err := downloadImage(ctx)
  115. defer downloadcancel()
  116. if err != nil {
  117. if newRelicEnabled {
  118. sendErrorToNewRelic(ctx, err)
  119. }
  120. if prometheusEnabled {
  121. incrementPrometheusErrorsTotal("download")
  122. }
  123. if fallbackImage == nil {
  124. panic(err)
  125. }
  126. if ierr, ok := err.(*imgproxyError); !ok || ierr.Unexpected {
  127. reportError(err, r)
  128. }
  129. logWarning("Could not load image. Using fallback image: %s", err.Error())
  130. ctx = context.WithValue(ctx, imageDataCtxKey, fallbackImage)
  131. }
  132. checkTimeout(ctx)
  133. if conf.ETagEnabled {
  134. eTag := calcETag(ctx)
  135. rw.Header().Set("ETag", eTag)
  136. if eTag == r.Header.Get("If-None-Match") {
  137. respondWithNotModified(ctx, reqID, r, rw)
  138. return
  139. }
  140. }
  141. checkTimeout(ctx)
  142. imageData, processcancel, err := processImage(ctx)
  143. defer processcancel()
  144. if err != nil {
  145. if newRelicEnabled {
  146. sendErrorToNewRelic(ctx, err)
  147. }
  148. if prometheusEnabled {
  149. incrementPrometheusErrorsTotal("processing")
  150. }
  151. panic(err)
  152. }
  153. checkTimeout(ctx)
  154. respondWithImage(ctx, reqID, r, rw, imageData)
  155. }