[英]How should Transfer-encoding: chunked be rendered?
当浏览器呈现使用分块编码传输的数据时,浏览器应呈现原始数据,而无需添加分块大小和添加CRLF来编码数据,对吗?
以以下代码为例:
https://gist.github.com/josiahcarlson/3250376
我的浏览器(Chrome和FF)呈现
12
this is chunk: 0
12
this is chunk: 1
12
this is chunk: 2
12
this is chunk: 3
12
this is chunk: 4
12
this is chunk: 5
12
this is chunk: 6
12
this is chunk: 7
12
this is chunk: 8
12
this is chunk: 9
0
我没想到看到块大小。
是否应在浏览器中不使用端编码信息的情况下使用我们的数据进行渲染?
不需要HTTP 1.0客户端对分块数据进行解码。 python的BaseHTTPServer类发送的默认http版本是HTTP 1.0。 如果您发送的版本为1.1,则浏览器将按预期呈现数据。 我以为curl只是通过做正确的事来试图变得聪明,甚至认为服务器发送了错误的协议版本。
在发送响应之前,对代码进行修补以设置BaseHTTPServer实例的protocol_version属性。 在您的示例的第73行添加此代码。
self.protocol_version ='HTTP / 1.1'
有关HTTP 1.0和HTTP 1.1之间差异的更多详细信息,您可以参考此http://www8.org/w8-papers/5c-protocols/key/key.html
该代码显式发送该消息。 生成器创建以下块:
yield "this is chunk: %s\r\n"%i
然后将它们写入套接字
def write_chunk():
tosend = '%X\r\n%s\r\n'%(len(chunk), chunk)
self.wfile.write(tosend)
如果您愿意,可以发送任何您想要的东西。
因此,如果生成的块是"this is chunk: 0\\r\\n"
则write_chunk方法实际上会发送"18\\r\\nthis is chunk: 0\\r\\n\\r\\n"
"\\r\\n"
是转义序列,表示回车换行符。 或Windows的换行符版本。 在Linux上,您只能使用\\n
您是否在标题中指定了内容编码?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.