/// <summary>
 /// Log a collection of packets by calling their PrintPacket methods
 /// Requires BONCODEAJP13_LOG_LEVEL >= 2
 /// </summary>
 public void LogPackets(BonCodeAJP13PacketCollection packets, bool logAllways = false, int onlyAboveLogLevel = BonCodeAJP13LogLevels.BONCODEAJP13_NO_LOG)
 {
     foreach (BonCodeAJP13Packet packet in packets)
     {
         this.LogPacket(packet, logAllways, onlyAboveLogLevel);
     }
 }
 /// <summary>
 /// Log a collection of packets by calling their PrintPacket methods
 /// Requires BONCODEAJP13_LOG_LEVEL >= 2
 /// </summary>
 public void LogPackets(BonCodeAJP13PacketCollection packets, bool logAllways = false, int minLogLevel = BonCodeAJP13LogLevels.BONCODEAJP13_LOG_ERRORS)
 {
     foreach (BonCodeAJP13Packet packet in packets)
     {
         this.LogPacket(packet, logAllways, minLogLevel);
     }
 }
 /// <summary>
 /// Initialize new connection to tomcat using server and port input and packet collection
 /// </summary>
 public BonCodeAJP13ServerConnection(string server, int port, BonCodeAJP13PacketCollection packetsToSend)
 {
     CheckMutex();
     this.Port   = port;
     this.Server = server;
     //call connection creation
     p_CreateConnection(packetsToSend);
 }
        /// <summary>
        /// Initialize new connection from server to tomcat in new thread.
        /// this connection will run in new thread spawned from the listener thread.
        /// </summary>
        public BonCodeAJP13ServerConnection(BonCodeAJP13Packet singlePacket)
        {
            CheckMutex();
            //package single package into packets to Send
            BonCodeAJP13PacketCollection packetsToSend = new BonCodeAJP13PacketCollection();

            packetsToSend.Add(singlePacket);
            //call connection creation
            p_CreateConnection(packetsToSend);
        }
 public void ContainsTest()
 {
     BonCodeAJP13PacketCollection target = new BonCodeAJP13PacketCollection(); // TODO: Initialize to an appropriate value
     BonCodeAJP13Packet value = null; // TODO: Initialize to an appropriate value
     bool expected = false; // TODO: Initialize to an appropriate value
     bool actual;
     actual = target.Contains(value);
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }
        /// <summary>
        /// Initialize new connection to tomcat using server and port input
        /// </summary>
        public BonCodeAJP13ServerConnection(string server, int port, BonCodeAJP13Packet singlePacket)
        {
            CheckMutex();
            this.Port   = port;
            this.Server = server;
            //create collection and add packet passed
            BonCodeAJP13PacketCollection packetsToSend = new BonCodeAJP13PacketCollection();

            packetsToSend.Add(singlePacket);
            //call connection creation
            p_CreateConnection(packetsToSend);
        }
        /// <summary>
        /// Initialize new connection from server to tomcat in new thread.
        /// Delayed connection init. Will wait until connection is initialized
        /// </summary>
        public BonCodeAJP13ServerConnection(BonCodeAJP13Packet singlePacket, bool delayConnection = true)
        {
            CheckMutex();
            //package single package into packets to Send
            BonCodeAJP13PacketCollection packetsToSend = new BonCodeAJP13PacketCollection();

            packetsToSend.Add(singlePacket);
            p_PacketsToSend = packetsToSend; //assign to instance store
            //call connection creation if desired
            if (!delayConnection)
            {
                p_CreateConnection(packetsToSend);
            }
        }
        /// <summary>
        /// Create Actual Connection and initiate wait for response within this thread
        /// </summary>
        private void p_CreateConnection(BonCodeAJP13PacketCollection packetsToSend)
        {
            p_AbortConnection = false;
            p_ConnectionsID++;                         //increment the number of connections
            p_ConnectionsCounter++;
            p_ThisConnectionID = p_ConnectionsCounter; //assign the connection id


            if (p_ConnectionsID > BonCodeAJP13Settings.MAX_BONCODEAJP13_CONCURRENT_CONNECTIONS)
            {
                //throw exception if we exceeded concurrent connections
                //Exception e = new Exception("0:Maximum connection limit exceeded. Please change limit setting if change is desired. Dropping connection.");
                //throw e;
            }

            try
            {
                //create new connection and timer if we maintain connection timeout within this class
                if (p_TCPClient == null)
                {
                    p_TCPClient = new TcpClient(p_Server, p_Port);

                    p_KeepAliveTimer           = new System.Timers.Timer();
                    p_KeepAliveTimer.Interval  = BonCodeAJP13Consts.BONCODEAJP13_SERVER_KEEP_ALIVE_TIMEOUT;
                    p_KeepAliveTimer.Elapsed  += new System.Timers.ElapsedEventHandler(p_KeepAliveTimer_Elapsed);
                    p_KeepAliveTimer.AutoReset = false;
                    p_KeepAliveTimer.Enabled   = true; //starts the timer
                }
                //start p_StopWatch
                p_StopWatch.Start();

                //assign package and wait for response
                p_PacketsToSend = packetsToSend;

                //handle connection
                HandleConnection();
            }
            catch (Exception e)
            {
                if (p_Logger != null)
                {
                    p_Logger.LogException(e, "TCP Client level -- Server/Port:" + p_Server + "/" + p_Port.ToString());
                }
                ConnectionError();
                return;
            }
        }
 /// <summary>
 /// Initialize new connection from server to tomcat in new thread.
 /// Delayed connection init. Will wait until connection is initialized
 /// DEPRECATED DO NOT USE
 /// </summary>
 public BonCodeAJP13ServerConnection(BonCodeAJP13Packet singlePacket, bool delayConnection = true)
 {
     CheckMutex();
     //package single package into packets to Send
     BonCodeAJP13PacketCollection packetsToSend = new BonCodeAJP13PacketCollection();
     packetsToSend.Add(singlePacket);
     p_PacketsToSend = packetsToSend; //assign to instance store
     //call connection creation if desired
     if (!delayConnection)
     {
         p_CreateConnection(packetsToSend);
     }
 }
        /// <summary>
        /// Create Actual Connection and initiate wait for response within this thread
        /// </summary>
        private void p_CreateConnection(BonCodeAJP13PacketCollection packetsToSend)
        {
            p_AbortConnection = false;
            p_ConnectionsCounter++;
            p_ThisConnectionID = p_ConnectionsCounter; //assign the connection id

            try
            {
                //create new connection and timer if we maintain connection timeout within this class
                if (p_TCPClient == null)
                {
                    p_TCPClient = new TcpClient(p_Server, p_Port);

                    p_KeepAliveTimer = new System.Timers.Timer();
                    p_KeepAliveTimer.Interval = BonCodeAJP13Consts.BONCODEAJP13_SERVER_KEEP_ALIVE_TIMEOUT;
                    p_KeepAliveTimer.Elapsed += new System.Timers.ElapsedEventHandler(p_KeepAliveTimer_Elapsed);
                    p_KeepAliveTimer.AutoReset = false;
                    p_KeepAliveTimer.Enabled = true; //starts the timer
                }
                //start p_StopWatch
                p_StopWatch.Start();

                //assign package and wait for response
                p_PacketsToSend = packetsToSend;

                //handle connection
                HandleConnection();
            }
            catch (Exception e)
            {
                if (p_Logger != null) p_Logger.LogException(e, "TCP Client level -- Server/Port:" + p_Server + "/" + p_Port.ToString());
                ConnectionError();
                string errMsg = "Connection to Tomcat has been closed. Tomcat may be restarting. Please retry later.";
                if (p_Logger != null)
                {
                    errMsg = errMsg + "<br><small>Administrator: please check your log file for detail on this error if needed.</small>";
                }
                else
                {
                    errMsg = errMsg + "<br><small>Administrator: please enable logging with log level 1 or greater for detail problem capture if needed.</small>";
                }

                if (BonCodeAJP13Settings.BONCODEAJP13_TOMCAT_TCPCLIENT_ERRORMSG.Length > 1) errMsg = BonCodeAJP13Settings.BONCODEAJP13_TOMCAT_TCPCLIENT_ERRORMSG;
                p_PacketsReceived.Add(new TomcatSendBodyChunk(errMsg));
                return;
            }
        }
        /// <summary>
        /// Create Actual Connection and initiate wait for response within this thread
        /// </summary>
        private void p_CreateConnection(BonCodeAJP13PacketCollection packetsToSend)
        {
            p_AbortConnection = false;
            p_ConnectionsID++; //increment the number of connections
            p_ConnectionsCounter++;
            p_ThisConnectionID = p_ConnectionsCounter; //assign the connection id

            if (p_ConnectionsID > BonCodeAJP13Settings.MAX_BONCODEAJP13_CONCURRENT_CONNECTIONS)
            {
                //throw exception if we exceeded concurrent connections
                //Exception e = new Exception("0:Maximum connection limit exceeded. Please change limit setting if change is desired. Dropping connection.");
                //throw e;

            }

            try
            {
                //create new connection and timer if we maintain connection timeout within this class
                if (p_TCPClient == null)
                {
                    p_TCPClient = new TcpClient(p_Server, p_Port);

                    p_KeepAliveTimer = new System.Timers.Timer();
                    p_KeepAliveTimer.Interval = BonCodeAJP13Consts.BONCODEAJP13_SERVER_KEEP_ALIVE_TIMEOUT;
                    p_KeepAliveTimer.Elapsed += new System.Timers.ElapsedEventHandler(p_KeepAliveTimer_Elapsed);
                    p_KeepAliveTimer.AutoReset = false;
                    p_KeepAliveTimer.Enabled = true; //starts the timer
                }
                //start p_StopWatch
                p_StopWatch.Start();

                //assign package and wait for response
                p_PacketsToSend = packetsToSend;

                //handle connection
                HandleConnection();
            }
            catch (Exception e)
            {
                if (p_Logger != null) p_Logger.LogException(e, "TCP Client level -- Server/Port:" + p_Server + "/" + p_Port.ToString());
                ConnectionError();
                return;
            }
        }
 /// <summary>
 /// Initialize new connection from server to tomcat in new thread.
 /// We use a packet collection (in case of Form posts)
 /// </summary>
 public BonCodeAJP13ServerConnection(BonCodeAJP13PacketCollection packetsToSend)
 {
     CheckMutex();
     //call connection creation
     p_CreateConnection(packetsToSend);
 }
 public void BonCodeAJP13PacketCollectionConstructorTest()
 {
     BonCodeAJP13PacketCollection target = new BonCodeAJP13PacketCollection();
     Assert.Inconclusive("TODO: Implement code to verify target");
 }
 public void RemoveTest()
 {
     BonCodeAJP13PacketCollection target = new BonCodeAJP13PacketCollection(); // TODO: Initialize to an appropriate value
     BonCodeAJP13Packet value = null; // TODO: Initialize to an appropriate value
     target.Remove(value);
     Assert.Inconclusive("A method that does not return a value cannot be verified.");
 }
 public void ItemTest()
 {
     BonCodeAJP13PacketCollection target = new BonCodeAJP13PacketCollection(); // TODO: Initialize to an appropriate value
     int index = 0; // TODO: Initialize to an appropriate value
     BonCodeAJP13Packet expected = null; // TODO: Initialize to an appropriate value
     BonCodeAJP13Packet actual;
     target[index] = expected;
     actual = target[index];
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }
 /// <summary>
 /// Initialize new connection from server to tomcat in new thread.
 /// We use a packet collection (in case of Form posts) 
 /// DEPRECATED DO NOT USE
 /// </summary>
 public BonCodeAJP13ServerConnection(BonCodeAJP13PacketCollection packetsToSend)
 {
     CheckMutex();
     //call connection creation
     p_CreateConnection(packetsToSend);
 }
 /// <summary>
 /// Initialize new connection from server to tomcat in new thread.
 /// this connection will run in new thread spawned from the listener thread.
 /// DEPRECATED DO NOT USE
 /// </summary>
 public BonCodeAJP13ServerConnection(BonCodeAJP13Packet singlePacket)
 {
     CheckMutex();
     //package single package into packets to Send
     BonCodeAJP13PacketCollection packetsToSend = new BonCodeAJP13PacketCollection();
     packetsToSend.Add(singlePacket);
     //call connection creation
     p_CreateConnection(packetsToSend);
 }
        /// <summary>
        /// Analyze the provided buffer and returns a collection of packets represented by the buffer
        /// </summary>
        public static BonCodeAJP13PacketCollection GetPackets(byte[] Buffer)
        {
            BonCodeAJP13PacketCollection AnalyzedResponses = new BonCodeAJP13PacketCollection();

            if (Buffer != null)
            {
                int start = 0;
                for (int i = 0; i < Buffer.Length; i++)
                {
                    if (Buffer[start] == BonCodeAJP13PacketFormat.BONCODEAJP13_PACKET_START)
                    {
                        UInt16 UserDataLength = 0;
                        GetUInt16(Buffer, ref UserDataLength, start + USERDATALENGTH_POS);
                        i = start + UserDataLength + 8;

                        // here we need to truncate the buffer and analyze the packet.
                        if (Buffer[i] == BonCodeAJP13PacketFormat.BONCODEAJP13_PACKET_END)
                        {

                            byte[] NewPacket = new byte[i + 1 - start];
                            Array.Copy(Buffer, start, NewPacket, 0, i + 1 - start);

                            BonCodeAJP13Packet packet = BonCodeAJP13Packet.GetPacket(NewPacket);
                            if (packet != null)
                            {
                                AnalyzedResponses.Add(packet);
                            }
                            else
                            {
                                return null;
                            }

                            start = i + 1;
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
                return AnalyzedResponses;
            }
            else
            {
                return null;
            }
        }
 /// <summary>
 /// Log a collection of packets by calling their PrintPacket methods
 /// Requires BONCODEAJP13_LOG_LEVEL >= 2
 /// </summary>
 public void LogPackets(BonCodeAJP13PacketCollection packets, bool logAllways = false, int minLogLevel = BonCodeAJP13LogLevels.BONCODEAJP13_LOG_ERRORS)
 {
     foreach (BonCodeAJP13Packet packet in packets)
     {
         this.LogPacket(packet,logAllways,minLogLevel);
     }
 }
 /// <summary>
 /// Log a collection of packets by calling their PrintPacket methods
 /// Requires BONCODEAJP13_LOG_LEVEL >= 2
 /// </summary>
 public void LogPackets(BonCodeAJP13PacketCollection packets, bool logAllways = false, int onlyAboveLogLevel = BonCodeAJP13LogLevels.BONCODEAJP13_NO_LOG)
 {
     foreach (BonCodeAJP13Packet packet in packets)
     {
         this.LogPacket(packet,logAllways,onlyAboveLogLevel);
     }
 }
 /// <summary>
 /// Initialize new connection to tomcat using server and port input
 /// DEPRECATED DO NOT USE
 /// </summary>
 public BonCodeAJP13ServerConnection(string server, int port, BonCodeAJP13Packet singlePacket)
 {
     CheckMutex();
     this.Port = port;
     this.Server = server;
     //create collection and add packet passed
     BonCodeAJP13PacketCollection packetsToSend = new BonCodeAJP13PacketCollection();
     packetsToSend.Add(singlePacket);
     //call connection creation
     p_CreateConnection(packetsToSend);
 }
Exemple #22
0
        /// <summary>
        /// Function to be passed as delegate to BonCodeAJP13 process
        /// Will pass packet collection content to user browser and flush
        /// </summary>        
        void PrintFlush(BonCodeAJP13PacketCollection flushCollection)
        {
            p_FlushInProgress = true;

            string keyName = "";
            string keyValue = "";

            bool isBinary = false;
            long contentLength = 0; //only assigned if content is known
            long transferredBytes = 0;

            foreach (TomcatReturn flushPacket in flushCollection)
            {
                try
                {
                    //check by packet type and do different processing before calling flush
                    if (flushPacket is TomcatSendHeaders)
                    {
                        TomcatSendHeaders tcshPackage = (TomcatSendHeaders)flushPacket;
                        //get Headers
                        NameValueCollection tomcatHeaders = tcshPackage.GetHeaders();
                        //iterate through headers and set
                        for (int i = 0; i < tomcatHeaders.AllKeys.Length; i++)
                        {
                            keyName = tomcatHeaders.AllKeys[i];
                            keyValue = tomcatHeaders[keyName];

                            //check for repeated headers of the same type they are seperated by pipe+comma combination
                            string[] sHeaders = keyValue.Split(new string[] { "|," }, StringSplitOptions.None);
                            string tempValue = "";
                            if (sHeaders.Length > 1)
                            {
                                //check for multiple headers of same type returned, e.g. cookies
                                for (int i2 = 0; i2 < sHeaders.Length; i2++)
                                {

                                    if (i2 == sHeaders.Length - 1)
                                    {
                                        tempValue = sHeaders[i2].Substring(0, sHeaders[i2].Length - 1); //last array element
                                    }
                                    else
                                    {
                                        tempValue = sHeaders[i2]; //regular array element
                                    }
                                    p_Context.Response.AddHeader(keyName, tempValue);
                                }
                            }

                            else
                            {
                                //single header remove pipe character at the end
                                tempValue = keyValue.Substring(0, keyValue.Length - 1);
                                p_Context.Response.AddHeader(keyName, tempValue);

                            }

                            //check for binary or text disposition
                            if (!isBinary && (keyName == "Content-Type" || keyName == "Content-Encoding"))
                            {
                                //set encoding seperatly if needed
                                if (keyName == "Content-Encoding" && ( tempValue.Contains("gzip") || tempValue.Contains("deflate") ))
                                {
                                    isBinary = true;
                                }
                                else
                                {
                                    isBinary = TestBinary(keyValue);
                                }

                            }
                            //check for known content length
                            if (keyName == "Content-Length")
                            {
                                try
                                {
                                    contentLength = System.Convert.ToInt64(tempValue);
                                }
                                catch (Exception) {
                                    contentLength = 0;
                                };

                            }

                            //check whether we can represent a given header in native IIS Response context
                            IISNativeHeaders(keyName, tempValue);

                        }
                        //set response status code
                        p_Context.Response.StatusCode = tcshPackage.GetStatus();

                    }
                    else if (flushPacket is TomcatEndResponse)
                    {
                        //if this is the last package and we know the content length we need to write empty strings
                        //this is a fix if content-length is misrepresented by tomcat
                        if (contentLength > 0 && transferredBytes < contentLength)
                        {
                            string fillEmpty = new string(' ', System.Convert.ToInt32(contentLength - transferredBytes));
                            p_Context.Response.Write(fillEmpty);

                        }

                    }
                    else if (flushPacket is TomcatSendBodyChunk)
                    {
                        transferredBytes = transferredBytes + flushPacket.GetUserDataBytes().Length;
                        p_Context.Response.BinaryWrite(flushPacket.GetUserDataBytes());

                    }
                }
                catch (Exception)
                {
                    //TODO: add exception handler logging
                    p_Context.Response.Write("Error in transfer of data from tomcat to browser.");

                }

            } //loop over packets

            //attempt to flush now
            try
            {
                p_Context.Response.Flush();
            }
            catch (Exception)
            {
                //do nothing. Mostly this occurs if the browser already closed connection with server or headers were already transferred
                bool errFlag = true;
            }

            p_FlushInProgress = false;
        }
 /// <summary>
 /// Initialize new connection to tomcat using server and port input and packet collection
 /// DEPRECATED DO NOT USE
 /// </summary>
 public BonCodeAJP13ServerConnection(string server, int port, BonCodeAJP13PacketCollection packetsToSend)
 {
     CheckMutex();
     this.Port = port;
     this.Server = server;
     //call connection creation
     p_CreateConnection(packetsToSend);
 }
Exemple #24
0
        //this will be passed in as delegate
        static void PrintFlush(BonCodeAJP13PacketCollection flushCollection)
        {
            foreach (TomcatReturn flushPacket in flushCollection)
            {
                //TODO: check by packet type and do different processing before calling flush
                //TomcatSendHeaders tcsh = new TomcatSendHeaders();
                if (flushPacket is TomcatSendHeaders)
                {
                    TomcatSendHeaders tcsh = (TomcatSendHeaders)flushPacket;
                    NameValueCollection tcHeaders = tcsh.GetHeaders();

                    for (int i = 0; i < tcHeaders.AllKeys.Length; i++)
                    {
                        Console.WriteLine(tcHeaders[i]);
                    }

                }
                else
                {
                    //generic flush of data
                    string outString = flushPacket.GetUserDataString();
                    Console.WriteLine(outString);
                }

            }
        }