forked from xellio/whois
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripe.go
80 lines (64 loc) · 1.61 KB
/
ripe.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
package whois
//
// parseRipeFormat parses whois output format of ripe.
//
func parseRipeFormat(in []byte) QueryResult {
result := NewQueryResult(in)
lastKey := ""
lastToken := []byte{}
currentToken := []byte{}
valueMode := false
currentRecord := make(map[string]string)
for i := 0; i < len(in); i++ {
tok := in[i]
switch {
case (tok == '%' && (i == 0 || (i > 0 && in[i-1] == '\n'))):
for ; ; i++ {
if in[i] == '\n' {
break
}
}
continue
case (tok == '\n' && (i > 0 && in[i-1] == '\n')):
// save old record and create a new one
if len(currentRecord) > 0 {
result.records = append(result.Records(), NewQueryRecord(currentRecord, 0))
currentRecord = make(map[string]string)
}
continue
case (tok == '\n' && len(in) > i+1 && in[i+1] == ' '):
i++
for ; in[i] == ' '; i++ {
// loop until next character found
}
i -= 2 // want to keep a space
continue
case (tok == '\n' && len(in) > i+1 && in[i+1] != ' '):
key := normalizeKey(string(lastToken))
value := normalizeValue(string(currentToken))
if key != "" && value != "" {
if lastKey == key {
currentRecord[key] += "\n" + value
} else {
currentRecord[key] = value
}
lastKey = key
}
valueMode = false
lastToken = []byte{}
currentToken = []byte{}
continue
case (tok == ':' && !valueMode):
valueMode = true
lastToken = currentToken
currentToken = []byte{}
continue
default:
currentToken = append(currentToken, tok)
}
}
if len(currentRecord) > 0 {
result.records = append(result.Records(), NewQueryRecord(currentRecord, 0))
}
return result
}