forked from dunglas/mercure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
88 lines (70 loc) · 2.52 KB
/
publish.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
package mercure
import (
"io"
"net/http"
"strconv"
"go.uber.org/zap"
)
// PublishHandler allows publisher to broadcast updates to all subscribers.
//
//nolint:funlen
func (h *Hub) PublishHandler(w http.ResponseWriter, r *http.Request) {
var claims *claims
var err error
if h.publisherJWTKeyFunc != nil {
claims, err = authorize(r, h.publisherJWTKeyFunc, h.publishOrigins, h.cookieName)
if err != nil || claims == nil || claims.Mercure.Publish == nil {
h.httpAuthorizationError(w, r, err)
return
}
}
if r.ParseForm() != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
topics := r.PostForm["topic"]
if len(topics) == 0 {
http.Error(w, "Missing \"topic\" parameter", http.StatusBadRequest)
return
}
var retry uint64
if retryString := r.PostForm.Get("retry"); retryString != "" {
if retry, err = strconv.ParseUint(retryString, 10, 64); err != nil {
http.Error(w, `Invalid "retry" parameter`, http.StatusBadRequest)
return
}
}
private := len(r.PostForm["private"]) != 0
if !canDispatch(h.topicSelectorStore, topics, claims.Mercure.Publish) {
if private {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
if h.isBackwardCompatiblyEnabledWith(7) {
h.logger.Info("Deprecated: posting public updates to topics not listed in the 'mercure.publish' JWT claim is deprecated since the version 7 of the protocol, use '[\"*\"]' as value to allow publishing on all topics.")
} else {
h.logger.Info("Unsupported: posting public updates to topics not listed in the 'mercure.publish' JWT claim is not supported anymore, use '[\"*\"]' as value to allow publishing on all topics or enable backward compatibility with the version 7 of the protocol.")
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
}
u := &Update{
Topics: topics,
Private: private,
Debug: h.debug,
Event: Event{r.PostForm.Get("data"), r.PostForm.Get("id"), r.PostForm.Get("type"), retry},
}
// Broadcast the update
if err := h.transport.Dispatch(u); err != nil {
panic(err)
}
if _, err := io.WriteString(w, u.ID); err != nil {
if c := h.logger.Check(zap.WarnLevel, "Failed to write publish response"); c != nil {
c.Write(zap.Object("update", u), zap.Error(err), zap.String("remote_addr", r.RemoteAddr))
}
}
if c := h.logger.Check(zap.DebugLevel, "Update published"); c != nil {
c.Write(zap.Object("update", u), zap.String("remote_addr", r.RemoteAddr))
}
h.metrics.UpdatePublished(u)
}