handler.go 4.1 KB

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