forked from awslabs/aws-lambda-go-api-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
120 lines (98 loc) · 3.21 KB
/
response.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
// Package core provides utility methods that help convert proxy events
// into an http.Request and http.ResponseWriter
package core
import (
"bytes"
"encoding/base64"
"errors"
"net/http"
"unicode/utf8"
"github.com/aws/aws-lambda-go/events"
)
const (
defaultStatusCode = -1
contentTypeHeaderKey = "Content-Type"
)
// ProxyResponseWriter implements http.ResponseWriter and adds the method
// necessary to return an events.APIGatewayProxyResponse object
type ProxyResponseWriter struct {
headers http.Header
body bytes.Buffer
status int
observers []chan<- bool
}
// NewProxyResponseWriter returns a new ProxyResponseWriter object.
// The object is initialized with an empty map of headers and a
// status code of -1
func NewProxyResponseWriter() *ProxyResponseWriter {
return &ProxyResponseWriter{
headers: make(http.Header),
status: defaultStatusCode,
observers: make([]chan<- bool, 0),
}
}
func (r *ProxyResponseWriter) CloseNotify() <-chan bool {
ch := make(chan bool, 1)
r.observers = append(r.observers, ch)
return ch
}
func (r *ProxyResponseWriter) notifyClosed() {
for _, v := range r.observers {
v <- true
}
}
// Header implementation from the http.ResponseWriter interface.
func (r *ProxyResponseWriter) Header() http.Header {
return r.headers
}
// Write sets the response body in the object. If no status code
// was set before with the WriteHeader method it sets the status
// for the response to 200 OK.
func (r *ProxyResponseWriter) Write(body []byte) (int, error) {
if r.status == defaultStatusCode {
r.status = http.StatusOK
}
// if the content type header is not set when we write the body we try to
// detect one and set it by default. If the content type cannot be detected
// it is automatically set to "application/octet-stream" by the
// DetectContentType method
if r.Header().Get(contentTypeHeaderKey) == "" {
r.Header().Add(contentTypeHeaderKey, http.DetectContentType(body))
}
return (&r.body).Write(body)
}
// WriteHeader sets a status code for the response. This method is used
// for error responses.
func (r *ProxyResponseWriter) WriteHeader(status int) {
r.status = status
}
// GetProxyResponse converts the data passed to the response writer into
// an events.APIGatewayProxyResponse object.
// Returns a populated proxy response object. If the response is invalid, for example
// has no headers or an invalid status code returns an error.
func (r *ProxyResponseWriter) GetProxyResponse() (events.APIGatewayProxyResponse, error) {
r.notifyClosed()
if r.status == defaultStatusCode {
return events.APIGatewayProxyResponse{}, errors.New("Status code not set on response")
}
var output string
isBase64 := false
bb := (&r.body).Bytes()
if utf8.Valid(bb) {
output = string(bb)
} else {
output = base64.StdEncoding.EncodeToString(bb)
isBase64 = true
}
return events.APIGatewayProxyResponse{
StatusCode: r.status,
MultiValueHeaders: http.Header(r.headers),
Body: output,
IsBase64Encoded: isBase64,
}, nil
}
// Flush implements the http.Flusher interface to allow an HTTP handler to flush
//// buffered data to the client.
//// See [http.Flusher](https://golang.org/pkg/net/http/#Flusher)
func (r *ProxyResponseWriter) Flush() {
}