File: middleware_capture_request.go

package info (click to toggle)
golang-github-aws-smithy-go 1.19.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 2,680 kB
  • sloc: java: 15,917; xml: 166; sh: 131; makefile: 66
file content (47 lines) | stat: -rw-r--r-- 1,223 bytes parent folder | download | duplicates (2)
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
package protocol

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

const captureRequestID = "CaptureProtocolTestRequest"

// AddCaptureRequestMiddleware captures serialized http request during protocol test for check
func AddCaptureRequestMiddleware(stack *middleware.Stack, req *http.Request) error {
	return stack.Build.Add(&captureRequestMiddleware{
		req: req,
	}, middleware.After)
}

type captureRequestMiddleware struct {
	req *http.Request
}

func (*captureRequestMiddleware) ID() string {
	return captureRequestID
}

func (m *captureRequestMiddleware) HandleBuild(ctx context.Context, input middleware.BuildInput, next middleware.BuildHandler,
) (
	output middleware.BuildOutput, metadata middleware.Metadata, err error,
) {
	request, ok := input.Request.(*smithyhttp.Request)
	if !ok {
		return output, metadata, fmt.Errorf("error while retrieving http request")
	}

	*m.req = *request.Build(ctx)
	if len(m.req.URL.RawPath) == 0 {
		m.req.URL.RawPath = m.req.URL.Path
	}
	if v := m.req.ContentLength; v != 0 {
		m.req.Header.Set("Content-Length", strconv.FormatInt(v, 10))
	}

	return next.HandleBuild(ctx, input)
}