【发布时间】:2019-02-14 06:47:47
【问题描述】:
对不起,标题这么模糊,我真的不知道这个问题的标题是什么。 基本上,当我得到传输编码所告诉的分块流时,我会执行以下代码:
private IEnumerable<byte[]> ReceiveMessageBodyChunked() {
readChunk:
#region Read a line from the Stream which should be a Block Length (Chunk Body Length)
string blockLength = _receiverHelper.ReadLine();
#endregion
#region If the end of the block is reached, re-read from the stream
if (blockLength == Http.NewLine) {
goto readChunk;
}
#endregion
#region Trim it so it should end up with JUST the number
blockLength = blockLength.Trim(' ', '\r', '\n');
#endregion
#region If the end of the message body is reached
if (blockLength == string.Empty) {
yield break;
}
#endregion
int blockLengthInt = 0;
#region Convert the Block Length String to an Int32 base16 (hex)
try {
blockLengthInt = Convert.ToInt32(blockLength, 16);
} catch (Exception ex) {
if (ex is FormatException || ex is OverflowException) {
throw new Exception(string.Format(ExceptionValues.HttpException_WrongChunkedBlockLength, blockLength), ex);
}
throw;
}
#endregion
// If the end of the message body is reached.
if (blockLengthInt == 0) {
yield break;
}
byte[] buffer = new byte[blockLengthInt];
int totalBytesRead = 0;
while (totalBytesRead != blockLengthInt) {
int length = blockLengthInt - totalBytesRead;
int bytesRead = _receiverHelper.HasData ? _receiverHelper.Read(buffer, 0, length) : _request.ClientStream.Read(buffer, 0, length);
if (bytesRead == 0) {
WaitData();
continue;
}
totalBytesRead += bytesRead;
System.Windows.Forms.MessageBox.Show("Chunk Length: " + blockLengthInt + "\nBytes Read/Total:" + bytesRead + "/" + totalBytesRead + "\n\n" + Encoding.ASCII.GetString(buffer));
yield return buffer;
}
goto readChunk;
}
这样做是从流中读取应该是块长度的 1 行数据,在这里和那里进行一些检查,但最终将其转换为 Int32 Radix16 整数。
从那里它基本上创建了一个 int32 的字节缓冲区作为它的长度大小。
然后它继续从流中读取,直到它读取到与我们转换的 Int32 相同的数量。
这很好用,但是,无论出于何种原因,它在最后一次读取时响应不正确。
它将读取确切的字节数,因为块长度非常好,并且我期望的所有数据都被读取。但是它还再次读取了另一小块数据,这些数据已经在最后读取,导致我们说从<!DOCTYPE html> 到</html> ASWELL 的所有数据作为来自内部某处的一些数据,例如<form> e.t.c
这是发生的一个例子:
如您所见,不应该从阅读中返回突出显示的红色文本!它应该在</html> 结束。
为什么 Chunk's Length 对我说谎,我怎样才能找到合适的尺寸来阅读?
【问题讨论】:
-
如果有人想对此进行测试并且知道如何为此测试手动获取流设置,请针对始终需要 cloudflare 块检查的任何网站进行测试。 Cloudflares 块始终是传输编码的块响应
-
"...针对任何总是需要 cloudflare 块检查的网站进行测试..." - 所以您希望所有想帮助您的人首先找到这样的网站?或者您能否提供一个您看到此错误的实际 URL?
-
@SteffenUllrich torrentleech.org
-
奇怪的是,如果我将请求交给另一个 TCPStream 代理(127.0.0.1:8888 作为代理,它是提琴手),它工作得很好......