示例#1
0
        /// <summary>
        /// Executes the parsing operation
        /// </summary>
        /// <param name="pathOfFileToImport"></param>
        /// <param name="currentFile"></param>
        /// <param name="options"></param>
        public void Parse(string pathOfFileToImport, ITrafficDataAccessor currentFile, ParsingOptions options)
        {
            _options = options;
            var exclusions = options.GetExclusions();

            _status = TrafficParserStatus.Running;
            Har har = HarConvert.DeserializeFromFile(pathOfFileToImport);

            foreach (Entry entry in har.Log.Entries)
            {
                try
                {
                    if (!IsExcluded(entry.Request.Url, exclusions))
                    {
                        AddRequest(currentFile, entry);
                    }
                }
                catch (Exception ex)
                {
                    SdkSettings.Instance.Logger.Log(TraceLevel.Error, "URI Parser - Failed to add request: {0}", ex.Message);
                }
            }

            _status = TrafficParserStatus.Stopped;
        }
 private void HandleHalt()
 {
     _parserStatus = TrafficParserStatus.Stopped;
     if (_threads.Count > 0)
     {
         //save all requests that are in process without removing them from their stack, so they can be resumed later
         foreach (ThreadInfo threadInfo in _threads.Values)
         {
             SaveThreadRequests(threadInfo);
         }
     }
     else if (_currentThreadInfo != null)
     {
         SaveThreadRequests(_currentThreadInfo);
     }
 }
        /// <summary>
        /// This is the method resposible for parsing the raw traffic log
        /// </summary>
        private void ParseFunction()
        {
            _parserStatus = TrafficParserStatus.Running;
            byte[]   bytes;          //will store a line of bytes
            string   line;           //will store the converted bytes to string
            LineType lineType;

            _thisSessionRequestCount = 0;

            do
            {
                try
                {
                    //read a line and handle end of file or stop requested
                    bytes = Utils.ReadLine(_rawLog, ESTIMATED_LINE_SIZE);

                    line = Utils.ByteToString(bytes);

                    lineType = _lineTypeSelector.GetLineType(line);

                    //if the end of file was reached or the user stopped the parse
                    if (lineType == LineType.EndOfFile || _stopRequested)
                    {
                        HandleHalt();
                        return;
                    }

                    //according to the line type perform the coresponding action
                    switch (lineType)
                    {
                    case LineType.BeginThread:
                        HandleBeginThread(line);
                        break;

                    case LineType.EndThread:
                        HandleEndThread();
                        break;

                    case LineType.FirstRequestLine:
                        HandleFirstRequestLine(line, bytes);
                        break;

                    case LineType.FirstResponseLine:
                        HandleFirstResponseLine(line, bytes);
                        break;

                    case LineType.HttpTraffic:
                        HandleHttpTraffic(line, bytes);
                        break;

                    case LineType.ResponseReceived:
                        HandleResponseReceivedMessage();
                        break;

                    case LineType.SendingRequest:
                        HandleSendingRequestLine(line);
                        break;

                    case LineType.ReceivingResponse:
                        HandleReceivingResponseLine(line);
                        break;
                    }

                    //handle tail (if is enabled _tailChunk > 0)
                    if (_tailChunk > 0 && _thisSessionRequestCount >= _tailChunk)
                    {
                        _stopRequested = true;
                    }

                    //extract custom fields
                    HandleCustomFields(line);
                }
                catch (OutOfMemoryException)
                {
                    SdkSettings.Instance.Logger.Log(TraceLevel.Error, "Out of memory exception occured during parsing {0}. Calling the garbage collector.", _rawLog.Name);
                    GC.Collect();
                }
                catch (Exception ex)
                {
                    SdkSettings.Instance.Logger.Log(TraceLevel.Error, "Error parsing {0}:{1}", _rawLog.Name, ex.Message);
                }
            }while (true);
        }
示例#4
0
        /// <summary>
        /// Executes the parsing operation
        /// </summary>
        /// <param name="pathOfFileToImport"></param>
        /// <param name="currentFile"></param>
        /// <param name="options"></param>
        public void Parse(string pathOfFileToImport, ITrafficDataAccessor currentFile, ParsingOptions options)
        {
            _options = options;
            _status  = TrafficParserStatus.Running;

            string text = File.ReadAllText(pathOfFileToImport);

            MatchCollection matches = Regex.Matches(text, URI_REGEX);

            Dictionary <string, List <Uri> > uniqueRequests = new Dictionary <string, List <Uri> >();

            foreach (Match m in matches)
            {
                try
                {
                    Uri    uri = new Uri(m.Value);
                    string key = String.Format("{0}://{1}:{2}/{3}", uri.Scheme, uri.Host, uri.Port, uri.AbsolutePath);
                    if (!uniqueRequests.ContainsKey(key))
                    {
                        uniqueRequests.Add(key, new List <Uri>());
                    }
                    uniqueRequests[key].Add(uri);
                }
                catch (Exception ex)
                {
                    SdkSettings.Instance.Logger.Log(TraceLevel.Error, "URI Parser - Invalid uri: {0}", ex.Message);
                }
            }


            foreach (string key in uniqueRequests.Keys)
            {
                try
                {
                    List <Uri> uriList = uniqueRequests[key];
                    //construct querystring
                    StringBuilder queryStringBuilder = new StringBuilder();
                    foreach (Uri uri in uriList)
                    {
                        string query = uri.Query.Trim('?');
                        if (!String.IsNullOrWhiteSpace(query))
                        {
                            queryStringBuilder.Append(query);
                            queryStringBuilder.Append('&');
                        }
                    }
                    //now get the full query
                    string fullQuery = queryStringBuilder.ToString().Trim('&');
                    Uri    firstUri  = uriList[0];

                    AddRequest(currentFile, firstUri, fullQuery, Resources.UriParserGetRequest);
                    AddRequest(currentFile, firstUri, fullQuery, Resources.UriParserPostRequest);
                }
                catch (Exception ex)
                {
                    SdkSettings.Instance.Logger.Log(TraceLevel.Error, "URI Parser - Failed to add request: {0}", ex.Message);
                }
            }

            _status = TrafficParserStatus.Stopped;
        }