关于http:如何确定服务器是否支持Range标头?

How can I find out whether a server supports the Range header?

我一直在尝试通过使用Range标头值从特定点流式传输音频,但是我总是从一开始就正确理解歌曲。 我正在通过程序执行此操作,因此不确定问题出在我的代码中还是服务器上。

如何确定服务器是否支持Range标头参数?

谢谢。


HTTP规范定义它的方式,如果服务器知道如何支持Range标头,则它将。反过来,当它向您返回内容时,要求它返回带有Content-Range标头的206部分内容响应代码。否则,它将仅忽略请求中的Range标头,并返回200响应代码。

这似乎很愚蠢,但是您确定要制作有效的HTTP请求标头吗?通常,我忘记了在请求中指定HTTP / 1.1,或者忘记了指定范围说明符,例如" bytes"。

哦,如果您要做的只是检查,那么只需发送HEAD请求而不是GET请求即可。相同的标题,所有内容相同,只是" HEAD"而不是" GET"。如果收到206响应,您将知道支持Range,否则将收到200响应。


这是供其他人搜索如何执行此操作的。您可以使用curl:

1
curl -I http://exampleserver.com/example_video.mp4

在标题中,您应该看到

1
Accept-Ranges: bytes

您可以进一步测试并检索范围

1
curl --header"Range: bytes=100-107" -I http://exampleserver.com/example_vide0.mp4

在标题中,您应该看到

1
HTTP/1.1 206 Partial Content

1
2
Content-Range: bytes 100-107/10000000
Content-Length: 8

[您会看到文件的长度,而不是10000000]


尽管我回答这个问题有点晚,但我认为我的回答将对将来的访问者有所帮助。这是一个python方法,用于检测服务器是否支持范围查询。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def accepts_byte_ranges(self, effective_url):
   """Test if the server supports multi-part file download. Method expects effective (absolute) url."""
    import pycurl
    import cStringIO
    import re

    c = pycurl.Curl()
    header = cStringIO.StringIO()

    # Get http header
    c.setopt(c.URL, effective_url)
    c.setopt(c.NOBODY, 1)
    c.setopt(c.HEADERFUNCTION, header.write)
    c.perform()
    c.close()

    header_text = header.getvalue()
    header.close()

    verbose_print(header_text)

    # Check if server accepts byte-ranges
    match = re.search('Accept-Ranges:\\s+bytes', header_text)
    if match:
        return True
    else:
        # If server explicitly specifies"Accept-Ranges: none" in the header, we do not attempt partial download.
        match = re.search('Accept-Ranges:\\s+none', header_text)
        if match:
            return False
        else:
            c = pycurl.Curl()

            # There is still hope, try a simple byte range query
            c.setopt(c.RANGE, '0-0') # First byte
            c.setopt(c.URL, effective_url)
            c.setopt(c.NOBODY, 1)
            c.perform()

            http_code = c.getinfo(c.HTTP_CODE)
            c.close()

            if http_code == 206: # Http status code 206 means byte-ranges are accepted
                return True
            else:
                return False

一种方法是尝试并检查响应。在您的情况下,服务器似乎不支持范围。

或者,在URI上执行GET或HEAD,然后检查Accept-Ranges响应标头。


  • 您可以将GET方法与0-0 Range请求标头一起使用,并检查响应代码是否为206,该代码将响应
    响应正文的第一个和最后一个字节
  • 您还可以使用HEAD方法执行与第一个会话相同的操作,这将获得相同的响应标头和代码,而没有响应主体

Furthermore, you can check Accept-Ranges on the response header to judge whether it can support range, but please notice if the value is none on Accept-Ranges field, it means it can't support range, and if the response header doesn't have Accept-Ranges field you also can't finger out it can't support range from it.

您还需要知道另一件事,如果您正在使用带有GET方法的请求标头上使用0- Range来检查响应代码,则响应正文消息将自动缓存在TCP接收窗口上,直到充分。