This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
344 lines (296 loc) · 7.4 KB
/
server.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
package httpfstream
import (
"bytes"
"errors"
"github.com/garyburd/go-websocket/websocket"
"io"
"log"
"net/http"
"os"
pathpkg "path"
"path/filepath"
"sync"
"time"
)
// New returns a new http.Handler for httpfstream.
func New(root string) Handler {
return Handler{
Root: root,
httpFS: http.Dir(root),
writers: make(map[string]struct{}),
followers: make(map[string]map[*http.Request]chan []byte),
}
}
type Handler struct {
Root string
Log *log.Logger
httpFS http.FileSystem
writers map[string]struct{}
writersMu sync.Mutex
followers map[string]map[*http.Request]chan []byte
followersMu sync.Mutex
}
const xVerb = "X-Verb"
// ServeHTTP implements net/http.Handler.
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
verb := r.Header.Get(xVerb)
if verb == "" {
verb = r.URL.Query().Get("verb")
}
switch r.Method {
case "GET":
switch verb {
case "APPEND":
h.Append(w, r)
default:
h.Follow(w, r)
}
default:
http.Error(w, "method not supported", http.StatusMethodNotAllowed)
}
}
func (h Handler) logf(msg string, v ...interface{}) {
if h.Log != nil {
h.Log.Printf(msg, v...)
}
}
const (
readBufSize = 10 * 1024 // 10 kb
writeBufSize = 10 * 1024 // 10 kb
writeChanSize = 50
)
var (
followKeepaliveInterval = 3 * time.Second
readWait = 25 * time.Second
writeWait = 5 * time.Second
)
func (h Handler) resolve(path string) string {
path = pathpkg.Clean("/" + path)
return filepath.Join(string(h.Root), path)
}
// ErrWriterConflict indicates that the requested path is currently being
// written by another writer. A path may have at most one active writer.
var ErrWriterConflict = errors.New("path already has an active writer")
func (h Handler) addWriter(path string) error {
h.writersMu.Lock()
defer h.writersMu.Unlock()
if _, present := h.writers[path]; !present {
h.writers[path] = struct{}{}
return nil
}
return ErrWriterConflict
}
func (h Handler) removeWriter(path string) {
h.writersMu.Lock()
defer h.writersMu.Unlock()
delete(h.writers, path)
}
func (h Handler) addFollower(path string, r *http.Request, c chan []byte) {
h.followersMu.Lock()
defer h.followersMu.Unlock()
if _, present := h.followers[path]; !present {
h.followers[path] = make(map[*http.Request]chan []byte)
}
h.followers[path][r] = c
}
func (h Handler) getFollowers(path string) []chan []byte {
h.followersMu.Lock()
defer h.followersMu.Unlock()
fs := make([]chan []byte, len(h.followers[path]))
i := 0
for _, f := range h.followers[path] {
fs[i] = f
i++
}
return fs
}
func (h Handler) removeFollower(path string, r *http.Request) {
h.followersMu.Lock()
defer h.followersMu.Unlock()
delete(h.followers[path], r)
if len(h.followers[path]) == 0 {
delete(h.followers, path)
}
}
func (h Handler) isWriting(path string) bool {
h.writersMu.Lock()
defer h.writersMu.Unlock()
_, present := h.writers[path]
return present
}
func (h Handler) serveFile(w http.ResponseWriter, r *http.Request) {
http.FileServer(h.httpFS).ServeHTTP(w, r)
}
// Follow handles FOLLOW requests to retrieve the contents of a file and a
// real-time stream of data that is appended to the file.
func (h Handler) Follow(w http.ResponseWriter, r *http.Request) {
path := h.resolve(r.URL.Path)
h.logf("FOLLOW %s", path)
// If this file isn't currently being written to, we don't need to update to
// a WebSocket; we can just return the static file.
if !h.isWriting(path) {
h.serveFile(w, r)
return
}
c := make(chan []byte)
h.addFollower(path, r, c)
defer h.removeFollower(path, r)
// TODO(sqs): race conditions galore
f, err := os.Open(path)
if err != nil {
http.Error(w, "failed to open file: "+err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
// Open WebSocket.
ws, err := websocket.Upgrade(w, r.Header, nil, readBufSize, writeBufSize)
if err != nil {
if _, ok := err.(websocket.HandshakeError); ok {
// Serve file via HTTP (not WebSocket).
h.serveFile(w, r)
return
}
h.logf("failed to upgrade to WebSocket: %s", err)
return
}
defer ws.Close()
// Send persisted file contents.
for {
sw, err := ws.NextWriter(websocket.OpText)
if err != nil {
h.logf("NextWriter for file failed: %s", err)
return
}
n, err := io.Copy(sw, f)
if err != nil {
h.logf("File write to WebSocket failed: %s", err)
sw.Close()
return
}
err = sw.Close()
if err != nil {
h.logf("Failed to close WebSocket file writer: %s", err)
return
}
// Finished reading file.
if n == 0 {
break
}
}
// Follow new writes to file.
var lastPing time.Time
for {
tick := time.NewTicker(50 * time.Millisecond)
select {
case <-tick.C:
if !h.isWriting(path) {
goto done
}
if time.Since(lastPing) > followKeepaliveInterval {
ws.WriteMessage(websocket.OpPing, []byte{})
lastPing = time.Now()
}
case data := <-c:
sw, err := ws.NextWriter(websocket.OpText)
if err != nil {
h.logf("NextWriter failed: %s", err)
return
}
_, err = sw.Write(data)
if err != nil {
h.logf("Write to WebSocket failed: %s", err)
sw.Close()
return
}
err = sw.Close()
if err != nil {
h.logf("Failed to close WebSocket writer: %s", err)
return
}
}
}
done:
err = ws.WriteControl(websocket.OpClose, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Time{})
if err != nil {
h.logf("Failed to close WebSocket: %s", err)
return
}
err = f.Close()
if err != nil {
h.logf("Failed to close destination file: %s", err)
return
}
}
// Append handles APPEND requests and appends data to a file.
func (h Handler) Append(w http.ResponseWriter, r *http.Request) {
path := h.resolve(r.URL.Path)
h.logf("APPEND %s", path)
defer r.Body.Close()
err := h.addWriter(path)
if err != nil {
h.logf("addWriter %s: %s", err)
http.Error(w, "addWriter: "+err.Error(), http.StatusForbidden)
return
}
defer h.removeWriter(path)
err = os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
http.Error(w, "failed to create dir: "+err.Error(), http.StatusInternalServerError)
return
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
http.Error(w, "failed to open destination file for writing: "+err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
ws, err := websocket.Upgrade(w, r.Header, nil, readBufSize, writeBufSize)
if err != nil {
if _, ok := err.(websocket.HandshakeError); ok {
h.logf("not a WebSocket handshake: %s", err)
return
}
h.logf("failed to upgrade to WebSocket: %s", err)
return
}
defer ws.Close()
ws.SetReadDeadline(time.Now().Add(readWait))
for {
op, rd, err := ws.NextReader()
if err != nil {
if err != io.ErrUnexpectedEOF {
h.logf("NextReader failed: %s", err)
}
break
}
switch op {
case websocket.OpPong:
ws.SetReadDeadline(time.Now().Add(readWait))
case websocket.OpText:
var buf bytes.Buffer
mw := io.MultiWriter(f, &buf)
// Persist to file.
_, err := io.Copy(mw, rd)
if err != nil {
h.logf("Read from WebSocket failed: %s", err)
return
}
// Broadcast to followers.
followers := h.getFollowers(path)
for _, fc := range followers {
fc <- buf.Bytes()
}
ws.SetReadDeadline(time.Now().Add(readWait))
}
}
err = r.Body.Close()
if err != nil {
h.logf("failed to close upload stream: %s", err)
return
}
err = f.Close()
if err != nil {
h.logf("failed to close destination file: %s", err)
return
}
}