-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathraw_response.go
280 lines (255 loc) · 8.82 KB
/
raw_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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2023-2024 The Connect Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package referenceserver
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"connectrpc.com/conformance/internal"
conformancev1 "connectrpc.com/conformance/internal/gen/proto/go/connectrpc/conformance/v1"
"connectrpc.com/conformance/internal/gen/proto/go/connectrpc/conformance/v1/conformancev1connect"
"connectrpc.com/connect"
"google.golang.org/protobuf/proto"
)
var errNoRawResponseHolder = errors.New("reference server needs to use raw response but no RawHTTPResponse holder in context")
var errNonRawResponseStarted = errors.New("reference server needs to use raw response but non-raw response already started")
// rawResponseKey is used to store the raw response that the server
// should send in the context. The value type will be **conformancev1.RawHTTPResponse.
type rawResponseKey struct{}
// rawResponder is HTTP middleware that can send back a raw HTTP response
// if so directed. The handler directs it to do so by storing a raw response
// in a placeholder context value before otherwise interacting with the
// response writer. (If response writer interactions and no raw response has
// been stored, those response writer interactions take precedence and no
// raw response can be sent.)
func rawResponder(handler http.Handler) http.Handler {
return http.HandlerFunc(func(respWriter http.ResponseWriter, req *http.Request) {
snapshotHeaders := respWriter.Header().Clone()
rawResponder := &rawResponseWriter{respWriter: respWriter}
ctx := context.WithValue(req.Context(), rawResponseKey{}, rawResponder)
req = req.WithContext(ctx)
handler.ServeHTTP(rawResponder, req)
rawResponder.finish(snapshotHeaders)
})
}
type rawResponseWriter struct {
respWriter http.ResponseWriter
mu sync.Mutex
rawResp *conformancev1.RawHTTPResponse
startedResponse bool
}
// canSendResponse returns true if the server handler can use the
// http.ResponseWriter methods to send a response. This returns false
// if we will instead be finishing the call with a raw response.
func (r *rawResponseWriter) canSendResponse() bool {
r.mu.Lock()
defer r.mu.Unlock()
if r.startedResponse {
return true
}
if r.rawResp == nil {
r.startedResponse = true
return true
}
return false
}
// rawResponse returns non-nil if the call will be finished with a
// raw response. If it returns nil, nothing need be done to finish
// the call; the server handler was already allowed to send it.
func (r *rawResponseWriter) rawResponse() *conformancev1.RawHTTPResponse {
r.mu.Lock()
defer r.mu.Unlock()
return r.rawResp
}
func (r *rawResponseWriter) setRawResponse(resp *conformancev1.RawHTTPResponse) bool {
r.mu.Lock()
defer r.mu.Unlock()
if r.startedResponse {
return false
}
r.rawResp = resp
return true
}
func (r *rawResponseWriter) Header() http.Header {
return r.respWriter.Header()
}
func (r *rawResponseWriter) Write(bytes []byte) (int, error) {
if r.canSendResponse() {
return r.respWriter.Write(bytes)
}
return len(bytes), nil
}
func (r *rawResponseWriter) WriteHeader(statusCode int) {
if r.canSendResponse() {
r.respWriter.WriteHeader(statusCode)
}
}
func (r *rawResponseWriter) Flush() {
if r.canSendResponse() {
if flusher, ok := r.respWriter.(http.Flusher); ok {
flusher.Flush()
}
}
}
func (r *rawResponseWriter) Unwrap() http.ResponseWriter {
return r.respWriter
}
func (r *rawResponseWriter) finish(snapshotHeaders http.Header) {
resp := r.rawResponse()
if resp == nil {
return
}
// clean any headers that may have been set by the handler
// and restore to the snapshot we initially took (which
// may have headers set by earlier middleware, like CORS)
for k := range r.respWriter.Header() {
delete(r.respWriter.Header(), k)
}
for k, v := range snapshotHeaders {
r.respWriter.Header()[k] = v
}
internal.AddHeaders(resp.Headers, r.respWriter.Header())
r.respWriter.Header()["Date"] = nil // suppress automatic date header
// We must pre-declare trailers to make sure that chunked encoding is used and
// trailers can actually be sent.
for _, hdr := range resp.Trailers {
r.respWriter.Header().Add("Trailer", hdr.Name)
}
statusCode := int(resp.StatusCode)
// If no status code was specified in the raw response, default to 200
if statusCode == 0 {
statusCode = 200
}
r.respWriter.WriteHeader(statusCode)
switch contents := resp.Body.(type) {
case *conformancev1.RawHTTPResponse_Unary:
_ = internal.WriteRawMessageContents(contents.Unary, r.respWriter)
case *conformancev1.RawHTTPResponse_Stream:
_ = internal.WriteRawStreamContents(contents.Stream, r.respWriter)
}
internal.AddTrailers(resp.Trailers, r.respWriter.Header())
}
type rawResponseRecorder struct{}
func (r rawResponseRecorder) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
if msg, ok := req.Any().(*conformancev1.UnaryRequest); ok {
rawResponse := msg.GetResponseDefinition().GetRawResponse()
if rawResponse != nil {
if err := setRawResponse(ctx, rawResponse); err != nil {
return nil, err
}
return nil, connect.NewError(connect.CodeAborted, errors.New("use raw response instead"))
}
}
return next(ctx, req)
}
}
func (r rawResponseRecorder) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
return next
}
func (r rawResponseRecorder) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
return func(ctx context.Context, stream connect.StreamingHandlerConn) error {
var req proto.Message
var rawResponseFunc func() *conformancev1.RawHTTPResponse
switch stream.Spec().Procedure {
case conformancev1connect.ConformanceServiceClientStreamProcedure:
streamReq := &conformancev1.ClientStreamRequest{}
rawResponseFunc = func() *conformancev1.RawHTTPResponse {
return streamReq.GetResponseDefinition().GetRawResponse()
}
req = streamReq
case conformancev1connect.ConformanceServiceServerStreamProcedure:
streamReq := &conformancev1.ServerStreamRequest{}
rawResponseFunc = func() *conformancev1.RawHTTPResponse {
return streamReq.GetResponseDefinition().GetRawResponse()
}
req = streamReq
case conformancev1connect.ConformanceServiceBidiStreamProcedure:
streamReq := &conformancev1.BidiStreamRequest{}
rawResponseFunc = func() *conformancev1.RawHTTPResponse {
return streamReq.GetResponseDefinition().GetRawResponse()
}
req = streamReq
}
var reqErr error
if req == nil {
return next(ctx, stream)
}
reqErr = stream.Receive(req)
if reqErr == nil { //nolint:nestif
rawResponse := rawResponseFunc()
if rawResponse != nil {
if err := setRawResponse(ctx, rawResponse); err != nil {
return err
}
// If we have a raw response, go ahead and drain the request stream
// before sending back the raw response.
// NOTE: This means that raw responses cannot be used with full-duplex
// request definitions.
for {
if err := stream.Receive(req); err != nil {
break
}
}
return connect.NewError(connect.CodeAborted, errors.New("use raw response instead"))
}
}
return next(ctx, &firstReqCachingStream{
StreamingHandlerConn: stream,
request: req,
recvErr: reqErr,
})
}
}
type firstReqCachingStream struct {
connect.StreamingHandlerConn
request any
recvErr error
}
func (str *firstReqCachingStream) Receive(dest any) error {
if str.recvErr != nil {
err := str.recvErr
str.request, str.recvErr = nil, nil
return err
}
if str.request != nil {
destMsg, ok := dest.(proto.Message)
if !ok {
return fmt.Errorf("%T does not implement proto.Message", dest)
}
srcMsg, ok := str.request.(proto.Message)
if !ok {
return fmt.Errorf("%T does not implement proto.Message", dest)
}
proto.Reset(destMsg)
proto.Merge(destMsg, srcMsg)
str.request, str.recvErr = nil, nil
return nil
}
// Otherwise, we've already provided the cached first request.
// So all subsequent receives use the underlying stream.
return str.StreamingHandlerConn.Receive(dest)
}
func setRawResponse(ctx context.Context, resp *conformancev1.RawHTTPResponse) error {
respWriter, ok := ctx.Value(rawResponseKey{}).(*rawResponseWriter)
if !ok {
return errNoRawResponseHolder
}
if !respWriter.setRawResponse(resp) {
return errNonRawResponseStarted
}
return nil
}