private bool TryParseBuffer()

in iothub/service/src/IotHubClientWebSocket.cs [866:939]


            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;
            }