首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正确的分块传输编码格式

正确的分块传输编码格式
EN

Stack Overflow用户
提问于 2012-01-31 09:43:58
回答 2查看 6.5K关注 0票数 0

我很好奇,与规范相比,分块数据的正确格式是什么,以及Twitter从他们的活动流中返回了什么。

当使用curl尝试从Twitter获取分块流时,curl报告:

代码语言:javascript
复制
~$ curl -v https://stream.twitter.com/1/statuses/sample.json?delimited=length -u ...:...
< HTTP/1.1 200 OK
< Content-Type: application/json
< Transfer-Encoding: chunked
<
1984
{"place":null,"text":...
1984
{"place":null,"text":...
1984
{"place":null,"text":...

我已经编写了一个基于Wikipedia info和HTTP规范的分块数据发射器(本质上是:\r\n\r\n),我的结果如下:

代码语言:javascript
复制
~$ curl -vN http://localhost:7080/stream
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Transfer-Encoding: chunked
< 
{"foo":{"bar":...
{"foo":{"bar":...
{"foo":{"bar":...

不同之处在于,Twitter似乎将字符串的长度作为块主体的一部分作为整数(与十六进制的值一起包含,该值也必须在那里),我想确保我没有遗漏什么。推特文档没有提到长度值,它不在他们的examples中,我也没有在规范中看到任何关于它的内容。

EN

回答 2

Stack Overflow用户

发布于 2012-01-31 16:33:13

如果您的代码没有发出长度信息,那么它显然是不正确的。参见http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.3.6.1

票数 0
EN

Stack Overflow用户

发布于 2019-08-08 15:32:36

RCF2616-19.4.6传输编码简介

代码语言:javascript
复制
A process for decoding the "chunked" transfer-coding (section 3.6) can be represented in pseudo-code as:
   length := 0
   read chunk-size, chunk-extension (if any) and CRLF
   while (chunk-size > 0) {
      read chunk-data and CRLF
      append chunk-data to entity-body
      length := length + chunk-size
      read chunk-size and CRLF
   }
   read entity-header
   while (entity-header not empty) {
      append entity-header to existing header fields
      read entity-header
   }
   Content-Length := length
   Remove "chunked" from Transfer-Encoding

正如RFC所说,块大小不会附加到实体主体。我已经阅读了curl(函数Curl_httpchunk_read)的源代码,并确保它跳过了块大小\r\n,只需将块大小的字节附加到body中即可。

twitter的回复是分块大小的,我想这是因为使用了https,整个数据都是加密的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9073393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档