net/http Get and Head assumes https when using a port?

polaris · · 494 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>From what I&#39;m seeing, it looks like as soon as I add a port number to a URL, <code>net/http</code> is assuming that it&#39;s <code>https://</code> even when I provided <code>http://</code>? I&#39;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 ( &#34;fmt&#34; &#34;net/http&#34; ) func main() { resp, err := http.Head(&#34;http://github.com&#34;) 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 ( &#34;fmt&#34; &#34;net/http&#34; ) func main() { resp, err := http.Head(&#34;http://github.com:80&#34;) 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&#39;m more curious for future debugging stuff.</p></pre>tomheng: <pre><p>just use tcpdump.</p></pre>jahayhurst: <pre><p>That&#39;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&#39;s bulkier than I wanted tho. I was hoping there was a hidden debug setting or something in net/http that I just hadn&#39;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&#39;t found yet.</p> </blockquote> <p>There is: import &#34;net/http/httptest&#34;</p></pre>jahayhurst: <pre><p>How did you retrieve the raw content?</p> <p>I&#39;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&#39;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&#39;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&#39;re following redirects. Maybe there&#39;s something else at play?</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

494 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传