handler.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  33. // Handler handles image processing requests
  34. type Handler struct {
  35. HandlerContext
  36. stream *stream.Handler // Stream handler for raw image streaming
  37. config *Config // Handler configuration
  38. }
  39. // New creates new handler object
  40. func New(
  41. hCtx HandlerContext,
  42. stream *stream.Handler,
  43. config *Config,
  44. ) (*Handler, error) {
  45. if err := config.Validate(); err != nil {
  46. return nil, err
  47. }
  48. return &Handler{
  49. HandlerContext: hCtx,
  50. config: config,
  51. stream: stream,
  52. }, nil
  53. }
  54. // Execute handles the image processing request
  55. func (h *Handler) Execute(
  56. reqID string,
  57. rw server.ResponseWriter,
  58. req *http.Request,
  59. ) error {
  60. // Increment the number of requests in progress
  61. h.Monitoring().Stats().IncRequestsInProgress()
  62. defer h.Monitoring().Stats().DecRequestsInProgress()
  63. ctx := req.Context()
  64. // Verify URL signature and extract image url and processing options
  65. imageURL, o, mm, err := h.newRequest(ctx, req)
  66. if err != nil {
  67. return err
  68. }
  69. // if processing options indicate raw image streaming, stream it and return
  70. if o.GetBool(keys.Raw, false) {
  71. return h.stream.Execute(ctx, req, imageURL, reqID, o, rw)
  72. }
  73. hReq := &request{
  74. HandlerContext: h,
  75. reqID: reqID,
  76. req: req,
  77. rw: rw,
  78. config: h.config,
  79. opts: o,
  80. secops: h.Security().NewOptions(o),
  81. imageURL: imageURL,
  82. monitoringMeta: mm,
  83. }
  84. return hReq.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. req *http.Request,
  90. ) (string, *options.Options, monitoring.Meta, error) {
  91. // let's extract signature and valid request path from a request
  92. path, signature, err := handlers.SplitPathSignature(req)
  93. if err != nil {
  94. return "", nil, nil, err
  95. }
  96. // verify the signature (if any)
  97. if err = h.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. o, imageURL, err := h.OptionsParser().ParsePath(path, req.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.MetaOptions: o.Map(),
  111. }
  112. // set error reporting and monitoring context
  113. errorreport.SetMetadata(req, "Source Image URL", imageURL)
  114. errorreport.SetMetadata(req, "Source Image Origin", imageOrigin)
  115. errorreport.SetMetadata(req, "Options", o.NestedMap())
  116. h.Monitoring().SetMetadata(ctx, mm)
  117. // verify that image URL came from the valid source
  118. err = h.Security().VerifySourceURL(imageURL)
  119. if err != nil {
  120. return "", options.New(), mm, ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategorySecurity))
  121. }
  122. return imageURL, o, 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. }