goupnp/v2alpha/soap/client/client.go

164 lines
4.1 KiB
Go
Raw Normal View History

2022-03-24 08:18:59 +00:00
// Package client provides a basic SOAP client.
package client
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"github.com/huin/goupnp/v2alpha/soap/envelope"
2022-03-24 08:18:59 +00:00
)
var _ HttpClient = &http.Client{}
2022-03-24 08:18:59 +00:00
// HttpClient defines the interface required of an HTTP client. It is a subset of *http.Client.
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}
// Option is the type for optional configuration of a Client.
type Option func(*options)
// WithHTTPClient specifies an *http.Client to use instead of
// http.DefaultClient.
func WithHTTPClient(httpClient HttpClient) Option {
return func(o *options) {
o.httpClient = httpClient
}
}
type options struct {
httpClient HttpClient
}
// Client is a SOAP client, attached to a specific SOAP endpoint.
// the zero value is not usable, use NewClient() to create an instance.
type Client struct {
httpClient HttpClient
endpointURL string
maxErrorResponseBytes int
}
// New creates a new SOAP client, which will POST its requests to the
// given URL.
func New(endpointURL string, opts ...Option) *Client {
co := options{
httpClient: http.DefaultClient,
}
for _, opt := range opts {
opt(&co)
}
return &Client{
httpClient: co.httpClient,
endpointURL: endpointURL,
}
}
2022-03-26 18:17:39 +00:00
// PerformAction makes a SOAP request, with the given action values to provide
// arguments (`args`) and capture the `reply` into.
func (c *Client) Do(
2022-03-24 08:18:59 +00:00
ctx context.Context,
2022-03-26 18:17:39 +00:00
actionIn, actionOut *envelope.Action,
2022-03-24 08:18:59 +00:00
) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpointURL, nil)
if err != nil {
return err
}
2022-03-26 18:17:39 +00:00
if err := SetRequestAction(req, actionIn); err != nil {
2022-03-24 08:18:59 +00:00
return err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("SOAP request got HTTP %s (%d)",
resp.Status, resp.StatusCode)
}
2022-03-26 18:17:39 +00:00
return ParseResponseAction(resp, actionOut)
2022-03-24 08:18:59 +00:00
}
// PerformAction makes a SOAP request, with the given `argsIn` as input
// arguments, and `argsOut` to capture the output arguments into.
// `serviceType` is the SOAP service type URN, `actionName` is the action to
// call.
//
// This is a convenience for calling `Client.Do` without creating `*Action` values.
func PerformAction(
ctx context.Context, c *Client,
serviceType, actionName string,
argsIn, argsOut any,
) error {
actionIn := envelope.NewSendAction(serviceType, actionName, argsIn)
actionOut := &envelope.Action{Args: argsOut}
return c.Do(ctx, actionIn, actionOut)
}
2022-03-24 08:18:59 +00:00
// SetRequestAction updates fields in `req` with the given SOAP action.
// Specifically it sets Body, ContentLength, Method, and the SOAPACTION and
// CONTENT-TYPE headers.
2022-03-26 18:17:39 +00:00
func SetRequestAction(
req *http.Request,
actionIn *envelope.Action,
) error {
2022-03-24 08:18:59 +00:00
buf := &bytes.Buffer{}
2022-03-26 18:17:39 +00:00
err := envelope.Write(buf, actionIn)
2022-03-24 08:18:59 +00:00
if err != nil {
return fmt.Errorf("encoding envelope: %w", err)
}
req.Body = io.NopCloser(buf)
req.ContentLength = int64(buf.Len())
req.Method = http.MethodPost
req.Header["SOAPACTION"] = []string{fmt.Sprintf(
2022-03-26 18:17:39 +00:00
`"%s#%s"`, actionIn.XMLName.Space, actionIn.XMLName.Local)}
2022-03-24 08:18:59 +00:00
req.Header["CONTENT-TYPE"] = []string{`text/xml; charset="utf-8"`}
return nil
}
// ParseResponse extracts a parsed action from an HTTP response.
// The caller is responsible for calling resp.Body.Close(), but this function
// will consume the entire response body.
2022-03-26 18:17:39 +00:00
func ParseResponseAction(
resp *http.Response,
actionOut *envelope.Action,
) error {
2022-03-24 08:18:59 +00:00
if resp.Body == nil {
return errors.New("missing response body")
}
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, resp.Body); err != nil {
return fmt.Errorf("reading response body: %w", err)
}
2022-03-26 18:17:39 +00:00
if err := envelope.Read(buf, actionOut); err != nil {
2022-03-24 08:18:59 +00:00
if _, ok := err.(*envelope.Fault); ok {
// Parsed cleanly, got SOAP fault.
return err
}
// Parsing problem, provide some information for context.
dispLen := buf.Len()
truncMessage := ""
if dispLen > 1024 {
dispLen = 1024
truncMessage = fmt.Sprintf("first %d bytes: ", dispLen)
}
return fmt.Errorf(
"parsing response body (%s%q): %w",
truncMessage, buf.Bytes()[:dispLen],
err,
)
}
return nil
}