http 编码chunked

之前针对这个也不是很关注,这个chunked在nginx上是默认开启的,但是在apache上没有,所以当切换服务器的时候,同时遇到了这个问题,发现数据内容存在乱码。

定位我就知道应该是http header的问题,但是具体是什么也不是很清楚。


仔细查阅发现,这个chunked编码。

http以trunked编码方式传输的数据表示规则

一般HTTP通信时会使用是Content-Length头信息性来指定小,但是有时候无法确定信息大小,就要使用trunked编码动态的提供body内容的长度。

进行Chunked编码传输的HTTP数据要在消息头部设置:

Transfer-Encoding: chunked

表示Content Body将用chunked编码传输内容。

Chunked编码一般使用若干个chunk串连而成,最后由一个标明长度为0的chunk标示结束。
每个chunk分为头部正文两部分,
头部内容指定下一段正文的字符总数(非零开头的十六进制的数字)和数量单位(一般不写,表示字节).
正文部分就是指定长度的实际内容,两部分之间用回车换行(CRLF)隔开。
在最后一个长度为0的chunk中的内容是称为footer的内容,是一些附加的Header信息(通常可以直接忽略)。具体的chunk编码格式如下:

  1.   chunked -body = *chunk
  2.           "0" CRLF
  3.          footer
  4.          CRLF
  5.   chunk = chunk -size [ chunk -ext ] CRLF
  6.        chunk -data CRLF
  7.   hex -no -zero = <HEX excluding "0">
  8.   chunk -size = hex -no -zero *HEX
  9.   chunk -ext = * ( ";" chunk -ext -name [ "=" chunk -ext -value ] )
  10.   chunk -ext -name = token
  11.   chunk -ext -val = token | quoted - string
  12.   chunk -data = chunk -size (OCTET )
  13.   footer = *entity -header

RFC文档中的chunked解码过程如下:

  1.   length := 0
  2.   read chunk -size, chunk -ext ( if any ) and CRLF
  3.    while (chunk -size > 0 ) {
  4.       read chunk -data and CRLF
  5.       append chunk -data to entity -body
  6.       length := length + chunk -size
  7.       read chunk -size and CRLF
  8.    }
  9.   read entity -header
  10.    while (entity -header not empty ) {
  11.       append entity -header to existing header fields
  12.       read entity -header
  13.    }
  14.   Content -Length := length
  15.   Remove "chunked" from Transfer -Encoding

nginx服务器关闭chunked编码方式:

chunked_transfer_encoding off;


参考链接:

http://www.6san.com/759/

http://www.sunnyu.com/?p=175

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钱国正

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值