Example #1
0
        public void Deserialize(NetworkStream ns, Request req, VSslHandler vsh = null)
        {
            string sResult  = version + " " + statusCode + " " + httpMessage + "\r\n";
            int    ctLength = 0;

            //edit bodyText here

            VDecoder vd = new VDecoder();

            if (headers.ContainsKey("Content-Length") && headers["Content-Length"] != "0" && headers["Content-Length"] != null)
            {
                if (mitm != null && mitm.started) //MITM Media and Text injection
                {
                    if (bodyText != "")
                    {
                        if (mitm.CheckBody(bodyText))
                        {
                            return;
                        }
                        string cType = (headers.ContainsKey("Content-Type")) ? headers["Content-Type"] : null;
                        if (cType != null)
                        {
                            string nt = "";
                            nt = mitm.Inject(bodyText, headers["Content-Type"]);
                            if (nt != null)
                            {
                                bodyText = nt;
                            }
                        }
                    }
                    else
                    {
                        byte[] n = mitm.MediaRewrite(this, req);
                        if (n != null)
                        {
                            body = n;
                        }
                    }
                }

                if (bodyText != "" && headers.ContainsKey("Content-Encoding"))
                {
                    Array.Clear(body, 0, body.Length);
                    byte[] toCode = vd.EncodeCharset(headers["Content-Type"], bodyText);
                    string enc    = headers["Content-Encoding"];
                    if (enc == "gzip")
                    {
                        body = vd.EncodeGzip(toCode);
                    }
                    else if (enc == "deflate")
                    {
                        body = vd.EncodeDeflate(toCode);
                    }
                    else if (enc == "br")
                    {
                        body = vd.EncodeBrotli(toCode);
                    }
                    Array.Clear(toCode, 0, toCode.Length);
                }
                else if (bodyText == "" && headers.ContainsKey("Content-Encoding"))
                {
                    string enc = headers["Content-Encoding"];
                    if (enc == "gzip")
                    {
                        body = vd.EncodeGzip(body);
                    }
                    else if (enc == "deflate")
                    {
                        body = vd.EncodeDeflate(body);
                    }
                    else if (enc == "br")
                    {
                        body = vd.EncodeBrotli(body);
                    }
                }
                else if (bodyText != "" && !headers.ContainsKey("Content-Encoding"))
                {
                    body = vd.EncodeCharset(headers["Content-Type"], bodyText);
                }

                ctLength = body.Length;
            }

            foreach (KeyValuePair <string, string> kvp in headers.Items)
            {
                string line = "";
                if (kvp.Key == "Content-Length" && ctLength > 0)
                {
                    line = "Content-Length: " + ctLength + "\r\n";
                }
                else if (kvp.Key == "Transfer-Encoding" && kvp.Value == "chunked" && ctLength > 0)
                {
                    // insert the content-length and skip the transfer-encoding header, because we concatanated it.
                    line = "Content-Length: " + ctLength.ToString() + "\r\n";
                }
                else
                {
                    line = kvp.Key + ": " + kvp.Value + "\r\n";
                }

                sResult += line;
            }

            //console.Debug($"{req.target} - responded with content-type: {headers["Content-Type"]}");

            sResult += "\r\n";
            byte[] text = Encoding.ASCII.GetBytes(sResult);
            if (vsh == null)
            {
                ns.Write(text, 0, text.Length);
                if (ctLength > 0)
                {
                    ns.Write(body, 0, body.Length);
                }
                ns.Flush();
            }
            else
            {
                //console.Debug("Handler " + vsh.HandlerID + " receiving " + (headers.ContainsKey("Content-Type") ? headers["Content-Type"] : "No content type sent"));
                vsh.WriteSslStream(text);
                if (ctLength > 0)
                {
                    vsh.WriteSslStream(body);
                }
                vsh.FlushSslStream();
            }
        }
Example #2
0
        private void DecodeArray()
        {
            notEnded = false;
            string cType = headers["Content-Type"];

            if (cType.Contains(";"))
            {
                cType = cType.Substring(0, cType.IndexOf(';'));
            }
            VDecoder vd            = new VDecoder();
            bool     isConvertable = false;

            if (filterNames.Count > 0)
            {
                isConvertable = SearchFilter("or", "mime_white_list", cType);
            }
            if (isConvertable && !headers.ContainsKey("Content-Encoding"))
            {
                bodyText = vd.DecodeCharset(headers["Content-Type"], body, body.Length);
            }
            else if (isConvertable && headers.ContainsKey("Content-Encoding"))
            {
                string enc = headers["Content-Encoding"];
                if (enc == "gzip")
                {
                    body = vd.DecodeGzipToBytes(body);
                }
                else if (enc == "deflate")
                {
                    body = vd.DecodeDeflate(body);
                }
                else if (enc == "br")
                {
                    body = vd.DecodeBrotli(body);
                }

                bodyText = vd.DecodeCharset(headers["Content-Type"], body, body.Length);
                //IMPORTANT: Use push end -- the data is converted to text correctly
            }
            else if (!isConvertable && headers.ContainsKey("Content-Encoding"))
            {
                //Decode contents to byte array
                string enc = headers["Content-Encoding"];
                if (enc == "gzip")
                {
                    body = vd.DecodeGzipToBytes(body);
                }
                else if (enc == "deflate")
                {
                    body = vd.DecodeDeflate(body);
                }
                else if (enc == "br")
                {
                    body = vd.DecodeBrotli(body);
                }
            }
            else
            {
                //Data is in clearText, not convertable to printable (text) format for ex. image file, exe file
                bodyText = "";
            }
        }