-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
624 lines (555 loc) · 16.6 KB
/
proxy.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package jvproxy
import (
"io"
"io/ioutil"
"net"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/seehuhn/httputil"
"github.com/seehuhn/jvproxy/cache"
"github.com/seehuhn/trace"
)
type Proxy struct {
Name string
upstream http.RoundTripper
cache cache.Cache
logger chan<- *LogEntry
AdminMux *http.ServeMux
shared bool
}
func NewProxy(name string, transport http.RoundTripper, cache cache.Cache, shared bool) *Proxy {
if transport == nil {
transport = http.DefaultTransport
}
return &Proxy{
Name: name,
upstream: transport,
cache: cache,
logger: NewLogger(),
AdminMux: http.NewServeMux(),
shared: shared,
}
}
func (proxy *Proxy) Close() error {
return proxy.cache.Close()
}
func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.URL.Host == "" || req.URL.Host == proxy.Name {
proxy.AdminMux.ServeHTTP(w, req)
return
}
requestTime := time.Now()
log := &LogEntry{
RequestTime: requestTime,
RemoteAddr: req.RemoteAddr,
Method: req.Method,
RequestURI: req.RequestURI,
}
defer func() {
log.HandlerCompleteNano =
int64(time.Since(requestTime) / time.Nanosecond)
proxy.logger <- log
}()
if req.Method == "CONNECT" {
dest := req.Host
hj, ok := w.(http.Hijacker)
if !ok {
trace.T("jvproxy/handler", trace.PrioError,
"cannot hijack connection for tunnel to %q",
dest)
code := http.StatusInternalServerError
http.Error(w, http.StatusText(code), code)
log.StatusCode = code
return
}
// check that we can resolve the server in the DNS
destAddr, err := net.ResolveTCPAddr("tcp", dest)
if err != nil {
trace.T("jvproxy/handler", trace.PrioInfo,
"tunnel to %q on behalf of %s failed: %s",
dest, req.RemoteAddr, err.Error())
code := http.StatusNotFound
http.Error(w, http.StatusText(code), code)
log.StatusCode = code
return
}
if destAddr.Port != 80 && destAddr.Port != 443 &&
!strings.HasPrefix(req.RemoteAddr, "127.0.0.1:") {
trace.T("jvproxy/handler", trace.PrioInfo,
"tunnel to %s on behalf of %s not allowed",
destAddr, req.RemoteAddr)
code := http.StatusForbidden
http.Error(w, http.StatusText(code), code)
log.StatusCode = code
return
}
destConn, err := net.DialTCP("tcp", nil, destAddr)
if err != nil {
trace.T("jvproxy/handler", trace.PrioInfo,
"error while connecting to %s for tunnel: %s",
destAddr, err.Error())
code := http.StatusNotFound
http.Error(w, http.StatusText(code), code)
log.StatusCode = code
return
}
defer destConn.Close()
destConn.SetLinger(60)
conn, client, err := hj.Hijack()
if err != nil {
trace.T("jvproxy/handler", trace.PrioDebug,
"error while setting up tunnel on behalf of %s: %s",
req.RemoteAddr, err.Error())
code := http.StatusInternalServerError
http.Error(w, http.StatusText(code), code)
log.StatusCode = code
return
}
defer conn.Close()
// From this point on, the usual http methods on `w` won't
// work any more.
var zeroTime time.Time
conn.SetDeadline(zeroTime)
trace.T("jvproxy/handler", trace.PrioDebug,
"created tunnel to %s on behalf of %q",
destAddr, req.RemoteAddr)
client.WriteString("HTTP/1.1 200 OK\r\n\r\n")
client.Flush()
tunnel(destConn, conn)
return
}
cacheInfo := proxy.getCacheability(req)
var respData *cache.Entry
// step 1: check whether any cached responses are available
var choices []*cache.Entry
if cacheInfo.canServeFromCache {
choices = proxy.cache.Retrieve(req)
if len(choices) > 0 {
sort.Sort(byDate(choices))
// TODO(voss): is the following what we want? The code in
// .requestFromUpstream() seems prepared for there to be
// more than one stale response.
respData = choices[0]
}
}
// step 2: if the responses are stale, send a validation request
if respData != nil {
freshnessLifetime := proxy.getFreshnessLifetime(respData)
currentAge := proxy.getCurrentAge(respData)
stale := freshnessLifetime <= currentAge
// TODO(voss): revalidate, if the cached response contains the
// no-cache directive
if stale || cacheInfo.mustRevalidate {
log.CacheResult += "REVALIDATE,"
respData = proxy.requestFromUpstream(req, choices)
}
}
// step 3: make sure we still have the body of the selected response
var body io.ReadCloser
if respData != nil {
body = respData.GetBody()
if body == nil {
log.CacheResult += "DROPPED,"
respData = nil
}
}
// step 4: if the above fails, forward the request upstream
isHit := respData != nil
if isHit {
log.CacheResult += "HIT"
cacheInfo.canStore = false
} else {
log.CacheResult += "MISS"
respData = proxy.requestFromUpstream(req, nil)
}
log.ResponseReceivedNano = int64(time.Since(requestTime) / time.Nanosecond)
log.StatusCode = respData.StatusCode
proxy.updateCacheability(respData, cacheInfo)
log.Comments = append(log.Comments, cacheInfo.log...)
h := w.Header()
copyHeader(h, respData.Header)
w.WriteHeader(respData.StatusCode)
if body == nil {
body = respData.GetBody()
}
// TODO(voss): retry if body==nil ?
defer body.Close()
var n int64
var err error
if cacheInfo.canStore {
entry := proxy.cache.StoreStart(req.URL.String(), &respData.MetaData)
n, err = io.Copy(w, entry.Reader(body))
if err != nil {
entry.Discard()
} else {
entry.Commit(n)
}
log.CacheResult += ",STORE"
} else {
n, err = io.Copy(w, body)
if !isHit {
log.CacheResult += ",NOSTORE"
}
}
if err != nil {
trace.T("jvproxy/handler", trace.PrioDebug,
"error while writing response: %s", err.Error())
}
log.ContentLength = n
// TODO(voss): compare n to the server-provided Content-Length?
// Or maybe unconditionally add a Content-Length header to the
// cached version?
}
type byDate []*cache.Entry
func (x byDate) Len() int { return len(x) }
func (x byDate) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x byDate) Less(i, j int) bool {
dateI := httputil.ParseDate(x[i].Header.Get("Date"))
dateJ := httputil.ParseDate(x[i].Header.Get("Date"))
return dateI.After(dateJ)
}
// Hop-by-hop headers, as specified in
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
var perHopHeaders = []string{
"Connection",
"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"Te", // canonicalized version of "TE"
"Trailers",
"Transfer-Encoding",
"Upgrade",
}
// requestFromUpstream forwards a client request to the upstream
// server. If `stale` is set, it should be a slice of cached
// responses; in this case a validation request is sent which asks the
// server to select one of the available responses.
//
// `stale` must be ordered in order of the Date header field, the
// newest item first.
func (proxy *Proxy) requestFromUpstream(req *http.Request, stale []*cache.Entry) *cache.Entry {
upReq := new(http.Request)
*upReq = *req // includes shallow copies of maps, care is needed below ...
upReq.Proto = "HTTP/1.1"
upReq.ProtoMajor = 1
upReq.ProtoMinor = 1
upReq.Close = false
// Remove hop-by-hop headers to upstream. Especially important is
// "Connection" because we want a persistent connection,
// regardless of what the client sent to us. upReq is sharing the
// underlying map from req (shallow copied above), we copy it if
// necessary.
copiedHeaders := false
for _, name := range perHopHeaders {
if upReq.Header.Get(name) != "" {
if !copiedHeaders {
upReq.Header = make(http.Header)
copyHeader(upReq.Header, req.Header)
copiedHeaders = true
}
upReq.Header.Del(name)
}
}
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
// If we aren't the first proxy, retain prior X-Forwarded-For
// information as a comma+space separated list and fold
// multiple headers into one.
if prior, ok := upReq.Header["X-Forwarded-For"]; ok {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
upReq.Header.Set("X-Forwarded-For", clientIP)
}
proxy.setVia(upReq.Header, req.Proto)
// TODO(voss): what to do about pre-existing If-None-Match and
// If-Modified-Since headers?
conditional := false
var lastModified time.Time
for _, resp := range stale {
etag := resp.Header.Get("Etag")
if etag != "" {
upReq.Header.Add("If-None-Match", etag)
conditional = true
}
lm := httputil.ParseDate(resp.Header.Get("Last-Modified"))
if lm.After(lastModified) {
lastModified = lm
}
}
if req.Method == "GET" || req.Method == "HEAD" {
if !lastModified.IsZero() {
upReq.Header.Set("If-Modified-Since",
lastModified.Format(time.RFC1123))
conditional = true
}
}
requestTime := time.Now()
upResp, err := proxy.upstream.RoundTrip(upReq)
responseTime := time.Now()
if err != nil {
// TODO(voss): serve stale responses if available?
trace.T("jvproxy/handler", trace.PrioDebug,
"upstream server request failed: %s %s: %s",
req.Method, req.RequestURI, err.Error())
msg := "error: " + err.Error()
h := http.Header{}
h.Add("Content-Type", "text/plain")
// TODO(voss): use the correct 502/504 responses.
return &cache.Entry{ // TODO(voss): invent error reporting mechanism
MetaData: cache.MetaData{
StatusCode: 555,
Header: h,
},
Source: "error",
GetBody: func() io.ReadCloser {
return ioutil.NopCloser(strings.NewReader(msg))
},
}
}
// Fix upstream-provided headers as required for forwarding and
// storage.
for _, name := range perHopHeaders {
upResp.Header.Del(name)
}
proxy.setVia(upResp.Header, upResp.Proto)
if len(upResp.Header["Date"]) == 0 {
upResp.Header.Set("Date", responseTime.Format(time.RFC1123))
}
if conditional && upResp.StatusCode == http.StatusNotModified {
var selected []*cache.Entry
done := false
eTag1 := upResp.Header.Get("Etag")
lastModified1 := upResp.Header.Get("Last-Modified")
lm := httputil.ParseDate(lastModified1)
// RFC 7234, section 4.3.4a: If the new response contains a
// strong validator (see Section 2.1 of [RFC7232]), then that
// strong validator identifies the selected representation for
// update. All of the stored responses with the same strong
// validator are selected. If none of the stored responses
// contain the same strong validator, then the cache MUST NOT
// use the new response to update any stored responses.
if eTag1 != "" && !strings.HasPrefix(eTag1, "W/") {
for _, resp := range stale {
eTag2 := resp.Header.Get("Etag")
if eTag1 == eTag2 {
selected = append(selected, resp)
}
}
done = true
}
if !done && !lm.IsZero() {
// RFC 7232, section-2.2.2b: [A Last-Modified header can
// be used as a strong validator, if the] cache entry
// includes a Date value, which gives the time when the
// origin server sent the original response, and [the]
// presented Last-Modified time is at least 60 seconds
// before the Date value.
for _, resp := range stale {
date := httputil.ParseDate(resp.Header.Get("Date"))
if !date.IsZero() && date.Sub(lm) >= 60*time.Second {
selected = append(selected, resp)
done = true
}
}
}
// RFC 7234, section 4.3.4b: If the new response contains a
// weak validator and that validator corresponds to one of the
// cache's stored responses, then the most recent of those
// matching stored responses is selected for update.
if !done && eTag1 != "" {
if !strings.HasPrefix(eTag1, "W/") {
panic("something went wrong")
}
eTag1 = eTag1[2:]
for _, resp := range stale {
eTag2 := resp.Header.Get("Etag")
if strings.HasPrefix(eTag2, "W/") {
eTag2 = eTag2[2:]
}
if eTag1 == eTag2 {
selected = append(selected, resp)
}
}
if len(selected) > 0 {
selected = selected[:1]
done = true
}
}
if !done && !lm.IsZero() {
for _, resp := range stale {
lastModified2 := resp.Header.Get("Last-Modified")
if lastModified1 == lastModified2 {
selected = append(selected, resp)
}
}
if len(selected) > 0 {
selected = selected[:1]
done = true
}
}
// RFC 7234, section 4.3.4c: If the new response does not
// include any form of validator (such as in the case where a
// client generates an If-Modified-Since request from a source
// other than the Last-Modified response header field), and
// there is only one stored response, and that stored response
// also lacks a validator, then that stored response is
// selected for update.
if !done && eTag1 == "" && len(stale) == 1 &&
stale[0].Header.Get("Last-Modified") == "" {
selected = stale
done = true
}
if len(selected) > 0 {
// RFC 7234, section 4.3.4: If a stored response is
// selected for update, the cache MUST:
for _, entry := range selected {
// RFC 7234, section 4.3.4d: delete any Warning header
// fields in the stored response with warn-code 1xx;
warn := entry.Header["Warning"]
i := 0
for i < len(warn) {
if strings.HasPrefix(warn[i], "1") {
warn = append(warn[:i], warn[i+1:]...)
} else {
i++
}
}
// RFC 7234, section 4.3.4f: use other header fields
// provided in the 304 (Not Modified) response to
// replace all instances of the corresponding header
// fields in the stored response.
//
// TODO(voss): what is "other"?
for key, val := range upResp.Header {
entry.Header[key] = val
}
entry.ResponseTime = responseTime
entry.ResponseDelay = responseTime.Sub(requestTime)
proxy.cache.Update(req.URL.String(), entry)
}
sort.Sort(byDate(selected))
return selected[0]
}
return nil
}
return &cache.Entry{
MetaData: cache.MetaData{
StatusCode: upResp.StatusCode,
Header: upResp.Header,
ResponseTime: responseTime,
ResponseDelay: responseTime.Sub(requestTime),
},
Source: "upstream",
GetBody: func() io.ReadCloser { return upResp.Body },
}
}
func (proxy *Proxy) setVia(header http.Header, proto string) {
via := proto + " " + proxy.Name + " (jvproxy)"
if strings.HasPrefix(via, "HTTP/") {
via = via[5:]
}
if prior, ok := header["Via"]; ok {
via = strings.Join(prior, ", ") + ", " + via
}
header.Set("Via", via)
}
type decision struct {
canServeFromCache bool
canStore bool
hasAuthorization bool
mustRevalidate bool
log []string
}
func (proxy *Proxy) getCacheability(req *http.Request) *decision {
res := &decision{}
headers := req.Header
cc, _ := parseHeaders(headers["Cache-Control"])
// RFC 7234, section 3
res.canStore = true
if req.Method != "GET" && req.Method != "HEAD" {
// TODO(voss): handle more method types
res.canStore = false
res.log = append(res.log, "req:method="+req.Method)
}
if _, hasNoStore := cc["no-store"]; hasNoStore {
res.canStore = false
res.log = append(res.log, "req:CC=NS")
}
if proxy.shared && len(headers["Authorization"]) > 0 {
res.hasAuthorization = true
// decision defered to `proxy.updateCacheability()`
// TODO(voss): does the comment above still make sense?
}
// RFC 7234, section 4
res.canServeFromCache = true
pragma, _ := parseHeaders(headers["Pragma"])
if _, hasNoCache := pragma["no-cache"]; hasNoCache {
res.mustRevalidate = true
res.log = append(res.log, "req:P=NC")
}
if _, hasNoCache := cc["no-cache"]; hasNoCache {
res.mustRevalidate = true
res.log = append(res.log, "req:CC=NC")
}
return res
}
func (proxy *Proxy) updateCacheability(resp *cache.Entry, res *decision) {
// At this point we already have obtained a new response from the
// server: only the .canStore field is still interesting.
// RFC 7234, section 3
if !res.canStore {
return
}
headers := resp.Header
cc, _ := parseHeaders(headers["Cache-Control"])
switch resp.StatusCode {
case 200, 203, 204, 300, 301, 404, 405, 410, 414, 501:
// status codes understood by the proxy
// pass
default:
// This currently includes 206 (partial content)
res.canStore = false
res.log = append(res.log, "resp:code="+strconv.Itoa(resp.StatusCode))
}
if _, hasNoStore := cc["no-store"]; hasNoStore {
res.canStore = false
res.log = append(res.log, "resp:CC=NS")
}
if _, hasPrivate := cc["private"]; proxy.shared && hasPrivate {
res.canStore = false
res.log = append(res.log, "resp:CC=P")
}
if res.hasAuthorization {
_, ok1 := cc["must-revalidate"]
_, ok2 := cc["public"]
_, ok3 := cc["s-maxage"]
if !(ok1 || ok2 || ok3) {
res.canStore = false
res.log = append(res.log, "resp:Auth")
}
}
cacheable := false
if len(headers["Expires"]) > 0 {
cacheable = true
}
if _, hasMaxAge := cc["max-age"]; hasMaxAge {
cacheable = true
}
if _, hasSMaxage := cc["s-maxage"]; proxy.shared && hasSMaxage {
cacheable = true
}
switch resp.StatusCode {
case 200, 203, 204, 206, 300, 301, 404, 405, 410, 414, 501:
// status codes defined as cacheable by default
cacheable = true
}
if _, hasPublic := cc["public"]; hasPublic {
cacheable = true
}
if !cacheable {
res.canStore = false
res.log = append(res.log, "resp:nc")
}
}