aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/aws-sdk-go-v2/service/internal/s3shared/s3100continue.go
blob: 0f43ec0d4febd0f2b7186783f1179decaff93e53 (plain) (blame)
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
package s3shared

import (
	"context"
	"fmt"
	"github.com/aws/smithy-go/middleware"
	smithyhttp "github.com/aws/smithy-go/transport/http"
)

const s3100ContinueID = "S3100Continue"
const default100ContinueThresholdBytes int64 = 1024 * 1024 * 2

// Add100Continue add middleware, which adds {Expect: 100-continue} header for s3 client HTTP PUT request larger than 2MB
// or with unknown size streaming bodies, during operation builder step
func Add100Continue(stack *middleware.Stack, continueHeaderThresholdBytes int64) error {
	return stack.Build.Add(&s3100Continue{
		continueHeaderThresholdBytes: continueHeaderThresholdBytes,
	}, middleware.After)
}

type s3100Continue struct {
	continueHeaderThresholdBytes int64
}

// ID returns the middleware identifier
func (m *s3100Continue) ID() string {
	return s3100ContinueID
}

func (m *s3100Continue) HandleBuild(
	ctx context.Context, in middleware.BuildInput, next middleware.BuildHandler,
) (
	out middleware.BuildOutput, metadata middleware.Metadata, err error,
) {
	sizeLimit := default100ContinueThresholdBytes
	switch {
	case m.continueHeaderThresholdBytes == -1:
		return next.HandleBuild(ctx, in)
	case m.continueHeaderThresholdBytes > 0:
		sizeLimit = m.continueHeaderThresholdBytes
	default:
	}

	req, ok := in.Request.(*smithyhttp.Request)
	if !ok {
		return out, metadata, fmt.Errorf("unknown request type %T", req)
	}

	if req.ContentLength == -1 || (req.ContentLength == 0 && req.Body != nil) || req.ContentLength >= sizeLimit {
		req.Header.Set("Expect", "100-continue")
	}

	return next.HandleBuild(ctx, in)
}