不带端口的HTTP连接被重置

陪她去流浪 桃子 2019年03月02日 阅读次数:2912

我的一个网络小工具(???)在测试网络连接的时候,对建立主机端口的连接一视同仁,采用的同样的方式来构建目标URL。

代码是下面这样的:

u := "http://" + hostport + "/"

hostport见名知义,是域名+端口的组合。比如:http://www.example.com:80/是一个可能的结果。

这本不应该有任何问题,毕竟HTTP的默认端口就是80,加不加应该是没区别的。实际上,区别有一点:Host字段的值不一样。

以下是两者实际的请求头部:

GET / HTTP/1.1
Host: www.example.com:80

GET / HTTP/1.1
Host: www.example.com

到此为止,我仍然没不觉得这有什么问题,或者说Web服务器会区别对待这两者。直到……

直到我访问这个网址时:av.movie。 带端口的正确访问了,不带端口的连接被重置。

当我直接访问时:

$ curl -sv av.movie > /dev/null
* Rebuilt URL to: av.movie/
*   Trying 104.27.166.237...
* TCP_NODELAY set
* Connected to av.movie (104.27.166.237) port 80 (#0)
> GET / HTTP/1.1
> Host: av.movie
> User-Agent: curl/7.54.0
> Accept: */*
>
* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 0

当我带端口访问时:


$ curl -sv --header 'Host: av.movie:80' av.movie > /dev/null
* Rebuilt URL to: av.movie/
*   Trying 104.27.167.237...
* TCP_NODELAY set
* Connected to av.movie (104.27.167.237) port 80 (#0)
> GET / HTTP/1.1
> Host: av.movie:80
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 01 Mar 2019 16:21:08 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/7.2.14
< Server: cloudflare
<
{ [673 bytes data]
* Connection #0 to host av.movie left intact

可以明显地看到,一个被重置了,一个正常返回。

抓包看来也是同样的结果:

正常情况

不正常情况

奇怪的是,数据包是在返回的时候被重置的。

于是,我觉得,我又发现了什么…………

这篇文章的内容已被作者标记为“过时”/“需要更新”/“不具参考意义”。

标签:HTTP · cURL