handler.go 3.9 KB

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