ReadLine() статический приватный Метод

static private ReadLine ( byte buffer, int &start, int max, string &output ) : bool
buffer byte
start int
max int
output string
Результат bool
Пример #1
0
        private WebHeaderCollection ReadHeaders(HttpWebRequest request, Stream stream, out byte[] retBuffer, out int status)
        {
            retBuffer = null;
            status    = 200;
            byte[]              array        = new byte[1024];
            MemoryStream        memoryStream = new MemoryStream();
            bool                flag         = false;
            int                 num2;
            WebHeaderCollection webHeaderCollection;

            for (;;)
            {
                int num = stream.Read(array, 0, 1024);
                if (num == 0)
                {
                    break;
                }
                memoryStream.Write(array, 0, num);
                num2 = 0;
                string text = null;
                webHeaderCollection = new WebHeaderCollection();
                while (WebConnection.ReadLine(memoryStream.GetBuffer(), ref num2, (int)memoryStream.Length, ref text))
                {
                    if (text == null)
                    {
                        goto Block_2;
                    }
                    if (flag)
                    {
                        webHeaderCollection.Add(text);
                    }
                    else
                    {
                        int num3 = text.IndexOf(' ');
                        if (num3 == -1)
                        {
                            goto Block_5;
                        }
                        status = (int)uint.Parse(text.Substring(num3 + 1, 3));
                        flag   = true;
                    }
                }
            }
            this.HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders");
            return(null);

Block_2:
            if (memoryStream.Length - (long)num2 > 0L)
            {
                retBuffer = new byte[memoryStream.Length - (long)num2];
                Buffer.BlockCopy(memoryStream.GetBuffer(), num2, retBuffer, 0, retBuffer.Length);
            }
            return(webHeaderCollection);

Block_5:
            this.HandleError(WebExceptionStatus.ServerProtocolViolation, null, "ReadHeaders2");
            return(null);
        }
Пример #2
0
        bool GetResponse(BufferOffsetSize buffer, ref int pos, ref ReadState state)
        {
            string line           = null;
            bool   lineok         = false;
            bool   isContinue     = false;
            bool   emptyFirstLine = false;

            do
            {
                if (state == ReadState.Aborted)
                {
                    throw GetReadException(WebExceptionStatus.RequestCanceled, null, "GetResponse");
                }

                if (state == ReadState.None)
                {
                    lineok = WebConnection.ReadLine(buffer.Buffer, ref pos, buffer.Offset, ref line);
                    if (!lineok)
                    {
                        return(false);
                    }

                    if (line == null)
                    {
                        emptyFirstLine = true;
                        continue;
                    }
                    emptyFirstLine = false;
                    state          = ReadState.Status;

                    string[] parts = line.Split(' ');
                    if (parts.Length < 2)
                    {
                        throw GetReadException(WebExceptionStatus.ServerProtocolViolation, null, "GetResponse");
                    }

                    if (String.Compare(parts[0], "HTTP/1.1", true) == 0)
                    {
                        Version = HttpVersion.Version11;
                        ServicePoint.SetVersion(HttpVersion.Version11);
                    }
                    else
                    {
                        Version = HttpVersion.Version10;
                        ServicePoint.SetVersion(HttpVersion.Version10);
                    }

                    StatusCode = (HttpStatusCode)UInt32.Parse(parts[1]);
                    if (parts.Length >= 3)
                    {
                        StatusDescription = String.Join(" ", parts, 2, parts.Length - 2);
                    }
                    else
                    {
                        StatusDescription = string.Empty;
                    }

                    if (pos >= buffer.Offset)
                    {
                        return(true);
                    }
                }

                emptyFirstLine = false;
                if (state == ReadState.Status)
                {
                    state   = ReadState.Headers;
                    Headers = new WebHeaderCollection();
                    var  headerList = new List <string> ();
                    bool finished   = false;
                    while (!finished)
                    {
                        if (WebConnection.ReadLine(buffer.Buffer, ref pos, buffer.Offset, ref line) == false)
                        {
                            break;
                        }

                        if (line == null)
                        {
                            // Empty line: end of headers
                            finished = true;
                            continue;
                        }

                        if (line.Length > 0 && (line[0] == ' ' || line[0] == '\t'))
                        {
                            int count = headerList.Count - 1;
                            if (count < 0)
                            {
                                break;
                            }

                            string prev = headerList[count] + line;
                            headerList[count] = prev;
                        }
                        else
                        {
                            headerList.Add(line);
                        }
                    }

                    if (!finished)
                    {
                        return(false);
                    }

                    // .NET uses ParseHeaders or ParseHeadersStrict which is much better
                    foreach (string s in headerList)
                    {
                        int pos_s = s.IndexOf(':');
                        if (pos_s == -1)
                        {
                            throw new ArgumentException("no colon found", "header");
                        }

                        var header = s.Substring(0, pos_s);
                        var value  = s.Substring(pos_s + 1).Trim();

                        if (WebHeaderCollection.AllowMultiValues(header))
                        {
                            Headers.AddInternal(header, value);
                        }
                        else
                        {
                            Headers.SetInternal(header, value);
                        }
                    }

                    if (StatusCode == HttpStatusCode.Continue)
                    {
                        ServicePoint.SendContinue = true;
                        if (pos >= buffer.Offset)
                        {
                            return(true);
                        }

                        if (Request.ExpectContinue)
                        {
                            Request.DoContinueDelegate((int)StatusCode, Headers);
                            // Prevent double calls when getting the
                            // headers in several packets.
                            Request.ExpectContinue = false;
                        }

                        state      = ReadState.None;
                        isContinue = true;
                    }
                    else
                    {
                        state = ReadState.Content;
                        return(true);
                    }
                }
            } while (emptyFirstLine || isContinue);

            throw GetReadException(WebExceptionStatus.ServerProtocolViolation, null, "GetResponse");
        }
