1
0

handler.go 4.2 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/stream"
  9. "github.com/imgproxy/imgproxy/v3/headerwriter"
  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/semaphores"
  17. )
  18. // Handler handles image processing requests
  19. type Handler struct {
  20. hw *headerwriter.Writer // Configured HeaderWriter instance
  21. stream *stream.Handler // Stream handler for raw image streaming
  22. config *Config // Handler configuration
  23. semaphores *semaphores.Semaphores
  24. fallbackImage auximageprovider.Provider
  25. watermarkImage auximageprovider.Provider
  26. imageData *imagedata.Factory
  27. }
  28. // New creates new handler object
  29. func New(
  30. stream *stream.Handler,
  31. hw *headerwriter.Writer,
  32. semaphores *semaphores.Semaphores,
  33. fi auximageprovider.Provider,
  34. wi auximageprovider.Provider,
  35. idf *imagedata.Factory,
  36. config *Config,
  37. ) (*Handler, error) {
  38. if err := config.Validate(); err != nil {
  39. return nil, err
  40. }
  41. return &Handler{
  42. hw: hw,
  43. config: config,
  44. stream: stream,
  45. semaphores: semaphores,
  46. fallbackImage: fi,
  47. watermarkImage: wi,
  48. imageData: idf,
  49. }, nil
  50. }
  51. // Execute handles the image processing request
  52. func (h *Handler) Execute(
  53. reqID string,
  54. rw http.ResponseWriter,
  55. imageRequest *http.Request,
  56. ) error {
  57. // Increment the number of requests in progress
  58. stats.IncRequestsInProgress()
  59. defer stats.DecRequestsInProgress()
  60. ctx := imageRequest.Context()
  61. // Verify URL signature and extract image url and processing options
  62. imageURL, po, mm, err := h.newRequest(ctx, imageRequest)
  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, imageRequest, imageURL, reqID, po, rw)
  69. }
  70. req := &request{
  71. handler: h,
  72. imageRequest: imageRequest,
  73. reqID: reqID,
  74. rw: rw,
  75. config: h.config,
  76. po: po,
  77. imageURL: imageURL,
  78. monitoringMeta: mm,
  79. semaphores: h.semaphores,
  80. hwr: h.hw.NewRequest(),
  81. idf: h.imageData,
  82. }
  83. return req.execute(ctx)
  84. }
  85. // newRequest extracts image url and processing options from request URL and verifies them
  86. func (h *Handler) newRequest(
  87. ctx context.Context,
  88. imageRequest *http.Request,
  89. ) (string, *options.ProcessingOptions, monitoring.Meta, error) {
  90. // let's extract signature and valid request path from a request
  91. path, signature, err := splitPathSignature(imageRequest, h.config)
  92. if err != nil {
  93. return "", nil, nil, err
  94. }
  95. // verify the signature (if any)
  96. if err = security.VerifySignature(signature, path); err != nil {
  97. return "", nil, nil, ierrors.Wrap(err, 0, ierrors.WithCategory(categorySecurity))
  98. }
  99. // parse image url and processing options
  100. po, imageURL, err := options.ParsePath(path, imageRequest.Header)
  101. if err != nil {
  102. return "", nil, nil, ierrors.Wrap(err, 0, ierrors.WithCategory(categoryPathParsing))
  103. }
  104. // get image origin and create monitoring meta object
  105. imageOrigin := imageOrigin(imageURL)
  106. mm := monitoring.Meta{
  107. monitoring.MetaSourceImageURL: imageURL,
  108. monitoring.MetaSourceImageOrigin: imageOrigin,
  109. monitoring.MetaProcessingOptions: po.Diff().Flatten(),
  110. }
  111. // set error reporting and monitoring context
  112. errorreport.SetMetadata(imageRequest, "Source Image URL", imageURL)
  113. errorreport.SetMetadata(imageRequest, "Source Image Origin", imageOrigin)
  114. errorreport.SetMetadata(imageRequest, "Processing Options", po)
  115. monitoring.SetMetadata(ctx, mm)
  116. // verify that image URL came from the valid source
  117. err = security.VerifySourceURL(imageURL)
  118. if err != nil {
  119. return "", nil, mm, ierrors.Wrap(err, 0, ierrors.WithCategory(categorySecurity))
  120. }
  121. return imageURL, po, mm, nil
  122. }
  123. // imageOrigin extracts image origin from URL
  124. func imageOrigin(imageURL string) string {
  125. if u, uerr := url.Parse(imageURL); uerr == nil {
  126. return u.Scheme + "://" + u.Host
  127. }
  128. return ""
  129. }