-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.go
119 lines (105 loc) · 3.27 KB
/
lambda.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
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
// LambdaResponse is the wrapped response from the lambda function that needs to be transformed into an http.Response.
type LambdaResponse struct {
Body string `json:"body"`
Cookies []string `json:"cookies"`
Headers map[string]string `json:"headers"`
IsBase64Encoded bool `json:"isBase64Encoded"`
MultiValueHeaders []map[string]string `json:"multiValueHeaders"`
StatusCode int `json:"statusCode"`
}
// transformLambdaBody puts the body into the response
func transformLambdaBody(resp *http.Response) error {
rbody := strings.Builder{}
if resp.Body != nil {
_, err := io.Copy(&rbody, resp.Body)
if err != nil {
log.Printf("transformLambdaBody err=%v\n", err)
return err
}
resp.Body.Close()
}
body := rbody.String()
lambdaResp := &LambdaResponse{}
err := json.Unmarshal([]byte(body), &lambdaResp)
if err != nil {
log.Printf("ratproxy: transformLambdaBody json.Unmarshal err=%v\n", err)
return err
}
resp.StatusCode = lambdaResp.StatusCode
var bbody []byte
if lambdaResp.IsBase64Encoded {
bbody = make([]byte, base64.StdEncoding.DecodedLen(len(lambdaResp.Body)))
n, err := base64.StdEncoding.Decode(bbody, []byte(lambdaResp.Body))
if err != nil {
log.Printf("ratproxy: transformLambdaBody error %v\n", err)
return err
}
bbody = bbody[:n]
} else {
bbody = []byte(lambdaResp.Body)
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(bbody))
for k, v := range lambdaResp.Headers {
resp.Header.Set(k, v)
}
for _, c := range lambdaResp.Cookies {
resp.Header.Set("Set-Cookie", c)
}
resp.ContentLength = int64(len(bbody))
resp.Header.Set("Content-Length", fmt.Sprintf("%v", len(bbody)))
return nil
}
// proxyTargetLambda treats the target as a lambda function.
func proxyTargetLambda(ctx context.Context, target *Target) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
targetServer, err := url.Parse(target.Target)
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(targetServer)
proxy.Director = func(req *http.Request) {
req.Header = r.Header
req.Host = targetServer.Host
req.URL.Scheme = targetServer.Scheme
req.URL.Host = targetServer.Host
req.Method = http.MethodPost
data := make(map[string]interface{})
data["rawPath"] = req.URL.Path
data["cookies"] = req.Header["Cookie"]
if r.Method == http.MethodPost {
data["requestContext"] = map[string]interface{}{"http": map[string]interface{}{"method": "POST"}}
posted, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("ratproxy: could not read posted body %v\n", err)
posted = []byte("{}")
}
data["body"] = string(posted)
}
b, err := json.Marshal(data)
if err != nil {
panic(err)
}
req.URL.Path = "/2015-03-31/functions/function/invocations"
req.Body = ioutil.NopCloser(bytes.NewBuffer(b))
req.ContentLength = int64(len(b))
log.Printf("ratproxy: %v %v: %v %v\n", r.Method, target.Name, req.URL.Path, string(b))
}
proxy.ModifyResponse = transformLambdaBody
proxy.ServeHTTP(w, r)
}
}