-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.go
52 lines (43 loc) · 1.22 KB
/
clean.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
package main
import (
"fmt"
"log"
"net/url"
"strings"
)
// ensureLeadingSlash ensures the href starts with a '/'.
func ensureLeadingSlash(href string) string {
if !strings.HasPrefix(href, "/") {
return "/" + href
}
return href
}
// clean takes a host URL and a href, and returns the fully formatted URL.
func clean(host, href string) (string, error) {
relativeURL, err := parseURL(href)
if err != nil {
log.Printf("Could not parseHREF: %q\n", href)
return href, err
}
// Ignore fragment urls
if relativeURL.Fragment != "" || relativeURL.RawFragment != "" {
return "", fmt.Errorf("ERROR! Url cannot be a fragment\n")
}
if relativeURL.RawQuery != "" {
return "", fmt.Errorf("ERROR! URL With Query Params Cannot Be Cleaned %q\n", href)
}
// Return the href if it is already a full URL.
if relativeURL.Scheme != "" {
return href, nil
}
hostUrl, err := url.Parse(host)
if err != nil {
log.Printf("Could not parse host URL: %q\n", host)
return href, err
}
// Ensure the href starts with a '/' if it's a relative path.
href = ensureLeadingSlash(href)
// Construct the full URL using the host's scheme and host.
var source = "%s://%s%s"
return fmt.Sprintf(source, hostUrl.Scheme, hostUrl.Host, href), nil
}