-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp.go
293 lines (253 loc) · 6.83 KB
/
http.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"net/http"
"path"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/porjo/ytdl-web/internal/jobs"
"github.com/porjo/ytdl-web/internal/util"
"github.com/porjo/ytdl-web/internal/ytworker"
)
const (
// timeout opus stream if no new data read from file in this time
StreamSourceTimeout = 30 * time.Second
// FIXME: need a better way of detecting and timing out slow clients
// http response deadline (slow reading clients)
HTTPWriteTimeout = 1800 * time.Second
WSPingInterval = 10 * time.Second
WSWriteWait = 2 * time.Second
// default content expiry in seconds
DefaultExpiry = 24 * time.Hour
)
var (
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
type Request struct {
URL string
DeleteURLs []string `json:"delete_urls"`
}
type Conn struct {
sync.Mutex
*websocket.Conn
}
type wsHandler struct {
WebRoot string
OutPath string
RemoteAddr string
FFProbeCmd string
Dispatcher *jobs.Dispatcher
Downloader *ytworker.Download
Logger *slog.Logger
}
func (ws *wsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
gconn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
slog.Error(err.Error())
return
}
id, outCh := ws.Downloader.Subscribe()
defer ws.Downloader.Unsubscribe(id)
ws.RemoteAddr = gconn.RemoteAddr().String()
logger := ws.Logger.With("ws", id)
logger.Info("client connected")
// wrap Gorilla conn with our conn so we can extend functionality
conn := Conn{sync.Mutex{}, gconn}
wg := sync.WaitGroup{}
wg.Add(1)
// setup ping/pong to keep connection open
go func() {
ticker := time.NewTicker(WSPingInterval)
defer ticker.Stop()
defer wg.Done()
for {
select {
case <-ctx.Done():
logger.Info("ping, context done")
return
case <-ticker.C:
//slog.Debug("ping")
// WriteControl can be called concurrently
if err := conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(WSWriteWait)); err != nil {
logger.Error("ping client error", "error", err)
cancel()
return
}
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
defer logger.Debug("serveHTTP outCh read loop, done")
for {
select {
case <-ctx.Done():
return
case m, open := <-outCh:
if !open {
return
}
err := conn.writeMsg(m)
if err != nil {
m := util.Msg{Key: "error", Value: err.Error()}
conn.writeMsg(m)
}
if m.Key == ytworker.KeyCompleted {
// on completion, also send recent URLs
gruCtx, _ := context.WithTimeout(ctx, 10*time.Second)
recentURLs, err := GetRecentURLs(gruCtx, ws.WebRoot, ws.OutPath, ws.FFProbeCmd)
if err != nil {
logger.Error("GetRecentURLS error", "error", err)
return
}
m := util.Msg{Key: "recent", Value: recentURLs}
conn.writeMsg(m)
}
}
}
}()
// Send recently retrieved URLs
gruCtx, _ := context.WithTimeout(ctx, 10*time.Second)
recentURLs, err := GetRecentURLs(gruCtx, ws.WebRoot, ws.OutPath, ws.FFProbeCmd)
if err != nil {
logger.Error("GetRecentURLS error", "error", err)
return
}
m := util.Msg{Key: "recent", Value: recentURLs}
conn.writeMsg(m)
wg.Add(1)
go func() {
defer wg.Done()
defer logger.Debug("ending ws read goroutine")
defer conn.Close()
for {
msgType, raw, err := conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
logger.Error("ReadMessage error", "error", err)
}
return
}
logger.Debug("websocket read", "msg", string(raw))
switch msgType {
case websocket.TextMessage:
var req Request
err = json.Unmarshal(raw, &req)
if err != nil {
logger.Error("json unmarshal error", "error", err)
return
}
err := ws.msgHandler(req)
if err != nil {
logger.Error("msgHandler error", "error", err)
m := util.Msg{Key: "error", Value: err.Error()}
conn.writeMsg(m)
}
default:
logger.Error("unknown message type - close websocket\n")
return
}
}
}()
logger.Debug("serveHTTP waitgroup wait")
wg.Wait()
logger.Debug("serveHTTP end")
}
func (c *Conn) writeMsg(val interface{}) error {
c.Lock()
defer c.Unlock()
j, err := json.Marshal(val)
if err != nil {
return err
}
slog.Debug("websocket write", "ws", c.RemoteAddr(), "msg", string(j))
if err = c.WriteMessage(websocket.TextMessage, j); err != nil {
return err
}
return nil
}
func (ws *wsHandler) msgHandler(req Request) error {
if req.URL == "" && len(req.DeleteURLs) == 0 {
return fmt.Errorf("unknown parameters")
}
if len(req.DeleteURLs) > 0 {
err := DeleteFiles(req.DeleteURLs, ws.WebRoot)
if err != nil {
return err
}
} else if req.URL != "" {
job := &jobs.Job{Payload: req.URL}
ws.Dispatcher.Enqueue(job)
}
return nil
}
// ServeStream sends the file data to the client as a stream
//
// Because we are reading a file that is growing as we read it, we can't use normal FileServer as
// that would send a Content-Length header with a value smaller than the ultimate filesize.
//
// By copying the raw bytes into ResponseWriter it causes the response to be sent using
// HTTP chunked encoding so the client will continue to request more data until the server signals the end.
//
// There are a couple of challenges to overcome:
// - how to know when the encoding has finished? The current solution is to wait StreamSourceTimeoutSec and
// end the handler if no data is copied in that time. Is that the best approach?
// - how to handle clients that delay requesting more data? In this case ResponseWriter blocks the
// Copy operation.
//
// I think the only solution is to set WriteTimeout on http.Server
func ServeStream(webRoot string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
dir := http.Dir(webRoot)
filename := strings.Replace(path.Clean(r.URL.Path), "stream/", "", 1)
f, err := dir.Open(filename)
if err != nil {
msg, code := toHTTPError(err)
http.Error(w, msg, code)
return
}
defer f.Close()
lastData := time.Now()
for {
// io.Copy doesn't return error on EOF
i, err := io.Copy(w, f)
if err != nil {
slog.Info("servestream copy error", "error", err)
return
}
if i == 0 {
if time.Since(lastData) > StreamSourceTimeout {
slog.Info("servestream timeout", "timeout", StreamSourceTimeout)
return
}
time.Sleep(time.Second)
} else {
lastData = time.Now()
}
}
}
}
func toHTTPError(err error) (msg string, httpStatus int) {
if errors.Is(err, fs.ErrNotExist) {
return "404 page not found", http.StatusNotFound
}
if errors.Is(err, fs.ErrPermission) {
return "403 Forbidden", http.StatusForbidden
}
// Default:
return "500 Internal Server Error", http.StatusInternalServerError
}