-
Notifications
You must be signed in to change notification settings - Fork 5
/
urlUtils.go
70 lines (65 loc) · 1.33 KB
/
urlUtils.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
package gojsonld
import (
"net/url"
"path/filepath"
"strings"
)
func resolve(base, ref *string) (*string, error) {
if isNil(base) || *base == "" {
if isNil(ref) {
return nil, nil
}
returnValue := *ref
return &returnValue, nil
}
if isNil(ref) || strings.Trim(*base, "") == "" {
returnValue := *base
return &returnValue, nil
}
baseUrl, baseErr := url.Parse(*base)
refUrl, refErr := url.Parse(*ref)
if !isNil(baseErr) {
return nil, baseErr
}
if !isNil(refErr) {
return nil, refErr
}
resolvedUrl := baseUrl.ResolveReference(refUrl)
returnValue := resolvedUrl.String()
return &returnValue, nil
}
func removeBase(base, ref string) (string, error) {
if base == "" {
return ref, nil
}
baseUrl, baseErr := url.Parse(base)
refUrl, refErr := url.Parse(ref)
if baseErr != nil {
return "", baseErr
}
if refErr != nil {
return "", refErr
}
if baseUrl.Host != refUrl.Host || baseUrl.Scheme != refUrl.Scheme {
return ref, nil
}
rel, err := filepath.Rel(baseUrl.Path, refUrl.Path)
if err != nil {
return "", err
}
if !strings.HasSuffix(base, "/") {
if rel == "." {
rel = ""
} else if rel == ".." {
rel = "./"
} else if rel == "../.." {
rel = "../"
} else if strings.HasPrefix(rel, "../") {
rel = rel[3:]
}
}
refUrl.Host = ""
refUrl.Scheme = ""
refUrl.Path = rel
return refUrl.String(), nil
}