forked from openrdap/rdap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
83 lines (65 loc) · 1.53 KB
/
file.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
// OpenRDAP
// Copyright 2017 Tom Harwood
// MIT License, see the LICENSE file.
package bootstrap
import (
"encoding/json"
"errors"
"net/url"
)
// File represents a bootstrap registry file (i.e. {asn,dns,ipv4,ipv6}.json).
type File struct {
// Fields from the JSON document.
Description string
Publication string
Version string
// Map of service entries to RDAP base URLs.
//
// e.g. in ipv6.json, the following mapping:
// "2c00::/12" => https://rdap.afrinic.net/rdap/,
// http://rdap.afrinic.net/rdap/.
Entries map[string][]*url.URL
// The file's JSON document.
JSON []byte
}
// NewFile constructs a File from a bootstrap registry file.
func NewFile(jsonDocument []byte) (*File, error) {
var doc struct {
Description string
Publication string
Version string
Services [][][]string
}
err := json.Unmarshal(jsonDocument, &doc)
if err != nil {
return nil, err
}
f := &File{}
f.Description = doc.Description
f.Publication = doc.Publication
f.Version = doc.Version
f.JSON = jsonDocument
f.Entries = make(map[string][]*url.URL)
for _, s := range doc.Services {
if len(s) != 2 {
return nil, errors.New("Malformed bootstrap (bad services array)")
}
entries := s[0]
rawURLs := s[1]
var urls []*url.URL
for _, rawURL := range rawURLs {
url, err := url.Parse(rawURL)
// Ignore unparsable URLs.
if err != nil {
continue
}
urls = append(urls, url)
}
if len(urls) > 0 {
for _, entry := range entries {
f.Entries[entry] = urls
}
}
}
return f, nil
}