handler.go 4.4 KB

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