-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.go
269 lines (244 loc) · 6.97 KB
/
util.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
package restconf
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/freeconf/yang/patch/xml"
"github.com/freeconf/yang/fc"
)
// SplitAddress takes a complete address and breaks it into pieces according
// to RESTCONF standards so you can use each piece in appropriate API call
// Example:
//
// http://server[:port]/restconf[=device]/module:path/here
func SplitAddress(fullurl string) (address string, module string, path string, err error) {
eoSlashSlash := strings.Index(fullurl, "//") + 2
if eoSlashSlash < 2 {
err = ErrBadAddress
return
}
eoSlash := eoSlashSlash + strings.IndexRune(fullurl[eoSlashSlash:], '/') + 1
if eoSlash <= eoSlashSlash {
err = ErrBadAddress
return
}
colon := eoSlash + strings.IndexRune(fullurl[eoSlash:], ':')
if colon <= eoSlash {
err = ErrBadAddress
return
}
moduleBegin := strings.LastIndex(fullurl[:colon], "/")
address = fullurl[:moduleBegin+1]
module = fullurl[moduleBegin+1 : colon]
path = fullurl[colon+1:]
return
}
func SplitUri(uri string) (module string, path string, err error) {
colon := strings.IndexRune(uri, ':')
if colon < 0 {
err = ErrBadAddress
return
}
module = uri[:colon]
if slash := strings.LastIndex(module, "/"); slash >= 0 {
module = module[slash+1:]
}
path = uri[colon+1:]
return
}
// FindDeviceIdInUrl picks out device id in URL
func FindDeviceIdInUrl(addr string) string {
segs := strings.SplitAfter(addr, "/restconf=")
if len(segs) == 2 {
post := segs[1]
return post[:len(post)-1]
}
return ""
}
// only call this when you know that no content has been sent to client
// otherwise go will emit error that you're trying to change header when
// it's too late. i think harmless, but still not what you intended and
// actuall error is eatten.
func handleErr(compliance ComplianceOptions, err error, r *http.Request, w http.ResponseWriter, mime MimeType) bool {
if err == nil {
return false
}
fc.Debug.Printf("web request error [%s] %s %s", r.Method, r.URL, err.Error())
msg := err.Error()
code := fc.HttpStatusCode(err)
if !compliance.SimpleErrorResponse {
errResp := errResponse{
Type: "protocol",
Tag: decodeErrorTag(code, err),
Path: decodeErrorPath(r.RequestURI),
Message: msg,
}
var buff bytes.Buffer
if mime.IsXml() {
emsg := struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:yang:ietf-restconf errors"`
Errors []errResponse `xml:"error"`
}{
Errors: []errResponse{errResp},
}
if eerr := xml.NewEncoder(&buff).Encode(emsg); eerr != nil {
fc.Err.Printf("error encoding xml error response %s", eerr)
}
} else {
emsg := map[string]interface{}{
"ietf-restconf:errors": map[string]interface{}{
"error": []errResponse{errResp},
},
}
if eerr := json.NewEncoder(&buff).Encode(emsg); eerr != nil {
fc.Err.Printf("error encoding json error response %s", eerr)
}
}
msg = buff.String()
}
w.Header().Set("Content-Type", string(mime))
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
fmt.Fprintln(w, msg)
return true
}
// https://datatracker.ietf.org/doc/html/rfc8040#section-7
func decodeErrorTag(code int, _err error) string {
// This is bare minimum to return formatted error message response.
// but also all that can be done until more error types are defined
// beyond the few in github.com/freeconf/yang/fc/err.go or a more
// flexible error handling is implemented
switch code {
case 409:
return "in-use"
case 400:
return "invalid-value"
case 401:
return "access-denied"
}
return "operation-failed"
}
func decodeErrorPath(fullPath string) string {
module, path, err := SplitUri(fullPath)
if err != nil {
fc.Debug.Printf("unexpected path '%s', %s", fullPath, err)
return fullPath
}
return fmt.Sprint(module, ":", path)
}
type errResponse struct {
Type string `json:"error-type" xml:"error-type"`
Tag string `json:"error-tag" xml:"error-tag"`
Path string `json:"error-path" xml:"error-path"`
Message string `json:"error-message" xml:"error-message"`
}
func ipAddrSplitHostPort(addr string) (host string, port string) {
bracket := strings.IndexRune(addr, ']')
dblColon := strings.Index(addr, "::")
isIpv6 := (bracket >= 0 || dblColon >= 0)
if isIpv6 {
if bracket > 0 {
host = addr[:bracket+1]
if len(addr) > bracket+2 {
port = addr[bracket+2:]
}
} else {
host = addr
}
} else {
colon := strings.IndexRune(addr, ':')
if colon < 0 {
host = addr
} else {
host = addr[:colon]
port = addr[colon+1:]
}
}
return
}
func appendUrlSegment(a string, b string) string {
if a == "" || b == "" {
return a + b
}
slashA := a[len(a)-1] == '/'
slashB := b[0] == '/'
if slashA != slashB {
return a + b
}
if slashA && slashB {
return a + b[1:]
}
return a + "/" + b
}
func shift(orig *url.URL, delim rune) (string, *url.URL) {
if orig.Path == "" {
return "", orig
}
copy := *orig
var segment string
segment, copy.Path = shiftInString(copy.Path, delim)
_, copy.RawPath = shiftInString(copy.RawPath, delim)
return segment, ©
}
func shiftInString(orig string, delim rune) (string, string) {
termPos := strings.IndexRune(orig, delim)
// deisgn decision : ignore when path starts with the delim
if termPos == 0 {
orig = orig[1:]
termPos = strings.IndexRune(orig, delim)
}
var shifted string
var segment string
if termPos < 0 {
segment = orig
// shifted = empty
} else {
segment = orig[:termPos]
shifted = orig[termPos+1:]
}
return segment, shifted
}
func shiftOptionalParamWithinSegment(orig *url.URL, optionalDelim rune, segDelim rune) (string, string, *url.URL) {
copy := *orig
var segment, optional string
// trickery here - mutating a copy of the URL
segment, optional, copy.Path = shiftOptionalParamWithinSegmentInString(copy.Path, optionalDelim, segDelim)
// NOTE: the segment and optional param are returned unescaped presumably because caller
// would want that. If not, keep these results and not the ones from above
_, _, copy.RawPath = shiftOptionalParamWithinSegmentInString(copy.RawPath, optionalDelim, segDelim)
return segment, optional, ©
}
// this will not work of unescaped paths that contain optionalDelim or segDelim in the part of the
// url it's trying to shift.
func shiftOptionalParamWithinSegmentInString(orig string, optionalDelim rune, segDelim rune) (string, string, string) {
termPos := strings.IndexRune(orig, segDelim)
// design decision : ignore when path starts with the delim
if termPos == 0 {
orig = orig[1:]
termPos = strings.IndexRune(orig, segDelim)
}
// find the next segment first...
var shifted string
var segment string
if termPos < 0 {
segment = orig
// shifted = empty
} else {
segment = orig[:termPos]
shifted = orig[termPos+1:]
}
// ...now look for optional param in the found segment
optPos := strings.IndexRune(segment, optionalDelim)
if optPos < 0 {
return segment, "", shifted
}
var optional string
if len(segment) > optPos+1 {
optional = segment[optPos+1:]
}
segment = segment[:optPos]
return segment, optional, shifted
}