Fix #41 - collisions with same USN as multiple locations.

This commit is contained in:
John Beisley 2021-06-26 17:01:14 +01:00
parent 16e24d5762
commit 33cdcbb30d
2 changed files with 10 additions and 12 deletions

View File

@ -51,7 +51,8 @@ func (err ContextError) Error() string {
// MaybeRootDevice contains either a RootDevice or an error.
type MaybeRootDevice struct {
// Identifier of the device.
// Identifier of the device. Note that this in combination with Location
// uniquely identifies a result from DiscoverDevices.
USN string
// Set iff Err == nil.

View File

@ -72,7 +72,7 @@ func SSDPRawSearch(
isExactSearch := searchTarget != SSDPAll && searchTarget != UPNPRootDevice
seenUSNs := make(map[string]bool)
seenIDs := make(map[string]bool)
var responses []*http.Response
for _, response := range allResponses {
if response.StatusCode != 200 {
@ -83,17 +83,14 @@ func SSDPRawSearch(
continue
}
usn := response.Header.Get("USN")
if usn == "" {
// Empty/missing USN in search response - using location instead.
location, err := response.Location()
loc, err := response.Location()
if err != nil {
// No usable location in search response - discard.
continue
}
usn = location.String()
}
if _, alreadySeen := seenUSNs[usn]; !alreadySeen {
seenUSNs[usn] = true
id := loc.String() + "\x00" + usn
if _, alreadySeen := seenIDs[id]; !alreadySeen {
seenIDs[id] = true
responses = append(responses, response)
}
}