-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxywriter.go
149 lines (141 loc) · 2.88 KB
/
proxywriter.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
package cache
import (
"bufio"
"fmt"
"io"
"net/http"
"net/textproto"
"os"
"strconv"
"strings"
"time"
)
// proxyWriter writes to the ResponseWriter and the cache
type proxyWriter struct {
c *CacheHandler
st *serveState
lock func() // syncher lock
rw http.ResponseWriter
statusCode int
createf func() io.WriteCloser
f io.WriteCloser
bw *bufio.Writer
}
func (p *proxyWriter) Header() http.Header {
return p.rw.Header()
}
func (p *proxyWriter) WriteHeader(code int) {
age, valid := p.cacheAge(p.rw.Header(), code)
if valid {
p.rw.Header().Set("Cache-Control", fmt.Sprintf("maxage=%d", age))
}
p.rw.WriteHeader(code)
if p.statusCode == 0 {
p.statusCode = code
if valid {
p.cacheHeader()
}
}
}
func (p *proxyWriter) Write(b []byte) (int, error) {
if p.statusCode == 0 {
age, valid := p.cacheAge(p.rw.Header(), 200)
if valid {
p.rw.Header().Set("Cache-Control", fmt.Sprintf("maxage=%d", age))
} else {
// don't cache the header below
p.statusCode = 200
}
}
c, err := p.rw.Write(b)
if err != nil {
return c, err
}
if p.statusCode == 0 {
p.statusCode = 200
p.cacheHeader()
}
if p.f != nil {
return p.bw.Write(b)
} else {
return c, err
}
}
func (p *proxyWriter) Close() {
if p.f != nil {
p.bw.Flush()
p.f.Close()
if p.c.useStale&Supdating != 0 {
p.lock()
}
os.Remove(p.st.path)
os.Rename(p.st.tmpPath, p.st.path)
}
}
// Cache file format looks a bit like an HTTP response:
/*
CACHE <statusCode> <crc32>\r\n
Headers: Values\r\n
\r\n
<body>
*/
func (p *proxyWriter) cacheHeader() {
if p.c.useStale&Supdating == 0 {
p.lock()
}
hdr := p.rw.Header()
p.f = p.createf()
p.bw = bufio.NewWriter(p.f)
tpw := textproto.NewWriter(p.bw)
err := tpw.PrintfLine("CACHE %d %d", p.statusCode, p.st.crc)
if err != nil {
panic(err)
}
for k, v := range hdr {
switch k {
case "Cache-Control", "Expires":
continue
}
for _, s := range v {
err = tpw.PrintfLine("%s: %s", k, s)
if err != nil {
panic(err)
}
}
}
_, err = p.bw.WriteString("\r\n")
if err != nil {
panic(err)
}
}
func (p *proxyWriter) cacheAge(hdr http.Header, statusCode int) (int64, bool) {
if _, ok := hdr["Set-Cookie"]; ok {
return 0, false
}
if v := hdr.Get("Expires"); v != "" {
t, err := time.Parse(http.TimeFormat, v)
if err != nil || time.Now().After(t) {
return 0, false
}
}
if vals, ok := hdr["Cache-Control"]; ok {
for _, v := range vals {
fields := strings.Fields(v)
for _, f := range fields {
if f == "no-store" ||
strings.HasPrefix(f, "no-cache") ||
strings.HasPrefix(f, "private") {
return 0, false
}
if strings.HasPrefix(f, "max-age=") {
age, err := strconv.ParseInt(f[len("max-age="):], 10, 64)
if err != nil || age <= 0 {
return 0, false
}
return age, true
}
}
}
}
return int64(p.c.age(statusCode) / time.Second), true
}