-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathzipstatic.go
179 lines (150 loc) · 3.48 KB
/
zipstatic.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
package main
import (
"archive/zip"
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
type zipHandler struct {
ts time.Time
data map[string][]byte
}
func (z *zipHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
data, ok := z.data[req.URL.Path]
if !ok {
http.Error(w, "Not found", 404)
return
}
http.ServeContent(w, req, req.URL.Path, z.ts, bytes.NewReader([]byte(data)))
}
var httpTimeFormats = []string{
http.TimeFormat,
time.RFC850,
time.ANSIC,
}
// Stolen from go ~1.1
func parseHTTPTime(text string) (t time.Time, err error) {
for _, layout := range httpTimeFormats {
t, err = time.Parse(layout, text)
if err == nil {
return
}
}
return
}
func updateStatic(path, cachePath string) (time.Time, error) {
lastTs := time.Now()
res, err := http.Get(path)
if err != nil {
return lastTs, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return lastTs, fmt.Errorf("HTTP error getting %v: %v", path, res.Status)
}
ftmp := cachePath + ".tmp"
os.Remove(ftmp)
defer os.Remove(ftmp)
f, err := os.OpenFile(ftmp, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return lastTs, err
}
defer f.Close()
hash := sha1.New()
_, err = io.Copy(f, io.TeeReader(res.Body, hash))
if err != nil {
return lastTs, err
}
h := `"` + hex.EncodeToString(hash.Sum(nil)) + `"`
et := res.Header.Get("Etag")
if et != "" && h != et {
err := fmt.Errorf("invalid download: etag=%v, computed=%v",
res.Header.Get("Etag"), h)
return lastTs, err
}
t, parseErr := parseHTTPTime(res.Header.Get("Last-Modified"))
if parseErr == nil {
lastTs = t
}
os.Remove(cachePath)
os.Rename(f.Name(), cachePath)
return lastTs, nil
}
func zipStatic(path, cachePath string) (*zipHandler, error) {
log.Printf("loading static content from: %v", path)
lastTs, err := updateStatic(path, cachePath)
if err != nil {
st, e2 := os.Stat(cachePath)
if e2 == nil {
log.Printf("Error loading static data (%v). Proceeding with local copy",
err)
err = nil
lastTs = st.ModTime()
} else {
err = fmt.Errorf("Both remote (%v) and local (%v) static content "+
"is unavailable.", err, e2)
}
}
if err != nil {
return nil, err
}
zf, zerr := zipStaticServe(cachePath, lastTs)
if zerr != nil {
// If we couldn't serve from cache, errors from http take precendence.
if err != nil {
return nil, err
}
return nil, zerr
}
log.Printf("Static init complete")
return zf, nil
}
func zipStaticServe(fn string, lastTs time.Time) (*zipHandler, error) {
zr, err := zip.OpenReader(fn)
if err != nil {
return nil, err
}
defer zr.Close()
zf := &zipHandler{
ts: lastTs,
data: map[string][]byte{},
}
for _, f := range zr.File {
rc, err := f.Open()
if err != nil {
return nil, err
}
d, err := ioutil.ReadAll(rc)
if err != nil {
return nil, err
}
rc.Close()
zf.data[f.Name] = d
}
return zf, nil
}
type lastResortHandler struct{}
func (h *lastResortHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(lastResortText))
}
const lastResortText = `<html>
<head><title>cbgb missing UI</title></head>
<body>
<h1>cbgb is missing its UI</h1>
<p>
cbgb was not given a usable UI and could not fetch one from the
internet. The REST API will continue to work, but no admin UI
will be available to you until you fix whatever got you here.
</p>
<p>
For more info, check your logs and stuff.
</p>
</body> </html>`