public FileConfigurationExtended GetExtended() { try { return(_innerRepository.GetExtended()); } catch (Exception ex) { //Try look into backups var snapShots = Directory.GetFiles(_snapShotsPath, "*.json"); if (snapShots.Any()) { var contents = File.ReadAllText(snapShots.Last()); return(JsonConvert.DeserializeObject <FileConfigurationExtended>(contents)); } else { throw ex; } } }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { try { var configuration = _config.GetExtended(); var routeKey = httpContext.HttpContext.Items["RouteKey"]; if (routeKey == null) { return(await base.SendAsync(request, cancellationToken)); } var currentConf = configuration.RouteExtensions.ReplaceHandler.Rules .Where(p => p.AppliesTo != null && p.AppliesTo.Contains(routeKey)) .FirstOrDefault(); if (currentConf != null) { //Downstream headers var headers = currentConf.ReplaceDownstreamHeaders .GroupBy(p => p.Header).ToDictionary(p => p.Key, p => p); foreach (var header in headers) { if (request.Headers.Contains(header.Key)) { var values = request.Headers.GetValues(header.Key); var newValues = new List <string>(); foreach (var val in values) { var value = new StringBuilder(val); foreach (var toReplace in header.Value) { value.Replace(toReplace.Find, toReplace.Replace); } newValues.Add(value.ToString()); } request.Headers.Remove(header.Key); request.Headers.Add(header.Key, newValues); } } //Disable compression request.Headers.Remove("Accept-Encoding"); //Downstream content if (request.Content != null && IsSupportedMediaType(request.Content?.Headers?.ContentType?.MediaType)) { var content = new StringBuilder(await request.Content.ReadAsStringAsync()); foreach (var toReplace in currentConf.ReplaceDownstreamContent) { content.Replace(toReplace.Find, toReplace.Replace); } request.Content = new StringContent(content.ToString(), Encoding.UTF8, request.Content.Headers.ContentType.MediaType); } //Go with request var response = await base.SendAsync(request, cancellationToken); if (response.IsSuccessStatusCode && IsSupportedMediaType(response.Content?.Headers?.ContentType?.MediaType)) { //Upstream content var content = await response.Content.ReadAsStringAsync(); var result = new StringBuilder(content); foreach (var toReplace in currentConf.ReplaceUpstreamContent) { result.Replace(toReplace.Find, toReplace.Replace); } response.Content = new StringContent(result.ToString(), Encoding.UTF8, response.Content.Headers.ContentType.MediaType); } return(response); } else { return(await base.SendAsync(request, cancellationToken)); } } catch (Exception ex) { _logger.LogError(ex, ex.Message); throw; } }