Debian Codegraph @

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 "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 // ContextError is an error that wraps an error with some context information.
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 // MaybeRootDevice contains either a RootDevice or an error.
41 type MaybeRootDevice struct {
42 // Set iff Err == nil.
43 Root *RootDevice
44
45 // The location the device was discovered at. This can be used with
46 // DeviceByURL, assuming the device is still present. A location represents
47 // the discovery of a device, regardless of if there was an error probing it.
48 Location *url.URL
49
50 // Any error encountered probing a discovered device.
51 Err error
52 }
53
54 // DiscoverDevices attempts to find targets of the given type. This is
55 // typically the entry-point for this package. searchTarget is typically a URN
56 // in the form "urn:schemas-upnp-org:device:..." or
57 // "urn:schemas-upnp-org:service:...". A single error is returned for errors
58 // while attempting to send the query. An error or RootDevice is returned for
59 // each discovered RootDevice.
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 }