3

我为 HTTP/HTTPS 请求编写了一个原始 TCP 客户端,但是我遇到了分块编码响应的问题。HTTP/1.1 是必需的,因此我应该支持它。

原始 TCP 是我需要保留的业务要求,因此我无法切换到 .NET HTTPWebRequest/HTTPWebResponse但是,如果有办法将原始 HTTP 请求/响应转换为有效的 HTTPWebRequest/HTTPWebResponse。

4

2 回答 2

5

最好的起点是http 1.1 规范,它阐述了分块的工作原理。特别是第 3.6.1 节。

3.6.1 分块传输编码

分块编码修改消息的主体,以便
将其作为一系列块传输,每个块都有自己的大小指示符,
后跟一个包含实体头字段的可选尾部。
这允许动态生成的内容与接收者验证其已
收到完整消息所需的信息一起传输。

   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)

chunk-size 字段是一串十六进制数字,表示
块的大小。分块编码以任何大小
为零的块结束,然后是尾部,以空行终止。

尾部允许发送者
在消息末尾包含额外的 HTTP 头字段。Trailer 头字段可用于指示尾中包含哪些头字段(参见第 14.40 节)。

假设您已经从响应中读取了标头并指向流中的下一个字节,您的伪代码将如下所示:

done = false;
uint8 bytes[];
while (!done)
{
  chunksizeString = readuntilCRLF(); // read in the chunksize as a string
  chunksizeString.strip(); // strip off the CRLF
  chunksize = chunksizeString.convertHexString2Int(); // convert the hex string to an integer.
  bytes.append(readXBytes(chunksize)); // read in the x bytes and append them to your buffer.
  readCRLF(); // read the trailing CRLF and throw it away.
  if (chunksize == 0)
     done = true; //

}
// now read the trailer if any
// trailer is optional, so it may be just the empty string
trailer = readuntilCRLF()
trailer = trailer.strip()
if (trailer != "")
   readCRLF(); // read out the last CRLF and we are done.

这是忽略块扩展部分,但因为它是用“;”分隔的 应该很容易将其拆分出来。这应该足以让你开始。请记住,块大小字符串没有前导“0x”。

于 2008-11-25T19:35:06.110 回答
1

为了将来的参考,我还发现了这个:

 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
于 2008-11-28T22:24:53.053 回答