Пример #3
0
        private int GetResponse(byte[] buffer, int max)
        {
            int    num   = 0;
            string text  = null;
            bool   flag  = false;
            bool   flag2 = false;

            for (;;)
            {
                if (this.readState != ReadState.None)
                {
                    goto IL_114;
                }
                if (!WebConnection.ReadLine(buffer, ref num, max, ref text))
                {
                    break;
                }
                if (text == null)
                {
                    flag2 = true;
                }
                else
                {
                    flag2          = false;
                    this.readState = ReadState.Status;
                    string[] array = text.Split(new char[]
                    {
                        ' '
                    });
                    if (array.Length < 2)
                    {
                        return(-1);
                    }
                    if (string.Compare(array[0], "HTTP/1.1", true) == 0)
                    {
                        this.Data.Version = HttpVersion.Version11;
                        this.sPoint.SetVersion(HttpVersion.Version11);
                    }
                    else
                    {
                        this.Data.Version = HttpVersion.Version10;
                        this.sPoint.SetVersion(HttpVersion.Version10);
                    }
                    this.Data.StatusCode = (int)uint.Parse(array[1]);
                    if (array.Length >= 3)
                    {
                        this.Data.StatusDescription = string.Join(" ", array, 2, array.Length - 2);
                    }
                    else
                    {
                        this.Data.StatusDescription = string.Empty;
                    }
                    if (num >= max)
                    {
                        return(num);
                    }
                    goto IL_114;
                }
IL_2CA:
                if (!flag2 && !flag)
                {
                    return(-1);
                }
                continue;
IL_114:
                flag2 = false;
                if (this.readState != ReadState.Status)
                {
                    goto IL_2CA;
                }
                this.readState    = ReadState.Headers;
                this.Data.Headers = new WebHeaderCollection();
                ArrayList arrayList = new ArrayList();
                bool      flag3     = false;
                while (!flag3)
                {
                    if (!WebConnection.ReadLine(buffer, ref num, max, ref text))
                    {
                        break;
                    }
                    if (text == null)
                    {
                        flag3 = true;
                    }
                    else if (text.Length > 0 && (text[0] == ' ' || text[0] == '\t'))
                    {
                        int num2 = arrayList.Count - 1;
                        if (num2 < 0)
                        {
                            break;
                        }
                        string value = (string)arrayList[num2] + text;
                        arrayList[num2] = value;
                    }
                    else
                    {
                        arrayList.Add(text);
                    }
                }
                if (!flag3)
                {
                    return(-1);
                }
                foreach (object obj in arrayList)
                {
                    string @internal = (string)obj;
                    this.Data.Headers.SetInternal(@internal);
                }
                if (this.Data.StatusCode != 100)
                {
                    goto IL_2C1;
                }
                this.sPoint.SendContinue = true;
                if (num >= max)
                {
                    return(num);
                }
                if (this.Data.request.ExpectContinue)
                {
                    this.Data.request.DoContinueDelegate(this.Data.StatusCode, this.Data.Headers);
                    this.Data.request.ExpectContinue = false;
                }
                this.readState = ReadState.None;
                flag           = true;
                goto IL_2CA;
            }
            return(-1);

IL_2C1:
            this.readState = ReadState.Content;
            return(num);
        }
