linux curl命令验证服务器断点续传支持

时间:2022-05-22 18:04:35

有个同事说,发现现在对外下载安装包的服务器不支持断点续传,我听了一阵纳闷,lighttpd server对于静态文件应该默认支持断点续传的,登机器查看lighttpd配置文件发现

linux curl命令验证服务器断点续传支持

对断点续传的支持被禁用了,lighttpd的说明里对该配置是这样表述的:

server.range-requests

Allowed values: enable , disable

Default: enable

This option defines whether range requests are allowed or not.

Range request are requests of one or more sub-ranges of a file. Range requests are very helpful for resuming interrupted downloads and fetching small portions of huge files.

对于PDF还有特殊的说明,断点续传pdf的时候会crash

Note: Adobe Acrobat Reader can crash when it tries to open a PDF file if range requests are enabled.

用以下匹配规则设置对pdf文件断点续传的禁用

$HTTP["url"] =~ "\.pdf$" {
server.range-requests = "disable"
}

然后,想怎么能马上检查一个服务器是否支持断点续传,用curl实现

$ curl -r 0-1 -o range_test.part1 'url'

其中url为文件的下载地址

如果在目录下生成了一个2字节大小的 range_test.part1 文件,那么说明服务器支持断点续传,如果把整个文件拉下来了,说明不支持


刚写完就被同事说直接curl -I 就能马上看出来服务器是否支持断点续传,执行

$ curl -I 'url'

看返回的http头信息,如果有 Accept-Ranges: bytes 表示服务器支持Range请求,以及服务器所支持的单位是字节(这也是唯一可用的单位)。并且,服务器支持断点续传,以及支持同时下载文件的多个部分,也就是说下载工具可以利用范围请求加速下载该文件。如果有 Accept-Ranges: none 响应头表示服务器不支持范围请求。

例如:

$ curl -I http://zhangmenshiting.baidu.com/data2/music/118358164/14385500158400128.mp3

返回

HTTP/1.1 200 OK
Accept-Ranges: bytes
Last-Modified: Tue, 22 Apr 2014 12:42:15 GMT
Expires: Sun, 25 May 2014 11:22:57 GMT
x-bs-version: D18E23AE8230A245A8EB6B77EFA5B92D
ETag: d5bd29010e1bf1c861d4b34f0f74a968
Content-Type: audio/mpeg
x-bs-request-id: MTAuNDYuMTU4LjIxOjgwODA6MTQ1MzE2ODg3ODoyNS9BcHIvMjAxNCAxOToyMjo1NyA=
Content-Disposition: attachment; filename="ʱ¼䶼ȥń¶魭p3"
x-bs-meta-crc32: 3366589278
Content-MD5: d5bd29010e1bf1c861d4b34f0f74a968
x-bs-client-ip: MTE1LjIzOS4yMTIuMTMz
x-bs-uncopyable: enable
Cache-Control: max-age=2592000
Content-Length: 3537110
Connection: close
Date: Fri, 25 Apr 2014 11:22:57 GMT
Server: BaiduBS

说明服务器支持范围请求和断点续传,换一个

$ curl -I http://www.taobao.com

返回

HTTP/1.1 200 OK
Server: Tengine
Date: Fri, 25 Apr 2014 11:26:06 GMT
Content-Type: text/html; charset=gbk
Connection: keep-alive
Vary: Accept-Encoding
Expires: Fri, 25 Apr 2014 12:26:06 GMT
Cache-Control: max-age=3600

就不支持