-
Notifications
You must be signed in to change notification settings - Fork 1
/
jaws.go
594 lines (545 loc) · 15.8 KB
/
jaws.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Package jaws provides a mechanism to create dynamic
// webpages using Javascript and WebSockets.
//
// It integrates well with Go's html/template package,
// but can be used without it. It can be used with any
// router that supports the standard ServeHTTP interface.
package jaws
import (
"bufio"
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"html/template"
"io"
"log"
"net"
"net/http"
"net/textproto"
"net/url"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/linkdata/deadlock"
)
const CookieNameDefault = "jaws"
type Jaws struct {
CookieName string // Name for session cookies, defaults to "jaws"
Logger *log.Logger // If not nil, send debug info and errors here
doneCh <-chan struct{}
bcastCh chan *Message
subCh chan chan *Message
unsubCh chan chan *Message
headHTML template.HTML
mu deadlock.RWMutex // protects following
kg *bufio.Reader
closeCh chan struct{}
reqs map[uint64]*Request
sessions map[uint64]*Session
}
// NewWithDone returns a new JaWS object using the given completion channel.
// This is expected to be created once per HTTP server and handles
// publishing HTML changes across all connections.
func NewWithDone(doneCh <-chan struct{}) *Jaws {
return &Jaws{
CookieName: CookieNameDefault,
doneCh: doneCh,
bcastCh: make(chan *Message, 1),
subCh: make(chan chan *Message, 1),
unsubCh: make(chan chan *Message, 1),
headHTML: HeadHTML([]string{JavascriptPath}, nil),
kg: bufio.NewReader(rand.Reader),
reqs: make(map[uint64]*Request),
sessions: make(map[uint64]*Session),
}
}
// New returns a new JaWS object that must be closed using Close().
// This is expected to be created once per HTTP server and handles
// publishing HTML changes across all connections.
func New() (jw *Jaws) {
closeCh := make(chan struct{})
jw = NewWithDone(closeCh)
jw.closeCh = closeCh
return
}
// Close frees resources associated with the JaWS object, and
// closes the completion channel if the JaWS was created with New().
// Once the completion channel is closed, broadcasts and sends are discarded.
// Subsequent calls to Close() have no effect.
func (jw *Jaws) Close() {
jw.mu.Lock()
if jw.closeCh != nil {
close(jw.closeCh)
jw.closeCh = nil
}
jw.mu.Unlock()
}
// Done returns the completion channel.
func (jw *Jaws) Done() <-chan struct{} {
return jw.doneCh
}
// Log sends an error to the Logger set in the Jaws.
// Has no effect if the err is nil or the Logger is nil.
// Returns err.
func (jw *Jaws) Log(err error) error {
if err != nil && jw != nil && jw.Logger != nil {
jw.Logger.Println(err.Error())
}
return err
}
// MustLog sends an error to the Logger set in the Jaws or
// panics with the given error if no Logger is set.
// Has no effect if the err is nil.
func (jw *Jaws) MustLog(err error) {
if err != nil {
if jw != nil && jw.Logger != nil {
jw.Logger.Println(err.Error())
} else {
panic(err)
}
}
}
var nextId uint64 // atomic
// MakeID returns a string in the form 'jaws.X' where X is a unique string within lifetime of the program.
func MakeID() string {
return "jaws." + strconv.FormatUint(atomic.AddUint64(&nextId, 1), 32)
}
// NewRequest returns a new JaWS request.
//
// Call this as soon as you start processing a HTML request, and store the
// returned Request pointer so it can be used while constructing the HTML
// response in order to register the JaWS id's you use in the response, and
// use it's Key attribute when sending the Javascript portion of the reply
// with GetBodyFooter.
//
// Don't use the http.Request's Context, as that will expire before the WebSocket call comes in.
func (jw *Jaws) NewRequest(ctx context.Context, hr *http.Request) (rq *Request) {
jw.mu.Lock()
defer jw.mu.Unlock()
for rq == nil {
jawsKey := jw.nonZeroRandomLocked()
if _, ok := jw.reqs[jawsKey]; !ok {
rq = newRequest(ctx, jw, jawsKey, hr)
jw.reqs[jawsKey] = rq
}
}
return
}
func (jw *Jaws) nonZeroRandomLocked() (val uint64) {
random := make([]byte, 8)
for val == 0 {
if _, err := io.ReadFull(jw.kg, random); err != nil {
panic(err)
}
val = binary.LittleEndian.Uint64(random)
}
return
}
// UseRequest extracts the JaWS request with the given key from the request
// map if it exists and the HTTP request remote IP matches.
//
// Call it when receiving the WebSocket connection on '/jaws/:key' to get the
// associated Request, and then call it's ServeHTTP method to process the
// WebSocket messages.
//
// Returns nil if the key was not found or the IP doesn't match, in which
// case you should return a HTTP "404 Not Found" status.
func (jw *Jaws) UseRequest(jawsKey uint64, hr *http.Request) (rq *Request) {
if jawsKey != 0 {
var err error
jw.mu.Lock()
if waitingRq, ok := jw.reqs[jawsKey]; ok {
if err = waitingRq.start(hr); err == nil {
delete(jw.reqs, jawsKey)
rq = waitingRq
}
}
jw.mu.Unlock()
_ = jw.Log(err)
}
return
}
func (jw *Jaws) createSession(remoteIP net.IP, expires time.Time) (sess *Session) {
jw.mu.Lock()
for sess == nil {
sessionID := jw.nonZeroRandomLocked()
if _, ok := jw.sessions[sessionID]; !ok {
sess = newSession(jw, sessionID, remoteIP, expires)
jw.sessions[sessionID] = sess
}
}
jw.mu.Unlock()
return
}
// SessionCount returns the number of active sessions.
func (jw *Jaws) SessionCount() (n int) {
jw.mu.RLock()
n = len(jw.sessions)
jw.mu.RUnlock()
return
}
// Sessions returns a list of all active sessions, which may be nil.
func (jw *Jaws) Sessions() (sl []*Session) {
jw.mu.RLock()
if n := len(jw.sessions); n > 0 {
sl = make([]*Session, 0, n)
for _, sess := range jw.sessions {
sl = append(sl, sess)
}
}
jw.mu.RUnlock()
return
}
func (jw *Jaws) getSessionLocked(sessIds []uint64, remoteIP net.IP) *Session {
for _, sessId := range sessIds {
if sess, ok := jw.sessions[sessId]; ok && equalIP(remoteIP, sess.remoteIP) {
return sess
}
}
return nil
}
func cutString(s string, sep byte) (before, after string) {
if i := strings.IndexByte(s, sep); i >= 0 {
return s[:i], s[i+1:]
}
return s, ""
}
func getCookieSessionsIds(h http.Header, wanted string) (cookies []uint64) {
for _, line := range h["Cookie"] {
if strings.Contains(line, wanted) {
var part string
line = textproto.TrimString(line)
for len(line) > 0 {
part, line = cutString(line, ';')
if part = textproto.TrimString(part); part != "" {
name, val := cutString(part, '=')
name = textproto.TrimString(name)
if name == wanted {
if len(val) > 1 && val[0] == '"' && val[len(val)-1] == '"' {
val = val[1 : len(val)-1]
}
if sessId := JawsKeyValue(val); sessId != 0 {
cookies = append(cookies, sessId)
}
}
}
}
}
}
return
}
// GetSession retrieves the session associated with the given cookie value and remote address, or nil.
func (jw *Jaws) GetSession(hr *http.Request) (sess *Session) {
if sessIds := getCookieSessionsIds(hr.Header, jw.CookieName); len(sessIds) > 0 {
remoteIP := parseIP(hr.RemoteAddr)
jw.mu.RLock()
sess = jw.getSessionLocked(sessIds, remoteIP)
jw.mu.RUnlock()
}
return
}
// EnsureSession ensures a session exists with an expiry least `minAge` seconds in the future.
// Returns the session and optionally a session cookie to be set
// if a new session was created or if it's expiry time was updated.
//
// Subsequent Requests created with `NewRequest()` that have the cookie set and
// originates from the same IP will be able to access the Session.
//
// If a new session was created, the session cookie is added to `hr` so you can call
// `NewRequest()` with `hr` immediately. You still need to set the cookie in the response.
func (jw *Jaws) EnsureSession(hr *http.Request, minAge, maxAge int) (sess *Session, cookie *http.Cookie) {
if hr != nil && maxAge > 0 {
if sess = jw.GetSession(hr); sess != nil {
cookie = sess.Refresh(minAge, maxAge)
} else {
expires := time.Now().Add(time.Second * time.Duration(maxAge))
sess = jw.createSession(parseIP(hr.RemoteAddr), expires)
cookie = sess.Cookie()
hr.AddCookie(cookie)
}
}
return
}
func (jw *Jaws) deleteSession(sessionID uint64) {
jw.mu.Lock()
delete(jw.sessions, sessionID)
jw.mu.Unlock()
}
// GenerateHeadHTML (re-)generates the HTML code that goes in the HEAD section, ensuring
// that the provided scripts and stylesheets in `extra` are loaded.
//
// You only need to call this if you want to add your own scripts and stylesheets.
func (jw *Jaws) GenerateHeadHTML(extra ...string) error {
var js, css []string
addedJaws := false
for _, e := range extra {
if u, err := url.Parse(e); err == nil {
if strings.HasSuffix(u.Path, ".js") {
js = append(js, e)
addedJaws = addedJaws || strings.HasSuffix(u.Path, JavascriptPath)
} else if strings.HasSuffix(e, ".css") {
css = append(css, e)
} else {
return fmt.Errorf("%q: not .js or .css", u.Path)
}
} else {
return err
}
}
if !addedJaws {
js = append(js, JavascriptPath)
}
jw.headHTML = HeadHTML(js, css)
return nil
}
// Broadcast sends a message to all Requests.
func (jw *Jaws) Broadcast(msg *Message) {
select {
case <-jw.Done():
case jw.bcastCh <- msg:
}
}
// SetInner sends a jid and new inner HTML to all Requests.
//
// Only the requests that have registered the 'jid' (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) SetInner(jid string, innerHtml string) {
jw.Broadcast(&Message{
Elem: jid,
What: "inner",
Data: innerHtml,
})
}
// Remove removes the HTML element(s) with the given 'jid' on all Requests.
//
// Only the requests that have registered the 'jid' (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) Remove(jid string) {
jw.Broadcast(&Message{
Elem: jid,
What: "remove",
})
}
// Insert calls the Javascript 'insertBefore()' method on the given element on all Requests.
// The position parameter 'where' may be either a HTML ID, an child index or the text 'null'.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) Insert(parentId, where, html string) {
jw.Broadcast(&Message{
Elem: parentId,
What: "insert",
Data: where + "\n" + html,
})
}
// Append calls the Javascript 'appendChild()' method on the given element on all Requests.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) Append(parentId, html string) {
jw.Broadcast(&Message{
Elem: parentId,
What: "append",
Data: html,
})
}
// Replace calls the Javascript 'replaceChild()' method on the given element on all Requests.
// The position parameter 'where' may be either a HTML ID or an index.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) Replace(id, where, html string) {
jw.Broadcast(&Message{
Elem: id,
What: "replace",
Data: where + "\n" + html,
})
}
// SetAttr sends an HTML id and new attribute value to all Requests.
// If the value is an empty string, a value-less attribute will be added (such as "disabled")
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) SetAttr(id, attr, val string) {
jw.Broadcast(&Message{
Elem: id,
What: "sattr",
Data: attr + "\n" + val,
})
}
// RemoveAttr removes a given attribute from the HTML id for all Requests.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) RemoveAttr(id, attr string) {
jw.Broadcast(&Message{
Elem: id,
What: "rattr",
Data: attr,
})
}
// SetValue sends an HTML id and new input value to all Requests.
//
// Only the requests that have registered the ID (either with Register or OnEvent) will be sent the message.
func (jw *Jaws) SetValue(id, val string) {
jw.Broadcast(&Message{
Elem: id,
What: "value",
Data: val,
})
}
// Reload requests all Requests to reload their current page.
func (jw *Jaws) Reload() {
jw.Broadcast(&Message{
Elem: " reload",
})
}
// Redirect requests all Requests to navigate to the given URL.
func (jw *Jaws) Redirect(url string) {
jw.Broadcast(&Message{
Elem: " redirect",
What: url,
})
}
// Trigger invokes the event handler for the given ID with a 'trigger' event on all Requests.
func (jw *Jaws) Trigger(id, val string) {
jw.Broadcast(&Message{
Elem: id,
What: "trigger",
Data: val,
})
}
// Alert sends an alert to all Requests. The lvl argument should be one of Bootstraps alert levels:
// primary, secondary, success, danger, warning, info, light or dark.
func (jw *Jaws) Alert(lvl, msg string) {
jw.Broadcast(&Message{
Elem: " alert",
What: lvl,
Data: msg,
})
}
// Count returns the number of requests waiting for their WebSocket callbacks.
func (jw *Jaws) Pending() (n int) {
jw.mu.RLock()
n = len(jw.reqs)
jw.mu.RUnlock()
return
}
// ServeWithTimeout begins processing requests with the given timeout.
// It is intended to run on it's own goroutine.
// It returns when the completion channel is closed.
func (jw *Jaws) ServeWithTimeout(requestTimeout time.Duration) {
const minInterval = time.Millisecond * 10
const maxInterval = time.Second
maintenanceInterval := requestTimeout / 2
if maintenanceInterval > maxInterval {
maintenanceInterval = maxInterval
}
if maintenanceInterval < minInterval {
maintenanceInterval = minInterval
}
t := time.NewTicker(maintenanceInterval)
defer t.Stop()
subs := map[chan *Message]struct{}{}
for {
select {
case <-jw.Done():
return
case <-t.C:
jw.maintenance(requestTimeout)
case msgCh := <-jw.subCh:
if msgCh != nil {
subs[msgCh] = struct{}{}
}
case msgCh := <-jw.unsubCh:
if _, ok := subs[msgCh]; ok {
delete(subs, msgCh)
close(msgCh)
}
case msg := <-jw.bcastCh:
if msg != nil {
for msgCh := range subs {
select {
case msgCh <- msg:
default:
// it's critical that we keep the broadcast
// distribution loop running, so any Request
// that fails to process it's messages quickly
// enough must be terminated. the alternative
// would be to drop some messages, but that
// could mean nonreproducible and seemingly
// random failures in processing logic.
close(msgCh)
delete(subs, msgCh)
_ = jw.Log(fmt.Errorf("jaws: broadcast channel full sending %v", msg))
}
}
}
}
}
}
// Serve calls ServeWithTimeout(time.Second * 10).
func (jw *Jaws) Serve() {
jw.ServeWithTimeout(time.Second * 10)
}
func (jw *Jaws) subscribe(size int) chan *Message {
msgCh := make(chan *Message, size)
select {
case <-jw.Done():
close(msgCh)
return nil
case jw.subCh <- msgCh:
}
return msgCh
}
func (jw *Jaws) unsubscribe(msgCh chan *Message) {
select {
case <-jw.Done():
case jw.unsubCh <- msgCh:
}
}
func (jw *Jaws) maintenance(requestTimeout time.Duration) {
var killReqs []uint64
var killSess []uint64
jw.mu.RLock()
now := time.Now()
deadline := now.Add(-requestTimeout)
logger := jw.Logger
for k, rq := range jw.reqs {
if rq.Created.Before(deadline) {
killReqs = append(killReqs, k)
if logger != nil && rq.Initial != nil {
logger.Println(fmt.Errorf("jaws: request timed out: %q", rq.Initial.RequestURI))
}
}
}
for k, sess := range jw.sessions {
if sess.GetExpires().Before(now) {
killSess = append(killSess, k)
}
}
jw.mu.RUnlock()
if len(killReqs)+len(killSess) > 0 {
jw.mu.Lock()
for _, k := range killReqs {
delete(jw.reqs, k)
}
for _, k := range killSess {
delete(jw.sessions, k)
}
jw.mu.Unlock()
}
}
func equalIP(a, b net.IP) bool {
return a.Equal(b) || (a.IsLoopback() && b.IsLoopback())
}
func parseIP(remoteAddr string) (ip net.IP) {
if remoteAddr != "" {
if host, _, err := net.SplitHostPort(remoteAddr); err == nil {
ip = net.ParseIP(host)
} else {
ip = net.ParseIP(remoteAddr)
}
}
return
}
func maybePanic(err error) {
if err != nil {
panic(err)
}
}