Ejemplo n.º 1
0
 /// <summary>
 /// Log one packet by calling its PrintPacket method. Only if BONCODEAJP13_LOG_LEVEL >= 1
 /// if logAllways is set packet will be logged regardless of log level
 /// </summary>
 public void LogPacket(BonCodeAJP13Packet packet, bool logAllways = false, int onlyAboveLogLevel = BonCodeAJP13LogLevels.BONCODEAJP13_NO_LOG)
 {
     //only log packets if logging level allows
     if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL > BonCodeAJP13LogLevels.BONCODEAJP13_NO_LOG && BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL > onlyAboveLogLevel || logAllways)
     {
         p_Mut.WaitOne();
         using (StreamWriter logStream = File.AppendText(p_FileName))
         {
             if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL == BonCodeAJP13LogLevels.BONCODEAJP13_LOG_HEADERS)
             {
                 logStream.WriteLine("-- Packet Info:" + packet.ToString() + " at: " + DateTime.Now.ToShortDateString() + "    " + DateTime.Now.ToLongTimeString());
                 logStream.WriteLine(packet.PrintPacketHeader());
                 logStream.WriteLine("");
                 logStream.Flush();
                 logStream.Close();
             }
             ;
             //logs full packets. Log files may grow big in this case
             if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL == BonCodeAJP13LogLevels.BONCODEAJP13_LOG_DEBUG)
             {
                 logStream.WriteLine("-- Packet Info:" + packet.ToString() + " at: " + DateTime.Now.ToShortDateString() + "    " + DateTime.Now.ToLongTimeString());
                 logStream.WriteLine(packet.PrintPacket());
                 logStream.WriteLine("");
                 logStream.Flush();
                 logStream.Close();
             }
             ;
         }
         p_Mut.ReleaseMutex();
     }
 }
        /// <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);
        }
        /// <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);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Log one packet by calling its PrintPacket method. Only if BONCODEAJP13_LOG_LEVEL >= 1
        /// if logAllways is set packet will be logged regardless of log level.
        /// Packet logging only occurs if we have exception, Log Headers or Log Debug
        /// </summary>
        public void LogPacket(BonCodeAJP13Packet packet, bool logAllways = false, int minLogLevel = BonCodeAJP13LogLevels.BONCODEAJP13_LOG_ERRORS)
        {
            //only log packets if logging level allows
            if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL > BonCodeAJP13LogLevels.BONCODEAJP13_NO_LOG && BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL >= minLogLevel || logAllways)
            {
                try {
                    p_Mut.WaitOne();
                    using (StreamWriter logStream = File.AppendText(p_FileName))
                    {
                        //log packet headers only
                        if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL == BonCodeAJP13LogLevels.BONCODEAJP13_LOG_HEADERS)
                        {
                            logStream.WriteLine(DateTime.Now.ToString(p_timestampFormat) + packet.ToString() + " " + packet.PrintPacketHeader());

                            logStream.Flush();
                            logStream.Close();
                        }
                        ;

                        //logs full packets. Log files may grow big in this case
                        if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL == BonCodeAJP13LogLevels.BONCODEAJP13_LOG_DEBUG)
                        {
                            logStream.WriteLine(DateTime.Now.ToString(p_timestampFormat) + packet.ToString() + " " + packet.PrintPacketHeader());
                            logStream.WriteLine(packet.PrintPacket());
                            logStream.WriteLine("");

                            logStream.Flush();
                            logStream.Close();
                        }
                        ;
                    }
                    p_Mut.ReleaseMutex();
                }
                catch (Exception fileException)
                {
                    RecordSysEvent("Error during log write : " + fileException.Message, EventLogEntryType.Error);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Log one packet by calling its PrintPacket method. Only if BONCODEAJP13_LOG_LEVEL >= 1
        /// if logAllways is set packet will be logged regardless of log level.
        /// Packet logging only occurs if we have exception, Log Headers or Log Debug
        /// </summary>
        public void LogPacket(BonCodeAJP13Packet packet, bool logAllways = false, int minLogLevel = BonCodeAJP13LogLevels.BONCODEAJP13_LOG_ERRORS)
        {
            //only log packets if logging level allows
            if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL > BonCodeAJP13LogLevels.BONCODEAJP13_NO_LOG && BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL >= minLogLevel || logAllways)
            {
                try {
                    p_Mut.WaitOne();
                    using (StreamWriter logStream = File.AppendText(p_FileName))
                    {
                        //log packet headers only
                        if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL == BonCodeAJP13LogLevels.BONCODEAJP13_LOG_HEADERS)
                        {
                            logStream.WriteLine(DateTime.Now.ToString(p_timestampFormat) + packet.ToString() + " " + packet.PrintPacketHeader());

                            logStream.Flush();
                            logStream.Close();
                        }
                        ;

                        //logs full packets. Log files may grow big in this case
                        if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL == BonCodeAJP13LogLevels.BONCODEAJP13_LOG_DEBUG)
                        {
                            logStream.WriteLine(DateTime.Now.ToString(p_timestampFormat) + packet.ToString() + " " + packet.PrintPacketHeader());
                            logStream.WriteLine(packet.PrintPacket());
                            logStream.WriteLine("");

                            logStream.Flush();
                            logStream.Close();
                        }
                        ;
                    }
                    p_Mut.ReleaseMutex();
                }
                catch (Exception fileException)
                {
                    //don't like empty catches but if we cannot log, let's not throw more errors
                }
            }
        }
 public int Add(BonCodeAJP13Packet value)
 {
     return(List.Add(value));
 }
 public void Remove(BonCodeAJP13Packet value)
 {
     List.Remove(value);
 }
 /// <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);
 }
 /// <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);
     }
 }
 public void Remove(BonCodeAJP13Packet value)
 {
     List.Remove(value);
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Log one packet by calling its PrintPacket method. Only if BONCODEAJP13_LOG_LEVEL >= 1
        /// if logAllways is set packet will be logged regardless of log level
        /// </summary>
        public void LogPacket(BonCodeAJP13Packet packet, bool logAllways = false, int onlyAboveLogLevel = BonCodeAJP13LogLevels.BONCODEAJP13_NO_LOG)
        {
            //only log packets if logging level allows
            if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL > BonCodeAJP13LogLevels.BONCODEAJP13_NO_LOG && BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL > onlyAboveLogLevel || logAllways)
            {
                p_Mut.WaitOne();
                using (StreamWriter logStream = File.AppendText(p_FileName))
                {

                    if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL == BonCodeAJP13LogLevels.BONCODEAJP13_LOG_HEADERS)
                    {
                        logStream.WriteLine("-- Packet Info:" + packet.ToString() + " at: " + DateTime.Now.ToShortDateString() + "    " + DateTime.Now.ToLongTimeString());
                        logStream.WriteLine(packet.PrintPacketHeader());
                        logStream.WriteLine("");
                        logStream.Flush();
                        logStream.Close();
                    };
                    //logs full packets. Log files may grow big in this case
                    if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL == BonCodeAJP13LogLevels.BONCODEAJP13_LOG_DEBUG)
                    {
                        logStream.WriteLine("-- Packet Info:" + packet.ToString() + " at: " + DateTime.Now.ToShortDateString() + "    " + DateTime.Now.ToLongTimeString());
                        logStream.WriteLine(packet.PrintPacket());
                        logStream.WriteLine("");
                        logStream.Flush();
                        logStream.Close();
                    };

                }
                p_Mut.ReleaseMutex();
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Log one packet by calling its PrintPacket method. Only if BONCODEAJP13_LOG_LEVEL >= 1
        /// if logAllways is set packet will be logged regardless of log level.
        /// Packet logging only occurs if we have exception, Log Headers or Log Debug
        /// </summary>
        public void LogPacket(BonCodeAJP13Packet packet, bool logAllways = false, int minLogLevel = BonCodeAJP13LogLevels.BONCODEAJP13_LOG_ERRORS)
        {
            //only log packets if logging level allows
            if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL > BonCodeAJP13LogLevels.BONCODEAJP13_NO_LOG && BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL >= minLogLevel || logAllways)
            {
                try {

                    p_Mut.WaitOne();
                    using (StreamWriter logStream = File.AppendText(p_FileName))
                    {

                        //log packet headers only
                        if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL == BonCodeAJP13LogLevels.BONCODEAJP13_LOG_HEADERS)
                        {
                            logStream.WriteLine(DateTime.Now.ToString(p_timestampFormat) + packet.ToString() + " " + packet.PrintPacketHeader());

                            logStream.Flush();
                            logStream.Close();
                        };

                        //logs full packets. Log files may grow big in this case
                        if (BonCodeAJP13Settings.BONCODEAJP13_LOG_LEVEL == BonCodeAJP13LogLevels.BONCODEAJP13_LOG_DEBUG)
                        {
                            logStream.WriteLine(DateTime.Now.ToString(p_timestampFormat) + packet.ToString() + " " + packet.PrintPacketHeader());
                            logStream.WriteLine(packet.PrintPacket());
                            logStream.WriteLine("");

                            logStream.Flush();
                            logStream.Close();
                        };

                    }
                    p_Mut.ReleaseMutex();

                }
                catch (Exception fileException)
                {
                    //don't like empty catches but if we cannot log, let's not throw more errors
                }
            }
        }
        private void ComunicateWithTomcat()
        {
            int numOfBytesReceived = 0;

            byte[] receivedPacketBuffer = new byte[BonCodeAJP13Consts.MAX_BONCODEAJP13_PACKET_LENGTH];
            byte[] notProcessedBytes    = null;
            int    sendPacketCount      = 0;

            p_IsLastPacket = false;



            //send packages. If multiple forward requests (i.e. form data or files) there is a different behavior expected
            if (p_PacketsToSend.Count > 1)
            {
                foreach (Object oIterate in p_PacketsToSend)
                {
                    //we will continue sending all packets in queue unless tomcat sends us End Comm package
                    if (!p_IsLastPacket)
                    {
                        sendPacketCount++;
                        BonCodeAJP13Packet sendPacket = oIterate as BonCodeAJP13Packet; //only objects derived from this class should be in the collection

                        //send first two packets immediatly
                        p_NetworkStream.Write(sendPacket.GetDataBytes(), 0, sendPacket.PacketLength);

                        //log packet
                        if (p_Logger != null)
                        {
                            p_Logger.LogPacket(sendPacket, false, BonCodeAJP13LogLevels.BONCODEAJP13_LOG_HEADERS);
                        }

                        //after the second packet in a packet collection we have to listen and receive a TomcatGetBodyChunk
                        if (sendPacketCount >= 2)
                        {
                            numOfBytesReceived = p_NetworkStream.Read(receivedPacketBuffer, 0, receivedPacketBuffer.Length);
                            notProcessedBytes  = AnalyzePackage(receivedPacketBuffer, true); //no flush processing during sending of data
                            //we expect a 7 byte response except for the last package record, if not record a warning
                            if (sendPacketCount != p_PacketsToSend.Count && numOfBytesReceived > 7)
                            {
                                if (p_Logger != null)
                                {
                                    p_Logger.LogMessageAndType("Incorrect response received from Tomcat", "warning", 1);
                                }
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                //if the last received message from tomcat is "GET_BODY_CHUNK" we need to send terminator package
                if (p_PacketsReceived[p_PacketsReceived.Count - 1] is TomcatGetBodyChunk)
                {
                    BonCodeAJP13Packet sendPacket = new BonCodeAJP13ForwardRequest(new byte[] { }); //create terminator (empty) package
                    p_NetworkStream.Write(sendPacket.GetDataBytes(), 0, sendPacket.PacketLength);
                    //log packet as it is sent
                    if (p_Logger != null)
                    {
                        p_Logger.LogPacket(sendPacket, false, BonCodeAJP13LogLevels.BONCODEAJP13_LOG_HEADERS);
                    }
                }
            }
            else if (p_PacketsToSend.Count == 1)
            {
                //send package
                BonCodeAJP13Packet sendPacket = p_PacketsToSend[0] as BonCodeAJP13Packet; //only objects derived from this class should be in the collection
                p_NetworkStream.Write(sendPacket.GetDataBytes(), 0, sendPacket.PacketLength);
                //log each packet as it is sent
                if (p_Logger != null)
                {
                    p_Logger.LogPacket(sendPacket, false, BonCodeAJP13LogLevels.BONCODEAJP13_LOG_HEADERS);
                }
            }
            else
            {
                //nothing to do
                CloseConnectionNoError("Nothing to send. Closing Connection.");
                return;
            }



            //switch into Receiving Mode. Receive the TcpServer.response.
            if (!p_IsLastPacket)
            {
                p_NetworkStream.Read(receivedPacketBuffer, 0, 0); //call empty read so we block this thread until we receive a response or we time out
            }
            numOfBytesReceived = 0;


            try
            {
                int readCount = 0;

                while (p_NetworkStream.CanRead && !p_AbortConnection && !p_IsLastPacket)
                {
                    //check to see whether we need to send extra termination package
                    if (p_SendTermPacket)
                    {
                        p_SendTermPacket = false;
                        BonCodeAJP13ForwardRequest terminatorFR = new BonCodeAJP13ForwardRequest(new byte[] { });
                        p_NetworkStream.Write(terminatorFR.GetDataBytes(), 0, terminatorFR.PacketLength);
                    }

                    //clear reading array
                    Array.Clear(receivedPacketBuffer, 0, receivedPacketBuffer.Length);
                    //read incoming packets until timeout or last package has been received.
                    readCount++;
                    numOfBytesReceived = p_NetworkStream.Read(receivedPacketBuffer, 0, receivedPacketBuffer.Length);


                    //analyze packet so far (adjust bytes from Receiving buffer):combine notProcessed with new Read bytes into new Received buffer if needed
                    if (notProcessedBytes != null)
                    {
                        //create tempArray that contains new set of bytes to be send a combination of newly received bytes as well as bytes that we were not able to process yet
                        byte[] tempArray = new byte[numOfBytesReceived + notProcessedBytes.Length];
                        Array.Copy(notProcessedBytes, 0, tempArray, 0, notProcessedBytes.Length);
                        Array.Copy(receivedPacketBuffer, 0, tempArray, notProcessedBytes.Length, numOfBytesReceived);

                        notProcessedBytes = AnalyzePackage(tempArray);
                    }
                    else
                    {
                        //send bytes we received for analysis
                        byte[] tempArray = new byte[numOfBytesReceived];
                        Array.Copy(receivedPacketBuffer, 0, tempArray, 0, numOfBytesReceived);
                        notProcessedBytes = AnalyzePackage(tempArray);
                    }
                }
            }

            catch (System.IO.IOException ex)
            {
                ConnectionError("Server Connection is closing, Read timeout reached and no tomcat activity was detected.", "TimeOut");
                if (p_Logger != null)
                {
                    p_Logger.LogException(ex);
                }
                return;
            }

            if (p_AbortConnection)
            {
                ConnectionError("Server Connection was aborted:", "Failed");
                return;
            }

            if (numOfBytesReceived == 0)
            {
                // Nothing received from tomcat!
                ConnectionError("Nothing received from the tomcat. Closing the Connection.", "Failed");
                return;
            }


            if (p_IsLastPacket == true)
            {
                // keep alive timer needs reset (we are maintaining connection but resetting the timer
                if (p_KeepAliveTimer != null)
                {
                    p_KeepAliveTimer.Stop();

                    p_KeepAliveTimer.Start();
                }

                //CloseConnectionNoError();
            }
            else
            {
                //do nothing for now
            }
        }
 /// <summary>
 /// add a package to the collection of packets to be send to tomcat
 /// </summary>
 public void AddPacketToSendQueue(BonCodeAJP13Packet singlePacket)
 {
     p_PacketsToSend.Add(singlePacket);
 }
 public int IndexOf(BonCodeAJP13Packet value)
 {
     return(List.IndexOf(value));
 }
 public void Insert(int index, BonCodeAJP13Packet value)
 {
     List.Insert(index, value);
 }
 public int Add(BonCodeAJP13Packet value)
 {
     return (List.Add(value));
 }
 public bool Contains(BonCodeAJP13Packet value)
 {
     // If value is not of type BonCodeAJP13Packet, this will return false.
     return(List.Contains(value));
 }
 public bool Contains(BonCodeAJP13Packet value)
 {
     // If value is not of type BonCodeAJP13Packet, this will return false.
     return (List.Contains(value));
 }
 /// <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);
 }
 public int IndexOf(BonCodeAJP13Packet value)
 {
     return (List.IndexOf(value));
 }
 /// <summary>
 /// add a package to the collection of packets to be send to tomcat
 /// </summary>
 public void AddPacketToSendQueue(BonCodeAJP13Packet singlePacket)
 {
     p_PacketsToSend.Add(singlePacket);
 }
 public void Insert(int index, BonCodeAJP13Packet value)
 {
     List.Insert(index, value);
 }