Skip to content

Commit

Permalink
Reworked array resizing and body parsing. Added content-length check …
Browse files Browse the repository at this point in the history
…(MP2-424)
  • Loading branch information
morpheusxx committed Feb 22, 2014
1 parent 7d64c90 commit 3674c5e
Showing 1 changed file with 15 additions and 29 deletions.
Expand Up @@ -134,13 +134,6 @@ protected static byte[] EncodeHeaderAndBody(string header, byte[] messageBody)
return result;
}

protected static void IncreaseBuffer<T>(T[] buffer, uint increment)
{
T[] oldBuffer = buffer;
buffer = new T[oldBuffer.Length + 2048];
Array.Copy(oldBuffer, 0, buffer, 0, oldBuffer.Length);
}

/// <summary>
/// Parses the HTTP request out of the given <paramref name="stream"/> and stores the result
/// in this instance. The first line of the request remains to be parsed, because it is specific
Expand All @@ -159,8 +152,9 @@ internal void ParseHeaderAndBody(Stream stream, out string firstLine)
int delimiterLength = 3;
while ((b = stream.ReadByte()) != -1)
{
if (numHeaderBytes - 1 == data.Length)
IncreaseBuffer(data, HEADER_BUF_INC);
if (numHeaderBytes == data.Length)
Array.Resize(ref data, data.Length + HEADER_BUF_INC);

data[numHeaderBytes] = (byte)b;

// Regular CRLF line ending
Expand All @@ -184,7 +178,6 @@ internal void ParseHeaderAndBody(Stream stream, out string firstLine)
if (lines.Length < 1)
throw new InvalidDataException("Invalid empty HTTP header");

int contentLength = -1;
firstLine = lines[0].Trim();
for (int i = 1; i < lines.Length; i++)
{
Expand All @@ -197,35 +190,28 @@ internal void ParseHeaderAndBody(Stream stream, out string firstLine)
string key = line.Substring(0, index).Trim();
string value = line.Substring(index + 1).Trim();
SetHeader(key, value);
if (key.ToUpperInvariant() == "CONTENT-LENGTH" && !int.TryParse(value, out contentLength))
contentLength = -1;
}
catch (ArgumentException e)
{
throw new InvalidDataException("Invalid HTTP header line '{0}'", e, line);
}
}
byte[] bodyBuffer;
if (contentLength == -1)
using (MemoryStream bodyStream = new MemoryStream())
{
bodyBuffer = new byte[BODY_BUFF_INC];
contentLength = 0;
int len;
byte[] bodyBuffer = new byte[BODY_BUFF_INC];
int bytesRead;
do
{
len = stream.Read(bodyBuffer, contentLength, BODY_BUFF_INC);
contentLength += len;
IncreaseBuffer(bodyBuffer, BODY_BUFF_INC);
}
while (len == BODY_BUFF_INC);
}
else
{
bodyBuffer = new byte[contentLength];
contentLength = stream.Read(bodyBuffer, 0, contentLength);
bytesRead = stream.Read(bodyBuffer, 0, BODY_BUFF_INC);
bodyStream.Write(bodyBuffer, 0, bytesRead);
} while (bytesRead > 0);
_bodyBuffer = bodyStream.ToArray();

// Check for correct body length
int contentLength;
if (int.TryParse(this["CONTENT-LENGTH"], out contentLength) && contentLength != -1 && contentLength != _bodyBuffer.Length)
throw new InvalidDataException("Invalid HTTP content length: header {0}, actual body: {1}", contentLength, _bodyBuffer.Length);
}
_bodyBuffer = new byte[contentLength];
Array.Copy(bodyBuffer, 0, _bodyBuffer, 0, contentLength);
}

public override string ToString()
Expand Down

0 comments on commit 3674c5e

Please sign in to comment.