private void TrafficViewSaveRequested(RequestTrafficViewSaveArgs e) { if (_currentId > -1) { //update the request info //parse the data HttpRequestInfo reqInfo = new HttpRequestInfo(e.Request); _responseBytes = Constants.DefaultEncoding.GetBytes(e.Response); HttpResponseInfo respInfo = new HttpResponseInfo(); respInfo.ProcessResponse(_responseBytes); TVRequestInfo currentTVInfo = _dataSource.GetRequestInfo(_currentId); //do not set the dom uniqueness id, needs to be explicitly calculated currentTVInfo.DomUniquenessId = String.Empty; currentTVInfo.RequestLine = reqInfo.RequestLine; currentTVInfo.RequestTime = DateTime.Now; currentTVInfo.ResponseStatus = respInfo.Status.ToString(); currentTVInfo.ResponseTime = DateTime.Now; currentTVInfo.Description = "Traffic Viewer Request"; currentTVInfo.ThreadId = "[0000]"; //convert the strings to bytes RequestResponseBytes reqData = new RequestResponseBytes(); reqData.AddToRequest(Constants.DefaultEncoding.GetBytes(e.Request)); reqData.AddToResponse(_responseBytes); //save the requests to the current data source _dataSource.SaveRequest(_currentId, reqData); _dataSource.SaveResponse(_currentId, reqData); _requestText = e.Request; _responseText = e.Response; } }
public void TestRequestLineAfterReplace() { TrafficViewer.Instance.NewTvf(); ITrafficDataAccessor tvf = TrafficViewer.Instance.TrafficViewerFile; string firstRequest = "GET http://site.com/a1 HTTP/1.1\r\nHeader1: a1"; tvf.AddRequestResponse(firstRequest, String.Empty); TVRequestInfo reqInfo = tvf.GetRequestInfo(0); Assert.AreEqual("GET http://site.com/a1 HTTP/1.1", reqInfo.RequestLine); Assert.AreEqual(1, tvf.RequestCount); LineSearcher searcher = new LineSearcher(); LineMatches result = new LineMatches(); SearchCriteriaSet criteriaSet = new SearchCriteriaSet(); criteriaSet.Add(new SearchCriteria(SearchContext.RequestLine, true, "a1|a=2|<r>2</r>")); searcher.Search(tvf, criteriaSet, result); tvf.Replace(result, "replacement"); firstRequest = Constants.DefaultEncoding.GetString(tvf.LoadRequestData(0)); Assert.AreEqual("GET http://site.com/replacement HTTP/1.1\r\nHeader1: a1", firstRequest); Assert.AreEqual("GET http://site.com/replacement HTTP/1.1", reqInfo.RequestLine); }
private void DoDiff(int indexOfFirst, int indexOfSecond) { TVRequestInfo info1 = _source.GetRequestInfo(indexOfFirst); TVRequestInfo info2 = _source.GetRequestInfo(indexOfSecond); _requestID1.Text = info1.Id + ", " + info1.Description + ", " + info1.DomUniquenessId; _requestID2.Text = info2.Id + ", " + info2.Description + ", " + info2.DomUniquenessId;; _progress.Visible = true; _timer.Start(); //turn off the diff buttons so the user doesn't start another async operation by mistake DiffButtonsSwitch(false); _diffWorker.RunWorkerAsync(new int[2] { indexOfFirst, indexOfSecond }); }
private void SaveThreadRequests(ThreadInfo threadInfo) { foreach (KeyValuePair <int, RequestResponseBytes> request in threadInfo.CurrentRequests) { TVRequestInfo header = _trafficViewerFile.GetRequestInfo(request.Key); if (header != null) { if (request.Value.RequestSize > header.RequestLength) { _trafficViewerFile.SaveRequest(request.Key, request.Value); } if (request.Value.ResponseSize > header.ResponseLength) { _trafficViewerFile.SaveResponse(request.Key, request.Value); } _thisSessionRequestCount++; } } }
/// <summary> /// Identifies aliases and in the first stage replaces them with the original value /// </summary> /// <param name="requestInfo"></param> /// <returns></returns> protected override HttpRequestInfo OnBeforeRequestToSite(HttpRequestInfo requestInfo) { requestInfo = base.OnBeforeRequestToSite(requestInfo); _trackingReqInfo = null; //update the request info if (requestInfo.Path.Contains(AdvancedExploreProxyConnection.REQ_ID_STRING)) //this is an alias request { //get the req id string idString = Utils.RegexFirstGroupValue(requestInfo.Path, REQ_ID_RX); //load the original path int id; if (int.TryParse(idString, out id)) { _trackingReqInfo = _dataStore.GetRequestInfo(id); if (_trackingReqInfo != null) { var lastPath = _trackingReqInfo.UpdatedPath; if (String.IsNullOrWhiteSpace(lastPath)) { throw new Exception(String.Format("Original path not the traffic file for request id: {0}", id)); } else { HttpServerConsole.Instance.WriteLine("Path substituted to: '{0}'", lastPath); } requestInfo.Path = lastPath; AddSpecialHeader(requestInfo); _originalRawRequest = requestInfo.ToString(); } } } return(requestInfo); }
/// <summary> /// Searches only in the specified subset /// </summary> /// <param name="dataSource"></param> /// <param name="criteriaSet"></param> /// <param name="result"></param> /// <param name="subset"></param> private void SubsetSearch(ITrafficDataAccessor dataSource, SearchCriteriaSet criteriaSet, ISearchResult result, SearchSubset subset) { foreach (int reqId in subset) { TVRequestInfo header = dataSource.GetRequestInfo(reqId); if (_stopSearch) { break; } if (header != null) { RequestMatches(dataSource, header, criteriaSet, result); } } }
public void Execute() { int temp = _requestEventsQueueSize; //temporarily change the events queue size to allow the new request to be automatically added _requestEventsQueueSize = 0; int i, count = _dataGrid.SelectedRows.Count; for (i = count - 1; i > -1; i--) { DataGridViewRow row = _dataGrid.SelectedRows[i]; //copy each selected row at the end of the list //get the id string stringId = (string)row.Cells["_id"].Value; int id = Convert.ToInt32(stringId); TVRequestInfo reqInfo = _dataSource.GetRequestInfo(id); ActionFunction(reqInfo); } _requestEventsQueueSize = temp; }
/// <summary> /// Sends only the requests specified by id /// </summary> /// <param name="source"></param> /// <param name="idsToSend"></param> public void Send(ITrafficDataAccessor source, IEnumerable <int> idsToSend, int numberOfThreads = 1) { PatternTracker.Instance.PatternsToTrack = source.Profile.GetTrackingPatterns(); Queue <TVRequestInfo> requestsToSend = new Queue <TVRequestInfo>(); if (numberOfThreads == 1) { foreach (int id in idsToSend) { TVRequestInfo info = source.GetRequestInfo(id); if (info != null) { requestsToSend.Enqueue(info); SendRequest(source, info); } } } else { for (int idx = 0; idx < numberOfThreads; idx++) { Thread t = new Thread(new ParameterizedThreadStart(SendAsync)); t.Start(new object[2] { source, requestsToSend }); } do { Thread.Sleep(1000); lock (_lock) { if (requestsToSend.Count == 0) { return; } } }while (true); } }
private void ExtractForms(string fileContents) { MatchCollection matches = Regex.Matches(fileContents, "(?si)<form.*?</form>"); foreach (Match m in matches) { var form = m.Value; //extract the form uri and method var method = Utils.RegexFirstGroupValue(form, "(?si)method\\s*=\\s*['\"]?(\\w+)").ToLower(); if (String.IsNullOrWhiteSpace(method)) { method = "get"; } var url = Utils.RegexFirstGroupValue(form, "(?si)action\\s*=\\s*['\"]?(\\w+)"); //if the url contains script in it or is malformed skip it if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute)) { continue; } Uri formUri = _rootUri; if (!String.IsNullOrWhiteSpace(url)) { formUri = new Uri(url); if (!formUri.IsAbsoluteUri) { formUri = new Uri(_rootUri, formUri); } } //extract form fields var fieldMatches = Regex.Matches(form, "(?si)<input.*?</input>"); HttpRequestInfo formReqInfo; if (method.Equals("get")) { formReqInfo = new HttpRequestInfo(String.Format("GET {0} HTTP/1.1\r\n{1}\r\n\r\n", formUri.PathAndQuery, _headers), true); } else { formReqInfo = new HttpRequestInfo(String.Format("POST {0} HTTP/1.1\r\n{1}\r\n", formUri.PathAndQuery, _headers), true); formReqInfo.Headers["Content-Type"] = "application/x-www-form-urlencoded"; } formReqInfo.Host = formUri.Host; formReqInfo.Port = formUri.Port; foreach (Match fm in fieldMatches) { //get input name and value var input = fm.Value; var name = Utils.RegexFirstGroupValue(form, "(?si)name\\s*=\\s*['\"]?(\\w+)"); var value = Utils.RegexFirstGroupValue(form, "(?si)value\\s*=\\s*['\"]?(\\w+)"); if (method.Equals("get")) { formReqInfo.QueryVariables.Add(name, value); } else { formReqInfo.BodyVariables.Add(name, value); } } //finally if (method.Equals("get"))//add to the list of urls { url = formReqInfo.FullUrl; if (!_foundUrls.Contains(url)) { _foundUrls.Add(url); } } else { //add the post request on the spot TVRequestInfo reqInfo = _curDataAccessor.GetRequestInfo( _curDataAccessor.AddRequestResponse(formReqInfo.ToString(), "") ); reqInfo.IsHttps = formUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase); _curDataAccessor.UpdateRequestInfo(reqInfo); } } }
/// <summary> /// Actually gets a matching response from the mock data /// </summary> /// <param name="requestInfo"></param> /// <returns></returns> public HttpResponseInfo SendRequest(HttpRequestInfo requestInfo) { HttpResponseInfo responseInfo = null; string currentRequestString = requestInfo.ToString(); string currentAlertId = Utils.RegexFirstGroupValue(currentRequestString, ALERT_MATCH); TrafficServerMode currentMatchMode = _matchMode; if (!String.IsNullOrEmpty(currentAlertId)) //override the redundancy tuning if we are trying to match a alert { currentMatchMode = TrafficServerMode.BrowserFriendly; } //parse the request variables because we will need them to construct the hash requestInfo.ParseVariables(); TrafficServerResponseSet responseSet = null; //look in the server cache for the request ICacheable entry = TrafficServerCache.Instance.GetEntry(requestInfo.GetHashCode(currentMatchMode)); if (entry != null) { responseSet = entry.Reserve() as TrafficServerResponseSet; entry.Release(); } TrafficServerResponseSet similarRequests = new TrafficServerResponseSet(); if (responseSet == null) { //create a new empty response set responseSet = new TrafficServerResponseSet(); RequestSearcher searcher = new RequestSearcher(); SearchCriteriaSet criteriaSet; criteriaSet = GetCriteriaSet(requestInfo, currentMatchMode); RequestMatches matches = new RequestMatches(); //do the search! searcher.Search(_sourceStore, criteriaSet, matches); //normalize the matches and keep only the ones that have the same variables and values if (matches.Count > 0) { HttpRequestInfo original; HttpRequestInfo found; byte[] requestBytes; int i, n = matches.Count; for (i = 0; i < n & i < MATCHES_LIMIT; i++) { int match = matches[i]; TVRequestInfo header = _sourceStore.GetRequestInfo(match); if (_ignoreAuth) { if ( String.Compare(header.ResponseStatus, "401", true) == 0 || String.Compare(header.ResponseStatus, "407", true) == 0) { HttpServerConsole.Instance.WriteLine(LogMessageType.Warning, "Skipping authentication challenge"); //simply skip 401 matches continue; } } if (String.Compare(header.Description, Resources.TrafficLogProxyDescription, true) == 0) { //is likely that the source store is also the save store and this may be //the current request being saved HttpServerConsole.Instance.WriteLine(LogMessageType.Warning, "Skipping request to traffic store"); continue; } requestBytes = _sourceStore.LoadRequestData(match); string requestString = Constants.DefaultEncoding.GetString(requestBytes); if (String.IsNullOrEmpty(requestString)) { continue; //skip the current match is incorrect } original = new HttpRequestInfo(DynamicElementsRemover.Remove(currentRequestString)); found = new HttpRequestInfo(DynamicElementsRemover.Remove(requestString)); if (RequestMatcher.IsMatch(original, found, TrafficServerMode.Strict)) { responseSet.Add(match); } else if (currentMatchMode != TrafficServerMode.Strict && RequestMatcher.IsMatch(original, found, currentMatchMode)) { similarRequests.Add(match); } } //if no exact requests were found if (responseSet.Count == 0 && similarRequests.Count > 0) { HttpServerConsole.Instance.WriteLine (LogMessageType.Warning, "Warning, exact match was not found for {0} returning a similar request.", requestInfo.RequestLine); } responseSet.AddRange(similarRequests.Matches); } //add this response set to the cache TrafficServerCache.Instance.Add(requestInfo.GetHashCode(currentMatchMode), new CacheEntry(responseSet)); } //get the next response id from the response set int requestId = responseSet.GetNext(); if (requestId == NULL_INDEX) { HttpServerConsole.Instance.WriteLine(LogMessageType.Warning, "(404) Request not found: {0}" , requestInfo.RequestLine); //the request was not found at all, return a 404 return(new HttpResponseInfo(HttpErrorResponse.GenerateHttpErrorResponse(HttpStatusCode.NotFound, "Request Not Found", "<html><head><title>Error code: 404</title><body><h1>Request was not found or variables didn't match.</h1></body></html>"))); } if (requestId != NULL_INDEX) { HttpServerConsole.Instance.WriteLine(LogMessageType.Information, "Returning response from request id: {0}", requestId); byte[] responseBytes = _sourceStore.LoadResponseData(requestId); if (responseBytes != null) { responseInfo = new HttpResponseInfo(responseBytes); if (!String.IsNullOrEmpty(currentAlertId)) { Encoding encoding = HttpUtil.GetEncoding(responseInfo.Headers["Content-Type"]); string responseString = encoding.GetString(responseBytes); responseString = Utils.ReplaceGroups(responseString, ALERT_MATCH, currentAlertId); responseInfo = new HttpResponseInfo(encoding.GetBytes(responseString)); } } //add the request id header responseInfo.Headers.Add("Traffic-Store-Req-Id", requestId.ToString()); } return(responseInfo); }