0

当浏览器呈现使用分块编码传输的数据时,浏览器应该呈现原始数据而不添加块大小和 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

我没想到会看到块大小。

是否应该在浏览器中使用我们的不带编码信息的数据来呈现数据?

4

3 回答 3

1

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

于 2013-02-06T11:15:58.037 回答
0

您是否在标头中指定了内容编码?

于 2013-02-06T08:42:44.833 回答
0

代码显式发送该消息。生成器创建这些块:

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

于 2013-02-06T08:37:43.410 回答