Skip to content

Commit 836f647

Browse files
committed
Merge pull request #48 from wdreeveii/include_cache_fix2
Cleanup external schema fetching
2 parents f2461da + 11027a6 commit 836f647

File tree

5 files changed

+105
-53
lines changed

5 files changed

+105
-53
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GHACCOUNT := hooklift
22
NAME := gowsdl
3-
VERSION := v0.1.2
3+
VERSION := v0.2.0
44

55
include common.mk
66

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ Usage: gowsdl [options] myservice.wsdl
3131
File where the generated code will be saved (default "myservice.go")
3232
-p string
3333
Package under which code will be generated (default "myservice")
34+
-i Skips TLS Verification
3435
-v Shows gowsdl version
3536
```

Diff for: cmd/gowsdl/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ var Name string
6666
var vers = flag.Bool("v", false, "Shows gowsdl version")
6767
var pkg = flag.String("p", "myservice", "Package under which code will be generated")
6868
var outFile = flag.String("o", "myservice.go", "File where the generated code will be saved")
69+
var insecure = flag.Bool("i", false, "Skips TLS Verification")
6970

7071
func init() {
7172
log.SetFlags(0)
@@ -99,7 +100,7 @@ func main() {
99100
}
100101

101102
// load wsdl
102-
gowsdl, err := gen.NewGoWSDL(wsdlPath, *pkg, false)
103+
gowsdl, err := gen.NewGoWSDL(wsdlPath, *pkg, *insecure)
103104
if err != nil {
104105
log.Fatalln(err)
105106
}

Diff for: gowsdl.go

+39-28
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,19 @@ func (g *GoWSDL) unmarshal() error {
178178
return nil
179179
}
180180

181-
func (g *GoWSDL) resolveXSDExternals(schema *XSDSchema, url *url.URL) error {
182-
for _, incl := range schema.Includes {
183-
location, err := url.Parse(incl.SchemaLocation)
181+
func (g *GoWSDL) getSchema(schemaLocation string, url *url.URL) error {
182+
_, schemaFile := filepath.Split(schemaLocation)
183+
184+
data, err := ioutil.ReadFile(schemaFile)
185+
if err != nil {
186+
187+
location, err := url.Parse(schemaLocation)
184188
if err != nil {
185189
return err
186190
}
187191

188-
_, schemaName := filepath.Split(location.Path)
189-
if g.resolvedXSDExternals[schemaName] {
190-
continue
191-
}
192+
schemaLocation = location.String()
192193

193-
schemaLocation := location.String()
194194
if !location.IsAbs() {
195195
if !url.IsAbs() {
196196
return fmt.Errorf("Unable to resolve external schema %s through WSDL URL %s", schemaLocation, url)
@@ -200,55 +200,66 @@ func (g *GoWSDL) resolveXSDExternals(schema *XSDSchema, url *url.URL) error {
200200

201201
log.Println("Downloading external schema", "location", schemaLocation)
202202

203-
data, err := downloadFile(schemaLocation, g.ignoreTLS)
204-
newschema := new(XSDSchema)
205-
206-
err = xml.Unmarshal(data, newschema)
203+
data, err = downloadFile(schemaLocation, g.ignoreTLS)
207204
if err != nil {
208205
return err
209206
}
207+
}
208+
newschema := new(XSDSchema)
210209

211-
if len(newschema.Includes) > 0 &&
212-
maxRecursion > g.currentRecursionLevel {
210+
err = xml.Unmarshal(data, newschema)
211+
if err != nil {
212+
return err
213+
}
214+
215+
if len(newschema.Includes) > 0 || len(newschema.Imports) > 0 {
216+
if maxRecursion > g.currentRecursionLevel {
213217

214218
g.currentRecursionLevel++
215219

216220
//log.Printf("Entering recursion %d\n", g.currentRecursionLevel)
217221
g.resolveXSDExternals(newschema, url)
218222
}
223+
}
219224

220-
g.wsdl.Types.Schemas = append(g.wsdl.Types.Schemas, newschema)
225+
g.wsdl.Types.Schemas = append(g.wsdl.Types.Schemas, newschema)
221226

227+
return nil
228+
}
229+
230+
func (g *GoWSDL) resolveXSDExternals(schema *XSDSchema, url *url.URL) error {
231+
if len(schema.Includes) > 0 || len(schema.Imports) > 0 {
222232
if g.resolvedXSDExternals == nil {
223233
g.resolvedXSDExternals = make(map[string]bool, maxRecursion)
224234
}
225-
g.resolvedXSDExternals[schemaName] = true
226235
}
227236

228-
for _, incl := range schema.Imports {
229-
location, err := url.Parse(incl.SchemaLocation)
237+
var err error
238+
for _, incl := range schema.Includes {
239+
if g.resolvedXSDExternals[incl.SchemaLocation] {
240+
continue
241+
}
242+
243+
err = g.getSchema(incl.SchemaLocation, url)
230244
if err != nil {
231245
return err
232246
}
233247

234-
_, schemaName := filepath.Split(location.Path)
235-
if g.resolvedXSDExternals[schemaName] {
248+
g.resolvedXSDExternals[incl.SchemaLocation] = true
249+
}
250+
251+
for _, imp := range schema.Imports {
252+
if g.resolvedXSDExternals[imp.SchemaLocation] {
236253
continue
237254
}
238255

239-
data, err := ioutil.ReadFile(schemaName)
240-
newschema := new(XSDSchema)
241-
err = xml.Unmarshal(data, newschema)
256+
err = g.getSchema(imp.SchemaLocation, url)
242257
if err != nil {
243258
return err
244259
}
245260

246-
g.wsdl.Types.Schemas = append(g.wsdl.Types.Schemas, newschema)
261+
g.resolvedXSDExternals[imp.SchemaLocation] = true
247262

248-
if g.resolvedXSDExternals == nil {
249-
g.resolvedXSDExternals = make(map[string]bool, maxRecursion)
250-
}
251-
g.resolvedXSDExternals[schemaName] = true
252263
}
253264

254265
return nil

Diff for: soap_tmpl.go

+62-23
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,26 @@ func dialTimeout(network, addr string) (net.Conn, error) {
1313
1414
type SOAPEnvelope struct {
1515
XMLName xml.Name ` + "`" + `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"` + "`" + `
16-
Body SOAPBody ` + "`" + `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"` + "`" + `
16+
17+
Body SOAPBody
1718
}
1819
1920
type SOAPHeader struct {
21+
XMLName xml.Name ` + "`" + `xml:"http://schemas.xmlsoap.org/soap/envelope/ Header"` + "`" + `
22+
2023
Header interface{}
2124
}
2225
2326
type SOAPBody struct {
24-
Fault *SOAPFault ` + "`" + `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault,omitempty"` + "`" + `
25-
Content string ` + "`" + `xml:",innerxml"` + "`" + `
27+
XMLName xml.Name ` + "`" + `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"` + "`" + `
28+
29+
Fault *SOAPFault ` + "`" + `xml:",omitempty"` + "`" + `
30+
Content interface{} ` + "`" + `xml:",omitempty"` + "`" + `
2631
}
2732
2833
type SOAPFault struct {
34+
XMLName xml.Name ` + "`" + `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault"` + "`" + `
35+
2936
Code string ` + "`" + `xml:"faultcode,omitempty"` + "`" + `
3037
String string ` + "`" + `xml:"faultstring,omitempty"` + "`" + `
3138
Actor string ` + "`" + `xml:"faultactor,omitempty"` + "`" + `
@@ -43,6 +50,56 @@ type SOAPClient struct {
4350
auth *BasicAuth
4451
}
4552
53+
func (b *SOAPBody) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
54+
if b.Content == nil {
55+
return xml.UnmarshalError("Content must be a pointer to a struct")
56+
}
57+
58+
var (
59+
token xml.Token
60+
err error
61+
consumed bool
62+
)
63+
64+
Loop:
65+
for {
66+
if token, err = d.Token(); err != nil {
67+
return err
68+
}
69+
70+
if token == nil {
71+
break
72+
}
73+
74+
switch se := token.(type) {
75+
case xml.StartElement:
76+
if consumed {
77+
return xml.UnmarshalError("Found multiple elements inside SOAP body; not wrapped-document/literal WS-I compliant")
78+
} else if se.Name.Space == "http://schemas.xmlsoap.org/soap/envelope/" && se.Name.Local == "Fault" {
79+
b.Fault = &SOAPFault{}
80+
b.Content = nil
81+
82+
err = d.DecodeElement(b.Fault, &se)
83+
if err != nil {
84+
return err
85+
}
86+
87+
consumed = true
88+
} else {
89+
if err = d.DecodeElement(b.Content, &se); err != nil {
90+
return err
91+
}
92+
93+
consumed = true
94+
}
95+
case xml.EndElement:
96+
break Loop
97+
}
98+
}
99+
100+
return nil
101+
}
102+
46103
func (f *SOAPFault) Error() string {
47104
return f.String
48105
}
@@ -60,14 +117,7 @@ func (s *SOAPClient) Call(soapAction string, request, response interface{}) erro
60117
//Header: SoapHeader{},
61118
}
62119
63-
if request != nil {
64-
reqXml, err := xml.Marshal(request)
65-
if err != nil {
66-
return err
67-
}
68-
69-
envelope.Body.Content = string(reqXml)
70-
}
120+
envelope.Body.Content = request
71121
buffer := new(bytes.Buffer)
72122
73123
encoder := xml.NewEncoder(buffer)
@@ -118,28 +168,17 @@ func (s *SOAPClient) Call(soapAction string, request, response interface{}) erro
118168
119169
log.Println(string(rawbody))
120170
respEnvelope := new(SOAPEnvelope)
171+
respEnvelope.Body = SOAPBody{Content: response}
121172
err = xml.Unmarshal(rawbody, respEnvelope)
122173
if err != nil {
123174
return err
124175
}
125176
126-
body := respEnvelope.Body.Content
127177
fault := respEnvelope.Body.Fault
128-
if body == "" {
129-
log.Println("empty response body", "envelope", respEnvelope, "body", body)
130-
return nil
131-
}
132-
133-
log.Println("response", "envelope", respEnvelope, "body", body)
134178
if fault != nil {
135179
return fault
136180
}
137181
138-
err = xml.Unmarshal([]byte(body), response)
139-
if err != nil {
140-
return err
141-
}
142-
143182
return nil
144183
}
145184
`

0 commit comments

Comments
 (0)