This repository has been archived by the owner on Apr 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.go
69 lines (63 loc) · 1.77 KB
/
response.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
// Copyright 2009 The Ben Olive. All rights reserved.
// Use of this source code is governed by a BSD style
// license that can be found in the LICENSE file.
package xmlrpc
import (
"io"
"xml"
"strings"
"os"
)
type Response struct {
Value ParamValue
}
// This methods reads the xml representation of a response
// to a io.Writer. It is public mostly for debugging or alternate
// communication. To call a remote function you should use
// `xmlrpc.Call`.
func ReadResponse(in io.Reader) (Response, os.Error) {
p := tokenStream{xml.NewParser(in)}
p.next(false) // Discard <methodResponse>
t, err := p.next(false)
if err != nil {
return Response{}, err
}
resp, ok := t.(xml.StartElement)
if !ok {
return Response{}, error("Invalid Response")
}
switch strings.ToLower(resp.Name.Local) {
case "fault":
f, err := Fault{}.LoadXML(p.Parser)
if err == nil {
err = f.(Fault)
}
return Response{}, err
case "params":
p.next(false) // Eat <param>
p.next(false) // Eat <value>
a, err := parseMessage(p.Parser)
return Response{a}, err
}
return Response{}, error("Invalid Response")
}
func (r Response) SendXML(out io.Writer) {
io.WriteString(out, "<?xml version=\"1.0\"?>\n")
io.WriteString(out, "<methodResponse>")
io.WriteString(out, "<params>")
io.WriteString(out, "<param><value>")
io.WriteString(out, r.Value.ToXML())
io.WriteString(out, "</value></param>")
io.WriteString(out, "</params>")
io.WriteString(out, "</methodResponse>")
}
func (f Fault) SendXML(out io.Writer) {
io.WriteString(out, "<?xml version=\"1.0\"?>\n")
io.WriteString(out, "<methodResponse>")
io.WriteString(out, "<fault>")
io.WriteString(out, "<value>")
io.WriteString(out, f.ToXML())
io.WriteString(out, "</value>")
io.WriteString(out, "</fault>")
io.WriteString(out, "</methodResponse>")
}