请教一个transfer-encoding :chunked问题

yumingzhe1012 2009-12-06 07:13:16
各位大侠,小弟新手,请教一下。
当一个http请求发出去之后,返回一个http的响应头,内容如下,
注:因为响应头太长了,就没有全部都复制过来,实际还有一部分,直到最后有个 0 。

请问,我如我如何得到响应正文的长度。这个3c2c是什么意思

HTTP/1.1 200 OK
Date: Sun, 06 Dec 2009 11:02:54 GMT
Server: Apache/2.0.52 (Red Hat)
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html;charset=gb2312

3c2c





<html>
<head>
<title>中国同辉 -更多精华文章</title>

<script language="JavaScript">
此部分为中间的内容,太长就省略了

0


...全文
2798 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
GodGPF 2012-10-29
  • 打赏
  • 举报
回复
请问楼主,这个chunked的解码函数还有么?是什么语言的?可否借小弟参考一下,最近需要用,谢谢
iqoo_guotao 2011-11-20
  • 打赏
  • 举报
回复
你粘贴的响应消息长度就是“3c2c”,不过是16进制。
此为chunked方式。可以参考RFC2616的详细描述。
yuanoooo 2011-10-18
  • 打赏
  • 举报
回复
chunked 表示后面的长度不确定 3c2c 是16进制数表示数据长度
feichexia 2011-06-21
  • 打赏
  • 举报
回复
自私的泥啊,来找答案没看到答案,囧~
我的问题是Ajax发送请求,响应中显示:
Transfer-Encoding: chunked
有谁能帮帮忙么?
jrjr200411 2010-09-28
  • 打赏
  • 举报
回复
你好,我看到了这个帖子,我也想知道答案,上面的
HTTP/1.1 200 OK
Date: Sun, 06 Dec 2009 11:02:54 GMT
Server: Apache/2.0.52 (Red Hat)
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html;charset=gb2312

3c2c





<html>
<head>
<title>中国同辉 -更多精华文章</title>

<script language="JavaScript">
此部分为中间的内容,太长就省略了

0
前面的“3c2c”和后面的“0”分别代表了什么意思啊
hbcui1984 2010-06-03
  • 打赏
  • 举报
回复
问题解决了,却不公布解决方法。。。。
yumingzhe1012 2009-12-09
  • 打赏
  • 举报
回复
感谢楼上各位,我已经成功的将chunked解码了。
WingForce 2009-12-07
  • 打赏
  • 举报
回复
刚好最近的工作是写一个轻量的httpserver,下面是rfc相关信息:

首先是message 长度的部分:
4.4 Message Length

The transfer-length of a message is the length of the message-body as it appears in the message; that is, after any transfer-codings have been applied. When a message-body is included with a message, the transfer-length of that body is determined by one of the following (in order of precedence):

1.Any response message which "MUST NOT" include a message-body (such as the 1xx, 204, and 304 responses and any response to a HEAD request) is always terminated by the first empty line after the header fields, regardless of the entity-header fields present in the message.

2.If a Transfer-Encoding header field (section 14.41) is present and has any value other than "identity", then the transfer-length is defined by use of the "chunked" transfer-coding (section 3.6), unless the message is terminated by closing the connection.

3.If a Content-Length header field (section 14.13) is present, its decimal value in OCTETs represents both the entity-length and the transfer-length. The Content-Length header field MUST NOT be sent if these two lengths are different (i.e., if a Transfer-Encoding

header field is present). If a message is received with both a
Transfer-Encoding header field and a Content-Length header field,
the latter MUST be ignored.

4.If the message uses the media type "multipart/byteranges", and the transfer-length is not otherwise specified, then this self- delimiting media type defines the transfer-length. This media type MUST NOT be used unless the sender knows that the recipient can parse it; the presence in a request of a Range header with multiple byte- range specifiers from a 1.1 client implies that the client can parse multipart/byteranges responses.

A range header might be forwarded by a 1.0 proxy that does not
understand multipart/byteranges; in this case the server MUST
delimit the message using methods defined in items 1,3 or 5 of
this section.

5.By the server closing the connection. (Closing the connection cannot be used to indicate the end of a request body, since that would leave no possibility for the server to send back a response.)

For compatibility with HTTP/1.0 applications, HTTP/1.1 requests containing a message-body MUST include a valid Content-Length header field unless the server is known to be HTTP/1.1 compliant. If a request contains a message-body and a Content-Length is not given, the server SHOULD respond with 400 (bad request) if it cannot determine the length of the message, or with 411 (length required) if it wishes to insist on receiving a valid Content-Length.

All HTTP/1.1 applications that receive entities MUST accept the "chunked" transfer-coding (section 3.6), thus allowing this mechanism to be used for messages when the message length cannot be determined in advance.

