private static void ReadDone(IAsyncResult result)
{
WebConnection webConnection = (WebConnection)result.AsyncState;
WebConnectionData data = webConnection.Data;
Stream stream = webConnection.nstream;
if (stream == null)
{
webConnection.Close(sendNext: true);
return;
}
int num = -1;
try
{
num = stream.EndRead(result);
}
catch (Exception e)
{
webConnection.HandleError(WebExceptionStatus.ReceiveFailure, e, "ReadDone1");
return;
IL_004c :;
}
if (num == 0)
{
webConnection.HandleError(WebExceptionStatus.ReceiveFailure, null, "ReadDone2");
return;
}
if (num < 0)
{
webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadDone3");
return;
}
int num2 = -1;
num += webConnection.position;
if (webConnection.readState == ReadState.None)
{
Exception ex = null;
try
{
num2 = webConnection.GetResponse(webConnection.buffer, num);
}
catch (Exception ex2)
{
ex = ex2;
}
if (ex != null)
{
webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, ex, "ReadDone4");
return;
}
}
if (webConnection.readState != ReadState.Content)
{
int num3 = num * 2;
int num4 = (num3 >= webConnection.buffer.Length) ? num3 : webConnection.buffer.Length;
byte[] dst = new byte[num4];
Buffer.BlockCopy(webConnection.buffer, 0, dst, 0, num);
webConnection.buffer = dst;
webConnection.position = num;
webConnection.readState = ReadState.None;
InitRead(webConnection);
return;
}
webConnection.position = 0;
WebConnectionStream webConnectionStream = new WebConnectionStream(webConnection);
string text = data.Headers["Transfer-Encoding"];
webConnection.chunkedRead = (text != null && text.ToLower().IndexOf("chunked") != -1);
if (!webConnection.chunkedRead)
{
webConnectionStream.ReadBuffer = webConnection.buffer;
webConnectionStream.ReadBufferOffset = num2;
webConnectionStream.ReadBufferSize = num;
webConnectionStream.CheckResponseInBuffer();
}
else if (webConnection.chunkStream == null)
{
try
{
webConnection.chunkStream = new ChunkStream(webConnection.buffer, num2, num, data.Headers);
}
catch (Exception e2)
{
webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, e2, "ReadDone5");
return;
IL_01ef :;
}
}
else
{
webConnection.chunkStream.ResetBuffer();
try
{
webConnection.chunkStream.Write(webConnection.buffer, num2, num);
}
catch (Exception e3)
{
webConnection.HandleError(WebExceptionStatus.ServerProtocolViolation, e3, "ReadDone6");
return;
IL_0233 :;
}
}
data.stream = webConnectionStream;
if (!ExpectContent(data.StatusCode) || data.request.Method == "HEAD")
{
webConnectionStream.ForceCompletion();
}
data.request.SetResponseData(data);
}