Go: net/url: first path segment in URL cannot contain colon.

Created on 27 Feb 2017  Â·  16Comments  Â·  Source: golang/go

Description:

No Scheme, and the address format is ”IP: Port“ Address parsing failed.

OS Ver:
Mac OSX 10.12.3

Go Ver:
1.8

Go ENV:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/shibingli/Develop/Go_Code/webconsole"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/dy/lsbr_78131x3s9fjq15v8rg80000gn/T/go-build052845002=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

Code:

u, err := url.Parse("192.168.2.2:6688")
if nil != err {
       panic(err)
}
fmt.Println(u.Host, u.Port())

Error:

panic: parse 192.168.2.2:6688: first path segment in URL cannot contain colon

goroutine 1 [running]:
main.main()
    /Users/shibingli/Develop/Go_Code/webconsole/src/apibox.club/test/test.go:46 +0x138

Source:

https://github.com/golang/go/blob/release-branch.go1.8/src/net/url/url.go#L513

All 16 comments

Working as intended I think. That is not a URL.

On 27 Feb 2017, at 18:14, Eric Shi notifications@github.com wrote:

Description:

No Scheme, and the address format is ”IP: Port“ Address parsing failed.

OS Ver:
Mac OSX 10.12.3

Go Ver:
1.8

Go ENV:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/shibingli/Develop/Go_Code/webconsole"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/dy/lsbr_78131x3s9fjq15v8rg80000gn/T/go-build052845002=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
Code:

u, err := url.Parse("192.168.2.2:6688")
if nil != err {
panic(err)
}
fmt.Println(u.Host, u.Port())
Error:

panic: parse 192.168.2.2:6688: first path segment in URL cannot contain colon

goroutine 1 [running]:
main.main()
/Users/shibingli/Develop/Go_Code/webconsole/src/apibox.club/test/test.go:46 +0x138
Source:

https://github.com/golang/go/blob/release-branch.go1.8/src/net/url/url.go#L513

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

@davecheney

package main

import (
    "fmt"
    "net/url"
)

func main() {
    fmt.Println(url.Parse("www.google.com:443"))
    fmt.Println(url.Parse("192.168.1.2:443"))
}
www.google.com:443 <nil>
<nil> parse 192.168.1.2:443: first path segment in URL cannot contain colon

I don't make sure but it seems fail when the host is IP.

@mattn I think you're right。
@davecheney www.google.com: 443 and 192.168.1.2:443 should be the same.

Golang 1.7 version can do so.

https://github.com/golang/go/blob/release-branch.go1.8/src/net/url/url.go#L500

www.google.com is treated as scheme at this line.

In my opinion, this is not a bug because rfc3986 allow to use dot in scheme.

@shibingli if you want to parse such string, please use url.Parse("//192.168.1.2:443") instead.

@mattn Okay, I've done that. But I think it is normal to do that.

Closing as working-as-intended. It could be documented better. That is #18824.
But @mattn is right. The correct way is //192.168.1.2:44. Read #18824 for details.

@dsnet Thanks.

u, err := url.Parse("//192.168.2.2:6688")
if nil != err {
panic(err)
}
fmt.Println(u.Host, u.Port())

very ok

ERROR: failed to parse http://ddmwebrevo.digdes.com/#/document/ca061121-8679-4afb-bf96-5aefdcac2dbf : parse http://ddmwebrevo.digdes.com/: first path segment in URL cannot contain colon

@kirichenko Could you please show your minimal code to reproduce?

I'm sorry. There was a typo in URL (space in the begining).

Hi. I met the error: parse 127.0.0.1:65085/auth/v1.0/: first path segment in URL cannot contain colon. But 'localhost::65085/auth/v1.0/' was work.

first path segment in URL cannot contain colon

here is the url:
http://zhidao.baidu.com/special/view?id=a9105a24626975510000&preview=1

is there something wrong

This is a closed issue. Closed issues aren't tracked.

For questions about Go, see https://golang.org/wiki/Questions.

Was this page helpful?
0 / 5 - 0 ratings