【问题标题】:cURL hanging for 15 seconds when sending HEAD requests发送 HEAD 请求时 cURL 挂起 15 秒
【发布时间】:2012-01-08 01:58:36
【问题描述】:

背景

我一直在使用timewgetcurl 等工具通过CLI 对一些HTTP 请求进行计时,如下所示:

/usr/bin/time -v wget --spider http://localhost/index
/usr/bin/time -v curl http://localhost/index 2>&1 > /dev/null

我注意到,在使用 curl 时,我仅在第一个请求时获得了与 wget 相似的响应时间,而在后续请求中的响应时间要短得多,就好像对 curl 的响应是从cache 和 wget 没有。

经过调查,我发现在指定--spider 时,wget 发出HEAD 请求,如下所示,这可以解释为什么使用wget 绕过缓存:

请求

HEAD /index HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: localhost
Connection: Keep-Alive

回应

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。因此,我试图用curl 模拟HEAD 请求。

问题

我设法用curl 发送了HEAD 请求,如下所示:

curl "http://localhost/index" --request "HEAD" -H "Connection: Keep-Alive" -0

请求

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

回应

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

尽管请求/响应看起来没问题,但当我在使用 tcpdump 嗅探时执行上述 curl 命令时,我可以看到服务器立即响应,但是 我的 curl 命令始终保持挂起状态正好 15 秒 这显然是一个大问题,因为我正在尝试为我的 curl 命令计时(仅供参考,在服务器未正确处理 HEAD 并返回 @ 之前,我曾经获得 curl: (18) transfer closed with 3 bytes remaining to read 987654351@ 没有返回任何内容,但并非一切正常)。

我尝试使用--max-time--speed-time 参数来让curl 在收到200 OK 后立即超时,但这没有区别。

问:如何使用 curl 发送 HEAD 请求,以使 curl 命令在收到服务器响应后立即停止?

【问题讨论】:

    标签: http curl http-headers wget head


    【解决方案1】:

    为什么不直接使用-I 选项?

    -I/--head
        (HTTP/FTP/FILE) Fetch the HTTP-header only! HTTP-servers feature
        the command HEAD which this uses to get nothing but  the  header
        of  a  document.  When used on a FTP or FILE file, curl displays
        the file size and last modification time only
    

    【讨论】:

    • 因为我不知道该选项 :) 太好了,这就是诀窍:发送 HEAD 命令并在收到 wget --spider 之类的标头后立即停止。
    • 一个更通用的选项(不是特定于 curl 的)是发送“连接:关闭”标头...不是保持活动状态。 -I 标志导致 curl 自身短路并关闭连接本身。 “Connection:close”告诉服务器在处理完请求后关闭连接。 “keep-alive”告诉它等待更多请求。 :)
    猜你喜欢
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2020-04-05
    • 2011-12-22
    相关资源
    最近更新 更多