middleware_min_proto.go (2421B)
1 package http 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/aws/smithy-go/middleware" 9 ) 10 11 // MinimumProtocolError is an error type indicating that the established connection did not meet the expected minimum 12 // HTTP protocol version. 13 type MinimumProtocolError struct { 14 proto string 15 expectedProtoMajor int 16 expectedProtoMinor int 17 } 18 19 // Error returns the error message. 20 func (m *MinimumProtocolError) Error() string { 21 return fmt.Sprintf("operation requires minimum HTTP protocol of HTTP/%d.%d, but was %s", 22 m.expectedProtoMajor, m.expectedProtoMinor, m.proto) 23 } 24 25 // RequireMinimumProtocol is a deserialization middleware that asserts that the established HTTP connection 26 // meets the minimum major ad minor version. 27 type RequireMinimumProtocol struct { 28 ProtoMajor int 29 ProtoMinor int 30 } 31 32 // AddRequireMinimumProtocol adds the RequireMinimumProtocol middleware to the stack using the provided minimum 33 // protocol major and minor version. 34 func AddRequireMinimumProtocol(stack *middleware.Stack, major, minor int) error { 35 return stack.Deserialize.Insert(&RequireMinimumProtocol{ 36 ProtoMajor: major, 37 ProtoMinor: minor, 38 }, "OperationDeserializer", middleware.Before) 39 } 40 41 // ID returns the middleware identifier string. 42 func (r *RequireMinimumProtocol) ID() string { 43 return "RequireMinimumProtocol" 44 } 45 46 // HandleDeserialize asserts that the established connection is a HTTP connection with the minimum major and minor 47 // protocol version. 48 func (r *RequireMinimumProtocol) HandleDeserialize( 49 ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler, 50 ) ( 51 out middleware.DeserializeOutput, metadata middleware.Metadata, err error, 52 ) { 53 out, metadata, err = next.HandleDeserialize(ctx, in) 54 if err != nil { 55 return out, metadata, err 56 } 57 58 response, ok := out.RawResponse.(*Response) 59 if !ok { 60 return out, metadata, fmt.Errorf("unknown transport type: %T", out.RawResponse) 61 } 62 63 if !strings.HasPrefix(response.Proto, "HTTP") { 64 return out, metadata, &MinimumProtocolError{ 65 proto: response.Proto, 66 expectedProtoMajor: r.ProtoMajor, 67 expectedProtoMinor: r.ProtoMinor, 68 } 69 } 70 71 if response.ProtoMajor < r.ProtoMajor || response.ProtoMinor < r.ProtoMinor { 72 return out, metadata, &MinimumProtocolError{ 73 proto: response.Proto, 74 expectedProtoMajor: r.ProtoMajor, 75 expectedProtoMinor: r.ProtoMinor, 76 } 77 } 78 79 return out, metadata, err 80 }