handler.go 4.2 KB

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