<p>From what I'm seeing, it looks like as soon as I add a port number to a URL, <code>net/http</code> is assuming that it's <code>https://</code> even when I provided <code>http://</code>? I've created two test files included below.</p>
<p>This one works just fine, returning <code>github.com</code>.</p>
<pre><code>package main
import (
"fmt"
"net/http"
)
func main() {
resp, err := http.Head("http://github.com")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(resp.Request.URL.Host)
}
}
</code></pre>
<p>However, this one seems to always kick an error:</p>
<pre><code>package main
import (
"fmt"
"net/http"
)
func main() {
resp, err := http.Head("http://github.com:80")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(resp.Request.URL.Host)
}
}
</code></pre>
<p>This always results in an error being logged:</p>
<pre><code>Head https://github.com:80/: http: server gave HTTP response to HTTPS client
</code></pre>
<p>From what I can tell, as soon as you add a port number to a <code>http.Get()</code> or <code>http.Head()</code>, the library automatically switches over to an https client.</p>
<p>Can someone confirm this behavior? Does anyone already know more about this? Am I doing something obviously wrong here? And, should I report it as a bug?</p>
<hr/>**评论:**<br/><br/>tomheng: <pre><p>in fact, that is github server error. below is the raw content , which golang net/http send and github.com server respond.</p>
<pre><code>GET / HTTP/1.1
Host: github.com:80
User-Agent: Go-http-client/1.1
HTTP/1.1 301 Moved Permanently
Content-length: 0
Location: https://github.com:80/
Connection: close
</code></pre>
<p>golang net/http follow exactly to <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23" rel="nofollow">RFC2616#sec14.23</a>, sending Host header with a port. unfortunately, github.com server respond a wrong location header with that port.</p></pre>jahayhurst: <pre><p>How did you retrieve the raw content?</p>
<p>I'm more curious for future debugging stuff.</p></pre>tomheng: <pre><p>just use tcpdump.</p></pre>jahayhurst: <pre><p>That's too bad - but makes sense. I know you can use tcpdump for unencrypted stuff, and tcpdump + a self signed SSL install in your own trust chain to capture encrypted traffic - or similar.</p>
<p>That's bulkier than I wanted tho. I was hoping there was a hidden debug setting or something in net/http that I just hadn't found yet.</p></pre>nowayno: <pre><blockquote>
<p>I was hoping there was a hidden debug setting or something in net/http that I just hadn't found yet.</p>
</blockquote>
<p>There is: import "net/http/httptest"</p></pre>jahayhurst: <pre><p>How did you retrieve the raw content?</p>
<p>I'm more curious for future debugging stuff.</p></pre>TRAPFLAG_8: <pre><p>Seems like thats github trying to upgrade you. http.Head eventually calls this: <a href="https://golang.org/src/net/http/client.go?s=18195:18244#L427" rel="nofollow">https://golang.org/src/net/http/client.go?s=18195:18244#L427</a> look at line 427</p></pre>jahayhurst: <pre><p>Yeah, you had it - or at least, enough of it to put me on the right path.</p>
<p>When my Head gets upgraded to https, I'm getting a location header back of:</p>
<pre><code>Location: https://github.com
</code></pre>
<p>But it looks like <code>net/http</code> is retaining the manually defined port - 80 - and so it's sending an HTTPS request there.</p>
<p>[edit] I removed some stuff that just seems confusing here and not helpful [/edit]</p></pre>jahayhurst: <pre><p>It would seem normal to explicitly follow the Location header you get back when you're following redirects. Maybe there's something else at play?</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传