英文:
Go http.Request Header on Redirect and Proxy
问题
根据这个帖子,当http.Client
发送请求进行重定向时,头部会被重置。
有一个解决方法如下:
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return fmt.Errorf("too many redirects")
}
if len(via) == 0 {
return nil
}
for attr, val := range via[0].Header {
if _, ok := req.Header[attr]; !ok {
req.Header[attr] = val
}
}
return nil
}
但我的问题是,如果我想通过代理服务器进行HTTP请求,我该如何做呢?
当HTTP请求经过代理服务器时,头部是否会被重置?我需要在代理中设置另一个http.Client
吗?
我使用了https://github.com/elazarl/goproxy
来设置我的代理服务器。
谢谢。
英文:
https://groups.google.com/forum/#!topic/golang-nuts/OwGvopYXpwE
As seen in this thread, when http.Client
sends requests to redirects, the header gets reset.
There is a workaround like:
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return fmt.Errorf("too many redirects")
}
if len(via) == 0 {
return nil
}
for attr, val := range via[0].Header {
if _, ok := req.Header[attr]; !ok {
req.Header[attr] = val
}
}
return nil
}
But my question is how do I do this if I want to http Request through my proxy server.
When http request goes through proxy server, does header get all reset? Do I have to set up another http.Client
in proxy?
I set up my proxy server using https://github.com/elazarl/goproxy
Thanks,
答案1
得分: 0
我简要查看了代码,看起来goproxy没有以任何特殊方式处理3XX响应 - 它们只是简单地路由回到客户端,以便它可以相应地做出反应。在你的情况下,你将发出另一个带有所有头部设置的请求。
如果这不是你关心的问题,你只是想知道代理的存在是否需要在客户端上进行任何修改,那么不,当使用代理时,http.Client
不会删除任何头部,它甚至不知道有代理参与。
你可以通过设置一个简单的服务器(例如使用Go)来轻松测试这些假设,该服务器返回一个重定向响应(到自身或某个nc
监听器)并打印所有头部。当通过你的代理将客户端路由到服务器时,你可以确保服务器端一切正常。
英文:
I looked briefly at the code, at it looks like goproxy doesn't handle 3XX responses in any special way – they're simply routed back to your client, so it can react accordingly. In your case, you'll issue another request with all the headers set.
If it wasn't your concern and you were just wondering if simply existence of proxy requires any hack on the client side, then no, http.Client
won't remove any headers when proxy is in use, it won't even know there's a proxy involved.
You can easily test these assumption by setting up you own a simple server (in Go for example) which is returning a redirect response (to itself or some nc
listener) and printing all the headers. When routing the client to the server through your proxy, you can make sure everything looks good on the server side.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论