-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathclient.go
297 lines (258 loc) · 7.44 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package soap
import (
"bytes"
"encoding/xml"
"errors"
"io"
"io/ioutil"
"log"
"mime"
"mime/multipart"
"net"
"net/http"
"reflect"
"strings"
"time"
)
// ClientDialTimeout default timeout 30s
var ClientDialTimeout = time.Duration(30 * time.Second)
// UserAgent is the default user agent
var UserAgent = "go-soap-0.1"
// XMLMarshaller lets you inject your favourite custom xml implementation
type XMLMarshaller interface {
Marshal(v interface{}) ([]byte, error)
Unmarshal(xml []byte, v interface{}) error
}
type defaultMarshaller struct {
}
func (dm *defaultMarshaller) Marshal(v interface{}) (xmlBytes []byte, err error) {
return xml.MarshalIndent(v, "", " ")
}
func (dm *defaultMarshaller) Unmarshal(xmlBytes []byte, v interface{}) error {
return xml.Unmarshal(xmlBytes, v)
}
func newDefaultMarshaller() XMLMarshaller {
return &defaultMarshaller{}
}
func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, ClientDialTimeout)
}
// BasicAuth credentials for the client
type BasicAuth struct {
Login string
Password string
}
// Client generic SOAP client
type Client struct {
url string
tls bool
auth *BasicAuth
tr *http.Transport
Marshaller XMLMarshaller
ContentType string
SoapVersion string
}
// NewClient constructor. SOAP 1.1 is used by default. Switch to SOAP 1.2 with UseSoap12()
func NewClient(url string, auth *BasicAuth, tr *http.Transport) *Client {
return &Client{
url: url,
auth: auth,
tr: tr,
Marshaller: newDefaultMarshaller(),
ContentType: SoapContentType11, // default is SOAP 1.1
SoapVersion: SoapVersion11,
}
}
func (c *Client) UseSoap11() {
c.SoapVersion = SoapVersion11
c.ContentType = SoapContentType11
}
func (c *Client) UseSoap12() {
c.SoapVersion = SoapVersion12
c.ContentType = SoapContentType12
}
// Call make a SOAP call
func (c *Client) Call(soapAction string, request, response interface{}) (httpResponse *http.Response, err error) {
envelope := Envelope{}
envelope.Body.Content = request
xmlBytes, err := c.Marshaller.Marshal(envelope)
if err != nil {
return nil, err
}
// Adjust namespaces for SOAP 1.2
if c.SoapVersion == SoapVersion12 {
tmp := string(xmlBytes)
tmp = strings.Replace(tmp, NamespaceSoap11, NamespaceSoap12, -1)
xmlBytes = []byte(tmp)
}
//log.Println(string(xmlBytes))
//l("SOAP Client Call() => Marshalled Request\n", string(xmlBytes))
req, err := http.NewRequest("POST", c.url, bytes.NewBuffer(xmlBytes))
if err != nil {
return nil, err
}
if c.auth != nil {
req.SetBasicAuth(c.auth.Login, c.auth.Password)
}
req.Header.Add("Content-Type", c.ContentType)
req.Header.Set("User-Agent", UserAgent)
if soapAction != "" {
req.Header.Add("SOAPAction", soapAction)
}
req.Close = true
tr := c.tr
if tr == nil {
tr = http.DefaultTransport.(*http.Transport)
}
client := &http.Client{Transport: tr}
l("POST to", c.url, "with\n", string(xmlBytes))
l("Header")
LogJSON(req.Header)
httpResponse, err = client.Do(req)
if err != nil {
return nil, err
}
defer httpResponse.Body.Close()
l("\n\n## Response header:\n", httpResponse.Header)
mediaType, params, err := mime.ParseMediaType(httpResponse.Header.Get("Content-Type"))
if err != nil {
l("WARNING:", err)
}
l("MIMETYPE:", mediaType)
var rawbody = []byte{}
if strings.HasPrefix(mediaType, "multipart/") { // MULTIPART MESSAGE
mr := multipart.NewReader(httpResponse.Body, params["boundary"])
// If this is a multipart message, search for the soapy part
foundSoap := false
for {
p, err := mr.NextPart()
if err == io.EOF {
return nil, err
}
if err != nil {
return nil, err
}
slurp, err := ioutil.ReadAll(p)
if err != nil {
return nil, err
}
if strings.HasPrefix(string(slurp), "<soap") || strings.HasPrefix(string(slurp), "<SOAP") {
rawbody = slurp
foundSoap = true
break
}
}
if !foundSoap {
return nil, errors.New("Multipart message does contain a soapy part.")
}
} else { // SINGLE PART MESSAGE
rawbody, err = ioutil.ReadAll(httpResponse.Body)
if err != nil {
return httpResponse, err
}
// Check if there is a body and if yes if it's a soapy one.
if len(rawbody) == 0 {
l("INFO: Response Body is empty!")
return // Empty responses are ok. Sometimes Sometimes only a Status 200 or 202 comes back
}
// There is a message body, but it's not SOAP. We cannot handle this!
if !(strings.Contains(string(rawbody), "<soap") || strings.Contains(string(rawbody), "<SOAP")) {
l("This is not a SOAP-Message: \n" + string(rawbody))
return nil, errors.New("This is not a SOAP-Message: \n" + string(rawbody))
}
l("RAWBODY\n", string(rawbody))
}
// We have an empty body or a SOAP body
l("\n\n## Response body:\n", string(rawbody))
// Our structs for Envelope, Header, Body and Fault are tagged with namespace for SOAP 1.1
// Therefore we must adjust namespaces for incoming SOAP 1.2 messages
tmp := string(rawbody)
tmp = strings.Replace(tmp, NamespaceSoap12, NamespaceSoap11, -1)
rawbody = []byte(tmp)
respEnvelope := new(Envelope)
type Dummy struct {
}
// Response struct may be nil, e.g. if only a Status 200 is expected.
// In this case, we need a Dummy response to avoid a nil pointer if we receive a SOAP-Fault instead of the empty message (unmarshalling would fail)
if response == nil {
respEnvelope.Body = Body{Content: &Dummy{}}
} else {
respEnvelope.Body = Body{Content: response}
}
err = xml.Unmarshal(rawbody, respEnvelope)
if err != nil {
log.Println("soap/client.go Call(): COULD NOT UNMARSHAL\n", err)
}
// If we have a SOAP Fault, we return it as string in an error
fault := respEnvelope.Body.Fault
// If a SOAP Fault is received, try to jsonMarshal it and return it via the error.
if fault != nil {
return nil, errors.New("SOAP FAULT:\n" + formatFaultXML(rawbody, 1))
}
return
}
// Format the Soap Fault as indented string. Namespaces are dropped for better readability.
// Tags with lower level than start level is omitted
func formatFaultXML(xmlBytes []byte, startLevel int) string {
indent := " "
d := xml.NewDecoder(bytes.NewBuffer([]byte(xmlBytes)))
typeStart := reflect.TypeOf(xml.StartElement{})
typeEnd := reflect.TypeOf(xml.EndElement{})
typeCharData := reflect.TypeOf(xml.CharData{})
level := 0
out := bytes.NewBuffer([]byte(""))
ind := func() {
n := 0
if level-startLevel-1 > 0 {
n = level - startLevel - 1
}
out.Write([]byte(strings.Repeat(indent, n)))
}
lf := func() {
out.Write([]byte("\n"))
}
lastWasStart := false
lastWasCharData := false
lastWasEnd := false
for token, err := d.Token(); token != nil && err == nil; token, err = d.Token() {
r := reflect.ValueOf(token)
switch r.Type() {
case typeStart:
lastWasCharData = false
se := token.(xml.StartElement)
if lastWasEnd || lastWasStart {
lf()
}
lastWasStart = true
ind()
elementName := se.Name.Local
if level > startLevel {
out.WriteString("<" + elementName)
out.WriteString(">")
}
level++
lastWasEnd = false
case typeCharData:
lastWasCharData = true
_ = lastWasCharData
lastWasStart = false
cdata := token.(xml.CharData)
xml.EscapeText(out, cdata)
lastWasEnd = false
case typeEnd:
level--
if lastWasEnd {
lf()
ind()
}
lastWasEnd = true
lastWasStart = false
end := token.(xml.EndElement)
if level > startLevel {
endTagName := end.Name.Local
out.WriteString("</" + endTagName + ">")
}
}
}
return strings.Trim(string(out.Bytes()), " \n")
}