/// <summary> /// Adds the request to the proxy data store /// </summary> protected void AddRequestToDataStore() { TVRequestInfo currDataStoreRequestInfo = new TVRequestInfo(); currDataStoreRequestInfo.RequestLine = _requestInfo.RequestLine; currDataStoreRequestInfo.Description = _requestDescription; currDataStoreRequestInfo.RequestTime = _currentRequestTime; currDataStoreRequestInfo.ThreadId = Utils.GetCurrentWin32ThreadId().ToString(); currDataStoreRequestInfo.IsHttps = _requestInfo.IsSecure; currDataStoreRequestInfo.Host = _requestInfo.Host; int reqId = TrafficDataStore.AddRequestInfo(currDataStoreRequestInfo); if (reqId != -1) { _currentRequestResponseBytes = new RequestResponseBytes(); _currentRequestResponseBytes.AddToRequest(_requestInfo.ToArray(false)); //saving the request in a direct to site format, since it will come //in proxy format GET http://site.com/ rather than GET /, //if we need a server to connect to the target the client will handle that TrafficDataStore.SaveRequest(reqId, _currentRequestResponseBytes); _currDataStoreRequestInfo = TrafficDataStore.GetRequestInfo(reqId); } }
protected override HttpRequestInfo OnBeforeRequestToSite(HttpRequestInfo requestInfo) { requestInfo = base.OnBeforeRequestToSite(requestInfo); if (!_isNonEssential) { bool mutated; requestInfo = _parentProxy.HandleRequest(requestInfo, out mutated); if (mutated) { CurrDataStoreRequestInfo.Description = "Custom Test"; } TrafficDataStore.SaveRequest(CurrDataStoreRequestInfo.Id, requestInfo.ToArray(false)); TrafficDataStore.UpdateRequestInfo(CurrDataStoreRequestInfo); } return(requestInfo); }
private void TrackRequestContext(HttpRequestInfo requestInfo) { foreach (TrackingPattern pattern in _autoTrackingPatternList.Values) { string rawRequest = requestInfo.ToString(); string needle = Utils.RegexFirstGroupValue(rawRequest, pattern.RequestPattern); if (String.IsNullOrWhiteSpace(needle)) { continue; } //first search for the path of the current request in responses LineMatches results = SearchParameterValue(needle); if (results.Count == 0) { needle = Utils.UrlDecode(needle); results = SearchParameterValue(needle); } //if any of the two searches returned results if (results.Count != 0) { //get the last match to extract the request context var match = results[results.Count - 1]; CurrDataStoreRequestInfo.RefererId = match.RequestId; //replace the path in the match string requestContext = match.Line.Replace(needle, REQ_CONTEXT_ID); if (requestContext.Length > MAX_REQUEST_CONTEXT_SIZE) { requestContext = TrimRequestContext(requestContext); } //also replace hexadecimal values requestContext = Regex.Replace(requestContext, HEX_REGEX, HEX_VAL); //escape the line requestContext = Regex.Escape(requestContext); //insert the group requestContext = requestContext.Replace(REQ_CONTEXT_ID, RX_GROUP); //insert the HEX regex requestContext = requestContext.Replace(HEX_VAL, HEX_REGEX); CurrDataStoreRequestInfo.RequestContext = requestContext; CurrDataStoreRequestInfo.TrackingPattern = pattern.Name; TrafficDataStore.UpdateRequestInfo(CurrDataStoreRequestInfo); string originalPath = requestInfo.Path; CurrDataStoreRequestInfo.UpdatedPath = originalPath; //change the path of the request HttpRequestInfo newReq = new HttpRequestInfo(requestInfo.ToArray(false), false); //we are only replacing the last portion of the path and the query string to prevent relative path issues and also cookie path issues int lastIndexOfSlash = originalPath.LastIndexOf('/'); if (lastIndexOfSlash >= 0) { originalPath = originalPath.Substring(0, lastIndexOfSlash + 1); } newReq.Path = String.Format("{0}{1}{2}", originalPath, REQ_ID_STRING, CurrDataStoreRequestInfo.Id); TrafficDataStore.SaveRequest(CurrDataStoreRequestInfo.Id, newReq.ToArray(false)); HttpServerConsole.Instance.WriteLine ("Found request context for request '{0}' id: {1}, referer id:{2}", requestInfo.Path, CurrDataStoreRequestInfo.Id, CurrDataStoreRequestInfo.RefererId); HttpServerConsole.Instance.WriteLine (requestContext); return; //we can only have one tracking pattern per request } } }
/// <summary> /// Triggered before the request is sent to the site /// </summary> /// <param name="requestInfo"></param> /// <returns></returns> protected override HttpRequestInfo OnBeforeRequestToSite(HttpRequestInfo requestInfo) { bool wasModified = false; if (_requestReplacements != null) { string request = requestInfo.ToString(); foreach (string key in _requestReplacements.Keys) { if (Utils.IsMatch(request, key)) { request = Utils.ReplaceGroups(request, key, _requestReplacements[key]); wasModified = true; HttpServerConsole.Instance.WriteLine(LogMessageType.Information, "Request replacement applied for pattern: '{0}'", key); } } if (wasModified) { requestInfo = new HttpRequestInfo(request, false); //update the content length if (requestInfo.ContentData != null) { requestInfo.Headers["Content-Length"] = requestInfo.ContentData.Length.ToString(); } requestInfo.IsSecure = ClientStreamWrapper.IsSecure; } } if (!_isNonEssential) { var originalRawRequest = requestInfo.ToString(); //update tracked values string updatedRequest = PatternTracker.Instance.UpdateRequest(originalRawRequest); updatedRequest = UpdateDynamicPatterns(updatedRequest); if (!originalRawRequest.Equals(updatedRequest)) { bool isSecure = requestInfo.IsSecure; requestInfo = new HttpRequestInfo(updatedRequest, false); requestInfo.IsSecure = isSecure; wasModified = true; } if (wasModified) { //update the request in the file TrafficDataStore.SaveRequest(CurrDataStoreRequestInfo.Id, Constants.DefaultEncoding.GetBytes(updatedRequest)); } } if (CurrDataStoreRequestInfo != null) { //see if the request was trapped if (HttpTrap.Instance.TrapRequests && HttpTrap.Instance.TrapRequest(CurrDataStoreRequestInfo, requestInfo)) { //then we need to update the request info from the data source requestInfo = new HttpRequestInfo(TrafficDataStore.LoadRequestData(CurrDataStoreRequestInfo.Id), false); //update the content length if (requestInfo.ContentData != null) { requestInfo.Headers["Content-Length"] = requestInfo.ContentData.Length.ToString(); } requestInfo.IsSecure = ClientStreamWrapper.IsSecure; } } return(requestInfo); }