File: remove_bucket_middleware.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.24.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 554,032 kB
  • sloc: java: 15,941; makefile: 419; sh: 175
file content (63 lines) | stat: -rw-r--r-- 1,843 bytes parent folder | download | duplicates (6)
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
package customizations

import (
	"context"
	"fmt"

	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
	"github.com/aws/smithy-go/middleware"
	"github.com/aws/smithy-go/transport/http"
)

// removeBucketFromPathMiddleware needs to be executed after serialize step is performed
type removeBucketFromPathMiddleware struct {
}

func (m *removeBucketFromPathMiddleware) ID() string {
	return "S3:RemoveBucketFromPathMiddleware"
}

func (m *removeBucketFromPathMiddleware) HandleSerialize(
	ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler,
) (
	out middleware.SerializeOutput, metadata middleware.Metadata, err error,
) {
	if !awsmiddleware.GetRequiresLegacyEndpoints(ctx) {
		return next.HandleSerialize(ctx, in)
	}

	// check if a bucket removal from HTTP path is required
	bucket, ok := getRemoveBucketFromPath(ctx)
	if !ok {
		return next.HandleSerialize(ctx, in)
	}

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

	removeBucketFromPath(req.URL, bucket)
	return next.HandleSerialize(ctx, in)
}

type removeBucketKey struct {
	bucket string
}

// setBucketToRemoveOnContext sets the bucket name to be removed.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func setBucketToRemoveOnContext(ctx context.Context, bucket string) context.Context {
	return middleware.WithStackValue(ctx, removeBucketKey{}, bucket)
}

// getRemoveBucketFromPath returns the bucket name to remove from the path.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func getRemoveBucketFromPath(ctx context.Context) (string, bool) {
	v, ok := middleware.GetStackValue(ctx, removeBucketKey{}).(string)
	return v, ok
}