Ejemplo n.º 1
0
        public Session SendRequest(string sRequest, StringDictionary oNewFlags)
        {
            int num;
            int num2;
            HTTPHeaderParseWarnings warnings;

            byte[] buffer2;
            byte[] bytes = KPCONFIG.oHeaderEncoding.GetBytes(sRequest);
            if (!Parser.FindEntityBodyOffsetFromArray(bytes, out num, out num2, out warnings))
            {
                throw new ArgumentException("sRequest did not represent a valid HTTP request", "sRequest");
            }
            string             sHeaders = KPCONFIG.oHeaderEncoding.GetString(bytes, 0, num) + "\r\n\r\n";
            HTTPRequestHeaders oHeaders = new HTTPRequestHeaders();

            if (!oHeaders.AssignFromString(sHeaders))
            {
                throw new ArgumentException("sRequest did not contain valid HTTP headers", "sRequest");
            }
            if (1 > (bytes.Length - num2))
            {
                buffer2 = new byte[0];
            }
            else
            {
                buffer2 = new byte[bytes.Length - num2];
                Buffer.BlockCopy(bytes, num2, buffer2, 0, buffer2.Length);
            }
            return(this.SendRequest(oHeaders, buffer2, oNewFlags));
        }
Ejemplo n.º 2
0
 private bool isRequestComplete()
 {
     if (this.m_headers == null)
     {
         if (!this.HeadersAvailable())
         {
             return(false);
         }
         if (!this.ParseRequestForHeaders())
         {
             string str;
             if (this.m_requestData != null)
             {
                 str = Utilities.ByteArrayToHexView(this.m_requestData.GetBuffer(), 0x18, (int)Math.Min(this.m_requestData.Length, 0x800L));
             }
             else
             {
                 str = "{Kavprot Proxy:no data}";
             }
             if (this.m_headers == null)
             {
                 this.m_headers             = new HTTPRequestHeaders();
                 this.m_headers.HTTPMethod  = "BAD";
                 this.m_headers["Host"]     = "BAD-REQUEST";
                 this.m_headers.RequestPath = "/BAD_REQUEST";
             }
             this.FailSession(400, "Kavprot Proxy - Bad Request", "[Kavprot Proxy] Request Header parsing failed. Request was:\n" + str);
             return(true);
         }
         this.m_session.Timers.KProxyGotRequestHeaders = DateTime.Now;
         this.m_session._AssignID();
         KProxyApplication.DoRequestHeadersAvailable(this.m_session);
     }
     if (this.m_headers.ExistsAndEquals("Transfer-encoding", "chunked"))
     {
         long num;
         long num2;
         return(Utilities.IsChunkedBodyComplete(this.m_session, this.m_requestData, (long)this.iEntityBodyOffset, out num2, out num));
     }
     if (this.m_headers.Exists("Content-Length"))
     {
         long result = 0L;
         try
         {
             if (!long.TryParse(this.m_headers["Content-Length"], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out result) || (result < 0L))
             {
                 KProxyApplication.HandleHTTPError(this.m_session, SessionFlags.ProtocolViolationInRequest, true, true, "Request content length was invalid.\nContent-Length: " + this.m_headers["Content-Length"]);
                 this.FailSession(400, "Kavprot Proxy - Bad Request", "[Kavprot Proxy] Request Content-Length header parsing failed.\nContent-Length: " + this.m_headers["Content-Length"]);
                 return(true);
             }
             return(this.m_requestData.Length >= (this.iEntityBodyOffset + result));
         }
         catch
         {
             this.FailSession(400, "Kavprot Proxy - Bad Request", "[Kavprot Proxy] Unknown error: Check content length header?");
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 3
0
        public object Clone()
        {
            HTTPRequestHeaders headers = (HTTPRequestHeaders)base.MemberwiseClone();

            headers.storage = new List <HTTPHeaderItem>(base.storage.Count);
            foreach (HTTPHeaderItem item in base.storage)
            {
                headers.storage.Add((HTTPHeaderItem)item.Clone());
            }
            return(headers);
        }
Ejemplo n.º 4
0
        public void InjectCustomRequest(HTTPRequestHeaders oHeaders, byte[] arrRequestBodyBytes, bool bRunRequestRules, bool bViewResult)
        {
            StringDictionary oNewFlags = new StringDictionary();

            oNewFlags["x-From-Builder"] = "true";
            if (bViewResult)
            {
                oNewFlags["x-Builder-Inspect"] = "1";
            }
            this.InjectCustomRequest(oHeaders, arrRequestBodyBytes, oNewFlags);
        }
Ejemplo n.º 5
0
        public Session SendRequest(HTTPRequestHeaders oHeaders, byte[] arrRequestBodyBytes, StringDictionary oNewFlags)
        {
            Session session = new Session((HTTPRequestHeaders)oHeaders.Clone(), arrRequestBodyBytes);

            session.SetBitFlag(SessionFlags.RequestGeneratedByKProxy, true);
            if ((oNewFlags != null) && (oNewFlags.Count > 0))
            {
                foreach (DictionaryEntry entry in oNewFlags)
                {
                    session.oFlags[(string)entry.Key] = oNewFlags[(string)entry.Key];
                }
            }
            ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(session.Execute), null);
            return(session);
        }
Ejemplo n.º 6
0
        public bool AssignFromString(string sHeaders)
        {
            HTTPRequestHeaders headers = null;

            try
            {
                headers = Parser.ParseRequest(sHeaders);
            }
            catch
            {
            }
            if (headers != null)
            {
                this.HTTPMethod   = headers.HTTPMethod;
                this._Path        = headers._Path;
                this._RawPath     = headers._RawPath;
                this._UriScheme   = headers._UriScheme;
                base.HTTPVersion  = headers.HTTPVersion;
                this._uriUserInfo = headers._uriUserInfo;
                base.storage      = headers.storage;
                return(true);
            }
            return(false);
        }
Ejemplo n.º 7
0
        public static HTTPRequestHeaders ParseRequest(string sRequest)
        {
            HTTPRequestHeaders oHeaders = new HTTPRequestHeaders(KPCONFIG.oHeaderEncoding);

            string[] sHeaderLines = sRequest.Substring(0, sRequest.IndexOf("\r\n\r\n", StringComparison.Ordinal)).Replace("\r\n", "\n").Split(new char[] { '\n' });
            if (sHeaderLines.Length >= 1)
            {
                int index = sHeaderLines[0].IndexOf(' ');
                if (index > 0)
                {
                    oHeaders.HTTPMethod = sHeaderLines[0].Substring(0, index).ToUpper();
                    sHeaderLines[0]     = sHeaderLines[0].Substring(index).Trim();
                }
                index = sHeaderLines[0].LastIndexOf(' ');
                if (index > 0)
                {
                    oHeaders.RequestPath = sHeaderLines[0].Substring(0, index);
                    oHeaders.HTTPVersion = sHeaderLines[0].Substring(index).Trim().ToUpper();
                    if (oHeaders.RequestPath.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
                    {
                        oHeaders.UriScheme = "http";
                        index = oHeaders.RequestPath.IndexOfAny(new char[] { '/', '?' }, 7);
                        if (index == -1)
                        {
                            oHeaders.RequestPath = "/";
                        }
                        else
                        {
                            oHeaders.RequestPath = oHeaders.RequestPath.Substring(index);
                        }
                    }
                    else if (oHeaders.RequestPath.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                    {
                        oHeaders.UriScheme = "https";
                        index = oHeaders.RequestPath.IndexOfAny(new char[] { '/', '?' }, 8);
                        if (index == -1)
                        {
                            oHeaders.RequestPath = "/";
                        }
                        else
                        {
                            oHeaders.RequestPath = oHeaders.RequestPath.Substring(index);
                        }
                    }
                    else if (oHeaders.RequestPath.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase))
                    {
                        oHeaders.UriScheme = "ftp";
                        index = oHeaders.RequestPath.IndexOf('/', 6);
                        if (index == -1)
                        {
                            oHeaders.RequestPath = "/";
                        }
                        else
                        {
                            oHeaders.RequestPath = oHeaders.RequestPath.Substring(index);
                        }
                    }
                    string sErrors = string.Empty;
                    ParseNVPHeaders(oHeaders, sHeaderLines, 1, ref sErrors);
                    return(oHeaders);
                }
            }
            return(null);
        }
Ejemplo n.º 8
0
        private bool ParseRequestForHeaders()
        {
            int num;
            int num2;
            int num3;

            if ((this.m_requestData == null) || (this.iEntityBodyOffset < 4))
            {
                return(false);
            }
            this.m_headers = new HTTPRequestHeaders(KPCONFIG.oHeaderEncoding);
            byte[] arrRequest = this.m_requestData.GetBuffer();
            Parser.CrackRequestLine(arrRequest, out num2, out num3, out num);
            if ((num2 < 1) || (num3 < 1))
            {
                KProxyApplication.HandleHTTPError(this.m_session, SessionFlags.ProtocolViolationInRequest, true, false, "Incorrectly formed Request-Line");
                return(false);
            }
            this.m_headers.HTTPMethod  = Encoding.ASCII.GetString(arrRequest, 0, num2 - 1).ToUpper();
            this.m_headers.HTTPVersion = Encoding.ASCII.GetString(arrRequest, (num2 + num3) + 1, ((num - num3) - num2) - 2).Trim().ToUpper();
            int num4 = 0;

            if (arrRequest[num2] != 0x2f)
            {
                if (((num3 > 7) && (arrRequest[num2 + 4] == 0x3a)) && ((arrRequest[num2 + 5] == 0x2f) && (arrRequest[num2 + 6] == 0x2f)))
                {
                    this.m_headers.UriScheme = Encoding.ASCII.GetString(arrRequest, num2, 4);
                    num4  = num2 + 6;
                    num2 += 7;
                    num3 -= 7;
                }
                else if (((num3 > 8) && (arrRequest[num2 + 5] == 0x3a)) && ((arrRequest[num2 + 6] == 0x2f) && (arrRequest[num2 + 7] == 0x2f)))
                {
                    this.m_headers.UriScheme = Encoding.ASCII.GetString(arrRequest, num2, 5);
                    num4  = num2 + 7;
                    num2 += 8;
                    num3 -= 8;
                }
                else if (((num3 > 6) && (arrRequest[num2 + 3] == 0x3a)) && ((arrRequest[num2 + 4] == 0x2f) && (arrRequest[num2 + 5] == 0x2f)))
                {
                    this.m_headers.UriScheme = Encoding.ASCII.GetString(arrRequest, num2, 3);
                    num4  = num2 + 5;
                    num2 += 6;
                    num3 -= 6;
                }
            }
            if (num4 == 0)
            {
                if ((this.pipeClient != null) && this.pipeClient.bIsSecured)
                {
                    this.m_headers.UriScheme = "https";
                }
                else
                {
                    this.m_headers.UriScheme = "http";
                }
            }
            if (num4 > 0)
            {
                while (((num3 > 0) && (arrRequest[num2] != 0x2f)) && (arrRequest[num2] != 0x3f))
                {
                    num2++;
                    num3--;
                }
                if (num3 == 0)
                {
                    num2 = num4;
                    num3 = 1;
                }
                int index = num4 + 1;
                int count = num2 - index;
                if (count > 0)
                {
                    this.m_sHostFromURI = KPCONFIG.oHeaderEncoding.GetString(arrRequest, index, count);
                    if ((this.m_headers.UriScheme == "ftp") && this.m_sHostFromURI.Contains("@"))
                    {
                        int length = this.m_sHostFromURI.LastIndexOf("@") + 1;
                        this.m_headers._uriUserInfo = this.m_sHostFromURI.Substring(0, length);
                        this.m_sHostFromURI         = this.m_sHostFromURI.Substring(length);
                    }
                }
            }
            byte[] dst = new byte[num3];
            Buffer.BlockCopy(arrRequest, num2, dst, 0, num3);
            this.m_headers.RawPath = dst;
            string str = KPCONFIG.oHeaderEncoding.GetString(arrRequest, num, this.iEntityBodyOffset - num).Trim();

            arrRequest = null;
            if (str.Length >= 1)
            {
                string[] sHeaderLines = str.Replace("\r\n", "\n").Split(new char[] { '\n' });
                string   sErrors      = string.Empty;
                if (!Parser.ParseNVPHeaders(this.m_headers, sHeaderLines, 0, ref sErrors))
                {
                    KProxyApplication.HandleHTTPError(this.m_session, SessionFlags.ProtocolViolationInRequest, true, false, "Incorrectly formed request headers.\n" + sErrors);
                }
            }
            return(true);
        }
Ejemplo n.º 9
0
 public void InjectCustomRequest(HTTPRequestHeaders oHeaders, byte[] arrRequestBodyBytes, StringDictionary oNewFlags)
 {
     this.SendRequest(oHeaders, arrRequestBodyBytes, oNewFlags);
 }