Parsing URL with port and without scheme

user8421208 :

I am trying to parse a URL in Go and get host and scheme from the URL. But while parsing the URL with port and without scheme I am getting unexpected result.

u, err := url.ParseRequestURI("hello.com:81")
fmt.Println("host :",u.Host)
fmt.Println("scheme :",u.Scheme)

I am getting unexpected result

host :
scheme: hello.com

I wanted this instead

host : hello.com:80
scheme:
Nameless :

According to go doc, The general url form represented is:

[scheme:][//[userinfo@]host][/]path[?query][#fragment]

URLs that do not start with a slash after the scheme are interpreted as:

scheme:opaque[?query][#fragment]

Your URL is parsed as the second format.

You can use this method to get the result expected as yours. In the function, if there is no scheme in URL we add it then again parse it to get the expected result.

func parseRawURL(rawurl string) (domain string, scheme string, 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)
            return
        }
        domain = u.Host
        err = nil
        return
    }

    domain = u.Host
    scheme = u.Scheme
    return
}

You can try it out in go playground

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related