.NET 中的分块编码实现(或至少是伪代码) [英] Chunked Encoding Implementation in .NET (or at least pseudo code)

查看:32
本文介绍了.NET 中的分块编码实现(或至少是伪代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 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.

Raw TCP 是我需要保留的业务需求,因此我无法切换到 .NET HTTPWebRequest/HTTPWebResponse 但是,如果有办法将 RAW 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)

chunk-size 字段是一串表示
大小的十六进制数字块.分块编码是由大小为
的任何块结束零,然后是拖车,其中以空行结束.

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

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

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.

这忽略了块扩展部分,但因为它是用;"分隔的拆分它应该很容易.这应该足以让您入门.请记住,chunksize 字符串没有前导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".

这篇关于.NET 中的分块编码实现(或至少是伪代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