예제 #1
0
파일: Module.cs 프로젝트: rtezli/Doofy
        public byte[] ProcessRequest(MemoryStream ms)
        {
            byte[] responseData = new byte[0];

            var h = new HttpHandler(Log);

            var request = new HttpRequest();
            if (!request.TryParseProxyRequest(ms))
                return responseData;

            RequestFilterActions.ForEach(f => f.Invoke());

            var response = request.GetResponse();

            RequestFilterActions.ForEach(f => f.Invoke());

            return response.ResponseStream.GetBuffer();
        }
예제 #2
0
파일: HttpRequest.cs 프로젝트: tezli/Doofy
        public bool TryParseRequest(MemoryStream m)
        {
            RequestStream = m;
            RawContent    = m.GetBuffer();
            var p = UTF8Encoding.UTF8.GetString(RawContent);

            string[] HeaderAndData = p.Split(new String[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            try
            {
                var headerFields = HeaderAndData[0].Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                var MethodResourceversion         = headerFields[0];
                var MethodResourceversionSplitted = MethodResourceversion.Split(' ');

                Method   = MethodResourceversionSplitted[0];
                Resource = MethodResourceversionSplitted[1];
                Version  = Version.Parse(MethodResourceversionSplitted[2].ToLower().Replace("http/", ""));

                for (int i = 1; i < headerFields.Length; i++)
                {
                    var PropertyValue = headerFields[i].Split(new String[] { ": " }, StringSplitOptions.RemoveEmptyEntries);

                    var key   = PropertyValue[0].Trim().ToLower();
                    var value = PropertyValue[1].Trim();

                    if (Headers.Any(v => v.Key.ToLower().Equals(key)) && !string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                    {
                        Headers[key] = value;
                    }

                    if (!Headers.ContainsKey(key))
                    {
                        HttpHandler.Log(LogLevel.Warning, "Key \"" + key + "\" is missing in header list");
                    }
                }

                if (!string.IsNullOrEmpty(Headers["Content-Type"]))
                {
                    ContentType = Headers["Content-Type"];
                }
                if (!string.IsNullOrEmpty(Headers["Content-Length"]))
                {
                    ContentLength = Convert.ToInt32(Headers["Content-Length"]);
                }

                if (!string.IsNullOrEmpty(Headers["Cookie"]))
                {
                    if (Cookies == null)
                    {
                        Cookies = new CookieContainer();
                    }

                    var cookies = Headers["Cookie"].Split(';');
                    foreach (var cookie in cookies)
                    {
                        var keyValue = cookie.Trim().Split(':');
                        if (keyValue.Length >= 2)
                        {
                            Cookies.Add(new Cookie()
                            {
                                Name   = keyValue[0],
                                Value  = keyValue[1],
                                Domain = Domain
                            });
                        }
                    }
                }


                if (HeaderAndData.Length >= 2)
                {
                    HasPayload = true;
                    var contentLength = RawContent.Length - HeaderAndData[0].Length - 4;
                    Content = new byte[contentLength];
                    Array.Copy(RawContent, RawContent.Length - contentLength, Content, 0, contentLength);
                }
                return(true);
            }
            catch (Exception e)
            {
                HttpHandler.Log(LogLevel.Error, "Http request parsing error : " + e.Message);
                return(false);
            }
        }
예제 #3
0
파일: HttpRequest.cs 프로젝트: tezli/Doofy
        public bool TryParseProxyRequest(MemoryStream m)
        {
            RequestStream = m;
            RawContent    = m.GetBuffer();
            var p = UTF8Encoding.UTF8.GetString(RawContent);

            string[] HeaderAndData = p.Split(new String[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            try
            {
                var HeaderFields = HeaderAndData[0].Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                var MethodResourceversion         = HeaderFields[0];
                var MethodResourceversionSplitted = MethodResourceversion.Split(' ');

                Method       = MethodResourceversionSplitted[0];
                Resource     = MethodResourceversionSplitted[1];
                Url          = Resource;
                this.Version = Version.Parse(MethodResourceversionSplitted[2].ToLower().Replace("http/", ""));

                for (int i = 1; i < HeaderFields.Length; i++)
                {
                    var PropertyValue = HeaderFields[i].Split(new String[] { ": " }, StringSplitOptions.None);

                    var key = PropertyValue[0].Trim();

                    if (PropertyValue.Length == 2)
                    {
                        var value = PropertyValue[1].Trim();

                        if (Headers.Any(v => v.Key.ToLower().Equals(key.ToLower())) && !string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                        {
                            if (key == "Proxy-Connection")
                            {
                                Headers["Connection"] = value;
                            }

                            else if (key == "UA-CPU")
                            {
                                Headers["UA-CPU"] = value;
                            }
                            else
                            {
                                Headers[key] = value;
                            }
                        }
                    }
                    else
                    {
                        HttpHandler.Log(LogLevel.Warning, "Empty key in header : \"" + key);
                    }

                    if (!Headers.ContainsKey(key))
                    {
                        HttpHandler.Log(LogLevel.Warning, "Key \"" + key + "\" is missing in header list");
                    }
                }

                if (!string.IsNullOrEmpty(Headers["Content-Type"]))
                {
                    ContentType = Headers["Content-Type"];
                }
                if (!string.IsNullOrEmpty(Headers["Content-Length"]))
                {
                    ContentLength = Convert.ToInt32(Headers["Content-Length"]);
                }

                if (Headers["Host"] == null)
                {
                    var breakupresource = this.Resource.Split('/');
                    Headers["Host"] = breakupresource[2];
                }
                if (Regex.IsMatch(this.Resource, "http://.*/"))
                {
                    var hostPart = "http://" + Headers["Host"];
                    Resource = this.Resource.Replace(hostPart, "");
                }
                if (HeaderAndData.Length >= 2)
                {
                    HasPayload = true;
                    var contentLength = RawContent.Length - HeaderAndData[0].Length - 4;
                    Content = new byte[contentLength];
                    Array.Copy(RawContent, RawContent.Length - contentLength, Content, 0, contentLength);
                }
                this.Host = Headers["Host"];
                return(true);
            }
            catch (Exception e)
            {
                HttpHandler.Log(LogLevel.Error, "Http request parsing error : " + e.Message);
                return(false);
            }
        }
예제 #4
0
        public bool TryParseResponse(MemoryStream ms)
        {
            ResponseStream = ms;
            RawContent     = ms.GetBuffer();
            var p = UTF8Encoding.UTF8.GetString(RawContent);

            string[] HeaderAndData = p.Split(new String[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            try
            {
                if (HeaderAndData.Length < 1)
                {
                    throw new FormatException();
                }

                var headerFields = HeaderAndData[0].Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                var MethodResourceversion         = headerFields[0];
                var MethodResourceversionSplitted = MethodResourceversion.Split(' ');

                Version    = Version.Parse(MethodResourceversionSplitted[0].ToLower().Replace("http/", ""));
                StatusCode = Convert.ToInt32(MethodResourceversionSplitted[1]);

                for (int i = 2; i < MethodResourceversionSplitted.Length; i++)
                {
                    StatusMessage += MethodResourceversionSplitted[i] + " ";
                }

                StatusMessage = StatusMessage.Trim();

                for (int i = 1; i < headerFields.Length; i++)
                {
                    var PropertyValue = headerFields[i].Split(new String[] { ": " }, StringSplitOptions.None);

                    var key   = PropertyValue[0].Trim();
                    var value = PropertyValue[1].Trim();

                    if (Headers.Any(v => v.Key.ToLower().Equals(key.ToLower())) && !string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                    {
                        Headers[key] = value;
                    }

                    if (!Headers.ContainsKey(key))
                    {
                        HttpHandler.Log(LogLevel.Warning, "Key \"" + key + "\" is missing in header list");
                    }
                }

                if (!string.IsNullOrEmpty(Headers["Content-Type"]))
                {
                    ContentType = Headers["Content-Type"];
                }
                if (!string.IsNullOrEmpty(Headers["Content-Length"]))
                {
                    ContentLength = Convert.ToInt32(Headers["Content-Length"]);
                }

                if (HeaderAndData.Length >= 2)
                {
                    HasPayload = true;
                    var contentLength = RawContent.Length - HeaderAndData[0].Length - 4;
                    Content = new byte[contentLength];
                    Array.Copy(RawContent, RawContent.Length - contentLength, Content, 0, contentLength);
                }
                return(true);
            }
            catch (Exception e)
            {
                HttpHandler.Log(LogLevel.Error, "Http request parsing error : " + e.Message);
                return(false);
            }
        }