-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer_apigw.go
146 lines (121 loc) · 3.33 KB
/
transformer_apigw.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package delta
import (
"context"
"encoding/base64"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/aws/aws-lambda-go/events"
json "github.com/json-iterator/go"
)
type apigwCommon struct{}
// Response implements Transformer
func (apigwCommon) Response(ctx context.Context, r *ResponseWriter) ([]byte, error) {
res := &events.APIGatewayProxyResponse{
StatusCode: r.status,
Headers: convertFromHttpHeader(r.header),
IsBase64Encoded: r.encode,
Body: r.bodyString(),
}
return json.Marshal(res)
}
type ApiGatewayV2Transformer struct {
apigwCommon
}
func WithAPIGatewayV2() Options {
return WithTransformer(ApiGatewayV2Transformer{})
}
// Request implements Transformer
func (ApiGatewayV2Transformer) Request(ctx context.Context, payload []byte) (*http.Request, error) {
var e events.APIGatewayV2HTTPRequest
err := json.Unmarshal(payload, &e)
if err != nil {
return nil, err
}
var body io.Reader = strings.NewReader(e.Body)
if e.IsBase64Encoded {
body = base64.NewDecoder(base64.StdEncoding, body)
}
header := convertToHttpHeader(e.Headers)
header["Cookie"] = e.Cookies
host := e.RequestContext.DomainName
length, _ := strconv.ParseInt(header.Get("content-length"), 10, 64)
u := &url.URL{
Path: e.RequestContext.HTTP.Path,
Host: host,
RawPath: e.RawPath,
RawQuery: e.RawQueryString,
Scheme: "https",
}
req := http.Request{
RequestURI: u.RequestURI(),
Method: e.RequestContext.HTTP.Method,
URL: u,
// just hardcode it
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
// content
Header: header,
Body: io.NopCloser(body),
// from header
ContentLength: length,
TransferEncoding: []string{},
Close: true,
Host: host,
RemoteAddr: e.RequestContext.HTTP.SourceIP,
}
return withContextEvent(&req, ctx, &e), nil
}
func GetApiGatewayV2Event(ctx context.Context) (*events.APIGatewayV2HTTPRequest, error) {
return getEvent[*events.APIGatewayV2HTTPRequest](ctx)
}
type ApiGatewayV1Transformer struct {
apigwCommon
}
func WithAPIGatewayV1() Options {
return WithTransformer(ApiGatewayV1Transformer{})
}
// Request implements Transformer
func (ApiGatewayV1Transformer) Request(ctx context.Context, payload []byte) (*http.Request, error) {
var e events.APIGatewayProxyRequest
err := json.Unmarshal(payload, &e)
if err != nil {
return nil, err
}
var body io.Reader = strings.NewReader(e.Body)
if e.IsBase64Encoded {
body = base64.NewDecoder(base64.StdEncoding, body)
}
header := convertToHttpHeader(e.Headers)
host := header.Get("host")
u := &url.URL{
Scheme: e.RequestContext.Protocol,
Path: e.Path,
RawQuery: url.Values(e.MultiValueQueryStringParameters).Encode(),
}
req := http.Request{
Method: e.HTTPMethod,
URL: u,
RequestURI: u.RequestURI(),
// just hardcode it
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
// content
Header: header,
Body: io.NopCloser(body),
// from header
ContentLength: int64(len(e.Body)),
TransferEncoding: []string{},
Close: true,
Host: host,
RemoteAddr: e.RequestContext.Identity.SourceIP,
}
return withContextEvent(&req, ctx, &e), nil
}
func GetApiGatewayV1Event(ctx context.Context) (*events.APIGatewayProxyRequest, error) {
return getEvent[*events.APIGatewayProxyRequest](ctx)
}