handler.go 4.3 KB

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