-
Notifications
You must be signed in to change notification settings - Fork 5
/
read_write.go
192 lines (161 loc) · 4.3 KB
/
read_write.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
package epp
import (
"encoding/binary"
"encoding/xml"
"errors"
"fmt"
"io"
"math"
"net"
"time"
"aqwari.net/xml/xmltree"
"github.com/bombsimon/epp-go/types"
)
const (
rootLocalName = "epp"
)
// ReadMessage reads one full message from r.
func ReadMessage(conn net.Conn) ([]byte, error) {
// https://tools.ietf.org/html/rfc5734#section-4
var totalSize uint32
err := binary.Read(conn, binary.BigEndian, &totalSize)
if err != nil {
return nil, err
}
headerSize := binary.Size(totalSize)
contentSize := int(totalSize) - headerSize
// Ensure a reasonable time for reading the message.
err = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
if err != nil {
return nil, err
}
buf := make([]byte, contentSize)
_, err = io.ReadFull(conn, buf)
if err != nil {
return nil, err
}
return buf, nil
}
// WriteMessage writes data to w with the correct header.
func WriteMessage(conn net.Conn, data []byte) error {
// Begin by writing the len(b) as Big Endian uint32, including the
// size of the content length header.
// https://tools.ietf.org/html/rfc5734#section-4
contentSize := len(data)
headerSize := binary.Size(uint32(contentSize))
totalSize := contentSize + headerSize
// Bounds check.
if totalSize > math.MaxUint32 {
return errors.New("content is too large")
}
err := conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
if err != nil {
return err
}
err = binary.Write(conn, binary.BigEndian, uint32(totalSize))
if err != nil {
return err
}
_, err = conn.Write(data)
if err != nil {
return err
}
return nil
}
// ServerXMLAttributes defines the default attributes from the server response.
func ServerXMLAttributes() []xml.Attr {
return []xml.Attr{
{
Name: xml.Name{
Space: "",
Local: "xmlns",
},
Value: "urn:ietf:params:xml:ns:epp-1.0",
},
{
Name: xml.Name{
Space: "",
Local: "xmlns:xsi",
},
Value: "http://www.w3.org/2001/XMLSchema-instance",
},
{
Name: xml.Name{
Space: "",
Local: "xsi:schemaLocation",
},
Value: "urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd",
},
}
}
// ClientXMLAttributes defines the default attributes in the client request.
func ClientXMLAttributes() []xml.Attr {
return []xml.Attr{
{
Name: xml.Name{
Space: "",
Local: "xmlns",
},
Value: types.NameSpaceEPP10,
},
}
}
// Encode will take a type that can be marshalled to XML, add the EPP staring
// tag for all registered namespaces and return the XML as a byte slice.
func Encode(data interface{}, xmlAttributes []xml.Attr) ([]byte, error) {
// Marshal input data to XML, assume types implement required marshaling
// tags and features.
b, err := xml.Marshal(data)
if err != nil {
return nil, err
}
document, err := xmltree.Parse(b)
if err != nil {
return nil, err
}
addNameSpaceAlias(document, false)
// Replace the document root element with a proper EPP tag.
document.StartElement = xml.StartElement{
Name: xml.Name{
Space: "",
Local: rootLocalName,
},
Attr: xmlAttributes,
}
// Marshal the xmltree after fixing name spaces and attributes.
xmlBytes := xmltree.MarshalIndent(document, "", " ")
// Add XML header to the marshalled document.
xmlBytes = append([]byte(xml.Header), xmlBytes...)
return xmlBytes, nil
}
// addNameSpaceAlias will check each node/element in the XML tree and if the
// node has an xml.Name.Space value set an alias will be created and then added
// to all child nodes. The alias will only be setup for the root element.
func addNameSpaceAlias(document *xmltree.Element, nsAdded bool) *xmltree.Element {
namespaceAliases := map[string]string{
types.NameSpaceDomain: "domain",
types.NameSpaceHost: "host",
types.NameSpaceContact: "contact",
types.NameSpaceDNSSEC10: "sed",
types.NameSpaceDNSSEC11: "sec",
types.NameSpaceIIS12: "iis",
}
if document.Name.Space != "" {
alias, ok := namespaceAliases[document.Name.Space]
if !ok {
return nil
}
if !nsAdded {
xmlns := fmt.Sprintf("xmlns:%s", alias)
document.SetAttr("", xmlns, document.Name.Space)
// Namespace alias is now added so flip to true to skip child
// elements.
nsAdded = true
}
document.Name.Local = fmt.Sprintf("%s:%s", alias, document.Name.Local)
}
for i, child := range document.Children {
document.Children[i] = *addNameSpaceAlias(&child, nsAdded)
}
return document
}