-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
handler.go
145 lines (120 loc) · 3.96 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
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
package sse
import (
"context"
"log/slog"
"net/http"
"strings"
"github.com/anycable/anycable-go/common"
"github.com/anycable/anycable-go/node"
"github.com/anycable/anycable-go/server"
"github.com/anycable/anycable-go/version"
"github.com/anycable/anycable-go/ws"
)
// SSEHandler generates a new http handler for SSE connections
func SSEHandler(n *node.Node, shutdownCtx context.Context, headersExtractor server.HeadersExtractor, config *Config, l *slog.Logger) http.Handler {
var allowedHosts []string
if config.AllowedOrigins == "" {
allowedHosts = []string{}
} else {
allowedHosts = strings.Split(config.AllowedOrigins, ",")
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Write CORS headers
server.WriteCORSHeaders(w, r, allowedHosts)
// Respond to preflight requests
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
// SSE only supports GET and POST requests
if r.Method != http.MethodGet && r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Prepare common headers
w.Header().Set("X-AnyCable-Version", version.Version())
if r.ProtoMajor == 1 {
// An endpoint MUST NOT generate an HTTP/2 message containing connection-specific header fields.
// Source: RFC7540.
w.Header().Set("Connection", "keep-alive")
}
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-Accel-Buffering", "no")
w.Header().Set("Cache-Control", "private, no-cache, no-store, must-revalidate, max-age=0") // HTTP 1.1
w.Header().Set("Pragma", "no-cache") // HTTP 1.0
w.Header().Set("Expire", "0")
flusher, ok := w.(http.Flusher)
if !ok {
w.WriteHeader(http.StatusNotImplemented)
return
}
info, err := server.NewRequestInfo(r, headersExtractor)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
sessionCtx := l.With("sid", info.UID).With("transport", "sse")
subscribeCmds, err := subscribeCommandsFromRequest(r)
if err != nil {
sessionCtx.Error("failed to build subscribe command", "error", err)
w.WriteHeader(http.StatusBadRequest)
return
}
// Finally, we can establish a session
session, err := NewSSESession(n, w, r, info)
if err != nil {
sessionCtx.Error("failed to establish sesssion", "error", err)
w.WriteHeader(http.StatusBadRequest)
return
}
if session == nil {
sessionCtx.Error("authentication failed")
w.WriteHeader(http.StatusUnauthorized)
return
}
// Make sure we remove the session from the node when we're done (especially if we return earlier due to rejected subscription)
defer session.Disconnect("Closed", ws.CloseNormalClosure)
conn := session.UnderlyingConn().(*Connection)
for _, subscribeCmd := range subscribeCmds {
// Subscribe to the channel
res, err := n.Subscribe(session, subscribeCmd)
if err != nil || res == nil {
sessionCtx.Error("failed to subscribe", "error", err)
w.WriteHeader(http.StatusBadRequest)
return
}
// Subscription rejected
if res.Status != common.SUCCESS {
sessionCtx.Debug("subscription rejected")
w.WriteHeader(http.StatusBadRequest)
return
}
}
w.WriteHeader(http.StatusOK)
flusher.Flush()
conn.Established()
sessionCtx.Debug("session established")
shutdownReceived := false
for {
select {
case <-shutdownCtx.Done():
if !shutdownReceived {
shutdownReceived = true
sessionCtx.Debug("server shutdown")
session.DisconnectWithMessage(
&common.DisconnectMessage{Type: "disconnect", Reason: common.SERVER_RESTART_REASON, Reconnect: true},
common.SERVER_RESTART_REASON,
)
}
case <-r.Context().Done():
sessionCtx.Debug("request terminated")
session.DisconnectNow("Closed", ws.CloseNormalClosure)
return
case <-conn.Context().Done():
sessionCtx.Debug("session completed")
return
}
}
})
}