protocol.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package protocol
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/aws/aws-sdk-go/aws/awserr"
  6. "github.com/aws/aws-sdk-go/aws/request"
  7. )
  8. // RequireHTTPMinProtocol request handler is used to enforce that
  9. // the target endpoint supports the given major and minor HTTP protocol version.
  10. type RequireHTTPMinProtocol struct {
  11. Major, Minor int
  12. }
  13. // Handler will mark the request.Request with an error if the
  14. // target endpoint did not connect with the required HTTP protocol
  15. // major and minor version.
  16. func (p RequireHTTPMinProtocol) Handler(r *request.Request) {
  17. if r.Error != nil || r.HTTPResponse == nil {
  18. return
  19. }
  20. if !strings.HasPrefix(r.HTTPResponse.Proto, "HTTP") {
  21. r.Error = newMinHTTPProtoError(p.Major, p.Minor, r)
  22. }
  23. if r.HTTPResponse.ProtoMajor < p.Major || r.HTTPResponse.ProtoMinor < p.Minor {
  24. r.Error = newMinHTTPProtoError(p.Major, p.Minor, r)
  25. }
  26. }
  27. // ErrCodeMinimumHTTPProtocolError error code is returned when the target endpoint
  28. // did not match the required HTTP major and minor protocol version.
  29. const ErrCodeMinimumHTTPProtocolError = "MinimumHTTPProtocolError"
  30. func newMinHTTPProtoError(major, minor int, r *request.Request) error {
  31. return awserr.NewRequestFailure(
  32. awserr.New("MinimumHTTPProtocolError",
  33. fmt.Sprintf(
  34. "operation requires minimum HTTP protocol of HTTP/%d.%d, but was %s",
  35. major, minor, r.HTTPResponse.Proto,
  36. ),
  37. nil,
  38. ),
  39. r.HTTPResponse.StatusCode, r.RequestID,
  40. )
  41. }