Debian Codegraph @

goupnp/goupnp.go

raw symbols SCIP
1 // goupnp is an implementation of a client for various UPnP services.
2 //
3 // For most uses, it is recommended to use the code-generated packages under
4 // github.com/huin/goupnp/dcps. Example use is shown at
5 // http://godoc.org/github.com/huin/goupnp/example
6 //
7 // A commonly used client is internetgateway1.WANPPPConnection1:
8 // http://godoc.org/github.com/huin/goupnp/dcps/internetgateway1#WANPPPConnection1
9 //
10 // Currently only a couple of schemas have code generated for them from the
11 // UPnP example XML specifications. Not all methods will work on these clients,
12 // because the generated stubs contain the full set of specified methods from
13 // the XML specifications, and the discovered services will likely support a
14 // subset of those methods.
15 package goupnp
16
17 import (
18 "context"
19 "encoding/xml"
20 "fmt"
21 "net/http"
22 "net/url"
23 "time"
24
25 "github.com/NebulousLabs/go-upnp/goupnp/httpu"
26 "github.com/NebulousLabs/go-upnp/goupnp/ssdp"
27
28 "golang.org/x/net/html/charset"
29 )
30
31 // ContextError is an error that wraps an error with some context information.
32 type ContextError struct {
33 Context string
34 Err error
35 }
36
37 func (err ContextError) Error() string {
38 return fmt.Sprintf("%s: %v", err.Context, err.Err)
39 }
40
41 // MaybeRootDevice contains either a RootDevice or an error.
42 type MaybeRootDevice struct {
43 // Set iff Err == nil.
44 Root *RootDevice
45
46 // The location the device was discovered at. This can be used with
47 // DeviceByURL, assuming the device is still present. A location represents
48 // the discovery of a device, regardless of if there was an error probing it.
49 Location *url.URL
50
51 // Any error encountered probing a discovered device.
52 Err error
53 }
54
55 // DiscoverDevices is deprecated. Use DiscoverDevicesCtx instead.
56 func DiscoverDevices(searchTarget string) ([]MaybeRootDevice, error) {
57 return DiscoverDevicesCtx(context.Background(), searchTarget)
58 }
59
60 // DiscoverDevicesCtx attempts to find targets of the given type. This is
61 // typically the entry-point for this package. searchTarget is typically a URN
62 // in the form "urn:schemas-upnp-org:device:..." or
63 // "urn:schemas-upnp-org:service:...". A single error is returned for errors
64 // while attempting to send the query. An error or RootDevice is returned for
65 // each discovered RootDevice.
66 func DiscoverDevicesCtx(ctx context.Context, searchTarget string) ([]MaybeRootDevice, error) {
67 httpu, err := httpu.NewHTTPUClient()
68 if err != nil {
69 return nil, err
70 }
71 defer httpu.Close()
72 responses, err := ssdp.SSDPRawSearchCtx(ctx, httpu, string(searchTarget), 2, 3)
73 if err != nil {
74 return nil, err
75 }
76
77 results := make([]MaybeRootDevice, len(responses))
78 for i, response := range responses {
79 maybe := &results[i]
80 loc, err := response.Location()
81 if err != nil {
82 maybe.Err = ContextError{"unexpected bad location from search", err}
83 continue
84 }
85 maybe.Location = loc
86 if root, err := DeviceByURL(loc); err != nil {
87 maybe.Err = err
88 } else {
89 maybe.Root = root
90 }
91 }
92
93 return results, nil
94 }
95
96 func DeviceByURL(loc *url.URL) (*RootDevice, error) {
97 locStr := loc.String()
98 root := new(RootDevice)
99 if err := requestXml(locStr, DeviceXMLNamespace, root); err != nil {
100 return nil, ContextError{fmt.Sprintf("error requesting root device details from %q", locStr), err}
101 }
102 var urlBaseStr string
103 if root.URLBaseStr != "" {
104 urlBaseStr = root.URLBaseStr
105 } else {
106 urlBaseStr = locStr
107 }
108 urlBase, err := url.Parse(urlBaseStr)
109 if err != nil {
110 return nil, ContextError{fmt.Sprintf("error parsing location URL %q", locStr), err}
111 }
112 root.SetURLBase(urlBase)
113 return root, nil
114 }
115
116 func requestXml(url string, defaultSpace string, doc interface{}) error {
117 timeout := time.Duration(3 * time.Second)
118 client := http.Client{
119 Timeout: timeout,
120 }
121 resp, err := client.Get(url)
122 if err != nil {
123 return err
124 }
125 defer resp.Body.Close()
126
127 if resp.StatusCode != 200 {
128 return fmt.Errorf("goupnp: got response status %s from %q",
129 resp.Status, url)
130 }
131
132 decoder := xml.NewDecoder(resp.Body)
133 decoder.DefaultSpace = defaultSpace
134 decoder.CharsetReader = charset.NewReaderLabel
135
136 return decoder.Decode(doc)
137 }