Messages MUST NOT include both a Content-Length header field and a non-identity transfer-coding. If the message does include a non- identity transfer-coding, the Content-Length MUST be ignored.

When a Content-Length is given in a message where a message-body is allowed, its field value MUST exactly match the number of OCTETs in the message-body. HTTP/1.1 user agents MUST notify the user when an invalid length is received and detected.

然后是chunked encoding部分:
3.6 Transfer Codings

Transfer-coding values are used to indicate an encoding transformation that has been, can be, or may need to be applied to an entity-body in order to ensure "safe transport" through the network. This differs from a content coding in that the transfer-coding is a property of the message, not of the original entity.

transfer-coding = "chunked" | transfer-extension
transfer-extension = token *( ";" parameter )

Parameters are in the form of attribute/value pairs.

parameter = attribute "=" value
attribute = token
value = token | quoted-string

All transfer-coding values are case-insensitive. HTTP/1.1 uses transfer-coding values in the TE header field (section 14.39) and in the Transfer-Encoding header field (section 14.41).

Whenever a transfer-coding is applied to a message-body, the set of transfer-codings MUST include "chunked", unless the message is terminated by closing the connection. When the "chunked" transfer- coding is used, it MUST be the last transfer-coding applied to the message-body. The "chunked" transfer-coding MUST NOT be applied more than once to a message-body. These rules allow the recipient to determine the transfer-length of the message (section 4.4).

Transfer-codings are analogous to the Content-Transfer-Encoding values of MIME [7], which were designed to enable safe transport of binary data over a 7-bit transport service. However, safe transport has a different focus for an 8bit-clean transfer protocol. In HTTP, the only unsafe characteristic of message-bodies is the difficulty in determining the exact body length (section 7.2.2), or the desire to encrypt data over a shared transport.

The Internet Assigned Numbers Authority (IANA) acts as a registry for transfer-coding value tokens. Initially, the registry contains the following tokens: "chunked" (section 3.6.1), "identity" (section 3.6.2), "gzip" (section 3.5), "compress" (section 3.5), and "deflate" (section 3.5).

New transfer-coding value tokens SHOULD be registered in the same way as new content-coding value tokens (section 3.5).

A server which receives an entity-body with a transfer-coding it does not understand SHOULD return 501 (Unimplemented), and close the connection. A server MUST NOT send transfer-codings to an HTTP/1.0 client.
3.6.1 Chunked Transfer Coding

The chunked encoding modifies the body of a message in order to transfer it as a series of chunks, each with its own size indicator, followed by an OPTIONAL trailer containing entity-header fields. This allows dynamically produced content to be transferred along with the information necessary for the recipient to verify that it has received the full message.

Chunked-Body = *chunk
last-chunk
trailer
CRLF

chunk = chunk-size [ chunk-extension ] CRLF
chunk-data CRLF
chunk-size = 1*HEX
last-chunk = 1*("0") [ chunk-extension ] CRLF

chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token | quoted-string
chunk-data = chunk-size(OCTET)
trailer = *(entity-header CRLF)

The chunk-size field is a string of hex digits indicating the size of the chunk. The chunked encoding is ended by any chunk whose size is zero, followed by the trailer, which is terminated by an empty line.

The trailer allows the sender to include additional HTTP header fields at the end of the message. The Trailer header field can be used to indicate which header fields are included in a trailer (see section 14.40).

A server using chunked transfer-coding in a response MUST NOT use the trailer for any header fields unless at least one of the following is true:

a)the request included a TE header field that indicates "trailers" is acceptable in the transfer-coding of the response, as described in section 14.39; or,

b)the server is the origin server for the response, the trailer fields consist entirely of optional metadata, and the recipient could use the message (in a manner acceptable to the origin server) without receiving this metadata. In other words, the origin server is willing to accept the possibility that the trailer fields might be silently discarded along the path to the client.

This requirement prevents an interoperability failure when the message is being received by an HTTP/1.1 (or later) proxy and forwarded to an HTTP/1.0 recipient. It avoids a situation where compliance with the protocol would have necessitated a possibly infinite buffer on the proxy.

An example process for decoding a Chunked-Body is presented in appendix 19.4.6.

All HTTP/1.1 applications MUST be able to receive and decode the "chunked" transfer-coding, and MUST ignore chunk-extension extensions they do not understand.
z569362161 2009-12-07
  • 打赏
  • 举报
回复
这和C版应该无关啊。
chuengchuenghq 2009-12-07
  • 打赏
  • 举报
回复
呵呵 你发个head请求,就会有这个域
WingForce 2009-12-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chuengchuenghq 的回复:]
没有content-length这个域吗?
[/Quote]
这个header不是必选的。。。
chuengchuenghq 2009-12-07
  • 打赏
  • 举报
回复
没有content-length这个域吗?
breezes2008 2009-12-06
  • 打赏
  • 举报
回复
不清楚,具体得搜索下http协议。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