diff options
author | aqua <aqua@iserlohn-fortress.net> | 2021-07-29 13:41:38 +0300 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2021-07-29 13:41:38 +0300 |
commit | fa938b88c8889f294306a4171e7394a028d8d84e (patch) | |
tree | 3fb4300a57ac2c45d918a286b9393feea2087b93 /main.go | |
parent | Use cmd args to get request url (diff) | |
download | gemcat-fa938b88c8889f294306a4171e7394a028d8d84e.tar.xz |
Properly get hostname from requests
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 27 |
1 files changed, 23 insertions, 4 deletions
@@ -5,12 +5,26 @@ import ( "flag" "fmt" "log" + "net/url" "os" "strconv" + "strings" ) var port = 1965 +func get_host(path string) (string, error) { + if !strings.HasPrefix(path, "gemini://") { + path = "gemini://" + path + } + url, err := url.Parse(path) + if err != nil { + return "", err + } + + return url.Host + ":" + strconv.Itoa(port), nil +} + func usage() { fmt.Printf("Usage: %s [options] url\n", os.Args[0]) flag.PrintDefaults() @@ -18,9 +32,7 @@ func usage() { func main() { flag.Usage = usage - flag.IntVar(&port, "p", port, "server port number") - flag.Parse() if flag.NArg() != 1 { fmt.Printf("Got %d arguments, expected 1\n", flag.NArg()) @@ -28,14 +40,21 @@ func main() { os.Exit(1) } - url := flag.Arg(0) + ":" + strconv.Itoa(port) - request := "gemini://" + flag.Arg(0) + "/\r\n" + url, err := get_host(flag.Arg(0)) + if err != nil { + panic("Error parsing hostname: " + err.Error()) + } + request := flag.Arg(0) + "\r\n" + if !strings.HasPrefix(request, "gemini://") { + request = "gemini://" + request + } conf := tls.Config{ InsecureSkipVerify: true, } log.Println("connecting to: " + url) + log.Println("request: " + request) conn, err := tls.Dial("tcp", url, &conf) |