<commit_before>﻿using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Xml.Linq;
using NuCache.Infrastructure;
using NuCache.Rewriters;

namespace NuCache.PackageSources
{
	public class ProxyingPackageSource : IPackageSource
	{
		private readonly ApplicationSettings _settings;
		private readonly WebClient _client;
		private readonly XmlRewriter _xmlRewriter;
		private readonly UriRewriter _transformer;

		public ProxyingPackageSource(ApplicationSettings settings, WebClient client, XmlRewriter xmlRewriter, UriRewriter transformer)
		{
			_settings = settings;
			_client = client;
			_xmlRewriter = xmlRewriter;
			_transformer = transformer;
		}

		private Uri GetTargetUrl(Uri request)
		{
			return _transformer.TransformHost(_settings.RemoteFeed, request);
		}

		public async Task<HttpResponseMessage> Get(Uri request)
		{
			return await _client.GetResponseAsync(GetTargetUrl(request));
		}

		public async Task<HttpResponseMessage> Metadata(Uri request)
		{
			return await _client.GetResponseAsync(GetTargetUrl(request));
		}

		public async Task<HttpResponseMessage> List(Uri request)
		{
			var response = await _client.GetResponseAsync(GetTargetUrl(request));

			var inputStream = await response.Content.ReadAsStreamAsync();
			var pushContent = new PushStreamContent((outputStream, content, context) =>
			{
				_xmlRewriter.Rewrite(request, inputStream, outputStream);
				outputStream.Close();
				inputStream.Close();
			});

			pushContent.Headers.ContentType = response.Content.Headers.ContentType;
			response.Content = pushContent;

			return response;
		}

		public async Task<HttpResponseMessage> Search(Uri request)
		{
			return await _client.GetResponseAsync(GetTargetUrl(request));
		}

		public async Task<HttpResponseMessage> FindPackagesByID(Uri request)
		{
			return await _client.GetResponseAsync(GetTargetUrl(request));
		}

		public async Task<HttpResponseMessage> GetPackageByID(Uri request)
		{
			var result = await _client.GetResponseAsync(GetTargetUrl(request));

			//not certain why this gets missed by the web client on a download
			var name = Path.GetFileName(result.RequestMessage.RequestUri.AbsolutePath);
			result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = name };

			return result;
		}
	}
}<commit_msg>proxyingPackageSource: re-write all xml requests<commit_after>﻿using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Xml.Linq;
using NuCache.Infrastructure;
using NuCache.Rewriters;

namespace NuCache.PackageSources
{
	public class ProxyingPackageSource : IPackageSource
	{
		private readonly ApplicationSettings _settings;
		private readonly WebClient _client;
		private readonly XmlRewriter _xmlRewriter;
		private readonly UriRewriter _transformer;

		public ProxyingPackageSource(ApplicationSettings settings, WebClient client, XmlRewriter xmlRewriter, UriRewriter transformer)
		{
			_settings = settings;
			_client = client;
			_xmlRewriter = xmlRewriter;
			_transformer = transformer;
		}

		private Uri GetTargetUrl(Uri request)
		{
			return _transformer.TransformHost(_settings.RemoteFeed, request);
		}

		public async Task<HttpResponseMessage> Get(Uri request)
		{
			return await _client.GetResponseAsync(GetTargetUrl(request));
		}

		public async Task<HttpResponseMessage> Metadata(Uri request)
		{
			return await _client.GetResponseAsync(GetTargetUrl(request));
		}

		public async Task<HttpResponseMessage> List(Uri request)
		{
			var response = await _client.GetResponseAsync(GetTargetUrl(request));

			response.Content = await TransformContent(request, response.Content);

			return response;
		}

		public async Task<HttpResponseMessage> Search(Uri request)
		{
			var response = await _client.GetResponseAsync(GetTargetUrl(request));

			if (response.Content.Headers.ContentType.MediaType == "application/atom+xml")
			{
				response.Content = await TransformContent(request, response.Content);
			}

			return response;
		}

		public async Task<HttpResponseMessage> FindPackagesByID(Uri request)
		{
			var response = await _client.GetResponseAsync(GetTargetUrl(request));

			if (response.Content.Headers.ContentType.MediaType == "application/atom+xml")
			{
				response.Content = await TransformContent(request, response.Content);
			}

			return response;
		}

		public async Task<HttpResponseMessage> GetPackageByID(Uri request)
		{
			var result = await _client.GetResponseAsync(GetTargetUrl(request));

			//not certain why this gets missed by the web client on a download
			var name = Path.GetFileName(result.RequestMessage.RequestUri.AbsolutePath);
			result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = name };

			return result;
		}

		private async Task<HttpContent> TransformContent(Uri request, HttpContent inputContent)
		{
			var inputStream = await inputContent.ReadAsStreamAsync();
			var pushContent = new PushStreamContent((outputStream, content, context) =>
			{
				_xmlRewriter.Rewrite(request, inputStream, outputStream);
				outputStream.Close();
				inputStream.Close();
			});

			pushContent.Headers.ContentType = inputContent.Headers.ContentType;

			return pushContent;
		}
	}
}
{'fraction_non_alphanumeric': 0.08540660972892684, 'fraction_numerical': 0.0, 'mean_word_length': 5.0732807215332585, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 13}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2227632', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1483, 'n_words': 351}