diff --git a/server/server.go b/server/server.go index 3a4ba53..78715d2 100644 --- a/server/server.go +++ b/server/server.go @@ -287,7 +287,7 @@ func (s *Server) handleInternal(w http.ResponseWriter, r *http.Request) error { } else if r.Method == http.MethodOptions { return s.handleOptions(w, r) } else if r.Method == http.MethodGet && topicRegex.MatchString(r.URL.Path) { - return s.handleHome(w, r) + return s.handleTopic(w, r) } else if (r.Method == http.MethodPut || r.Method == http.MethodPost) && topicRegex.MatchString(r.URL.Path) { return s.withRateLimit(w, r, s.handlePublish) } else if r.Method == http.MethodGet && sendRegex.MatchString(r.URL.Path) { @@ -309,6 +309,17 @@ func (s *Server) handleHome(w http.ResponseWriter, r *http.Request) error { }) } +func (s *Server) handleTopic(w http.ResponseWriter, r *http.Request) error { + unifiedpush := readParam(r, "x-unifiedpush", "unifiedpush", "up") == "1" // see PUT/POST too! + if unifiedpush { + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Access-Control-Allow-Origin", "*") // CORS, allow cross-origin requests + _, err := io.WriteString(w, `{"unifiedpush":{"version":1}}`+"\n") + return err + } + return s.handleHome(w, r) +} + func (s *Server) handleEmpty(_ http.ResponseWriter, _ *http.Request) error { return nil } @@ -339,7 +350,7 @@ func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request, v *visito return err } m := newDefaultMessage(t.ID, strings.TrimSpace(string(b))) - cache, firebase, email, err := s.parseParams(r, m) + cache, firebase, email, err := s.parsePublishParams(r, m) if err != nil { return err } @@ -388,7 +399,7 @@ func (s *Server) handlePublish(w http.ResponseWriter, r *http.Request, v *visito return nil } -func (s *Server) parseParams(r *http.Request, m *message) (cache bool, firebase bool, email string, err error) { +func (s *Server) parsePublishParams(r *http.Request, m *message) (cache bool, firebase bool, email string, err error) { cache = readParam(r, "x-cache", "cache") != "no" firebase = readParam(r, "x-firebase", "firebase") != "no" email = readParam(r, "x-email", "x-e-mail", "email", "e-mail", "mail", "e") @@ -426,6 +437,10 @@ func (s *Server) parseParams(r *http.Request, m *message) (cache bool, firebase } m.Time = delay.Unix() } + unifiedpush := readParam(r, "x-unifiedpush", "unifiedpush", "up") == "1" // see GET too! + if unifiedpush { + firebase = false + } return cache, firebase, email, nil } diff --git a/server/server_test.go b/server/server_test.go index cf3fc58..589b982 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -583,6 +583,13 @@ func TestServer_PublishEmailNoMailer_Fail(t *testing.T) { require.Equal(t, 400, response.Code) } +func TestServer_UnifiedPushDiscovery(t *testing.T) { + s := newTestServer(t, newTestConfig(t)) + response := request(t, s, "GET", "/mytopic?up=1", "", nil) + require.Equal(t, 200, response.Code) + require.Equal(t, `{"unifiedpush":{"version":1}}`+"\n", response.Body.String()) +} + func newTestConfig(t *testing.T) *Config { conf := NewConfig() conf.CacheFile = filepath.Join(t.TempDir(), "cache.db")