forked from monzo/typhon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
198 lines (180 loc) · 5.26 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
package typhon
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/monzo/terrors"
)
// A Response is Typhon's wrapper around http.Response, used by both clients and servers.
//
// Note that Typhon makes no guarantees that a Response is safe to access or mutate concurrently. If a single Response
// object is to be used by multiple goroutines concurrently, callers must make sure to properly synchronise accesses.
type Response struct {
*http.Response
Error error
Request *Request // The Request that we are responding to
hijacked bool
}
// Encode serialises the passed object as JSON into the body (and sets appropriate headers).
func (r *Response) Encode(v interface{}) {
if r.Response == nil {
r.Response = newHTTPResponse(Request{})
}
// If we were given an io.ReadCloser or an io.Reader (that is not also a json.Marshaler), use it directly
switch v := v.(type) {
case json.Marshaler:
case io.ReadCloser:
r.Body = v
r.ContentLength = -1
return
case io.Reader:
r.Body = ioutil.NopCloser(v)
r.ContentLength = -1
return
}
if err := json.NewEncoder(r).Encode(v); err != nil {
r.Error = terrors.Wrap(err, nil)
return
}
r.Header.Set("Content-Type", "application/json")
}
// WrapDownstreamErrors is a context key that can be used to enable
// wrapping of downstream response errors on a per-request basis.
//
// This is implemented as a context key to allow us to migrate individual
// services from the old behaviour to the new behaviour without adding a
// dependency on config to Typhon.
type WrapDownstreamErrors struct{}
// Decode de-serialises the JSON body into the passed object.
func (r *Response) Decode(v interface{}) error {
if r.Error != nil {
if r.Request != nil && r.Request.Context != nil {
if s, ok := r.Request.Context.Value(WrapDownstreamErrors{}).(string); ok && s != "" {
return terrors.NewInternalWithCause(r.Error, "Downstream request error", nil, "downstream")
}
}
return r.Error
}
err := error(nil)
if r.Response == nil {
err = terrors.InternalService("", "Response has no body", nil)
} else {
var b []byte
b, err = r.BodyBytes(true)
if err == nil {
err = json.Unmarshal(b, v)
}
err = terrors.WrapWithCode(err, nil, terrors.ErrBadResponse)
}
if r.Error == nil {
r.Error = err
}
return err
}
// Write writes the passed bytes to the response's body.
func (r *Response) Write(b []byte) (n int, err error) {
if r.Response == nil {
r.Response = newHTTPResponse(Request{})
}
switch rc := r.Body.(type) {
// In the "regular" case, the response body will be a bufCloser; we can write
case io.Writer:
n, err = rc.Write(b)
if err != nil {
return n, err
}
// If a caller manually sets Response.Body, then we may not be able to write to it. In that case, we need to be
// cleverer.
default:
buf := &bufCloser{}
if rc != nil {
if _, err := io.Copy(buf, rc); err != nil {
// This can be quite bad; we have consumed (and possibly lost) some of the original body
return 0, err
}
// rc will never again be accessible: once it's copied it must be closed
rc.Close()
}
r.Body = buf
n, err = buf.Write(b)
if err != nil {
return n, err
}
}
if r.ContentLength >= 0 {
r.ContentLength += int64(n)
// If this write pushed the content length above the chunking threshold,
// set to -1 (unknown) to trigger chunked encoding
if r.ContentLength >= chunkThreshold {
r.ContentLength = -1
}
}
return n, nil
}
// BodyBytes fully reads the response body and returns the bytes read. If consume is false, the body is copied into a
// new buffer such that it may be read again.
func (r *Response) BodyBytes(consume bool) ([]byte, error) {
if consume {
defer r.Body.Close()
return ioutil.ReadAll(r.Body)
}
switch rc := r.Body.(type) {
case *bufCloser:
return rc.Bytes(), nil
default:
buf := &bufCloser{}
r.Body = buf
rdr := io.TeeReader(rc, buf)
// rc will never again be accessible: once it's copied it must be closed
defer rc.Close()
return ioutil.ReadAll(rdr)
}
}
// Writer returns a ResponseWriter which can be used to populate the response.
//
// This is useful when you want to use another HTTP library that is used to wrapping net/http directly. For example,
// it allows a Typhon Service to use a http.Handler internally.
func (r *Response) Writer() ResponseWriter {
if r.Request != nil && r.Request.hijacker != nil {
return hijackerRw{
responseWriterWrapper: responseWriterWrapper{
r: r},
Hijacker: r.Request.hijacker}
}
return responseWriterWrapper{
r: r}
}
func (r Response) String() string {
b := new(bytes.Buffer)
fmt.Fprint(b, "Response(")
if r.Response != nil {
fmt.Fprintf(b, "%d", r.StatusCode)
} else {
fmt.Fprint(b, "???")
}
if r.Error != nil {
fmt.Fprintf(b, ", error: %v", r.Error)
}
fmt.Fprint(b, ")")
return b.String()
}
func newHTTPResponse(req Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK, // Seems like a reasonable default
Proto: req.Proto,
ProtoMajor: req.ProtoMajor,
ProtoMinor: req.ProtoMinor,
ContentLength: 0,
Header: make(http.Header, 5),
Body: &bufCloser{}}
}
// NewResponse constructs a Response
func NewResponse(req Request) Response {
return Response{
Request: &req,
Error: nil,
Response: newHTTPResponse(req)}
}