-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathapigw_v2.go
90 lines (78 loc) · 2.77 KB
/
apigw_v2.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
package algnhsa
import (
"context"
"encoding/json"
"errors"
"net/http"
"path"
"strings"
"github.com/aws/aws-lambda-go/events"
)
/*
AWS Documentation:
- https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
- https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html
*/
var (
errAPIGatewayV2UnexpectedRequest = errors.New("expected APIGatewayV2HTTPRequest event")
)
func newAPIGatewayV2Request(ctx context.Context, payload []byte, opts *Options) (lambdaRequest, error) {
var event events.APIGatewayV2HTTPRequest
if err := json.Unmarshal(payload, &event); err != nil {
return lambdaRequest{}, err
}
if event.Version != "2.0" {
return lambdaRequest{}, errAPIGatewayV2UnexpectedRequest
}
req := lambdaRequest{
HTTPMethod: event.RequestContext.HTTP.Method,
Path: event.RawPath,
RawQueryString: event.RawQueryString,
Headers: event.Headers,
Body: event.Body,
IsBase64Encoded: event.IsBase64Encoded,
SourceIP: event.RequestContext.HTTP.SourceIP,
Context: context.WithValue(ctx, RequestTypeAPIGatewayV2, event),
requestType: RequestTypeAPIGatewayV2,
}
// APIGatewayV2 doesn't support multi-value headers.
// For cookies there is a workaround - Cookie headers are assigned to the event Cookies slice.
// All other multi-value headers are joined into a single value with a comma.
// It would be unsafe to split such values on a comma - it's impossible to distinguish a multi-value header
// joined with a comma and a single-value header that contains a comma.
if len(event.Cookies) > 0 {
if req.MultiValueHeaders == nil {
req.MultiValueHeaders = make(map[string][]string)
}
req.MultiValueHeaders["Cookie"] = event.Cookies
}
if opts.UseProxyPath {
req.Path = path.Join("/", event.PathParameters["proxy"])
}
return req, nil
}
func newAPIGatewayV2Response(r *http.Response) (lambdaResponse, error) {
resp := lambdaResponse{
Headers: make(map[string]string, len(r.Header)),
}
// APIGatewayV2 doesn't support multi-value headers.
for key, values := range r.Header {
// For cookies there is a workaround - Set-Cookie headers are assigned to the response Cookies slice.
if key == canonicalSetCookieHeaderKey {
resp.Cookies = values
continue
}
// All other multi-value headers are joined into a single value with a comma.
resp.Headers[key] = strings.Join(values, ",")
}
return resp, nil
}
// APIGatewayV2RequestFromContext extracts the APIGatewayV2HTTPRequest event from ctx.
func APIGatewayV2RequestFromContext(ctx context.Context) (events.APIGatewayV2HTTPRequest, bool) {
val := ctx.Value(RequestTypeAPIGatewayV2)
if val == nil {
return events.APIGatewayV2HTTPRequest{}, false
}
event, ok := val.(events.APIGatewayV2HTTPRequest)
return event, ok
}