processing_handler.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. select {
  107. case processingSem <- struct{}{}:
  108. case <-ctx.Done():
  109. panic(newError(499, "Request was cancelled before processing", "Cancelled"))
  110. }
  111. defer func() { <-processingSem }()
  112. ctx, timeoutCancel := context.WithTimeout(ctx, time.Duration(conf.WriteTimeout)*time.Second)
  113. defer timeoutCancel()
  114. ctx, err := parsePath(ctx, r)
  115. if err != nil {
  116. panic(err)
  117. }
  118. ctx, downloadcancel, err := downloadImage(ctx)
  119. defer downloadcancel()
  120. if err != nil {
  121. if newRelicEnabled {
  122. sendErrorToNewRelic(ctx, err)
  123. }
  124. if prometheusEnabled {
  125. incrementPrometheusErrorsTotal("download")
  126. }
  127. if fallbackImage == nil {
  128. panic(err)
  129. }
  130. if ierr, ok := err.(*imgproxyError); !ok || ierr.Unexpected {
  131. reportError(err, r)
  132. }
  133. logWarning("Could not load image. Using fallback image: %s", err.Error())
  134. ctx = context.WithValue(ctx, imageDataCtxKey, fallbackImage)
  135. }
  136. checkTimeout(ctx)
  137. if conf.ETagEnabled {
  138. eTag := calcETag(ctx)
  139. rw.Header().Set("ETag", eTag)
  140. if eTag == r.Header.Get("If-None-Match") {
  141. respondWithNotModified(ctx, reqID, r, rw)
  142. return
  143. }
  144. }
  145. checkTimeout(ctx)
  146. if len(conf.SkipProcessingFormats) > 0 {
  147. imgdata := getImageData(ctx)
  148. po := getProcessingOptions(ctx)
  149. if imgdata.Type == po.Format || po.Format == imageTypeUnknown {
  150. for _, f := range conf.SkipProcessingFormats {
  151. if f == imgdata.Type {
  152. po.Format = imgdata.Type
  153. respondWithImage(ctx, reqID, r, rw, imgdata.Data)
  154. return
  155. }
  156. }
  157. }
  158. }
  159. imageData, processcancel, err := processImage(ctx)
  160. defer processcancel()
  161. if err != nil {
  162. if newRelicEnabled {
  163. sendErrorToNewRelic(ctx, err)
  164. }
  165. if prometheusEnabled {
  166. incrementPrometheusErrorsTotal("processing")
  167. }
  168. panic(err)
  169. }
  170. checkTimeout(ctx)
  171. respondWithImage(ctx, reqID, r, rw, imageData)
  172. }