private async Task OnResponseReceivedAsync(HttpResponseMessage response, Guid requestId) { if (response == null) { return; } var content = response.Content; if (content != null && content.Headers.ContentType != null) { var contentType = content.Headers.ContentType; var mediaTypeParts = contentType.MediaType.Split(new[] { '/' }, 2); var mediaType = mediaTypeParts.Length > 0 ? mediaTypeParts[0].ToLowerInvariant() : null; var mediaSubType = mediaTypeParts.Length > 1 ? mediaTypeParts[1].ToLowerInvariant() : null; if (mediaType == "text" || (mediaType == "application" && (mediaSubType == "json" || mediaSubType == "xhtml+xml"))) { var htmlContent = await response.Content.ReadAsStringAsync(); // raise TextResponseReceived event and allow subscribers to modify the response var responseReceived = this.TextResponseReceivedRegistrationTokenTable.InvocationList; if (responseReceived != null) { var eventArgs = new ResponseReceivedEventArgs(response.RequestMessage.RequestUri, contentType.MediaType, htmlContent); Trace.Verbose(requestId, "Raising TextResponseReceived event for URI: {0}", response.RequestMessage.RequestUri); await this.RunThroughDispatcherAsync(() => responseReceived(this, eventArgs)); htmlContent = eventArgs.Content; } if (mediaType == "text" || mediaType == "application") { if (mediaSubType == "html" || mediaSubType == "xhtml+xml") { var processor = new HtmlProcessor(htmlContent, this); processor.RedirectLinks(response.RequestMessage.RequestUri); if (response.Content.Headers.Contains(WebServer.CachedContentKey)) { processor.AddOfflineClass(); } // Inject preload scripts in HTML content foreach (var preloadScript in this.PreloadScripts.OrderByDescending(p => p.Priority)) { processor.InjectHtml(preloadScript.Script); } htmlContent = processor.GetContent(); } else if (mediaSubType == "css") { htmlContent = Regex.Replace( htmlContent, @"(?<=url\((?<quote>['""])?)(?<url>[^'""]+?)(?=(\k<quote>)?\))", (match) => { var rewriteUri = this.BuildCurrentProxyUri(response.RequestMessage.RequestUri, match.Groups["url"].Value); return(rewriteUri.ToString()); }); } } response.Content.Dispose(); response.Content = new StringContent(htmlContent); response.Content.Headers.ContentType = contentType; } } }
private async Task OnResponseReceivedAsync(HttpResponseMessage response, Guid requestId) { if (response == null) { return; } var content = response.Content; if (content != null && content.Headers.ContentType != null) { var contentType = content.Headers.ContentType; var mediaTypeParts = contentType.MediaType.Split(new[] { '/' }, 2); var mediaType = mediaTypeParts.Length > 0 ? mediaTypeParts[0].ToLowerInvariant() : null; var mediaSubType = mediaTypeParts.Length > 1 ? mediaTypeParts[1].ToLowerInvariant() : null; if (mediaType == "text" || (mediaType == "application" && (mediaSubType == "json" || mediaSubType == "xhtml+xml"))) { var htmlContent = await response.Content.ReadAsStringAsync(); // raise TextResponseReceived event and allow subscribers to modify the response var responseReceived = this.TextResponseReceivedRegistrationTokenTable.InvocationList; if (responseReceived != null) { var eventArgs = new ResponseReceivedEventArgs(response.RequestMessage.RequestUri, contentType.MediaType, htmlContent); Trace.Verbose(requestId, "Raising TextResponseReceived event for URI: {0}", response.RequestMessage.RequestUri); await this.RunThroughDispatcherAsync(() => responseReceived(this, eventArgs)); htmlContent = eventArgs.Content; } if (mediaType == "text" || mediaType == "application") { if (mediaSubType == "html" || mediaSubType == "xhtml+xml") { var processor = new HtmlProcessor(htmlContent, this); processor.RedirectLinks(response.RequestMessage.RequestUri); if (response.Content.Headers.Contains(WebServer.CachedContentKey)) { processor.AddOfflineClass(); } // Inject preload scripts in HTML content foreach (var preloadScript in this.PreloadScripts.OrderByDescending(p => p.Priority)) { processor.InjectHtml(preloadScript.Script); } htmlContent = processor.GetContent(); } else if (mediaSubType == "css") { htmlContent = Regex.Replace( htmlContent, @"(?<=url\((?<quote>['""])?)(?<url>[^'""]+?)(?=(\k<quote>)?\))", (match) => { var rewriteUri = this.BuildCurrentProxyUri(response.RequestMessage.RequestUri, match.Groups["url"].Value); return rewriteUri.ToString(); }); } } response.Content.Dispose(); response.Content = new StringContent(htmlContent); response.Content.Headers.ContentType = contentType; } } }