handler.go 4.0 KB

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