-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
80 lines (69 loc) · 1.64 KB
/
handler.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
// Froxy - HTTP over SSH proxy
//
// Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
// See LICENSE for license terms and conditions
//
// Assortment of useful http.Handler implementations
package main
import (
"crypto/md5"
"fmt"
"net/http"
)
// ----- Constants -----
const (
//
// HTTP Header, used as a data tag for polling
//
PollTag = "Froxy-Tag"
)
//
// http.Handler with poll
//
// This handler modifies handling of GET requests as follows:
// 1) For the returned data, a crypto hash of its content is
// calculated and returned as "Froxy-Tag" header
// 2) If request contains a "Froxy-Tag" header, the handler
// waits until calculated hash of response content becomes
// different from hash in request
//
type HandlerWithPoll struct {
froxy *Froxy // Back link to Froxy
event Event // Event that notifies about data change
handler func( // Underlying handler callback
w http.ResponseWriter, r *http.Request)
}
//
// Serve HTTP request
//
func (h *HandlerWithPoll) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
h.handler(w, r)
return
}
// Prepare to polling for data change
rqTag := r.Header.Get(PollTag)
var events <-chan Event
if rqTag != "" {
events = h.froxy.Sub(h.event)
defer h.froxy.Unsub(events)
}
// Serve the request
w2 := &ResponseWriterWithBuffer{}
AGAIN:
h.handler(w2, r)
if w2.Status/100 == 2 {
rspTag := fmt.Sprintf("%x", md5.Sum(w2.Bytes()))
if events != nil && rqTag == rspTag {
select {
case <-events:
w2.Reset()
goto AGAIN
case <-r.Context().Done():
return
}
}
w2.Header().Set(PollTag, rspTag)
}
w2.Send(w)
}