-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctionurl.go
253 lines (209 loc) · 6.1 KB
/
functionurl.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//go:build !lambdahttpadapter.partial || (lambdahttpadapter.partial && lambdahttpadapter.functionurl)
package handler
import (
"bytes"
"context"
"encoding/base64"
"github.com/aws/aws-lambda-go/events"
"io"
"net/http"
"strconv"
"strings"
"sync/atomic"
"unicode/utf8"
)
func convertFunctionURLRequest(ctx context.Context, event events.LambdaFunctionURLRequest) (*http.Request, error) {
url := buildFullRequestURL(event.RequestContext.DomainName, event.RawPath, event.RequestContext.HTTP.Path, buildQuery(event.RawQueryString, event.QueryStringParameters))
req, err := http.NewRequestWithContext(ctx, event.RequestContext.HTTP.Method, url, getBody(event.Body, event.IsBase64Encoded))
if err != nil {
return nil, err
}
if event.Cookies != nil {
for _, v := range event.Cookies {
req.Header.Add("Cookie", v)
}
}
for k, v := range event.Headers {
req.Header.Add(k, v)
}
req.Proto = event.RequestContext.HTTP.Protocol
pMajor, pMinor, ok := http.ParseHTTPVersion(req.Proto)
if ok {
req.ProtoMajor, req.ProtoMinor = pMajor, pMinor
}
req.RemoteAddr = buildRemoteAddr(event.RequestContext.HTTP.SourceIP)
req.RequestURI = req.URL.RequestURI()
return req, nil
}
// region classic
type functionURLResponseWriter struct {
headersWritten bool
contentTypeSet bool
contentLengthSet bool
headers http.Header
body bytes.Buffer
res events.LambdaFunctionURLResponse
}
func (w *functionURLResponseWriter) Header() http.Header {
return w.headers
}
func (w *functionURLResponseWriter) Write(p []byte) (int, error) {
w.WriteHeader(http.StatusOK)
return w.body.Write(p)
}
func (w *functionURLResponseWriter) WriteHeader(statusCode int) {
if !w.headersWritten {
w.headersWritten = true
w.res.StatusCode = statusCode
for k, values := range w.headers {
if strings.EqualFold("set-cookie", k) {
w.res.Cookies = values
} else {
if len(values) == 0 {
w.res.Headers[k] = ""
} else if len(values) == 1 {
w.res.Headers[k] = values[0]
} else {
w.res.Headers[k] = strings.Join(values, ",")
}
}
if strings.EqualFold("content-type", k) {
w.contentTypeSet = true
} else if strings.EqualFold("content-length", k) {
w.contentLengthSet = true
}
}
}
}
func handleFunctionURL(ctx context.Context, event events.LambdaFunctionURLRequest, adapter AdapterFunc) (events.LambdaFunctionURLResponse, error) {
req, err := convertFunctionURLRequest(ctx, event)
if err != nil {
var def events.LambdaFunctionURLResponse
return def, err
}
w := functionURLResponseWriter{
headers: make(http.Header),
res: events.LambdaFunctionURLResponse{
Headers: make(map[string]string),
Cookies: make([]string, 0),
},
}
if err = adapter(ctx, req, &w); err != nil {
var def events.LambdaFunctionURLResponse
return def, err
}
b, err := io.ReadAll(&w.body)
if err != nil {
var def events.LambdaFunctionURLResponse
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 NewFunctionURLHandler(adapter AdapterFunc) func(context.Context, events.LambdaFunctionURLRequest) (events.LambdaFunctionURLResponse, error) {
return NewHandler(handleFunctionURL, adapter)
}
// endregion
// region streaming
type functionURLStreamingResponseWriter struct {
headers http.Header
headersWritten int32
body io.WriteCloser
resCh chan<- events.LambdaFunctionURLStreamingResponse
}
func (w *functionURLStreamingResponseWriter) Header() http.Header {
return w.headers
}
func (w *functionURLStreamingResponseWriter) Write(p []byte) (int, error) {
w.WriteHeader(http.StatusOK)
return w.body.Write(p)
}
func (w *functionURLStreamingResponseWriter) WriteHeader(statusCode int) {
if atomic.CompareAndSwapInt32(&w.headersWritten, 0, 1) {
pr, pw := io.Pipe()
w.body = pw
headers := make(map[string]string)
cookies := make([]string, 0)
for k, values := range w.headers {
if strings.EqualFold("set-cookie", k) {
cookies = values
} else {
if len(values) == 0 {
headers[k] = ""
} else if len(values) == 1 {
headers[k] = values[0]
} else {
headers[k] = strings.Join(values, ",")
}
}
}
w.resCh <- events.LambdaFunctionURLStreamingResponse{
StatusCode: statusCode,
Headers: headers,
Body: pr,
Cookies: cookies,
}
}
}
func (w *functionURLStreamingResponseWriter) Close() error {
if w.body == nil {
return nil
}
return w.body.Close()
}
func handleFunctionURLStreaming(ctx context.Context, event events.LambdaFunctionURLRequest, adapter AdapterFunc) (*events.LambdaFunctionURLStreamingResponse, error) {
req, err := convertFunctionURLRequest(ctx, event)
if err != nil {
return nil, err
}
resCh := make(chan events.LambdaFunctionURLStreamingResponse)
errCh := make(chan error)
panicCh := make(chan any)
go processRequestFunctionURLStreaming(ctx, req, adapter, resCh, errCh, panicCh)
select {
case res := <-resCh:
return &res, nil
case err = <-errCh:
return nil, err
case panicV := <-panicCh:
panic(panicV)
case <-ctx.Done():
return nil, ctx.Err()
}
}
func processRequestFunctionURLStreaming(ctx context.Context, req *http.Request, adapter AdapterFunc, resCh chan<- events.LambdaFunctionURLStreamingResponse, errCh chan<- error, panicCh chan<- any) {
ctx, cancel := context.WithCancel(ctx)
defer func() {
if panicV := recover(); panicV != nil {
panicCh <- panicV
}
close(panicCh)
close(resCh)
close(errCh)
cancel()
}()
w := functionURLStreamingResponseWriter{
headers: make(http.Header),
headersWritten: 0,
resCh: resCh,
}
defer w.Close()
if err := adapter(ctx, req, &w); err != nil {
errCh <- err
}
}
func NewFunctionURLStreamingHandler(adapter AdapterFunc) func(context.Context, events.LambdaFunctionURLRequest) (*events.LambdaFunctionURLStreamingResponse, error) {
return NewHandler(handleFunctionURLStreaming, adapter)
}
// endregion