-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
57 lines (46 loc) · 1.35 KB
/
Copy pathhttp.go
File metadata and controls
57 lines (46 loc) · 1.35 KB
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
package antidote
import (
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func fetch(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}
func addHttpProtocolIfNotExists(url string) string {
if strings.Contains(url, "http://") || strings.Contains(url, "https://") {
return url
}
return "http://" + url
}
// normalizeSourceUrl converts relative URL's like '/css/foo/bar.css' into HTTP requestable URL's
// like 'http://domain.com/css/foo/bar.css' based on the source origin.
func normalizeSourceUrl(assetPath string, origin *url.URL) (string, error) {
s, err := url.Parse(assetPath)
if err != nil {
return "", err
}
// Remove '//' from assets. Ex: //foo.bar/baz.css => foo.bar/baz.css
if strings.HasPrefix(assetPath, "//") {
assetPath = strings.Replace(assetPath, "//", "", 1)
}
// Remove relative '..' paths. Ex: ../../app.css => app.css
assetPath = strings.Replace(assetPath, "../", "", -1)
// If the asset path doesn't contain a host (meaning it's a relative path), prefix it with the origin host.
if len(s.Host) == 0 {
assetPath = addHttpProtocolIfNotExists(origin.Host + "/" + assetPath)
} else {
assetPath = addHttpProtocolIfNotExists(assetPath)
}
return assetPath, nil
}