processing_handler.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. if conf.EnableDebugHeaders {
  87. imgdata := getImageData(ctx)
  88. rw.Header().Set("X-Origin-Content-Length", strconv.Itoa(len(imgdata.Data)))
  89. }
  90. imageURL := getImageURL(ctx)
  91. logResponse(reqID, r, 200, nil, &imageURL, po)
  92. // logResponse(reqID, r, 200, getTimerSince(ctx), getImageURL(ctx), po))
  93. }
  94. func respondWithNotModified(ctx context.Context, reqID string, r *http.Request, rw http.ResponseWriter) {
  95. rw.WriteHeader(304)
  96. imageURL := getImageURL(ctx)
  97. logResponse(reqID, r, 304, nil, &imageURL, getProcessingOptions(ctx))
  98. }
  99. func handleProcessing(reqID string, rw http.ResponseWriter, r *http.Request) {
  100. ctx := r.Context()
  101. if newRelicEnabled {
  102. var newRelicCancel context.CancelFunc
  103. ctx, newRelicCancel, rw = startNewRelicTransaction(ctx, rw, r)
  104. defer newRelicCancel()
  105. }
  106. if prometheusEnabled {
  107. prometheusRequestsTotal.Inc()
  108. defer startPrometheusDuration(prometheusRequestDuration)()
  109. }
  110. select {
  111. case processingSem <- struct{}{}:
  112. case <-ctx.Done():
  113. panic(newError(499, "Request was cancelled before processing", "Cancelled"))
  114. }
  115. defer func() { <-processingSem }()
  116. ctx, timeoutCancel := context.WithTimeout(ctx, time.Duration(conf.WriteTimeout)*time.Second)
  117. defer timeoutCancel()
  118. ctx, err := parsePath(ctx, r)
  119. if err != nil {
  120. panic(err)
  121. }
  122. ctx, downloadcancel, err := downloadImage(ctx)
  123. defer downloadcancel()
  124. if err != nil {
  125. if newRelicEnabled {
  126. sendErrorToNewRelic(ctx, err)
  127. }
  128. if prometheusEnabled {
  129. incrementPrometheusErrorsTotal("download")
  130. }
  131. if fallbackImage == nil {
  132. panic(err)
  133. }
  134. if ierr, ok := err.(*imgproxyError); !ok || ierr.Unexpected {
  135. reportError(err, r)
  136. }
  137. logWarning("Could not load image. Using fallback image: %s", err.Error())
  138. ctx = context.WithValue(ctx, imageDataCtxKey, fallbackImage)
  139. }
  140. checkTimeout(ctx)
  141. if conf.ETagEnabled {
  142. eTag := calcETag(ctx)
  143. rw.Header().Set("ETag", eTag)
  144. if eTag == r.Header.Get("If-None-Match") {
  145. respondWithNotModified(ctx, reqID, r, rw)
  146. return
  147. }
  148. }
  149. checkTimeout(ctx)
  150. if len(conf.SkipProcessingFormats) > 0 {
  151. imgdata := getImageData(ctx)
  152. po := getProcessingOptions(ctx)
  153. if imgdata.Type == po.Format || po.Format == imageTypeUnknown {
  154. for _, f := range conf.SkipProcessingFormats {
  155. if f == imgdata.Type {
  156. po.Format = imgdata.Type
  157. respondWithImage(ctx, reqID, r, rw, imgdata.Data)
  158. return
  159. }
  160. }
  161. }
  162. }
  163. imageData, processcancel, err := processImage(ctx)
  164. defer processcancel()
  165. if err != nil {
  166. if newRelicEnabled {
  167. sendErrorToNewRelic(ctx, err)
  168. }
  169. if prometheusEnabled {
  170. incrementPrometheusErrorsTotal("processing")
  171. }
  172. panic(err)
  173. }
  174. checkTimeout(ctx)
  175. respondWithImage(ctx, reqID, r, rw, imageData)
  176. }