-
Notifications
You must be signed in to change notification settings - Fork 40
/
request.go
126 lines (109 loc) · 3.45 KB
/
request.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package algnhsa
import (
"context"
"encoding/base64"
"errors"
"io"
"net/http"
"net/url"
"strings"
)
var errUnsupportedPayloadFormat = errors.New("unsupported payload format; supported formats: APIGatewayV2HTTPRequest, APIGatewayProxyRequest, ALBTargetGroupRequest")
type lambdaRequest struct {
HTTPMethod string
Path string
QueryStringParameters map[string]string
MultiValueQueryStringParameters map[string][]string
RawQueryString string
Headers map[string]string
MultiValueHeaders map[string][]string
IsBase64Encoded bool
Body string
SourceIP string
Context context.Context
requestType RequestType
}
func newLambdaRequest(ctx context.Context, payload []byte, opts *Options) (lambdaRequest, error) {
switch opts.RequestType {
case RequestTypeAPIGatewayV1:
return newAPIGatewayV1Request(ctx, payload, opts)
case RequestTypeAPIGatewayV2:
return newAPIGatewayV2Request(ctx, payload, opts)
case RequestTypeALB:
return newALBRequest(ctx, payload, opts)
}
// The request type wasn't specified.
// Try to decode the payload as APIGatewayV2HTTPRequest, fall back to APIGatewayProxyRequest, then ALBTargetGroupRequest.
req, err := newAPIGatewayV2Request(ctx, payload, opts)
if err != nil && err != errAPIGatewayV2UnexpectedRequest {
return lambdaRequest{}, err
}
if err == nil {
return req, nil
}
req, err = newAPIGatewayV1Request(ctx, payload, opts)
if err != nil && err != errAPIGatewayV1UnexpectedRequest {
return lambdaRequest{}, err
}
if err == nil {
return req, nil
}
req, err = newALBRequest(ctx, payload, opts)
if err != nil && err != errALBUnexpectedRequest {
return lambdaRequest{}, err
}
if err == nil {
return req, nil
}
return lambdaRequest{}, errUnsupportedPayloadFormat
}
func newHTTPRequest(event lambdaRequest) (*http.Request, error) {
// Build request URL.
rawQuery := event.RawQueryString
if len(rawQuery) == 0 {
params := url.Values{}
for k, v := range event.QueryStringParameters {
params.Set(k, v)
}
for k, vals := range event.MultiValueQueryStringParameters {
params[k] = vals
}
rawQuery = params.Encode()
}
// https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
// If you specify values for both headers and multiValueHeaders, API Gateway V1 merges them into a single list.
// If the same key-value pair is specified in both, only the values from multiValueHeaders will appear
// in the merged list.
headers := make(http.Header)
for k, v := range event.Headers {
headers.Set(k, v)
}
for k, vals := range event.MultiValueHeaders {
headers[http.CanonicalHeaderKey(k)] = vals
}
unescapedPath, err := url.PathUnescape(event.Path)
if err != nil {
return nil, err
}
u := url.URL{
Host: headers.Get("Host"),
Path: unescapedPath,
RawQuery: rawQuery,
}
// Handle base64 encoded body.
var body io.Reader = strings.NewReader(event.Body)
if event.IsBase64Encoded {
body = base64.NewDecoder(base64.StdEncoding, body)
}
// Create a new request.
r, err := http.NewRequestWithContext(event.Context, event.HTTPMethod, u.String(), body)
if err != nil {
return nil, err
}
// Set remote IP address.
r.RemoteAddr = event.SourceIP
// Set request URI
r.RequestURI = u.RequestURI()
r.Header = headers
return r, nil
}