2012-10-16 70 views
0

某些播放实况视频的网站将使用HTTP实时流。默认情况下,Content-Length标题将被设置为2147483647忽略内容长度标题?

随着wget可以忽略使用命令行开关

--ignore-length   ignore `Content-Length' header field. 

的Firefox可以下载一个文件,而忽略了Content-Length头这个值?

+2

我认为这将有助于对你的问题有点背景。在我看来,HTTP服务器应该真的使用分块编码(如果你可以假设为HTTP/1.1),而不是将Content-Length设置为半任意数字。所描述的行为似乎打破了我。 – FooF

回答

1

我认为Web服务器行为不正确。如果内容长度未知,则HTTP服务器应使用分块编码。话虽如此,测试浏览器如何处理标准不兼容HTTP服务器很容易。

试验设置

与所描述的行为(我们使用Python)创建虚拟HTTP服务器:

#!/usr/bin/python 

import SocketServer 
import time 

class MyHttpHandler(SocketServer.BaseRequestHandler): 
    def send_stuff(self, msg): 
     print msg 
     self.request.send(msg) 

    def handle(self): 
     print("REQUEST: <<<" + self.request.recv(4096) + ">>>") 
     headers = """HTTP/1.1 200 OK\r 
Content-Length: 2147483647\r 
Content-Type: text/html\r 
rn""" 
     self.send_stuff(headers) 
     self.send_stuff("<html><body>n") 
     for count in range(1,10): 
      self.send_stuff("Hello, firefox!<p>n") 
      time.sleep(1) 
     self.send_stuff("</body></html>n") 

if __name__ == "__main__": 
    HOST, PORT = "0.0.0.0", 8888 
    server = SocketServer.TCPServer((HOST, PORT), MyHttpHandler) 
    server.serve_forever() 

(改编自http://docs.python.org/3.3/library/socketserver.html)。

说明

这是示例程序的作用:

  1. 等待传入TCP连接
  2. 读取请求(self.request.recv(4096)
  3. 写入响应标头(使用Content-Length: 2147483647
  4. 写十次Hello, Firefox!<p>睡觉1秒b在写入
  5. 关闭连接(通过从handle()方法返回。

插图

这里的说明的响应看起来像:

$ curl -i 10.20.32.85:8888/ 
HTTP/1.1 200 OK 
Content-Length: 2147483647 
Content-Type: text/html 

<html><body> 
Hello, firefox!<p> 
Hello, firefox!<p> 
Hello, firefox!<p> 
Hello, firefox!<p> 
Hello, firefox!<p> 
Hello, firefox!<p> 
Hello, firefox!<p> 
Hello, firefox!<p> 
Hello, firefox!<p> 
</body></html> 

Hello, firefox!线之间有一个一周秒钟的暂停,用10秒钟将完成接收内容)。

问题的答案

我的Firefox是17.0.1版本,(例如使用URL http://localhost:8888/这个例子似乎等待TCP关闭。 (相比之下,Chromium浏览器不会显示任何内容。)由于在浏览器中实现了缓存语义和动态渲染,这种行为似乎与较大内容不同。

测试 - 变化

我修改了handler()

def handle(self): 
     print("REQUEST: <<<" + self.request.recv(4096) + ">>>") 
     headers = """HTTP/1.1 200 OK\r 
Content-Length: 2147483647\r 
Content-Type: text/html\r 
\r\n""" 
     self.send_stuff(headers) 
     self.send_stuff("<html><body>\n") 
     for i in range(1,11): 
      for j in range(1,11): 
       self.request.send("%d:%d: Hello, firefox!<p>\n" % (i, j)) 
       #self.send_stuff("Hello, firefox!<p>\n") 
      print "i=%d" % i 
      time.sleep(3) 
     self.send_stuff("</body></html>\n") 
     time.sleep(10) 

具有较大的内容来测试浏览器的行为。在TCP关闭之前,我看到的内容已经是 (i,j) = (6,10)

TL; DR

随着小内容主体,火狐(17.0.1版)等待TCP关闭渲染任何东西之前,但如果内容主体是较大的,将开始渲染之前已经TCP连接关闭。请注意,这只能测试HTML内容,而不同的内容类型行为可能会有所不同。