UPnP/SSDP: respond with multiple service descriptors

Instead of violating the standard and putting multiple locations in one
SSDP response, just send a single response for each possible location.
This commit is contained in:
Sybren A. Stüvel 2022-03-08 16:46:03 +01:00
parent a19aa86c4e
commit ca2bf7ff25
3 changed files with 8 additions and 20 deletions

View File

@ -23,7 +23,6 @@ package upnp_ssdp
import ( import (
"context" "context"
"fmt" "fmt"
"strings"
"sync" "sync"
"time" "time"
@ -104,26 +103,21 @@ func (c *Client) Response(message gossdp.ResponseMessage) {
} }
logger.Debug().Msg("UPnP/SSDP message received") logger.Debug().Msg("UPnP/SSDP message received")
c.appendURLs(message.Location) c.appendURL(message.Location)
} }
func (c *Client) appendURLs(location string) { func (c *Client) appendURL(url string) {
urls := strings.Split(location, LocationSeparator)
c.mutex.Lock() c.mutex.Lock()
defer c.mutex.Unlock() defer c.mutex.Unlock()
// Only append URLs that we haven't seen yet. // Only append URLs that we haven't seen yet.
for _, url := range urls {
if c.seenURLs[url] { if c.seenURLs[url] {
continue return
} }
c.urls = append(c.urls, url) c.urls = append(c.urls, url)
c.seenURLs[url] = true c.seenURLs[url] = true
}
c.log.Debug(). c.log.Debug().
Int("new", len(urls)).
Int("total", len(c.urls)). Int("total", len(c.urls)).
Msg("new URLs received") Msg("new URLs received")
} }

View File

@ -23,7 +23,6 @@ package upnp_ssdp
import ( import (
"context" "context"
"net/url" "net/url"
"strings"
"github.com/fromkeith/gossdp" "github.com/fromkeith/gossdp"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -62,12 +61,9 @@ func (s *Server) AddAdvertisement(serviceLocation string) {
// AddAdvertisementURLs constructs a service location from the given URLs, and // AddAdvertisementURLs constructs a service location from the given URLs, and
// adds the advertisement for it. // adds the advertisement for it.
func (s *Server) AddAdvertisementURLs(urls []url.URL) { func (s *Server) AddAdvertisementURLs(urls []url.URL) {
urlStrings := make([]string, len(urls)) for _, url := range urls {
for idx := range urls { s.AddAdvertisement(url.String())
urlStrings[idx] = urls[idx].String()
} }
location := strings.Join(urlStrings, LocationSeparator)
s.AddAdvertisement(location)
} }
// Run starts the advertisement, and blocks until the context is closed. // Run starts the advertisement, and blocks until the context is closed.

View File

@ -24,6 +24,4 @@ package upnp_ssdp
const ( const (
FlamencoUUID = "aa80bc5f-d0af-46b8-8630-23bd7e80ec4d" FlamencoUUID = "aa80bc5f-d0af-46b8-8630-23bd7e80ec4d"
FlamencoServiceType = "urn:flamenco:manager:0" FlamencoServiceType = "urn:flamenco:manager:0"
LocationSeparator = ";"
) )