1
0

handler.go 3.9 KB

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