handler.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package processing
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "github.com/imgproxy/imgproxy/v3/auximageprovider"
  7. "github.com/imgproxy/imgproxy/v3/errorreport"
  8. "github.com/imgproxy/imgproxy/v3/handlers"
  9. "github.com/imgproxy/imgproxy/v3/handlers/stream"
  10. "github.com/imgproxy/imgproxy/v3/headerwriter"
  11. "github.com/imgproxy/imgproxy/v3/ierrors"
  12. "github.com/imgproxy/imgproxy/v3/imagedata"
  13. "github.com/imgproxy/imgproxy/v3/monitoring"
  14. "github.com/imgproxy/imgproxy/v3/monitoring/stats"
  15. "github.com/imgproxy/imgproxy/v3/options"
  16. "github.com/imgproxy/imgproxy/v3/security"
  17. "github.com/imgproxy/imgproxy/v3/semaphores"
  18. )
  19. // Handler handles image processing requests
  20. type Handler struct {
  21. hw *headerwriter.Writer // Configured HeaderWriter instance
  22. stream *stream.Handler // Stream handler for raw image streaming
  23. config *Config // Handler configuration
  24. semaphores *semaphores.Semaphores
  25. fallbackImage auximageprovider.Provider
  26. watermarkImage auximageprovider.Provider
  27. idf *imagedata.Factory
  28. }
  29. // New creates new handler object
  30. func New(
  31. stream *stream.Handler,
  32. hw *headerwriter.Writer,
  33. semaphores *semaphores.Semaphores,
  34. fi auximageprovider.Provider,
  35. wi auximageprovider.Provider,
  36. idf *imagedata.Factory,
  37. config *Config,
  38. ) (*Handler, error) {
  39. if err := config.Validate(); err != nil {
  40. return nil, err
  41. }
  42. return &Handler{
  43. hw: hw,
  44. config: config,
  45. stream: stream,
  46. semaphores: semaphores,
  47. fallbackImage: fi,
  48. watermarkImage: wi,
  49. idf: idf,
  50. }, nil
  51. }
  52. // Execute handles the image processing request
  53. func (h *Handler) Execute(
  54. reqID string,
  55. rw http.ResponseWriter,
  56. imageRequest *http.Request,
  57. ) error {
  58. // Increment the number of requests in progress
  59. stats.IncRequestsInProgress()
  60. defer stats.DecRequestsInProgress()
  61. ctx := imageRequest.Context()
  62. // Verify URL signature and extract image url and processing options
  63. imageURL, po, mm, err := h.newRequest(ctx, imageRequest)
  64. if err != nil {
  65. return err
  66. }
  67. // if processing options indicate raw image streaming, stream it and return
  68. if po.Raw {
  69. return h.stream.Execute(ctx, imageRequest, imageURL, reqID, po, rw)
  70. }
  71. req := &request{
  72. handler: h,
  73. imageRequest: imageRequest,
  74. reqID: reqID,
  75. rw: rw,
  76. config: h.config,
  77. po: po,
  78. imageURL: imageURL,
  79. monitoringMeta: mm,
  80. semaphores: h.semaphores,
  81. hwr: h.hw.NewRequest(),
  82. idf: h.idf,
  83. }
  84. return req.execute(ctx)
  85. }
  86. // newRequest extracts image url and processing options from request URL and verifies them
  87. func (h *Handler) newRequest(
  88. ctx context.Context,
  89. imageRequest *http.Request,
  90. ) (string, *options.ProcessingOptions, monitoring.Meta, error) {
  91. // let's extract signature and valid request path from a request
  92. path, signature, err := handlers.SplitPathSignature(imageRequest)
  93. if err != nil {
  94. return "", nil, nil, err
  95. }
  96. // verify the signature (if any)
  97. if err = security.VerifySignature(signature, path); err != nil {
  98. return "", nil, nil, ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategorySecurity))
  99. }
  100. // parse image url and processing options
  101. po, imageURL, err := options.ParsePath(path, imageRequest.Header)
  102. if err != nil {
  103. return "", nil, nil, ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategoryPathParsing))
  104. }
  105. // get image origin and create monitoring meta object
  106. imageOrigin := imageOrigin(imageURL)
  107. mm := monitoring.Meta{
  108. monitoring.MetaSourceImageURL: imageURL,
  109. monitoring.MetaSourceImageOrigin: imageOrigin,
  110. monitoring.MetaProcessingOptions: po.Diff().Flatten(),
  111. }
  112. // set error reporting and monitoring context
  113. errorreport.SetMetadata(imageRequest, "Source Image URL", imageURL)
  114. errorreport.SetMetadata(imageRequest, "Source Image Origin", imageOrigin)
  115. errorreport.SetMetadata(imageRequest, "Processing Options", po)
  116. monitoring.SetMetadata(ctx, mm)
  117. // verify that image URL came from the valid source
  118. err = security.VerifySourceURL(imageURL)
  119. if err != nil {
  120. return "", nil, mm, ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategorySecurity))
  121. }
  122. return imageURL, po, mm, nil
  123. }
  124. // imageOrigin extracts image origin from URL
  125. func imageOrigin(imageURL string) string {
  126. if u, uerr := url.Parse(imageURL); uerr == nil {
  127. return u.Scheme + "://" + u.Host
  128. }
  129. return ""
  130. }