-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
43 lines (36 loc) · 889 Bytes
/
api.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
package usableurl
import (
"bytes"
"context"
"fmt"
"os/exec"
"regexp"
"strings"
"time"
)
const curlTimeout = 2 * time.Minute
func Sanitize(url string) string {
ctx, cancel := context.WithTimeout(context.Background(), curlTimeout)
defer cancel()
url = strings.TrimSpace(url)
cmd := exec.CommandContext(ctx, "curl", "-sLI", url)
bs, _ := cmd.Output()
if ctx.Err() == context.DeadlineExceeded {
fmt.Printf("Command timed out. We will still attempt to expand the %v. "+
"If unssuccesful url will be expanded to empty string\n", url)
}
buf := bytes.Buffer{}
buf.Write(bs)
re := regexp.MustCompile(`[Ll]ocation:\s*(.*)\s`)
res := re.FindAllStringSubmatch(buf.String(), -1)
bestURL := ""
for _, matches := range res {
match := matches[1]
if strings.HasPrefix(match, "http") {
if len(bestURL) < len(match) {
bestURL = match
}
}
}
return bestURL
}