public void NotBeforeEndUnitTest() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); byte[] sample = Encoding.ASCII.GetBytes("The quick brown fox jumped over..."); bool hasFox = BufferUtils.HasString(sample, 0, Int32.MaxValue, "fox", "brown"); Assert.True(!hasFox, "The string was not found in the buffer."); }
/// <summary> /// Attempts to parse a SIP message from a single buffer that can only contain a single message. /// </summary> /// <param name="buffer">The buffer that will be parsed for a SIP message.</param> /// <param name="localSIPEndPoint">The end point the message was received on.</param> /// <param name="remoteSIPEndPoint">The end point the message was received from.</param> /// <returns>If successful a SIP message or null if not.</returns> public static SIPMessageBuffer ParseSIPMessage(byte[] buffer, SIPEndPoint localSIPEndPoint, SIPEndPoint remoteSIPEndPoint) { string message = null; try { if (buffer == null || buffer.Length < m_minFirstLineLength) { // Ignore. return(null); } else if (buffer.Length > SIPConstants.SIP_MAXIMUM_RECEIVE_LENGTH) { throw new ApplicationException( "SIP message received that exceeded the maximum allowed message length, ignoring."); } else if (!BufferUtils.HasString(buffer, 0, buffer.Length, SIP_MESSAGE_IDENTIFIER, m_CRLF)) { // Message does not contain "SIP" anywhere on the first line, ignore. return(null); } else { message = ByteToStr(buffer, Encoding.UTF8); if (message.ToUpper().Contains("ENCODING=\"GBK\"") || message.ToUpper().Contains("ENCODING=\"GB2312\"")) { message = ByteToStr(buffer, Encoding.GetEncoding("GBK")); //兼容gbk,gb2312编码中文字符串 } SIPMessageBuffer sipMessageBuffer = ParseSIPMessage(message, localSIPEndPoint, remoteSIPEndPoint); if (sipMessageBuffer != null) { sipMessageBuffer.RawBuffer = buffer; return(sipMessageBuffer); } else { return(null); } } } catch (Exception excp) { message = message.Replace("\n", "LF"); message = message.Replace("\r", "CR"); logger.LogError("Exception ParseSIPMessage. " + excp.Message + "\nSIP Message=" + message + "."); return(null); } }
public static RTSPMessage ParseRTSPMessage(byte[] buffer, IPEndPoint receivedFrom, IPEndPoint receivedOn) { string message = null; try { if (buffer == null || buffer.Length < m_minFirstLineLength) { // Ignore. return(null); } else if (buffer.Length > RTSPConstants.RTSP_MAXIMUM_LENGTH) { logger.LogError( "RTSP message received that exceeded the maximum allowed message length, ignoring."); return(null); } else if (!BufferUtils.HasString(buffer, 0, buffer.Length, RTSP_MESSAGE_IDENTIFIER, m_CRLF)) { // Message does not contain "RTSP" anywhere on the first line, ignore. return(null); } else { message = Encoding.UTF8.GetString(buffer); RTSPMessage rtspMessage = ParseRTSPMessage(message, receivedFrom, receivedOn); rtspMessage.RawBuffer = buffer; return(rtspMessage); } } catch (Exception excp) { message = message.Replace("\n", "LF"); message = message.Replace("\r", "CR"); logger.LogError("Exception ParseRTSPMessage. " + excp.Message + "\nRTSP Message=" + message + "."); return(null); } }
/// <summary> /// Attempts to parse a SIP message from a single buffer that can only contain a single message. /// </summary> /// <param name="buffer">The buffer that will be parsed for a SIP message.</param> /// <param name="sipBodyEncoding">SIP payload encoding</param> /// <param name="localSIPEndPoint">The end point the message was received on.</param> /// <param name="remoteSIPEndPoint">The end point the message was received from.</param> /// <param name="sipEncoding">SIP protocol encoding, according to RFC should be UTF-8 </param> /// <returns>If successful a SIP message or null if not.</returns> public static SIPMessageBuffer ParseSIPMessage( byte[] buffer, Encoding sipEncoding, Encoding sipBodyEncoding, SIPEndPoint localSIPEndPoint, SIPEndPoint remoteSIPEndPoint) { if (buffer == null || buffer.Length < m_minFirstLineLength) { // Ignore. return(null); } else if (buffer.Length > SIPConstants.SIP_MAXIMUM_RECEIVE_LENGTH) { throw new ApplicationException("SIP message received that exceeded the maximum allowed message length, ignoring."); } else if (!BufferUtils.HasString(buffer, 0, buffer.Length, SIP_MESSAGE_IDENTIFIER, m_CRLF)) { // Message does not contain "SIP" anywhere on the first line, ignore. return(null); } else { var sipMessage = new SIPMessageBuffer(sipEncoding, sipBodyEncoding); sipMessage.RawBuffer = buffer; sipMessage.LocalSIPEndPoint = localSIPEndPoint; sipMessage.RemoteSIPEndPoint = remoteSIPEndPoint; // For connection oriented transports the same connection should be used for responses and subsequent requests. if (sipMessage.LocalSIPEndPoint != null && remoteSIPEndPoint.ConnectionID != null) { sipMessage.LocalSIPEndPoint.ConnectionID = remoteSIPEndPoint.ConnectionID; } string message = sipEncoding.GetString(buffer); int endFistLinePosn = message.IndexOf(m_CRLF); if (endFistLinePosn != -1) { sipMessage.FirstLine = message.Substring(0, endFistLinePosn); if (sipMessage.FirstLine.Substring(0, 3) == SIP_RESPONSE_PREFIX) { sipMessage.SIPMessageType = SIPMessageTypesEnum.Response; } else { sipMessage.SIPMessageType = SIPMessageTypesEnum.Request; } int endHeaderPosn = message.IndexOf(m_CRLF + m_CRLF); if (endHeaderPosn == -1) { // Assume flakey implementation if message does not contain the required CRLFCRLF sequence and treat the message as having no body. string headerString = message.Substring(endFistLinePosn + 2, message.Length - endFistLinePosn - 2); sipMessage.SIPHeaders = SIPHeader.SplitHeaders(headerString); } else { string headerString = message.Substring(endFistLinePosn + 2, endHeaderPosn - endFistLinePosn - 2); sipMessage.SIPHeaders = SIPHeader.SplitHeaders(headerString); if (message.Length > endHeaderPosn + 4) { sipMessage.Body = new byte[buffer.Length - (endHeaderPosn + 4)]; Buffer.BlockCopy(buffer, endHeaderPosn + 4, sipMessage.Body, 0, buffer.Length - (endHeaderPosn + 4)); } } return(sipMessage); } else { logger.LogWarning("Error ParseSIPMessage, there were no end of line characters in the string being parsed."); return(null); } } }