/
server.go
196 lines (165 loc) · 4.55 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
// Copyright 2015-2021 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain
// rights in this software.
package main
import (
"bytes"
"fmt"
"html"
"image"
"image/draw"
"image/jpeg"
"io"
"mime/multipart"
log "minilog"
"net/http"
"net/textproto"
"net/url"
"path"
"strings"
"time"
)
// playbackServer is heavily based on http.FileServer. If the requested file is
// a directory, it will list the contents. Otherwise, it will try to stream
// JPEG images to the client that it reads from a recording.
type playbackServer struct {
root http.FileSystem
}
func (s *playbackServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
upath := r.URL.Path
if !strings.HasPrefix(upath, "/") {
upath = "/" + upath
r.URL.Path = upath
}
serveFile(w, r, s.root, path.Clean(upath))
}
func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string) {
f, err := fs.Open(name)
if err != nil {
http.NotFound(w, r)
return
}
defer f.Close()
d, err1 := f.Stat()
if err1 != nil {
http.NotFound(w, r)
return
}
if d.IsDir() {
// Directory, list the contents
dirList(w, f)
} else {
// Actually playback a recording
offset, err := time.ParseDuration(r.FormValue("offset"))
if err != nil {
log.Error("parse offset: %v: %v", err, r.FormValue("offset"))
offset = 0
}
streamRecording(w, f, offset)
}
}
// dirList writes an html page that provides links to all the files in a
// directory. This is copied from Go's net/http/fs.go file.
func dirList(w http.ResponseWriter, f http.File) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
for {
dirs, err := f.Readdir(100)
if err != nil || len(dirs) == 0 {
break
}
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
// name may contain '?' or '#', which must be escaped to remain
// part of the URL path, and not indicate the start of a query
// string or fragment.
url := url.URL{Path: name}
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), html.EscapeString(name))
}
}
fmt.Fprintf(w, "</pre>\n")
}
func streamRecording(w http.ResponseWriter, f http.File, start time.Duration) {
updateChan, _ := readFile(f)
imageChan := make(chan image.Image, 10)
// gorountine to rebuild the images
go func() {
var X, Y int
prev := time.Now()
img := image.NewRGBA(image.Rect(0, 0, X, Y))
var startOffset int64
// for each jpeg image
for update := range updateChan {
var skip bool
// fast forward to start
// we set skip instead of just continuing the outer
// loop so we can do other checks like resolution
// changes
if startOffset < start.Nanoseconds() {
startOffset += update.Offset
skip = true
}
// Check if the resolution has changed
last := update.Rectangles[len(update.Rectangles)-1]
if last.EncodingType == DesktopSize || X == 0 || Y == 0 {
X = last.Rect.Max.X
Y = last.Rect.Max.Y
}
nimg := image.NewRGBA(image.Rect(0, 0, X, Y))
// Copy in the previous image
dr := image.Rectangle{img.Rect.Min, img.Rect.Max}
draw.Draw(nimg, dr, img, img.Rect.Min, draw.Src)
for _, r := range update.Rectangles {
dr := image.Rectangle{r.Rect.Min, r.Rect.Max}
log.Debug("drawing in rectangle at %#v\n", dr)
draw.Draw(nimg, dr, r, r.Rect.Min, draw.Src)
}
offset := time.Now().Sub(prev).Nanoseconds()
img = nimg
if skip {
continue
}
prev = time.Now()
if offset < update.Offset {
// Sleep until the next image should be served
time.Sleep(time.Duration(update.Offset - offset))
} else {
log.Debugln("warning: longer to replay images than record them")
}
imageChan <- nimg
}
close(imageChan)
}()
mh := make(textproto.MIMEHeader)
mh.Set("Content-Type", "image/jpeg")
m := multipart.NewWriter(w)
defer m.Close()
h := w.Header()
boundary := m.Boundary()
h.Set("Content-type", fmt.Sprintf("multipart/x-mixed-replace; boundary=%s", boundary))
// encode and send the image
var buf bytes.Buffer
for image := range imageChan {
buf.Reset()
log.Debug("writing image: %v", image.Bounds())
err := jpeg.Encode(&buf, image, nil)
if err != nil {
log.Error("unable to encode jpeg: %v", err)
break
}
mh.Set("Content-length", fmt.Sprintf("%d", buf.Len()))
fm, err := m.CreatePart(mh)
if err != nil {
log.Error("unable to create multipart: %v", err)
return
}
_, err = io.Copy(fm, &buf)
if err != nil {
log.Error("unable to write multipart: %v", err)
break
}
}
}