File: middleware_min_proto.go

package info (click to toggle)
golang-github-aws-smithy-go 1.13.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,228 kB
  • sloc: java: 12,359; xml: 166; sh: 131; makefile: 47
file content (79 lines) | stat: -rw-r--r-- 2,420 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package http

import (
	"context"
	"fmt"
	"github.com/aws/smithy-go/middleware"
	"strings"
)

// MinimumProtocolError is an error type indicating that the established connection did not meet the expected minimum
// HTTP protocol version.
type MinimumProtocolError struct {
	proto              string
	expectedProtoMajor int
	expectedProtoMinor int
}

// Error returns the error message.
func (m *MinimumProtocolError) Error() string {
	return fmt.Sprintf("operation requires minimum HTTP protocol of HTTP/%d.%d, but was %s",
		m.expectedProtoMajor, m.expectedProtoMinor, m.proto)
}

// RequireMinimumProtocol is a deserialization middleware that asserts that the established HTTP connection
// meets the minimum major ad minor version.
type RequireMinimumProtocol struct {
	ProtoMajor int
	ProtoMinor int
}

// AddRequireMinimumProtocol adds the RequireMinimumProtocol middleware to the stack using the provided minimum
// protocol major and minor version.
func AddRequireMinimumProtocol(stack *middleware.Stack, major, minor int) error {
	return stack.Deserialize.Insert(&RequireMinimumProtocol{
		ProtoMajor: major,
		ProtoMinor: minor,
	}, "OperationDeserializer", middleware.Before)
}

// ID returns the middleware identifier string.
func (r *RequireMinimumProtocol) ID() string {
	return "RequireMinimumProtocol"
}

// HandleDeserialize asserts that the established connection is a HTTP connection with the minimum major and minor
// protocol version.
func (r *RequireMinimumProtocol) HandleDeserialize(
	ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler,
) (
	out middleware.DeserializeOutput, metadata middleware.Metadata, err error,
) {
	out, metadata, err = next.HandleDeserialize(ctx, in)
	if err != nil {
		return out, metadata, err
	}

	response, ok := out.RawResponse.(*Response)
	if !ok {
		return out, metadata, fmt.Errorf("unknown transport type: %T", out.RawResponse)
	}

	if !strings.HasPrefix(response.Proto, "HTTP") {
		return out, metadata, &MinimumProtocolError{
			proto:              response.Proto,
			expectedProtoMajor: r.ProtoMajor,
			expectedProtoMinor: r.ProtoMinor,
		}
	}

	if response.ProtoMajor < r.ProtoMajor || response.ProtoMinor < r.ProtoMinor {
		return out, metadata, &MinimumProtocolError{
			proto:              response.Proto,
			expectedProtoMajor: r.ProtoMajor,
			expectedProtoMinor: r.ProtoMinor,
		}
	}

	return out, metadata, err
}