public void HandleResponse(Session session) { var key = $"{session.RequestMethod}_{session.hostname}_{session.url}"; _logger.Verbose("{ThisMethod} for {Method} {Url} {StatusCode}", nameof(HandleResponse), session.RequestMethod, session.url, session.responseCode); if (IsResponseSuccessful(session)) { _logger.Verbose("Response was successful"); if (!_responseCache.IsCached(key)) { _logger.Info("Response was not previously cached, adding"); var responseBodyBytes = new byte[session.responseBodyBytes.Length]; session.responseBodyBytes.CopyTo(responseBodyBytes, 0); _responseCache.Add(key, new ResponseCacheItem { ResponseBodyBytes = responseBodyBytes, ResponseCode = session.responseCode }); } else { _logger.Verbose("Response was previously cached"); } } else { _logger.Info("Response was not successful"); if (_responseCache.IsCached(key)) { _logger.Info("Response was previously cached"); var cachedSession = _responseCache.Get(key); session.responseCode = cachedSession.ResponseCode; var newResponseBodyBytes = new byte[cachedSession.ResponseBodyBytes.Length]; cachedSession.ResponseBodyBytes.CopyTo(newResponseBodyBytes, 0); session.responseBodyBytes = newResponseBodyBytes; _logger.Info("Replaced failed response with cached response"); } else { _logger.Info("Response was not previously cached"); } } }
private void OnBeforeResponse(Session session, IListenerFilter filter, ITrafficHandler handler) { _logger.Verbose("{ThisMethod} for {Method} {Url}", nameof(OnBeforeResponse), session.RequestMethod, session.url); if (filter.Verbs.Any() && !filter.Verbs.Any(verbFilter => session.RequestMethod.Equals(verbFilter))) { return; } if (filter.ApplyFilters) { if (ShouldFilterHostnameAndRoute(filter)) { if (MatchesHostnameAndRouteFilter(session, filter)) { handler.HandleResponse(session); } } else if (ShouldFilterOnlyOnHostname(filter)) { if (ResponseHostnameMatchesAnyFilterHostname(session, filter)) { handler.HandleResponse(session); } } else if (ShouldFilterOnlyOnRoute(filter)) { if (ResponseRouteMatchesAnyFilterRoute(session, filter)) { handler.HandleResponse(session); } } } else { handler.HandleResponse(session); } }