doesnt work but I commit anyway lol
This commit is contained in:
parent
172b41dc91
commit
ef850fb51d
33
sparte.go
33
sparte.go
|
@ -11,22 +11,32 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// A better url parser see:
|
||||
// https://stackoverflow.com/questions/62083272/parsing-url-with-port-and-without-scheme
|
||||
func ParseRawURL(rawurl string) (u *url.URL, err error) {
|
||||
u, err := url.ParseRequestURI(rawurl)
|
||||
if err != nil || u.Host == "" {
|
||||
u, repErr := url.ParseRequestURI("https://" + rawurl)
|
||||
if repErr != nil {
|
||||
fmt.Printf("Could not parse raw url: %s, error: %v", rawurl, err)
|
||||
u = nil
|
||||
return
|
||||
}
|
||||
err = nil
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SpartanURL parses u and calls SpartanParsedURL with the parsed url
|
||||
func SpartanURL(u string) bool {
|
||||
// Parse URL
|
||||
parsed, err := url.Parse(u)
|
||||
parsed, err := ParseRawURL(u)
|
||||
if err != nil {
|
||||
fmt.Println("invalid url")
|
||||
return false
|
||||
}
|
||||
if parsed.Scheme == "" {
|
||||
// have to parse again
|
||||
// ignoring err since it shouldn't fail here if it succeeded above
|
||||
parsed, _ = url.Parse("spartan://" + u)
|
||||
}
|
||||
if parsed.Scheme != "spartan" {
|
||||
fmt.Printf("Unsupported scheme %s", parsed.Scheme)
|
||||
return false
|
||||
fmt.Println("Unsupported scheme")
|
||||
}
|
||||
return SpartanParsedURL(*parsed)
|
||||
}
|
||||
|
@ -35,9 +45,6 @@ func SpartanURL(u string) bool {
|
|||
func SpartanParsedURL(u url.URL) bool {
|
||||
host := u.Host
|
||||
// Connect to server
|
||||
if u.Port() == "" {
|
||||
host += ":300"
|
||||
}
|
||||
conn, err := net.Dial("tcp", host)
|
||||
if err != nil {
|
||||
fmt.Println("unable to connect to " + u.Host)
|
||||
|
|
Loading…
Reference in New Issue