http.NewRequest获取到了意外字符。

huangapple go评论94阅读模式
英文:

http NewRequest get unexpected characters

问题

我尝试使用以下代码解析一个页面:

client := &http.Client{}
profile := getEpiURL(login)

log.Print("Fetch " + profile)
req, err := http.NewRequest("GET", profile, nil)
if err != nil {
    log.Fatal(err)
}
req.AddCookie(cookieSessionIntra)
body, _ := httputil.DumpRequestOut(req, true)

其中getEpiURL函数如下:

func getEpiURL(login string) (url string) {
    url = "https://********/user/" + login + "/?format=json"
    return
}

当我查看输出时,profile变量是正确的,但在请求中似乎明显错误...

2016/11/24 12:53:53 Fetch https://******/user/le****in-vird@e*******/?format=json

然后请求的调试输出如下:

GET /user/%00l%0o%00.%00c0-%00v%00i%0*a%00r%00d%00@%00e%00i%00t%00e%00c%00.%000/?format=json
HTTP/1.1 Host: ****** User-Agent: Go-http-client/1.1 Cookie: PHPSESSID=
****** Accept-Encoding: gzip

英文:

I try to parse a page with this code :

client := &http.Client{}
profile := getEpiURL(login)

log.Print("Fetch " + profile)
req, err := http.NewRequest("GET", profile, nil)
if err != nil {
	log.Fatal(err)
}
req.AddCookie(cookieSessionIntra)
body, _ := httputil.DumpRequestOut(req, true)

With this as getEpiURL function :

func getEpiURL(login string) (url string) {
    url = "https://********/user/" + login + "/?format=json"
    return
}

And when i look at the output the profile variable is good, but in the Request it seems obviously wrong...

> 2016/11/24 12:53:53 Fetch https://******/user/le****in-vird@e*******/?format=json

Then the debug for the request prints me :
> GET /user/%00l%0o%00.%00c0-%00v%00i%0*a%00r%00d%00@%00e%00i%00t%00e%00c%00.%000/?format=json
> HTTP/1.1 Host: ****** User-Agent: Go-http-client/1.1 Cookie:
> PHPSESSID=
****** Accept-Encoding: gzip

答案1

得分: 1

我认为你的原始字符串中包含了NUL字符。在playground中尝试一下:

func main() {
    req, err := http.NewRequest("GET", "https://some/normal/path", nil)
    if err != nil {
        log.Fatal(err)
    }
    body, _ := httputil.DumpRequestOut(req, true)
    fmt.Printf("Hello, playground, %q", body)
}

你会得到:

"GET /normal/path HTTP/1.1\r\nHost: some...

现在,试试在字符串中插入NUL字符

req, err := http.NewRequest("GET", "https://some/
req, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0n
req, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0o
req, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0rmal/path"
, nil)

...

"GET /%00n%00o%00rmal/path HTTP/1.1\r\nHost: some...

不太确定你的字符串是怎么包含这些字符的。可以阅读更多关于百分比编码的信息。

英文:

I think your original string somehow contains NUL characters. Try this out in the playground:

func main() {
	req, err := http.NewRequest("GET", "https://some/normal/path", nil)
	if err != nil {
		log.Fatal(err)
	}
	body, _ := httputil.DumpRequestOut(req, true)
	fmt.Printf("Hello, playground, %q", body)
}

You'll get:

"GET /normal/path HTTP/1.1\r\nHost: some...

Now try it with a string in which you inserted NUL characters:

req, err := http.NewRequest("GET", "https://some/
req, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0n
req, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0o
req, err := http.NewRequest("GET", "https://some/\000n\000o\000rmal/path", nil)
0rmal/path", nil)

...

"GET /%00n%00o%00rmal/path HTTP/1.1\r\nHost: some...

Not quite sure how your string ended up containing those. Read more about percent encoding.

huangapple
  • 本文由 发表于 2016年11月24日 20:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/40786118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定