背景
我一直在使用 CLI 通过 CLI 对一些 HTTP 请求进行计时time
和工具,例如wget
and curl
如下:
/usr/bin/time -v wget --spider http://localhost/index
/usr/bin/time -v curl http://localhost/index 2>&1 > /dev/null
我注意到的是,当使用curl
,我得到的响应时间与wget
仅在第一个请求上,并且在后续请求上的时间要低得多,就好像对curl
是从缓存提供的并且wget
不是。
经过调查我发现指定时--spider
, wget
问题一个HEAD
下面附加的请求可以解释为什么缓存被绕过wget
:
Request
HEAD /index HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: localhost
Connection: Keep-Alive
Response
HTTP/1.1 200 OK
Date: Mon, 28 Nov 2011 14:45:59 GMT
Server: Apache/2.2.14 (Ubuntu)
Content-Location: index.php
Vary: negotiate,Accept-Encoding
TCN: choice
X-Powered-By: PHP/5.3.2-1ubuntu4.10
Set-Cookie: SESS421aa90e079fa326b6494f812ad13e79=16oqmug3loekjlb1tlvmsrtcr2; expires=Wed, 21-Dec-2011 18:19:19 GMT; path=/
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Mon, 28 Nov 2011 14:45:59 GMT
Cache-Control: store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
因为我正在做更高级的事情(在单独的文件中写入正文和标头、发布数据、将 cookie 保存在 jar 中...)我需要使用curl
代替wget
。因此我试图模仿HEAD
请求与curl
.
Issue
我设法发送了HEAD
请求与curl
如下:
curl "http://localhost/index" --request "HEAD" -H "Connection: Keep-Alive" -0
Request
HEAD /index HTTP/1.0
User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15
Host: localhost
Accept: */*
Connection: Keep-Alive
Response
HTTP/1.1 200 OK
Date: Mon, 28 Nov 2011 15:44:02 GMT
Server: Apache/2.2.14 (Ubuntu)
Content-Location: index.php
Vary: negotiate,Accept-Encoding
TCN: choice
X-Powered-By: PHP/5.3.2-1ubuntu4.10
Set-Cookie: SESS421aa90e079fa326b6494f812ad13e79=4001hcmhdbnkb9e2v8nok9lii1; expires=Wed, 21-Dec-2011 19:17:22 GMT; path=/
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Mon, 28 Nov 2011 15:44:02 GMT
Cache-Control: store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
尽管请求/响应看起来不错,但当我执行上述命令时curl
嗅探时发出命令tcpdump
我可以看到服务器立即响应,但是my curl
命令始终保持挂起状态 15 秒这显然是一个大问题,因为我正在尝试计时curl
命令(仅供参考,之前我曾经得到过curl: (18) transfer closed with 3 bytes remaining to read
当服务器没有处理时HEAD
正常并正在返回Content-Length: 3
不返回任何内容,但并非一切看起来都正常)。
我尝试玩--max-time
and --speed-time
论点有curl
收到后立即超时200 OK
但这没有什么区别。
问:我怎样才能发送HEAD
使用curl 请求的方式是curl 命令在收到服务器的响应后立即停止?