/
httpproxy.go
286 lines (244 loc) · 8.5 KB
/
httpproxy.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
/*
* Copyright 2018 Comcast Cable Communications Management, LLC
*
* 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 engines
import (
"bytes"
"io"
"io/ioutil"
"math"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/tricksterproxy/trickster/pkg/cache/status"
"github.com/tricksterproxy/trickster/pkg/proxy/forwarding"
"github.com/tricksterproxy/trickster/pkg/proxy/headers"
"github.com/tricksterproxy/trickster/pkg/proxy/methods"
"github.com/tricksterproxy/trickster/pkg/proxy/params"
"github.com/tricksterproxy/trickster/pkg/proxy/request"
"github.com/tricksterproxy/trickster/pkg/timeseries"
"github.com/tricksterproxy/trickster/pkg/tracing"
tspan "github.com/tricksterproxy/trickster/pkg/tracing/span"
"github.com/tricksterproxy/trickster/pkg/util/log"
"github.com/tricksterproxy/trickster/pkg/util/metrics"
othttptrace "go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/trace"
)
// Reqs is for Progressive Collapsed Forwarding
var reqs sync.Map
// HTTPBlockSize represents 32K of bytes
const HTTPBlockSize = 32 * 1024
// DoProxy proxies an inbound request to its corresponding upstream origin with no caching features
func DoProxy(w io.Writer, r *http.Request, closeResponse bool) *http.Response {
rsc := request.GetResources(r)
oc := rsc.OriginConfig
start := time.Now()
_, span := tspan.NewChildSpan(r.Context(), rsc.Tracer, "ProxyRequest")
if span != nil {
defer span.End()
}
pc := rsc.PathConfig
var elapsed time.Duration
var cacheStatusCode status.LookupStatus
var resp *http.Response
var reader io.ReadCloser
if pc == nil || pc.CollapsedForwardingType != forwarding.CFTypeProgressive ||
!methods.HasBody(r.Method) {
reader, resp, _ = PrepareFetchReader(r)
cacheStatusCode = setStatusHeader(resp.StatusCode, resp.Header)
writer := PrepareResponseWriter(w, resp.StatusCode, resp.Header)
if writer != nil && reader != nil {
io.Copy(writer, reader)
}
} else {
pr := newProxyRequest(r, w)
key := oc.CacheKeyPrefix + "." + pr.DeriveCacheKey(nil, "")
result, ok := reqs.Load(key)
if !ok {
var contentLength int64
reader, resp, contentLength = PrepareFetchReader(r)
cacheStatusCode = setStatusHeader(resp.StatusCode, resp.Header)
writer := PrepareResponseWriter(w, resp.StatusCode, resp.Header)
// Check if we know the content length and if it is less than our max object size.
if contentLength != 0 && contentLength < int64(oc.MaxObjectSizeBytes) {
pcf := NewPCF(resp, contentLength)
reqs.Store(key, pcf)
// Blocks until server completes
grClose := reader != nil && closeResponse
closeResponse = false
go func() {
io.Copy(pcf, reader)
pcf.Close()
reqs.Delete(key)
if grClose {
reader.Close()
}
}()
pcf.AddClient(writer)
}
} else {
pcf, _ := result.(ProgressiveCollapseForwarder)
resp = pcf.GetResp()
writer := PrepareResponseWriter(w, resp.StatusCode, resp.Header)
pcf.AddClient(writer)
}
}
if closeResponse && reader != nil {
reader.Close()
}
elapsed = time.Since(start)
recordResults(r, "HTTPProxy", cacheStatusCode, resp.StatusCode,
r.URL.Path, "", elapsed.Seconds(), nil, resp.Header)
return resp
}
// PrepareResponseWriter prepares a response and returns an io.Writer for the data to be written to.
// Used in Respond.
func PrepareResponseWriter(w io.Writer, code int, header http.Header) io.Writer {
if rw, ok := w.(http.ResponseWriter); ok {
h := rw.Header()
headers.Merge(h, header)
headers.AddResponseHeaders(h)
rw.WriteHeader(code)
return rw
}
return w
}
// PrepareFetchReader prepares an http response and returns io.ReadCloser to
// provide the response data, the response object and the content length.
// Used in Fetch.
func PrepareFetchReader(r *http.Request) (io.ReadCloser, *http.Response, int64) {
rsc := request.GetResources(r)
oc := rsc.OriginConfig
ctx, span := tspan.NewChildSpan(r.Context(), rsc.Tracer, "PrepareFetchReader")
if span != nil {
defer span.End()
}
pc := rsc.PathConfig
var rc io.ReadCloser
headers.AddForwardingHeaders(r, oc.ForwardedHeaders)
if pc != nil {
headers.UpdateHeaders(r.Header, pc.RequestHeaders)
qp, _, _ := params.GetRequestValues(r)
params.UpdateParams(qp, pc.RequestParams)
params.SetRequestValues(r, qp)
}
r.Close = false
r.RequestURI = ""
if rsc.Tracer != nil {
// Processing traces for proxies
// https://www.w3.org/TR/trace-context-1/#alternative-processing
ctx, r = othttptrace.W3C(ctx, r)
othttptrace.Inject(ctx, r)
}
ctx, doSpan := tspan.NewChildSpan(r.Context(), rsc.Tracer, "ProxyRequest")
if doSpan != nil {
defer doSpan.End()
}
// clear the Host header before proxying or it will be forwarded upstream
r.Host = ""
resp, err := oc.HTTPClient.Do(r)
if err != nil {
rsc.Logger.Error("error downloading url", log.Pairs{"url": r.URL.String(), "detail": err.Error()})
// if there is an err and the response is nil, the server could not be reached
// so make a 502 for the downstream response
if resp == nil {
resp = &http.Response{StatusCode: http.StatusBadGateway, Request: r, Header: make(http.Header)}
}
if pc != nil {
headers.UpdateHeaders(resp.Header, pc.ResponseHeaders)
}
if doSpan != nil {
doSpan.AddEvent(
"Failure",
trace.EventOption(trace.WithAttributes(
label.String("error", err.Error()),
label.Int("httpStatus", resp.StatusCode),
)),
)
doSpan.SetStatus(tracing.HTTPToCode(resp.StatusCode), "")
}
return nil, resp, 0
}
originalLen := int64(-1)
if v, ok := resp.Header[headers.NameContentLength]; ok {
originalLen, err = strconv.ParseInt(strings.Join(v, ""), 10, 64)
if err != nil {
originalLen = -1
}
resp.ContentLength = originalLen
}
// warn if the clock between trickster and the origin is off by more than 1 minute
if date := resp.Header.Get(headers.NameDate); date != "" {
d, err := http.ParseTime(date)
if err == nil {
if offset := time.Since(d); time.Duration(math.Abs(float64(offset))) > time.Minute {
rsc.Logger.WarnOnce("clockoffset."+oc.Name,
"clock offset between trickster host and origin is high and may cause data anomalies",
log.Pairs{
"originName": oc.Name,
"tricksterTime": strconv.FormatInt(d.Add(offset).Unix(), 10),
"originTime": strconv.FormatInt(d.Unix(), 10),
"offset": strconv.FormatInt(int64(offset.Seconds()), 10) + "s",
})
}
}
}
hasCustomResponseBody := false
resp.Header.Del(headers.NameContentLength)
if pc != nil {
headers.UpdateHeaders(resp.Header, pc.ResponseHeaders)
hasCustomResponseBody = pc.HasCustomResponseBody
}
if hasCustomResponseBody {
// Since we are not responding with the actual upstream response body, close it here
resp.Body.Close()
rc = ioutil.NopCloser(bytes.NewReader(pc.ResponseBodyBytes))
} else {
rc = resp.Body
}
return rc, resp, originalLen
}
// Respond sends an HTTP Response down to the requesting client
func Respond(w io.Writer, code int, header http.Header, body []byte) {
PrepareResponseWriter(w, code, header)
w.Write(body)
}
func setStatusHeader(httpStatus int, header http.Header) status.LookupStatus {
st := status.LookupStatusProxyOnly
if httpStatus >= http.StatusBadRequest {
st = status.LookupStatusProxyError
}
headers.SetResultsHeader(header, "HTTPProxy", st.String(), "", nil)
return st
}
func recordResults(r *http.Request, engine string, cacheStatus status.LookupStatus,
statusCode int, path, ffStatus string, elapsed float64, extents timeseries.ExtentList, header http.Header) {
rsc := request.GetResources(r)
pc := rsc.PathConfig
oc := rsc.OriginConfig
status := cacheStatus.String()
if pc != nil && !pc.NoMetrics {
httpStatus := strconv.Itoa(statusCode)
metrics.ProxyRequestStatus.WithLabelValues(oc.Name, oc.OriginType, r.Method, status, httpStatus, path).Inc()
if elapsed > 0 {
metrics.ProxyRequestDuration.WithLabelValues(oc.Name, oc.OriginType,
r.Method, status, httpStatus, path).Observe(elapsed)
}
}
headers.SetResultsHeader(header, engine, status, ffStatus, extents)
}