-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapigwv1.go
146 lines (122 loc) · 3.44 KB
/
apigwv1.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
//go:build !lambdahttpadapter.partial || (lambdahttpadapter.partial && lambdahttpadapter.apigwv1)
package handler
import (
"bytes"
"context"
"encoding/base64"
"github.com/aws/aws-lambda-go/events"
"io"
"net/http"
"net/url"
"strconv"
"unicode/utf8"
)
func convertApiGwV1Request(ctx context.Context, event events.APIGatewayProxyRequest) (*http.Request, error) {
q := make(url.Values)
if len(event.MultiValueQueryStringParameters) > 0 {
for k, values := range event.MultiValueQueryStringParameters {
for _, v := range values {
q.Add(k, v)
}
}
} else if len(event.QueryStringParameters) > 0 {
for k, v := range event.QueryStringParameters {
q.Add(k, v)
}
}
rUrl := buildFullRequestURL(event.RequestContext.DomainName, event.Path, event.RequestContext.Path, q.Encode())
req, err := http.NewRequestWithContext(ctx, event.HTTPMethod, rUrl, getBody(event.Body, event.IsBase64Encoded))
if err != nil {
return nil, err
}
if event.MultiValueHeaders != nil {
for k, values := range event.MultiValueHeaders {
for _, v := range values {
req.Header.Add(k, v)
}
}
} else {
for k, v := range event.Headers {
req.Header.Add(k, v)
}
}
req.Proto = event.RequestContext.Protocol
pMajor, pMinor, ok := http.ParseHTTPVersion(req.Proto)
if ok {
req.ProtoMajor, req.ProtoMinor = pMajor, pMinor
}
req.RemoteAddr = buildRemoteAddr(event.RequestContext.Identity.SourceIP)
req.RequestURI = req.URL.RequestURI()
return req, nil
}
type apiGwV1ResponseWriter struct {
headersWritten bool
contentTypeSet bool
contentLengthSet bool
headers http.Header
body bytes.Buffer
res events.APIGatewayProxyResponse
}
func (w *apiGwV1ResponseWriter) Header() http.Header {
return w.headers
}
func (w *apiGwV1ResponseWriter) Write(p []byte) (int, error) {
w.WriteHeader(http.StatusOK)
return w.body.Write(p)
}
func (w *apiGwV1ResponseWriter) WriteHeader(statusCode int) {
if !w.headersWritten {
w.headersWritten = true
w.res.StatusCode = statusCode
for k, values := range w.headers {
if len(values) == 0 {
w.res.Headers[k] = ""
} else if len(values) == 1 {
w.res.Headers[k] = values[0]
} else {
if w.res.MultiValueHeaders == nil {
w.res.MultiValueHeaders = make(map[string][]string)
}
w.res.MultiValueHeaders[k] = values
}
}
}
}
func handleApiGwV1(ctx context.Context, event events.APIGatewayProxyRequest, adapter AdapterFunc) (events.APIGatewayProxyResponse, error) {
req, err := convertApiGwV1Request(ctx, event)
if err != nil {
var def events.APIGatewayProxyResponse
return def, err
}
w := apiGwV1ResponseWriter{
headers: make(http.Header),
res: events.APIGatewayProxyResponse{
Headers: make(map[string]string),
},
}
if err = adapter(ctx, req, &w); err != nil {
var def events.APIGatewayProxyResponse
return def, err
}
b, err := io.ReadAll(&w.body)
if err != nil {
var def events.APIGatewayProxyResponse
return def, err
}
if !w.contentTypeSet {
w.res.Headers["Content-Type"] = http.DetectContentType(b)
}
if !w.contentLengthSet {
w.res.Headers["Content-Length"] = strconv.Itoa(len(b))
}
if utf8.Valid(b) {
w.res.Body = string(b)
} else {
w.res.IsBase64Encoded = true
w.res.Body = base64.StdEncoding.EncodeToString(b)
}
return w.res, nil
}
func NewAPIGatewayV1Handler(adapter AdapterFunc) func(context.Context, events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return NewHandler(handleApiGwV1, adapter)
}