summaryrefslogtreecommitdiff
path: root/internal/urls
diff options
context:
space:
mode:
Diffstat (limited to 'internal/urls')
-rw-r--r--internal/urls/url.go36
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
+}