| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package goupnp |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/xml" |
| 19 |
"fmt" |
| 20 |
"net/http" |
| 21 |
"net/url" |
| 22 |
"time" |
| 23 |
|
| 24 |
"golang.org/x/net/html/charset" |
| 25 |
|
| 26 |
"github.com/huin/goupnp/httpu" |
| 27 |
"github.com/huin/goupnp/ssdp" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
type ContextError struct { |
| 32 |
Context string |
| 33 |
Err error |
| 34 |
} |
| 35 |
|
| 36 |
func (err ContextError) Error() string { |
| 37 |
return fmt.Sprintf("%s: %v", err.Context, err.Err) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
type MaybeRootDevice struct { |
| 42 |
|
| 43 |
Root *RootDevice |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
Location *url.URL |
| 49 |
|
| 50 |
|
| 51 |
Err error |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func DiscoverDevices(searchTarget string) ([]MaybeRootDevice, error) { |
| 61 |
httpu, err := httpu.NewHTTPUClient() |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
defer httpu.Close() |
| 66 |
responses, err := ssdp.SSDPRawSearch(httpu, string(searchTarget), 2, 3) |
| 67 |
if err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
|
| 71 |
results := make([]MaybeRootDevice, len(responses)) |
| 72 |
for i, response := range responses { |
| 73 |
maybe := &results[i] |
| 74 |
loc, err := response.Location() |
| 75 |
if err != nil { |
| 76 |
maybe.Err = ContextError{"unexpected bad location from search", err} |
| 77 |
continue |
| 78 |
} |
| 79 |
maybe.Location = loc |
| 80 |
if root, err := DeviceByURL(loc); err != nil { |
| 81 |
maybe.Err = err |
| 82 |
} else { |
| 83 |
maybe.Root = root |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return results, nil |
| 88 |
} |
| 89 |
|
| 90 |
func DeviceByURL(loc *url.URL) (*RootDevice, error) { |
| 91 |
locStr := loc.String() |
| 92 |
root := new(RootDevice) |
| 93 |
if err := requestXml(locStr, DeviceXMLNamespace, root); err != nil { |
| 94 |
return nil, ContextError{fmt.Sprintf("error requesting root device details from %q", locStr), err} |
| 95 |
} |
| 96 |
var urlBaseStr string |
| 97 |
if root.URLBaseStr != "" { |
| 98 |
urlBaseStr = root.URLBaseStr |
| 99 |
} else { |
| 100 |
urlBaseStr = locStr |
| 101 |
} |
| 102 |
urlBase, err := url.Parse(urlBaseStr) |
| 103 |
if err != nil { |
| 104 |
return nil, ContextError{fmt.Sprintf("error parsing location URL %q", locStr), err} |
| 105 |
} |
| 106 |
root.SetURLBase(urlBase) |
| 107 |
return root, nil |
| 108 |
} |
| 109 |
|
| 110 |
func requestXml(url string, defaultSpace string, doc interface{}) error { |
| 111 |
timeout := time.Duration(3 * time.Second) |
| 112 |
client := http.Client{ |
| 113 |
Timeout: timeout, |
| 114 |
} |
| 115 |
resp, err := client.Get(url) |
| 116 |
if err != nil { |
| 117 |
return err |
| 118 |
} |
| 119 |
defer resp.Body.Close() |
| 120 |
|
| 121 |
if resp.StatusCode != 200 { |
| 122 |
return fmt.Errorf("goupnp: got response status %s from %q", |
| 123 |
resp.Status, url) |
| 124 |
} |
| 125 |
|
| 126 |
decoder := xml.NewDecoder(resp.Body) |
| 127 |
decoder.DefaultSpace = defaultSpace |
| 128 |
decoder.CharsetReader = charset.NewReaderLabel |
| 129 |
|
| 130 |
return decoder.Decode(doc) |
| 131 |
} |