forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
229 lines (192 loc) · 5.91 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
package proxy
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
opentracing "github.com/opentracing/opentracing-go"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/metrics"
"github.com/zalando/skipper/routing"
)
const unknownHost = "_unknownhost_"
type context struct {
responseWriter http.ResponseWriter
request *http.Request
response *http.Response
route *routing.Route
deprecatedServed bool
servedWithResponse bool // to support the deprecated way independently
pathParams map[string]string
stateBag map[string]interface{}
originalRequest *http.Request
originalResponse *http.Response
outgoingHost string
debugFilterPanics []interface{}
outgoingDebugRequest *http.Request
incomingDebugResponse *http.Response
loopCounter int
startServe time.Time
metrics *filterMetrics
tracer opentracing.Tracer
}
type filterMetrics struct {
prefix string
impl *metrics.Metrics
}
func defaultBody() io.ReadCloser {
return ioutil.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 http.ResponseWriter, r *http.Request, preserveOriginal bool, m *metrics.Metrics) *context {
c := &context{
responseWriter: w,
request: r,
stateBag: make(map[string]interface{}),
outgoingHost: r.Host,
metrics: &filterMetrics{impl: m},
}
if preserveOriginal {
c.originalRequest = cloneRequestMetadata(r)
}
return c
}
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) 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) setMetricsPrefix(prefix string) {
c.metrics.prefix = prefix + ".custom."
}
func (m *filterMetrics) IncCounter(key string) {
m.impl.IncCounter(m.prefix + key)
}
func (m *filterMetrics) MeasureSince(key string, start time.Time) {
m.impl.MeasureSince(m.prefix+key, start)
}