handler.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/headerwriter"
  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/monitoring/stats"
  15. "github.com/imgproxy/imgproxy/v3/options"
  16. "github.com/imgproxy/imgproxy/v3/security"
  17. "github.com/imgproxy/imgproxy/v3/semaphores"
  18. )
  19. // HandlerContext provides access to shared handler dependencies
  20. type HandlerContext interface {
  21. HeaderWriter() *headerwriter.Writer
  22. Semaphores() *semaphores.Semaphores
  23. FallbackImage() auximageprovider.Provider
  24. WatermarkImage() auximageprovider.Provider
  25. ImageDataFactory() *imagedata.Factory
  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 http.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. hwr: h.HeaderWriter().NewRequest(),
  77. }
  78. return hReq.execute(ctx)
  79. }
  80. // newRequest extracts image url and processing options from request URL and verifies them
  81. func (h *Handler) newRequest(
  82. ctx context.Context,
  83. req *http.Request,
  84. ) (string, *options.ProcessingOptions, monitoring.Meta, error) {
  85. // let's extract signature and valid request path from a request
  86. path, signature, err := handlers.SplitPathSignature(req)
  87. if err != nil {
  88. return "", nil, nil, err
  89. }
  90. // verify the signature (if any)
  91. if err = security.VerifySignature(signature, path); err != nil {
  92. return "", nil, nil, ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategorySecurity))
  93. }
  94. // parse image url and processing options
  95. po, imageURL, err := options.ParsePath(path, req.Header)
  96. if err != nil {
  97. return "", nil, nil, ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategoryPathParsing))
  98. }
  99. // get image origin and create monitoring meta object
  100. imageOrigin := imageOrigin(imageURL)
  101. mm := monitoring.Meta{
  102. monitoring.MetaSourceImageURL: imageURL,
  103. monitoring.MetaSourceImageOrigin: imageOrigin,
  104. monitoring.MetaProcessingOptions: po.Diff().Flatten(),
  105. }
  106. // set error reporting and monitoring context
  107. errorreport.SetMetadata(req, "Source Image URL", imageURL)
  108. errorreport.SetMetadata(req, "Source Image Origin", imageOrigin)
  109. errorreport.SetMetadata(req, "Processing Options", po)
  110. monitoring.SetMetadata(ctx, mm)
  111. // verify that image URL came from the valid source
  112. err = security.VerifySourceURL(imageURL)
  113. if err != nil {
  114. return "", nil, mm, ierrors.Wrap(err, 0, ierrors.WithCategory(handlers.CategorySecurity))
  115. }
  116. return imageURL, po, mm, nil
  117. }
  118. // imageOrigin extracts image origin from URL
  119. func imageOrigin(imageURL string) string {
  120. if u, uerr := url.Parse(imageURL); uerr == nil {
  121. return u.Scheme + "://" + u.Host
  122. }
  123. return ""
  124. }