-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer_lambdaurl.go
81 lines (71 loc) · 1.87 KB
/
transformer_lambdaurl.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
package delta
import (
"bytes"
"context"
"encoding/base64"
"io"
"net/http"
"net/url"
"github.com/aws/aws-lambda-go/events"
json "github.com/json-iterator/go"
)
type LambdaURLTransformer struct{}
func WithLambdaURL() Options {
return WithTransformer(LambdaURLTransformer{})
}
// Response implements Transformer
func (LambdaURLTransformer) Response(ctx context.Context, w *ResponseWriter) ([]byte, error) {
res := &events.LambdaFunctionURLResponse{
StatusCode: w.status,
Headers: convertFromHttpHeader(w.header),
IsBase64Encoded: w.encode,
Body: w.bodyString(),
}
return json.Marshal(res)
}
// Request implements Transformer
func (LambdaURLTransformer) Request(ctx context.Context, payload []byte) (*http.Request, error) {
var e events.LambdaFunctionURLRequest
json.Unmarshal(payload, &e)
header := convertToHttpHeader(e.Headers)
host := header.Get("host")
var body []byte
if e.IsBase64Encoded {
var err error
body, err = base64.StdEncoding.DecodeString(e.Body)
if err != nil {
return nil, err
}
} else {
body = []byte(e.Body)
}
u := url.URL{
Path: e.RequestContext.HTTP.Path,
RawPath: e.RawPath,
Host: host,
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(bytes.NewReader(body)),
// from header
ContentLength: int64(len(body)),
TransferEncoding: []string{},
Close: true,
Host: host,
RemoteAddr: header.Get("x-forwarded-for"),
}
return withContextEvent(&req, ctx, &e), nil
}
func GetLambdaUrlEvent(ctx context.Context) (*events.LambdaFunctionURLRequest, error) {
return getEvent[*events.LambdaFunctionURLRequest](ctx)
}