Skip to content

Commit 0352d99

Browse files
committed
Fix awslabs#14 by using bytes.Buffer to store the response body
1 parent 5aaec67 commit 0352d99

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

core/response.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package core
44

55
import (
6+
"bytes"
67
"encoding/base64"
78
"errors"
89
"net/http"
@@ -17,7 +18,7 @@ const defaultStatusCode = -1
1718
// necessary to return an events.APIGatewayProxyResponse object
1819
type ProxyResponseWriter struct {
1920
headers http.Header
20-
body []byte
21+
body bytes.Buffer
2122
status int
2223
}
2324

@@ -41,12 +42,11 @@ func (r *ProxyResponseWriter) Header() http.Header {
4142
// was set before with the WriteHeader method it sets the status
4243
// for the response to 200 OK.
4344
func (r *ProxyResponseWriter) Write(body []byte) (int, error) {
44-
r.body = body
4545
if r.status == -1 {
4646
r.status = http.StatusOK
4747
}
4848

49-
return len(body), nil
49+
return (&r.body).Write(body)
5050
}
5151

5252
// WriteHeader sets a status code for the response. This method is used
@@ -67,10 +67,12 @@ func (r *ProxyResponseWriter) GetProxyResponse() (events.APIGatewayProxyResponse
6767
var output string
6868
isBase64 := false
6969

70-
if utf8.Valid(r.body) {
71-
output = string(r.body)
70+
bb := (&r.body).Bytes()
71+
72+
if utf8.Valid(bb) {
73+
output = string(bb)
7274
} else {
73-
output = base64.StdEncoding.EncodeToString(r.body)
75+
output = base64.StdEncoding.EncodeToString(bb)
7476
isBase64 = true
7577
}
7678

0 commit comments

Comments
 (0)