in iothub/device/src/net451/IotHubClientWebSocket.cs [840:914]
private bool TryParseBuffer()
{
if (_bodyStartIndex == 0)
{
int firstSpace = IndexOfAsciiChar(_buffer, 0, _totalBytesRead, ' ');
if (firstSpace == -1)
{
return false;
}
////HttpVersion = Encoding.ASCII.GetString(array, arraySegment.Offset, firstSpace - arraySegment.Offset);
int secondSpace = IndexOfAsciiChar(_buffer, firstSpace + 1, _totalBytesRead - (firstSpace + 1), ' ');
if (secondSpace == -1)
{
return false;
}
string statusCodeString = Encoding.ASCII.GetString(_buffer, firstSpace + 1, secondSpace - (firstSpace + 1));
StatusCode = (HttpStatusCode)int.Parse(statusCodeString, CultureInfo.InvariantCulture);
int endOfLine = IndexOfAsciiChars(_buffer, secondSpace + 1, _totalBytesRead - (secondSpace + 1), '\r', '\n');
if (endOfLine == -1)
{
return false;
}
StatusDescription = Encoding.ASCII.GetString(_buffer, secondSpace + 1, endOfLine - (secondSpace + 1));
// Now parse the headers
Headers = new WebHeaderCollection();
while (true)
{
int startCurrentLine = endOfLine + 2;
if (startCurrentLine >= _totalBytesRead)
{
return false;
}
else if (_buffer[startCurrentLine] == '\r' && _buffer[startCurrentLine + 1] == '\n')
{
// \r\n\r\n indicates the end of the HTTP headers.
_bodyStartIndex = startCurrentLine + 2;
break;
}
int separatorIndex = IndexOfAsciiChars(_buffer, startCurrentLine, _totalBytesRead - startCurrentLine, ':', ' ');
if (separatorIndex == -1)
{
return false;
}
string headerName = Encoding.ASCII.GetString(_buffer, startCurrentLine, separatorIndex - startCurrentLine);
endOfLine = IndexOfAsciiChars(_buffer, separatorIndex + 2, _totalBytesRead - (separatorIndex + 2), '\r', '\n');
if (endOfLine == -1)
{
return false;
}
string headerValue = Encoding.ASCII.GetString(_buffer, separatorIndex + 2, endOfLine - (separatorIndex + 2));
Headers.Add(headerName, headerValue);
}
}
// check to see if all the body bytes have been received.
string contentLengthValue = Headers[HttpResponseHeader.ContentLength];
if (!string.IsNullOrEmpty(contentLengthValue)
&& contentLengthValue != "0")
{
int contentLength = int.Parse(contentLengthValue, CultureInfo.InvariantCulture);
if (contentLength > _totalBytesRead - _bodyStartIndex)
{
return false;
}
}
return true;
}