TrotiNet.HttpSocket.ReadAsciiLine C# (CSharp) Method

ReadAsciiLine() public method

Reads a LF-delimited (or CRLF-delimited) line from the socket, and returns it (without the trailing newline character)
public ReadAsciiLine ( ) : string
return string
        public string ReadAsciiLine()
        {
            sb.Length = 0;
            bool bHadCR = false;
            while (true)
            {
                if (AvailableData == 0)
                {
                    if (ReadRaw() == 0)
                        // Connection closed while we were waiting for data
                        throw new IoBroken();
                    UseLeftOverBytes = true;
                }

                // Newlines in HTTP headers are expected to be CRLF.
                // However, for better robustness, RFC 2616 recommends
                // ignoring CR, and considering LF as new lines (section 19.3)
                byte b = Buffer[BufferPosition++];
                AvailableData--;

                if (b == '\n')
                    break;
                if (bHadCR)
                    sb.Append('\r');
                if (b == '\r')
                    bHadCR = true;
                else
                {
                    bHadCR = false;
                    char c = (char)b;
                    sb.Append(c);
                }
            }
            return sb.ToString();
        }

Usage Example

Ejemplo n.º 1
0
        internal HttpStatusLine(HttpSocket hs)
        {
            string line;
            do
                line = hs.ReadAsciiLine().Trim();
            while (line.Length == 0);
            string[] items = line.Split(sp,
                StringSplitOptions.RemoveEmptyEntries);
            // Note: the status line has three items: the HTTP protocol
            // version, the status code, and the reason phrase.
            // Only the reason phrase may be empty.
            if (items.Length < 2)
                throw new HttpProtocolBroken("Unrecognized status line '" +
                    line + "'");

            ProtocolVersion = ParserHelper.ParseProtocolVersion(
                items[0]);

            string code = items[1];
            if (code.Length != 3 ||
                !char.IsDigit(code[0])) // we only test the first character
                throw new HttpProtocolBroken("Invalid status code '" +
                    code + "'");

            //string Reason = rest of the string; // we don't need it

            StatusCode = int.Parse(code);
            StatusLine = line;
        }
All Usage Examples Of TrotiNet.HttpSocket::ReadAsciiLine