transport.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Package transport provides a custom HTTP transport that supports multiple protocols
  2. // such as S3, GCS, ABS, Swift, and local file system.
  3. package transport
  4. import (
  5. "net/http"
  6. "github.com/imgproxy/imgproxy/v3/config"
  7. "github.com/imgproxy/imgproxy/v3/transport/generichttp"
  8. azureTransport "github.com/imgproxy/imgproxy/v3/transport/azure"
  9. fsTransport "github.com/imgproxy/imgproxy/v3/transport/fs"
  10. gcsTransport "github.com/imgproxy/imgproxy/v3/transport/gcs"
  11. s3Transport "github.com/imgproxy/imgproxy/v3/transport/s3"
  12. swiftTransport "github.com/imgproxy/imgproxy/v3/transport/swift"
  13. )
  14. // Transport is a wrapper around http.Transport which allows to track registered protocols
  15. type Transport struct {
  16. transport *http.Transport
  17. schemes map[string]struct{}
  18. }
  19. // NewTransport creates a new HTTP transport with no protocols registered
  20. func NewTransport() (*Transport, error) {
  21. transport, err := generichttp.New(true)
  22. if err != nil {
  23. return nil, err
  24. }
  25. // http and https are always registered
  26. schemes := map[string]struct{}{
  27. "http": {},
  28. "https": {},
  29. }
  30. t := &Transport{
  31. transport,
  32. schemes,
  33. }
  34. err = t.registerAllProtocols()
  35. if err != nil {
  36. return nil, err
  37. }
  38. return t, nil
  39. }
  40. // Transport returns the underlying http.Transport
  41. func (t *Transport) Transport() *http.Transport {
  42. return t.transport
  43. }
  44. // RegisterProtocol registers a new transport protocol with the transport
  45. func (t *Transport) RegisterProtocol(scheme string, rt http.RoundTripper) {
  46. t.transport.RegisterProtocol(scheme, rt)
  47. t.schemes[scheme] = struct{}{}
  48. }
  49. // IsProtocolRegistered checks if a protocol is registered in the transport
  50. func (t *Transport) IsProtocolRegistered(scheme string) bool {
  51. _, ok := t.schemes[scheme]
  52. return ok
  53. }
  54. // RegisterAllProtocols registers all enabled protocols in the given transport
  55. func (t *Transport) registerAllProtocols() error {
  56. if config.LocalFileSystemRoot != "" {
  57. t.RegisterProtocol("local", fsTransport.New())
  58. }
  59. if config.S3Enabled {
  60. if tr, err := s3Transport.New(); err != nil {
  61. return err
  62. } else {
  63. t.RegisterProtocol("s3", tr)
  64. }
  65. }
  66. if config.GCSEnabled {
  67. if tr, err := gcsTransport.New(); err != nil {
  68. return err
  69. } else {
  70. t.RegisterProtocol("gs", tr)
  71. }
  72. }
  73. if config.ABSEnabled {
  74. if tr, err := azureTransport.New(); err != nil {
  75. return err
  76. } else {
  77. t.RegisterProtocol("abs", tr)
  78. }
  79. }
  80. if config.SwiftEnabled {
  81. if tr, err := swiftTransport.New(); err != nil {
  82. return err
  83. } else {
  84. t.RegisterProtocol("swift", tr)
  85. }
  86. }
  87. return nil
  88. }