Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/url: URL.String() double encodes path #31835

Closed
LouisMT opened this issue May 4, 2019 · 4 comments
Closed

net/url: URL.String() double encodes path #31835

LouisMT opened this issue May 4, 2019 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@LouisMT
Copy link

LouisMT commented May 4, 2019

What version of Go are you using (go version)?

$ go version
go version go1.10.4 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/louis/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/louis/go"
GORACE=""
GOROOT="/usr/lib/go-1.10"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.10/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build814848893=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"log"
	"net/url"
)

func main() {
	apiURL, _ := url.Parse("https://gitlab.com")
	apiURL.Path = "api/v4/projects/0/repository/files/some%2Ffile/raw"
	log.Println(apiURL.String())
}

https://play.golang.org/p/SFl-9urTdRL

What did you expect to see?

(%2F)

2009/11/10 23:00:00 https://gitlab.com/api/v4/projects/0/repository/files/some%2Ffile/raw

What did you see instead?

(%252F)

2009/11/10 23:00:00 https://gitlab.com/api/v4/projects/0/repository/files/some%252Ffile/raw

I have to build an URL that contains both slashes and encoded slashes, because I'm using the GitLab API:

GET /projects/:id/repository/files/:file_path/raw

Parameters:

  • file_path (required) - Url encoded full path to new file. Ex. lib%2Fclass%2Erb
  • ref (required) - The name of branch, tag or commit

But when I call URL.String(), the path is escaped again. I read that this is intended behavior:

To obtain the path, String uses u.EscapedPath().

However, there seems to be no way to convert the URL to a string without automatically escaping the path. I think this should be an option for the URL.String() method, or there should be an URL.RawString() method of some kind.

Related: #25208, #10887

@crvv
Copy link
Contributor

crvv commented May 6, 2019

This is not about the String() method. The problem is what the URL is.
Why not use https://play.golang.org/p/eQzYBMgUQtz ?
it returns the expected string.

@LouisMT
Copy link
Author

LouisMT commented May 12, 2019

Because I'm building the URL dynamically. In my example, I could've used url.Parse, but it was just to show the issue in a simple way. In my application, I'm building the URL by setting various properties of the URL object. That way it's not possible to do this.

@andybons
Copy link
Member

@bradfitz

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 13, 2019
@andybons andybons added this to the Unplanned milestone May 13, 2019
@bradfitz
Copy link
Contributor

Sounds like you just need to use RawPath.

@golang golang locked and limited conversation to collaborators May 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants