-
Notifications
You must be signed in to change notification settings - Fork 5
/
loader.go
222 lines (205 loc) · 5.08 KB
/
loader.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
package conflate
import (
"io/ioutil"
"net"
"net/http"
pkgurl "net/url"
"os"
"regexp"
"runtime"
"strings"
"time"
)
var (
goos = runtime.GOOS
emptyURL = pkgurl.URL{}
getwd = os.Getwd
driveLetter = regexp.MustCompile(`^[A-Za-z]:.*$`)
)
type loader struct {
newFiledata func([]byte, pkgurl.URL) (filedata, error)
}
func (l *loader) loadURLsRecursive(parentUrls []pkgurl.URL, urls ...pkgurl.URL) (filedatas, error) {
var allData filedatas
for _, url := range urls {
data, err := l.loadURLRecursive(parentUrls, url)
if err != nil {
return nil, err
}
allData = append(allData, data...)
}
return allData, nil
}
func (l *loader) loadURLRecursive(parentUrls []pkgurl.URL, url pkgurl.URL) (filedatas, error) {
data, err := loadURL(url)
if err != nil {
return nil, err
}
fdata, err := l.newFiledata(data, url)
if err != nil {
return nil, err
}
return l.loadDatumRecursive(parentUrls, &url, fdata)
}
func (l *loader) loadDataRecursive(parentUrls []pkgurl.URL, data ...filedata) (filedatas, error) {
var allData filedatas
for _, datum := range data {
childData, err := l.loadDatumRecursive(parentUrls, nil, datum)
if err != nil {
return nil, err
}
allData = append(allData, childData...)
}
return allData, nil
}
func (l *loader) loadDatumRecursive(parentUrls []pkgurl.URL, url *pkgurl.URL, data filedata) (filedatas, error) {
if data.isEmpty() {
return nil, nil
}
if containsURL(url, parentUrls) {
return nil, makeError("The url recursively includes itself (%v)", url)
}
childUrls, err := toURLs(url, data.includes...)
if err != nil {
return nil, err
}
var newParentUrls []pkgurl.URL
newParentUrls = append(newParentUrls, parentUrls...)
if url != nil {
newParentUrls = append(newParentUrls, *url)
}
childData, err := l.loadURLsRecursive(newParentUrls, childUrls...)
if err != nil {
return nil, err
}
var allData filedatas
allData = append(allData, childData...)
allData = append(allData, data)
return allData, nil
}
func (l *loader) wrapFiledata(bytes []byte) (filedata, error) {
return l.newFiledata(bytes, emptyURL)
}
func (l *loader) wrapFiledatas(bytes ...[]byte) (filedatas, error) {
var fds []filedata
for _, b := range bytes {
fd, err := l.wrapFiledata(b)
if err != nil {
return nil, err
}
fds = append(fds, fd)
}
return fds, nil
}
func loadURL(url pkgurl.URL) ([]byte, error) {
if url.Scheme == "file" {
// attempt to load locally handling case where we are loading from fifo etc
b, err := ioutil.ReadFile(getPath(url.Path))
if err == nil {
return b, nil
}
}
client := http.Client{Transport: newTransport()}
resp, err := client.Get(url.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, makeError("Failed to load url : %v : %v", resp.StatusCode, url.String())
}
return data, err
}
func newTransport() *http.Transport {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
transport.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
return transport
}
func toURLs(rootURL *pkgurl.URL, paths ...string) ([]pkgurl.URL, error) {
var urls []pkgurl.URL
for _, path := range paths {
url, err := toURL(rootURL, path)
if err != nil {
return nil, err
}
urls = append(urls, url)
}
return urls, nil
}
func toURL(rootURL *pkgurl.URL, path string) (pkgurl.URL, error) {
if path == "" {
return emptyURL, makeError("The file path is blank")
}
var err error
if rootURL == nil {
rootURL, err = workingDir()
if err != nil {
return emptyURL, err
}
}
url, err := pkgurl.Parse(setPath(path))
if err != nil {
return emptyURL, wrapError(err, "Could not parse path")
}
if !url.IsAbs() {
url = rootURL.ResolveReference(url)
url.RawQuery = rootURL.RawQuery
}
return *url, nil
}
func containsURL(searchURL *pkgurl.URL, urls []pkgurl.URL) bool {
if searchURL == nil {
return false
}
for _, url := range urls {
if url == *searchURL {
return true
}
}
return false
}
func workingDir() (*pkgurl.URL, error) {
rootPath, err := getwd()
if err != nil {
return nil, err
}
rootURL, err := pkgurl.Parse("file://" + setPath(rootPath) + "/")
if err != nil {
return nil, err
}
return rootURL, nil
}
func setPath(path string) string {
if goos == "windows" {
// https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/
path = strings.Replace(path, `\`, `/`, -1)
path = strings.TrimLeft(path, `/`)
if driveLetter.MatchString(path) {
path = `/` + path
}
}
return path
}
func getPath(path string) string {
if goos == "windows" {
// https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/
path = strings.TrimLeft(path, `/`)
if !driveLetter.MatchString(path) {
path = `//` + path
}
path = strings.Replace(path, `/`, `\`, -1)
}
return path
}