-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapush_handler.go
183 lines (154 loc) · 3.78 KB
/
capush_handler.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
package main
import (
"compress/gzip"
"fmt"
"io"
"mime"
"net/http"
"os"
"path"
"strings"
"sync"
"time"
)
type CAPHandler struct {
Etags map[string]string
Deps map[string][]string
Root string
IndexFile string
NotFoundHandler http.Handler
sync.RWMutex
}
func (c *CAPHandler) send(rw http.ResponseWriter, f io.Reader, compr bool) {
if !compr {
rw.WriteHeader(200)
io.Copy(rw, f)
} else {
rw.Header().Set("Content-Encoding", "gzip")
rw.WriteHeader(200)
gz := gzip.NewWriter(rw)
io.Copy(gz, f)
gz.Close()
}
}
func (c *CAPHandler) sendFile(rw http.ResponseWriter, file string, compr bool) {
if f, err := os.Open(file); err == nil {
defer f.Close()
c.send(rw, f, compr)
}
}
func (c *CAPHandler) pushAllDeps(rw http.ResponseWriter, filename string) {
if deps, havedeps := c.Deps[filename]; havedeps {
p, noerr := rw.(http.Pusher)
if noerr {
for _, dep := range deps {
p.Push("/"+dep, nil)
}
}
}
rw.Header().Set("etag", "\""+c.Etags[filename]+"\"")
}
func (c *CAPHandler) pushModDeps(rw http.ResponseWriter, filename, oldetag string) bool {
newetag := c.Etags[filename]
if len(oldetag)%3 != 0 || len(oldetag) != len(newetag) {
c.pushAllDeps(rw, filename)
return true
}
if deps, havedeps := c.Deps[filename]; havedeps {
p, noerr := rw.(http.Pusher)
if noerr {
tl := len(newetag)
for i := 3; i < tl; i += 3 {
if oldetag[i:i+3] != newetag[i:i+3] {
p.Push("/"+deps[i/3-1], nil)
} else {
h := http.Header(make(map[string][]string))
h.Set("If-None-Match", "\""+oldetag[i:i+3]+"\"")
p.Push("/"+deps[i/3-1], &http.PushOptions{Header: h})
}
}
}
}
rw.Header().Set("etag", "\""+newetag+"\"")
return oldetag != newetag
}
func (c *CAPHandler) typeAndSendFile(rw http.ResponseWriter, filename string, compr bool) {
//Set content type
mime := mime.TypeByExtension(path.Ext(filename))
if mime != "" {
rw.Header().Set("content-type", mime)
}
//Send file
c.sendFile(rw, filename, compr)
}
func (c *CAPHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
//Read lock
c.RLock()
defer c.RUnlock()
//Set correct filename
filename := req.URL.Path[1:]
if filename == "" {
filename = c.IndexFile
}
//search for etag
_, found := c.Etags[filename]
//not found, send not found
if !found {
c.NotFoundHandler.ServeHTTP(rw, req)
} else /*found*/ {
oldetags, etf := req.Header["If-None-Match"]
accenc, compr := req.Header["Accept-Encoding"]
if compr {
compr = strings.Contains(accenc[0], "gzip")
}
//Fresh send
if !etf {
c.pushAllDeps(rw, filename)
//Write headers
rw.Header().Set("cache-control", "public, max-age=172800")
c.typeAndSendFile(rw, path.Join(c.Root, filename), compr)
} else /*Check for update and send */ {
//Extract correct etag
oldetag := oldetags[0]
oldetag = strings.Trim(oldetag, "\" ")
//Push modified deps
if c.pushModDeps(rw, filename, oldetag) {
rw.Header().Set("cache-control", "public, max-age=172800")
c.typeAndSendFile(rw, path.Join(c.Root, filename), compr)
} else {
rw.WriteHeader(304)
}
}
}
}
func createCAPHandler(root string) http.Handler {
ets, err := depEtags(root)
if err != nil {
fmt.Println("Push disabled: Error generating etags")
return http.FileServer(http.Dir(root))
}
dep, _ := genDeps(root)
handler := &CAPHandler{
Etags: ets,
Deps: dep,
Root: root,
IndexFile: "index.html",
NotFoundHandler: http.NotFoundHandler(),
}
go func(c *CAPHandler) {
for {
etags, err := depEtags(c.Root)
if err != nil {
fmt.Println("Fatal: Error refreshing Etags...")
os.Exit(1)
}
deps, _ := genDeps(c.Root)
c.Lock()
c.Etags = etags
c.Deps = deps
c.Unlock()
time.Sleep(time.Minute)
}
}(handler)
return handler
}