分块编码实现的。NET(或至少是伪code)NET、code

2023-09-04 00:04:10 作者:西羊夕下

我写的HTTP / HTTPS请求原始TCP客户端,但我有问题,分块编码的响应。 HTTP / 1.1的要求,因此,我应该支持。

I wrote a raw TCP client for HTTP/HTTPS requests, however I'm having problems with chunked encoding responses. HTTP/1.1 is requirement therefore I should support it.

原TCP的是,我需要保持业务需求,因此,我不能切换到.NET的HttpWebRequest / HTTPWebResponse 的但是如果有办法将一个原始的HTTP请求/响应到HttpWebRequest的/ HTTPWebResponse该会的工作。

Raw TCP is a business requirement that I need to keep, therefore I can't switch to .NET HTTPWebRequest/HTTPWebResponse However if there is way to convert a RAW HTTP Request/Response into HTTPWebRequest/HTTPWebResponse that'd work.

推荐答案

要启动的最佳位置是在的 HTTP 1.1规范,其中规定了如何分块工作。特别是第3.6.1。

The best place to start is the http 1.1 specification, which lays out how chunking works. Specifically section 3.6.1.

3.6.1分块传输编码

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.

拖车允许发送者   包括附加的HTTP头   在邮件的最​​后领域。该   拖车头字段可以被用来   表明这头域   包括在拖车(见   14.40)。

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).

假设您已经阅读从响应头,并指向下一个字节流中的伪code会是这个样子:

Assuming that you have already read the header from the response and are pointing to the next byte in the stream your pseudo code would look something like this:

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。

This is ignoring the chunk-extension portion, but since it is delimited with a ";" it should be easy to split it out. This should be enough to get you started. Keep in mind that the chunksize string does not have a leading "0x".