s3100continue.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package s3shared
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/aws/smithy-go/middleware"
  6. smithyhttp "github.com/aws/smithy-go/transport/http"
  7. )
  8. const s3100ContinueID = "S3100Continue"
  9. const default100ContinueThresholdBytes int64 = 1024 * 1024 * 2
  10. // Add100Continue add middleware, which adds {Expect: 100-continue} header for s3 client HTTP PUT request larger than 2MB
  11. // or with unknown size streaming bodies, during operation builder step
  12. func Add100Continue(stack *middleware.Stack, continueHeaderThresholdBytes int64) error {
  13. return stack.Build.Add(&s3100Continue{
  14. continueHeaderThresholdBytes: continueHeaderThresholdBytes,
  15. }, middleware.After)
  16. }
  17. type s3100Continue struct {
  18. continueHeaderThresholdBytes int64
  19. }
  20. // ID returns the middleware identifier
  21. func (m *s3100Continue) ID() string {
  22. return s3100ContinueID
  23. }
  24. func (m *s3100Continue) HandleBuild(
  25. ctx context.Context, in middleware.BuildInput, next middleware.BuildHandler,
  26. ) (
  27. out middleware.BuildOutput, metadata middleware.Metadata, err error,
  28. ) {
  29. sizeLimit := default100ContinueThresholdBytes
  30. switch {
  31. case m.continueHeaderThresholdBytes == -1:
  32. return next.HandleBuild(ctx, in)
  33. case m.continueHeaderThresholdBytes > 0:
  34. sizeLimit = m.continueHeaderThresholdBytes
  35. default:
  36. }
  37. req, ok := in.Request.(*smithyhttp.Request)
  38. if !ok {
  39. return out, metadata, fmt.Errorf("unknown request type %T", req)
  40. }
  41. if req.ContentLength == -1 || (req.ContentLength == 0 && req.Body != nil) || req.ContentLength >= sizeLimit {
  42. req.Header.Set("Expect", "100-continue")
  43. }
  44. return next.HandleBuild(ctx, in)
  45. }