goupnp/soap/soap.go

130 lines
3.8 KiB
Go
Raw Normal View History

2013-09-29 10:23:10 +00:00
// Definition for the SOAP structure required for UPnP's SOAP usage.
package soap
2013-09-29 10:23:10 +00:00
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const (
2013-10-06 11:57:26 +00:00
soapEncodingStyle = "http://schemas.xmlsoap.org/soap/encoding/"
soapPrefix = `<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>`
2013-10-06 11:57:26 +00:00
soapSuffix = `</s:Body></s:Envelope>`
2013-09-29 10:23:10 +00:00
)
2013-10-06 11:57:26 +00:00
type SOAPClient struct {
EndpointURL url.URL
HTTPClient http.Client
2013-09-29 10:23:10 +00:00
}
2013-10-06 11:57:26 +00:00
func NewSOAPClient(endpointURL url.URL) *SOAPClient {
return &SOAPClient{
EndpointURL: endpointURL,
2013-09-29 10:23:10 +00:00
}
}
2013-10-06 11:57:26 +00:00
// PerformSOAPAction makes a SOAP request, with the given action.
func (client *SOAPClient) PerformAction(actionNamespace, actionName string, inAction interface{}, outAction interface{}) error {
requestBytes, err := encodeRequestAction(actionNamespace, actionName, inAction)
if err != nil {
return err
}
2013-09-29 10:23:10 +00:00
2013-10-06 11:57:26 +00:00
response, err := client.HTTPClient.Do(&http.Request{
2013-09-29 10:23:10 +00:00
Method: "POST",
2013-10-06 11:57:26 +00:00
URL: &client.EndpointURL,
2013-09-29 10:23:10 +00:00
Header: http.Header{
"SOAPACTION": []string{actionNamespace + "#" + actionName},
"CONTENT-TYPE": []string{"text/xml; charset=\"utf-8\""},
},
Body: ioutil.NopCloser(bytes.NewBuffer(requestBytes)),
// Set ContentLength to avoid chunked encoding - some servers might not support it.
ContentLength: int64(len(requestBytes)),
})
if err != nil {
2013-10-06 11:57:26 +00:00
return err
2013-09-29 10:23:10 +00:00
}
defer response.Body.Close()
if response.StatusCode != 200 {
2013-10-06 11:57:26 +00:00
return fmt.Errorf("goupnp: SOAP request got HTTP %s", response.Status)
2013-09-29 10:23:10 +00:00
}
2013-10-06 11:57:26 +00:00
responseEnv := newSOAPEnvelope()
2013-09-29 10:23:10 +00:00
decoder := xml.NewDecoder(response.Body)
2013-10-06 11:57:26 +00:00
if err := decoder.Decode(responseEnv); err != nil {
return err
2013-09-29 10:23:10 +00:00
}
if responseEnv.Body.Fault != nil {
2013-10-06 11:57:26 +00:00
return responseEnv.Body.Fault
}
if outAction != nil {
if err := xml.Unmarshal(responseEnv.Body.RawAction, outAction); err != nil {
return err
}
}
2013-10-06 11:57:26 +00:00
return nil
}
// newSOAPAction creates a soapEnvelope with the given action and arguments.
func newSOAPEnvelope() *soapEnvelope {
return &soapEnvelope{
EncodingStyle: soapEncodingStyle,
2013-09-29 10:23:10 +00:00
}
2013-10-06 11:57:26 +00:00
}
2013-09-29 10:23:10 +00:00
2013-10-06 11:57:26 +00:00
// encodeRequestAction is a hacky way to create an encoded SOAP envelope
// containing the given action. Experiments with one router have shown that it
// 500s for requests where the outer default xmlns is set to the SOAP
// namespace, and then reassigning the default namespace within that to the
// service namespace. Hand-coding the outer XML to work-around this.
func encodeRequestAction(actionNamespace, actionName string, inAction interface{}) ([]byte, error) {
2013-10-06 11:57:26 +00:00
requestBuf := new(bytes.Buffer)
requestBuf.WriteString(soapPrefix)
requestBuf.WriteString(`<u:`)
xml.EscapeText(requestBuf, []byte(actionName))
requestBuf.WriteString(` xmlns:u="`)
xml.EscapeText(requestBuf, []byte(actionNamespace))
requestBuf.WriteString(`">`)
if inAction != nil {
requestEnc := xml.NewEncoder(requestBuf)
if err := requestEnc.Encode(inAction); err != nil {
return nil, err
}
2013-10-06 11:57:26 +00:00
}
requestBuf.WriteString(`</u:`)
xml.EscapeText(requestBuf, []byte(actionName))
requestBuf.WriteString(`>`)
2013-10-06 11:57:26 +00:00
requestBuf.WriteString(soapSuffix)
return requestBuf.Bytes(), nil
2013-09-29 10:23:10 +00:00
}
2013-10-06 11:57:26 +00:00
type soapEnvelope struct {
2013-09-29 10:23:10 +00:00
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
EncodingStyle string `xml:"http://schemas.xmlsoap.org/soap/envelope/ encodingStyle,attr"`
2013-10-06 11:57:26 +00:00
Body soapBody `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
2013-09-29 10:23:10 +00:00
}
2013-10-06 11:57:26 +00:00
type soapBody struct {
Fault *SOAPFaultError `xml:"Fault"`
RawAction []byte `xml:",innerxml"`
2013-09-29 10:23:10 +00:00
}
2013-10-06 11:57:26 +00:00
// SOAPFaultError implements error, and contains SOAP fault information.
type SOAPFaultError struct {
FaultCode string `xml:"faultcode"`
FaultString string `xml:"faultstring"`
Detail string `xml:"detail"`
}
2013-10-06 11:57:26 +00:00
func (err *SOAPFaultError) Error() string {
return fmt.Sprintf("SOAP fault: %s", err.FaultString)
}