This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
http.go
149 lines (126 loc) · 4.14 KB
/
http.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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/glassechidna/serverlessish/lambdaruntime"
"github.com/pkg/errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
)
type httpResponseOutput struct {
StatusCode int `json:"statusCode"`
StatusDescription string `json:"statusDescription,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
HeadersMV map[string][]string `json:"multiValueHeaders,omitempty"`
Body string `json:"body"`
IsBase64Encoded bool `json:"isBase64Encoded"`
}
type httpRequestInput struct {
HTTPMethod string `json:"httpMethod"`
Path string `json:"path"`
Headers map[string]string `json:"headers,omitempty"`
HeadersMV map[string][]string `json:"multiValueHeaders,omitempty"`
Query map[string]string `json:"queryStringParameters"`
QueryMV map[string][]string `json:"multiValueQueryStringParameters"`
Body string `json:"body"`
IsBase64Encoded *bool `json:"isBase64Encoded,omitempty"`
RequestContext struct {
Elb struct {
TargetGroupArn string `json:"targetGroupArn"`
} `json:"elb"`
} `json:"requestContext"`
}
func lambdaResponseForHttpResponse(input *lambdaruntime.FunctionNextOutput, resp *http.Response) (*httpResponseOutput, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.WithStack(err)
}
encoded := base64.StdEncoding.EncodeToString(body)
hrInput := &httpRequestInput{}
err = json.Unmarshal(input.Body, &hrInput)
if err != nil {
return nil, errors.WithStack(err)
}
output := &httpResponseOutput{
StatusCode: resp.StatusCode,
Body: encoded,
IsBase64Encoded: true,
}
isALB := len(hrInput.RequestContext.Elb.TargetGroupArn) > 0
if isALB {
output.StatusDescription = resp.Status
}
isSingleValuedHeadersALB := isALB && hrInput.HeadersMV == nil
if isSingleValuedHeadersALB {
output.Headers = map[string]string{}
for name, values := range resp.Header {
output.Headers[name] = values[0]
}
} else {
output.HeadersMV = resp.Header
}
return output, nil
}
func httpRequestForLambdaInvocation(input *lambdaruntime.FunctionNextOutput, port string) (*http.Request, bool, error) {
base := fmt.Sprintf("http://127.0.0.1:%s", port)
hrInput := &httpRequestInput{}
err := json.Unmarshal(input.Body, &hrInput)
if err != nil {
return nil, false, errors.WithStack(err)
}
var req *http.Request
isHttpRequest := false
if hrInput.IsBase64Encoded == nil {
path := strings.TrimPrefix(os.Getenv("LH_INVOKE_PATH"), "/")
if path == "" {
path = "invoke"
}
req, _ = http.NewRequest("POST", fmt.Sprintf("%s/%s", base, path), bytes.NewReader(input.Body))
} else {
isHttpRequest = true
var body io.Reader = strings.NewReader(hrInput.Body)
if *hrInput.IsBase64Encoded {
body = base64.NewDecoder(base64.StdEncoding, body)
}
q := url.Values{}
for name, values := range hrInput.QueryMV {
for _, value := range values {
q.Add(name, value)
}
}
if len(q) == 0 {
for name, value := range hrInput.Query {
q.Add(name, value)
}
}
u, _ := url.Parse(base + hrInput.Path)
u.RawQuery = q.Encode()
req, _ = http.NewRequest(hrInput.HTTPMethod, u.String(), body)
if len(hrInput.HeadersMV) > 0 {
for name, values := range hrInput.HeadersMV {
for _, value := range values {
req.Header.Add(name, value)
}
}
} else {
for name, value := range hrInput.Headers {
req.Header.Add(name, value)
}
}
req.Host = req.Header.Get("Host")
}
req.Header.Set("Lambda-Runtime-Aws-Request-Id", input.RequestId)
req.Header.Set("Lambda-Runtime-Deadline-Ms", input.DeadlineMs)
req.Header.Set("Lambda-Runtime-Invoked-Function-Arn", input.InvokedFunctionArn)
req.Header.Set("Lambda-Runtime-Trace-Id", input.TraceId)
req.Header.Set("X-Amzn-Trace-Id", input.TraceId) // seems useful to add this too
req.Header.Set("Lambda-Runtime-Client-Context", input.ClientContext)
req.Header.Set("Lambda-Runtime-Cognito-Identity", input.CognitoIdentity)
return req, isHttpRequest, nil
}