forked from infobloxopen/atlas-app-toolkit
/
response.go
251 lines (213 loc) · 8.71 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
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
package gateway
import (
"context"
"fmt"
"io"
"net/http"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
)
type (
// ForwardResponseMessageFunc forwards gRPC response to HTTP client inaccordance with REST API Syntax
ForwardResponseMessageFunc func(context.Context, *runtime.ServeMux, runtime.Marshaler, http.ResponseWriter, *http.Request, proto.Message, ...func(context.Context, http.ResponseWriter, proto.Message) error)
// ForwardResponseStreamFunc forwards gRPC stream response to HTTP client inaccordance with REST API Syntax
ForwardResponseStreamFunc func(context.Context, *runtime.ServeMux, runtime.Marshaler, http.ResponseWriter, *http.Request, func() (proto.Message, error), ...func(context.Context, http.ResponseWriter, proto.Message) error)
)
// ResponseForwarder implements ForwardResponseMessageFunc in method ForwardMessage
// and ForwardResponseStreamFunc in method ForwardStream
// in accordance with REST API Syntax Specification.
// See: https://github.com/infobloxopen/atlas-app-toolkit#responses
// for format of JSON response.
type ResponseForwarder struct {
OutgoingHeaderMatcher runtime.HeaderMatcherFunc
MessageErrHandler runtime.ProtoErrorHandlerFunc
StreamErrHandler ProtoStreamErrorHandlerFunc
}
var (
// ForwardResponseMessage is default implementation of ForwardResponseMessageFunc
ForwardResponseMessage = NewForwardResponseMessage(PrefixOutgoingHeaderMatcher, ProtoMessageErrorHandler, ProtoStreamErrorHandler)
// ForwardResponseStream is default implementation of ForwardResponseStreamFunc
ForwardResponseStream = NewForwardResponseStream(PrefixOutgoingHeaderMatcher, ProtoMessageErrorHandler, ProtoStreamErrorHandler)
)
// NewForwardResponseMessage returns ForwardResponseMessageFunc
func NewForwardResponseMessage(out runtime.HeaderMatcherFunc, meh runtime.ProtoErrorHandlerFunc, seh ProtoStreamErrorHandlerFunc) ForwardResponseMessageFunc {
fw := &ResponseForwarder{out, meh, seh}
return fw.ForwardMessage
}
// NewForwardResponseStream returns ForwardResponseStreamFunc
func NewForwardResponseStream(out runtime.HeaderMatcherFunc, meh runtime.ProtoErrorHandlerFunc, seh ProtoStreamErrorHandlerFunc) ForwardResponseStreamFunc {
fw := &ResponseForwarder{out, meh, seh}
return fw.ForwardStream
}
// ForwardMessage implements runtime.ForwardResponseMessageFunc
func (fw *ResponseForwarder) ForwardMessage(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, rw http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error) {
md, ok := runtime.ServerMetadataFromContext(ctx)
if !ok {
grpclog.Printf("forward response message: failed to extract ServerMetadata from context")
fw.MessageErrHandler(ctx, mux, marshaler, rw, req, fmt.Errorf("forward response message: internal error"))
}
handleForwardResponseServerMetadata(fw.OutgoingHeaderMatcher, rw, md)
handleForwardResponseTrailerHeader(rw, md)
rw.Header().Set("Content-Type", marshaler.ContentType())
if err := handleForwardResponseOptions(ctx, rw, resp, opts); err != nil {
fw.MessageErrHandler(ctx, mux, marshaler, rw, req, err)
return
}
// here we start doing a bit strange things
// 1. marshal response into bytes
// 2. unmarshal bytes into dynamic map[string]interface{}
// 3. add our custom metadata into dynamic map
// 4. marshal dynamic map into bytes again :\
// all that steps are needed because of this requirements:
// -- To allow compatibility with existing systems,
// -- the results tag name can be changed to a service-defined tag.
// -- In this way the success data becomes just a tag added to an existing structure.
data, err := marshaler.Marshal(resp)
if err != nil {
grpclog.Printf("forward response: failed to marshal response: %v", err)
fw.MessageErrHandler(ctx, mux, marshaler, rw, req, err)
}
var dynmap map[string]interface{}
if err := marshaler.Unmarshal(data, &dynmap); err != nil {
grpclog.Printf("forward response: failed to unmarshal response: %v", err)
fw.MessageErrHandler(ctx, mux, marshaler, rw, req, err)
}
retainFields(ctx, req, dynmap)
// FIXME: standard grpc JSON marshaller doesn't handle
// nil values inside maps.
for k := range dynmap {
dynmap[k] = fixNilValues(dynmap[k])
}
// Here we set "Location" header which contains a url to a long running task
// Using it we can retrieve its status
rst := Status(ctx, nil)
if rst.Code == CodeName(LongRunning) {
location, exists := Header(ctx, "Location")
if !exists || location == "" {
err := fmt.Errorf("Header Location should be set for long running operation")
grpclog.Printf("forward response: %v", err)
fw.MessageErrHandler(ctx, mux, marshaler, rw, req, err)
}
rw.Header().Add("Location", location)
}
// this is the edge case, if user sends response that has field 'success'
// let him see his response object instead of our status
if _, ok := dynmap["success"]; !ok {
dynmap["success"] = rst
}
data, err = marshaler.Marshal(dynmap)
if err != nil {
grpclog.Printf("forward response: failed to marshal response: %v", err)
fw.MessageErrHandler(ctx, mux, marshaler, rw, req, err)
}
rw.WriteHeader(rst.HTTPStatus)
if _, err = rw.Write(data); err != nil {
grpclog.Printf("forward response: failed to write response: %v", err)
}
handleForwardResponseTrailer(rw, md)
}
// ForwardStream implements runtime.ForwardResponseStreamFunc.
// RestStatus comes first in the chuncked result.
func (fw *ResponseForwarder) ForwardStream(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, rw http.ResponseWriter, req *http.Request, recv func() (proto.Message, error), opts ...func(context.Context, http.ResponseWriter, proto.Message) error) {
flusher, ok := rw.(http.Flusher)
if !ok {
grpclog.Printf("forward response stream: flush not supported in %T", rw)
fw.StreamErrHandler(ctx, false, mux, marshaler, rw, req, fmt.Errorf("forward response message: internal error"))
return
}
md, ok := runtime.ServerMetadataFromContext(ctx)
if !ok {
grpclog.Printf("forward response stream: failed to extract ServerMetadata from context")
fw.StreamErrHandler(ctx, false, mux, marshaler, rw, req, fmt.Errorf("forward response message: internal error"))
return
}
handleForwardResponseServerMetadata(fw.OutgoingHeaderMatcher, rw, md)
rw.Header().Set("Transfer-Encoding", "chunked")
rw.Header().Set("Content-Type", marshaler.ContentType())
if err := handleForwardResponseOptions(ctx, rw, nil, opts); err != nil {
fw.StreamErrHandler(ctx, false, mux, marshaler, rw, req, err)
return
}
rst := Status(ctx, nil)
// if user did not set status explicitly
if rst.Code == "" || rst.Code == CodeName(codes.OK) {
rst.Code = CodeName(PartialContent)
}
if rst.HTTPStatus == http.StatusOK {
rst.HTTPStatus = HTTPStatusFromCode(PartialContent)
}
v := map[string]interface{}{"success": rst}
rw.WriteHeader(rst.HTTPStatus)
data, err := marshaler.Marshal(v)
if err != nil {
fw.StreamErrHandler(ctx, true, mux, marshaler, rw, req, err)
return
}
if _, err := rw.Write(data); err != nil {
grpclog.Printf("forward response stream: failed to write status object: %s", err)
return
}
var delimiter []byte
if d, ok := marshaler.(runtime.Delimited); ok {
delimiter = d.Delimiter()
} else {
delimiter = []byte("\n")
}
for {
resp, err := recv()
if err == io.EOF {
return
}
if err != nil {
fw.StreamErrHandler(ctx, true, mux, marshaler, rw, req, err)
return
}
if err := handleForwardResponseOptions(ctx, rw, resp, opts); err != nil {
fw.StreamErrHandler(ctx, true, mux, marshaler, rw, req, err)
return
}
data, err := marshaler.Marshal(resp)
if err != nil {
fw.StreamErrHandler(ctx, true, mux, marshaler, rw, req, err)
return
}
if _, err := rw.Write(data); err != nil {
grpclog.Printf("forward response stream: failed to write response object: %s", err)
return
}
if _, err = rw.Write(delimiter); err != nil {
grpclog.Printf("forward response stream: failed to send delimiter chunk: %v", err)
return
}
flusher.Flush()
}
}
func handleForwardResponseOptions(ctx context.Context, rw http.ResponseWriter, resp proto.Message, opts []func(context.Context, http.ResponseWriter, proto.Message) error) error {
if len(opts) == 0 {
return nil
}
for _, opt := range opts {
if err := opt(ctx, rw, resp); err != nil {
grpclog.Printf("error handling ForwardResponseOptions: %v", err)
return err
}
}
return nil
}
// fixNilValues function walks v tree and removes
// map keys that have value nil.
func fixNilValues(v interface{}) interface{} {
switch v := v.(type) {
case map[string]interface{}:
for k := range v {
if v[k] == nil {
delete(v, k)
} else {
v[k] = fixNilValues(v[k])
}
}
}
return v
}