diff options
author | Benjamin Chausse <benjamin@chausse.xyz> | 2024-09-06 19:59:21 -0400 |
---|---|---|
committer | Benjamin Chausse <benjamin@chausse.xyz> | 2024-09-06 19:59:21 -0400 |
commit | f903439cbc504b9a7b676eb8a1c4e1d4c95a61fc (patch) | |
tree | a6314a10fb122d0cd729c969d1711f6120c5544f /internal/urls |
Batman
Diffstat (limited to 'internal/urls')
-rw-r--r-- | internal/urls/url.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/internal/urls/url.go b/internal/urls/url.go new file mode 100644 index 0000000..bb76594 --- /dev/null +++ b/internal/urls/url.go @@ -0,0 +1,36 @@ +package urls + +import ( + "net/http" + "regexp" +) + +// Find takes in a string (i.e. a discord message) +// and returns a list of all the urls it has found in this +// string. +func Find(s string) []string { + // Regular expression to match URLs starting with https + re := regexp.MustCompile(`https://[^\s]+`) + + // Find all matching URLs + return re.FindAllString(s, -1) +} + +func Resolve(url string) (string, error) { + client := &http.Client{ + // Automatically follow redirects + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return nil + }, + } + + resp, err := client.Head(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + + // The final resolved URL after following redirects + finalURL := resp.Request.URL.String() + return finalURL, nil +} |