-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathcontext.go
354 lines (303 loc) · 9.38 KB
/
context.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package proxy
import (
"bytes"
stdlibcontext "context"
"errors"
"io"
"net/http"
"net/url"
"time"
"github.com/opentracing/opentracing-go"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/metrics"
"github.com/zalando/skipper/routing"
"github.com/zalando/skipper/tracing"
log "github.com/sirupsen/logrus"
)
const unknownHost = "_unknownhost_"
type flushedResponseWriter interface {
http.ResponseWriter
http.Flusher
Unwrap() http.ResponseWriter
}
type context struct {
responseWriter flushedResponseWriter
request *http.Request
response *http.Response
route *routing.Route
deprecatedServed bool
servedWithResponse bool // to support the deprecated way independently
successfulUpgrade bool
pathParams map[string]string
stateBag map[string]interface{}
originalRequest *http.Request
originalResponse *http.Response
outgoingHost string
outgoingDebugRequest *http.Request
executionCounter int
startServe time.Time
metrics *filterMetrics
tracer opentracing.Tracer
initialSpan opentracing.Span
proxySpan opentracing.Span
parentSpan opentracing.Span
proxy *Proxy
routeLookup *routing.RouteLookup
cancelBackendContext stdlibcontext.CancelFunc
logger filters.FilterContextLogger
}
type filterMetrics struct {
prefix string
impl metrics.Metrics
}
type noopFlushedResponseWriter struct {
ignoredHeader http.Header
}
func defaultBody() io.ReadCloser {
return io.NopCloser(&bytes.Buffer{})
}
func defaultResponse(r *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusNotFound,
Header: make(http.Header),
Body: defaultBody(),
Request: r,
}
}
func cloneURL(u *url.URL) *url.URL {
uc := *u
return &uc
}
func cloneRequestMetadata(r *http.Request) *http.Request {
return &http.Request{
Method: r.Method,
URL: cloneURL(r.URL),
Proto: r.Proto,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Header: cloneHeader(r.Header),
Trailer: cloneHeader(r.Trailer),
Body: defaultBody(),
ContentLength: r.ContentLength,
TransferEncoding: r.TransferEncoding,
Close: r.Close,
Host: r.Host,
RemoteAddr: r.RemoteAddr,
RequestURI: r.RequestURI,
TLS: r.TLS,
}
}
func cloneResponseMetadata(r *http.Response) *http.Response {
return &http.Response{
Status: r.Status,
StatusCode: r.StatusCode,
Proto: r.Proto,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Header: cloneHeader(r.Header),
Trailer: cloneHeader(r.Trailer),
Body: defaultBody(),
ContentLength: r.ContentLength,
TransferEncoding: r.TransferEncoding,
Close: r.Close,
Request: r.Request,
TLS: r.TLS,
}
}
// this is required during looping to preserve the original set of
// params in the outer routes
func appendParams(to, from map[string]string) map[string]string {
if to == nil {
to = make(map[string]string)
}
for k, v := range from {
to[k] = v
}
return to
}
func newContext(
w flushedResponseWriter,
r *http.Request,
p *Proxy,
) *context {
c := &context{
responseWriter: w,
request: r,
stateBag: make(map[string]interface{}),
outgoingHost: r.Host,
metrics: &filterMetrics{impl: p.metrics},
proxy: p,
routeLookup: p.routing.Get(),
}
if p.flags.PreserveOriginal() {
c.originalRequest = cloneRequestMetadata(r)
}
return c
}
func (c *context) ResponseController() *http.ResponseController {
return http.NewResponseController(c.responseWriter)
}
func (c *context) applyRoute(route *routing.Route, params map[string]string, preserveHost bool) {
c.route = route
if preserveHost {
c.outgoingHost = c.request.Host
} else {
c.outgoingHost = route.Host
}
c.pathParams = appendParams(c.pathParams, params)
}
func (c *context) ensureDefaultResponse() {
if c.response == nil {
c.response = defaultResponse(c.request)
return
}
if c.response.Header == nil {
c.response.Header = make(http.Header)
}
if c.response.Body == nil {
c.response.Body = defaultBody()
}
}
func (c *context) deprecatedShunted() bool {
return c.deprecatedServed
}
func (c *context) shunted() bool {
return c.servedWithResponse
}
func (c *context) setResponse(r *http.Response, preserveOriginal bool) {
c.response = r
if preserveOriginal {
c.originalResponse = cloneResponseMetadata(r)
}
}
func (c *context) ResponseWriter() http.ResponseWriter { return c.responseWriter }
func (c *context) Request() *http.Request { return c.request }
func (c *context) Response() *http.Response { return c.response }
func (c *context) MarkServed() { c.deprecatedServed = true }
func (c *context) Served() bool { return c.deprecatedServed || c.servedWithResponse }
func (c *context) PathParam(key string) string { return c.pathParams[key] }
func (c *context) StateBag() map[string]interface{} { return c.stateBag }
func (c *context) BackendUrl() string { return c.route.Backend }
func (c *context) OriginalRequest() *http.Request { return c.originalRequest }
func (c *context) OriginalResponse() *http.Response { return c.originalResponse }
func (c *context) OutgoingHost() string { return c.outgoingHost }
func (c *context) SetOutgoingHost(h string) { c.outgoingHost = h }
func (c *context) Metrics() filters.Metrics { return c.metrics }
func (c *context) Tracer() opentracing.Tracer { return c.tracer }
func (c *context) ParentSpan() opentracing.Span { return c.parentSpan }
func (c *context) Logger() filters.FilterContextLogger {
if c.logger == nil {
traceId := tracing.GetTraceID(c.initialSpan)
if traceId != "" {
c.logger = log.WithFields(log.Fields{"trace_id": traceId})
} else {
c.logger = log.StandardLogger()
}
}
return c.logger
}
func (c *context) Serve(r *http.Response) {
r.Request = c.Request()
if r.Header == nil {
r.Header = make(http.Header)
}
if r.Body == nil {
r.Body = defaultBody()
}
c.servedWithResponse = true
c.response = r
}
func (c *context) metricsHost() string {
if c.route == nil || len(c.route.HostRegexps) == 0 {
return unknownHost
}
return c.request.Host
}
func (c *context) clone() *context {
cc := *c
// preserve the original path params by cloning the set:
cc.pathParams = appendParams(nil, c.pathParams)
return &cc
}
func (c *context) wasExecuted() bool {
return c.executionCounter != 0
}
func (c *context) setMetricsPrefix(prefix string) {
c.metrics.prefix = prefix + ".custom."
}
func (c *context) Split() (filters.FilterContext, error) {
originalRequest := c.Request()
if c.proxy.experimentalUpgrade && isUpgradeRequest(originalRequest) {
return nil, errors.New("context: cannot split the context that contains an upgrade request")
}
cc := c.clone()
cc.stateBag = map[string]interface{}{}
cc.responseWriter = noopFlushedResponseWriter{}
cc.metrics = &filterMetrics{
prefix: cc.metrics.prefix,
impl: cc.proxy.metrics,
}
u := new(url.URL)
*u = *originalRequest.URL
u.Host = originalRequest.Host
cr, body, err := cloneRequestForSplit(u, originalRequest)
if err != nil {
c.Logger().Errorf("context: failed to clone request: %v", err)
return nil, err
}
serverSpan := opentracing.SpanFromContext(originalRequest.Context())
cr = cr.WithContext(opentracing.ContextWithSpan(cr.Context(), serverSpan))
cr = cr.WithContext(routing.NewContext(cr.Context()))
originalRequest.Body = body
cc.request = cr
return cc, nil
}
func (c *context) Loopback() {
loopSpan := c.tracer.StartSpan(c.proxy.tracing.initialOperationName, opentracing.ChildOf(c.parentSpan.Context()))
defer loopSpan.Finish()
err := c.proxy.do(c, loopSpan)
if c.response != nil && c.response.Body != nil {
if _, err := io.Copy(io.Discard, c.response.Body); err != nil {
c.Logger().Errorf("context: error while discarding remainder response body: %v.", err)
}
err := c.response.Body.Close()
if err != nil {
c.Logger().Errorf("context: error during closing the response body: %v", err)
}
}
if c.proxySpan != nil {
c.proxy.tracing.setTag(c.proxySpan, "shadow", "true")
c.proxySpan.Finish()
}
perr, ok := err.(*proxyError)
if ok && perr.handled {
return
}
if err != nil {
c.Logger().Errorf("context: failed to execute loopback request: %v", err)
}
}
func (m *filterMetrics) IncCounter(key string) {
m.impl.IncCounter(m.prefix + key)
}
func (m *filterMetrics) IncCounterBy(key string, value int64) {
m.impl.IncCounterBy(m.prefix+key, value)
}
func (m *filterMetrics) MeasureSince(key string, start time.Time) {
m.impl.MeasureSince(m.prefix+key, start)
}
func (m *filterMetrics) IncFloatCounterBy(key string, value float64) {
m.impl.IncFloatCounterBy(m.prefix+key, value)
}
func (w noopFlushedResponseWriter) Header() http.Header {
if w.ignoredHeader == nil {
w.ignoredHeader = make(http.Header)
}
return w.ignoredHeader
}
func (w noopFlushedResponseWriter) Write([]byte) (int, error) {
return 0, nil
}
func (w noopFlushedResponseWriter) WriteHeader(_ int) {}
func (w noopFlushedResponseWriter) Flush() {}
func (w noopFlushedResponseWriter) Unwrap() http.ResponseWriter { return nil }