From cf8da7439eaa6e0bd71b34a9e16fc682e76d2731 Mon Sep 17 00:00:00 2001 From: sloum Date: Wed, 25 Nov 2020 04:54:02 +0000 Subject: [PATCH] Switches to using the url package for url handling --- main.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 4567e8f..6c62b10 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "io" "io/ioutil" "net" + "net/url" "os" "os/user" "path/filepath" @@ -217,24 +218,26 @@ func findItemRow(name string, position int, csv [][]string) []string { func retrieve(addr string) (string, error) { addr = strings.TrimSpace(addr) - noprot := strings.Replace(addr, "gemini://", "", 1) - hostResource := strings.SplitN(noprot, "/", 2) - if strings.LastIndex(hostResource[0], ":") == -1 { - hostResource[0] = hostResource[0] + ":1965" + u, err := url.Parse(addr) + if err != nil { + return "", fmt.Errorf("Retrieve error, parse url: %s", err.Error()) + } + if u.Port() == "" { + u.Host = u.Host + ":1965" } conf := &tls.Config{ InsecureSkipVerify: true, MinVersion: tls.VersionTLS12, } - conn, err := tls.DialWithDialer(&net.Dialer{Timeout: timeout}, "tcp", hostResource[0], conf) + conn, err := tls.DialWithDialer(&net.Dialer{Timeout: timeout}, "tcp", u.Host, conf) if err != nil { return "", fmt.Errorf("TLS Dial Error: %s", err.Error()) } defer conn.Close() - send := "gemini://" + hostResource[0] + "/" + hostResource[1] + "\r\n" + send := u.String() + "\r\n" _, err = conn.Write([]byte(send)) if err != nil {