Exemple #1
0
        private bool ParseResponseForHeaders()
        {
            if ((_responseData != null) && (_intEntityBodyOffset >= 4))
            {
                Headers = new HttpResponseHeaders();
                byte[] bytes = _responseData.GetBuffer();
                string str   = AppVars.Codepage.GetString(bytes, 0, _intEntityBodyOffset).Trim();
                if (string.IsNullOrEmpty(str))
                {
                    Headers = null;
                    return(false);
                }

                string[] strArray = str.Replace("\r\n", "\n").Split('\n');
                if (strArray.Length >= 1)
                {
                    int index = strArray[0].IndexOf(' ');
                    if (index > 0)
                    {
                        Headers.HttpVersion = strArray[0].Substring(0, index).ToUpper();
                        strArray[0]         = strArray[0].Substring(index + 1).Trim();
                        if (!Headers.HttpVersion.StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase))
                        {
                            return(false);
                        }

                        Headers.HttpResponseStatus = strArray[0];
                        index = strArray[0].IndexOf(' ');
                        int  responseCode;
                        bool flag = index > 0 ?
                                    int.TryParse(strArray[0].Substring(0, index).Trim(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out responseCode) :
                                    int.TryParse(strArray[0].Trim(), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out responseCode);
                        Headers.HttpResponseCode = responseCode;

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

                        for (int i = 1; i < strArray.Length; i++)
                        {
                            index = strArray[i].IndexOf(':');
                            if ((index > 0) && (index <= (strArray[i].Length - 1)))
                            {
                                try
                                {
                                    Headers.Add(strArray[i].Substring(0, index), strArray[i].Substring(index + 1).Trim());
                                }
                                catch
                                {
                                    return(false);
                                }
                            }
                        }

                        return(true);
                    }
                }
            }

            return(false);
        }
Exemple #2
0
        internal bool ReadResponse()
        {
            bool flag  = false;
            bool flag2 = false;
            bool flag3 = !_session.BufferResponse;
            bool flag4 = true;
            var  data  = new byte[0x8000];

            do
            {
                try
                {
                    if (flag4 && (Headers != null))
                    {
                        flag4 = false;
                        flag3 = !_session.BufferResponse;
                    }

                    int readCount = ServerSocket.Receive(data);
                    if (readCount <= 0)
                    {
                        flag = true;
                    }
                    else
                    {
                        _responseData.Write(data, 0, readCount);
                        if (flag3)
                        {
                            LeakResponseBytes();
                        }
                    }
                }
                catch (Exception)
                {
                    flag2 = true;
                }
            }while ((!flag && !IsResponseComplete()) && !flag2);

            if (_responseData.Length == 0L)
            {
                flag2 = true;
            }
            else
            {
                try
                {
                    if (AppVars.MainForm != null)
                    {
                        AppVars.MainForm.UpdateTrafficSafe(_responseData.Length);
                    }
                }
                catch (InvalidOperationException)
                {
                }
            }

            if ((Headers == null) && !flag2)
            {
                Headers = new HttpResponseHeaders
                {
                    HttpResponseCode   = 200,
                    HttpResponseStatus = "200 This buggy server did not return headers"
                };

                _intEntityBodyOffset = 0;
                return(true);
            }

            if (Headers != null && AppVars.Profile.DoHttpLog)
            {
                var sb = new StringBuilder();

                /*
                 * if (_session != null)
                 * {
                 *  sb.AppendLine("Session #" + _session.Id.ToString(CultureInfo.InvariantCulture));
                 * }
                 */

                sb.Append(Headers);
                try
                {
                    if (AppVars.MainForm != null)
                    {
                        AppVars.MainForm.BeginInvoke(
                            new UpdateHttpLogDelegate(AppVars.MainForm.UpdateHttpLog),
                            new object[] { sb.ToString() });
                    }
                }
                catch (InvalidOperationException)
                {
                }
            }

            return(!flag2);
        }
Exemple #3
0
        internal static HttpResponseHeaders ParseResponse(string strResponse)
        {
            var index = strResponse.IndexOf("\r\n\r\n", StringComparison.Ordinal);

            if (index < 1)
            {
                index = strResponse.Length;
            }

            if (index >= 1)
            {
                var strArray = strResponse.Substring(0, index).Replace("\r\n", "\n").Split('\n');
                if (strArray.Length < 1)
                {
                    return(null);
                }

                var headers = new HttpResponseHeaders();
                var length  = strArray[0].IndexOf(' ');
                if (length > 0)
                {
                    headers.HttpVersion = strArray[0].Substring(0, length).ToUpperInvariant();
                    strArray[0]         = strArray[0].Substring(length + 1).Trim();
                    if (string.Compare(headers.HttpVersion, 0, "HTTP/", 0, 5, StringComparison.OrdinalIgnoreCase) != 0)
                    {
                        return(null);
                    }

                    headers.HttpResponseStatus = strArray[0];
                    length = strArray[0].IndexOf(' ');

                    int code;
                    if (length > 0)
                    {
                        if (!int.TryParse(strArray[0].Substring(0, length).Trim(), out code))
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        if (!int.TryParse(strArray[0].Trim(), out code))
                        {
                            return(null);
                        }
                    }

                    headers.HttpResponseCode = code;
                    for (var i = 1; i < strArray.Length; i++)
                    {
                        length = strArray[i].IndexOf(':');
                        if ((length > 0) && (length <= (strArray[i].Length - 1)))
                        {
                            headers.Add(strArray[i].Substring(0, length), strArray[i].Substring(length + 1).Trim());
                        }
                    }

                    return(headers);
                }
            }

            return(null);
        }