Пример #4
0
        async Task <(WebHeaderCollection, byte[], int)> ReadHeaders(Stream stream, CancellationToken cancellationToken)
        {
            byte[] retBuffer = null;
            int    status    = 200;

            byte[]       buffer = new byte[1024];
            MemoryStream ms     = new MemoryStream();

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();
                int n = await stream.ReadAsync(buffer, 0, 1024, cancellationToken).ConfigureAwait(false);

                if (n == 0)
                {
                    throw WebConnection.GetException(WebExceptionStatus.ServerProtocolViolation, null);
                }

                ms.Write(buffer, 0, n);
                int    start                = 0;
                string str                  = null;
                bool   gotStatus            = false;
                WebHeaderCollection headers = new WebHeaderCollection();
                while (WebConnection.ReadLine(ms.GetBuffer(), ref start, (int)ms.Length, ref str))
                {
                    if (str == null)
                    {
                        int contentLen;
                        var clengthHeader = headers["Content-Length"];
                        if (string.IsNullOrEmpty(clengthHeader) || !int.TryParse(clengthHeader, out contentLen))
                        {
                            contentLen = 0;
                        }

                        if (ms.Length - start - contentLen > 0)
                        {
                            // we've read more data than the response header and conents,
                            // give back extra data to the caller
                            retBuffer = new byte[ms.Length - start - contentLen];
                            Buffer.BlockCopy(ms.GetBuffer(), start + contentLen, retBuffer, 0, retBuffer.Length);
                        }
                        else
                        {
                            // haven't read in some or all of the contents for the response, do so now
                            FlushContents(stream, contentLen - (int)(ms.Length - start));
                        }

                        return(headers, retBuffer, status);
                    }

                    if (gotStatus)
                    {
                        headers.Add(str);
                        continue;
                    }

                    string[] parts = str.Split(' ');
                    if (parts.Length < 2)
                    {
                        throw WebConnection.GetException(WebExceptionStatus.ServerProtocolViolation, null);
                    }

                    if (String.Compare(parts[0], "HTTP/1.1", true) == 0)
                    {
                        ProxyVersion = HttpVersion.Version11;
                    }
                    else if (String.Compare(parts[0], "HTTP/1.0", true) == 0)
                    {
                        ProxyVersion = HttpVersion.Version10;
                    }
                    else
                    {
                        throw WebConnection.GetException(WebExceptionStatus.ServerProtocolViolation, null);
                    }

                    status = (int)UInt32.Parse(parts[1]);
                    if (parts.Length >= 3)
                    {
                        StatusDescription = String.Join(" ", parts, 2, parts.Length - 2);
                    }

                    gotStatus = true;
                }
            }
        }