<commit_before>using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Xml.Linq;
using FormFactory;
using FormFactory.RazorEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OneOf;
using RazorEngine.Text;
using SirenSharp;
using Action = SirenSharp.Action;
using Exception = System.Exception;

namespace SirenToHtmlClientProxy
{
    public class ForwardProxyMessageHandler : DelegatingHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            var userIp = HttpContext.Current.Request.UserHostAddress;
            request.Headers.Add("X-Forwarded-For", userIp);

            IEnumerable<string> destinations;
            if (!request.Headers.TryGetValues("X-Destination", out destinations))
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest,
                    "Please add an X-Destination header e.g. firstproxy:80, secondproxy:8080 endwarehost:80");
            }

            if (request.Method == HttpMethod.Get || request.Method == HttpMethod.Trace) request.Content = null;

            var first = new Uri(destinations.First());

            Func<string, string> sanitiseUrls = s => s.Replace(first.ToString(), "/");

            var uri = new UriBuilder(first.ToString().TrimEnd('/') + '/' + request.RequestUri.PathAndQuery.TrimStart('/'));

            uri.Host = first.Host;
            uri.Port = first.Port;
            request.Headers.Clear();
            request.Headers.Remove("X-Destination");
            if (destinations.Skip(1).Any())
                request.Headers.Add("X-Destination", destinations.Skip(1));
            request.Headers.Accept.Clear();
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.siren+json"));

            request.Headers.Host = uri.Host;
            request.Headers.AcceptEncoding.Clear();

            var qs = request.RequestUri.ParseQueryString();

            var methodOverride = qs["_method"];
            
            
            if (methodOverride != null)
            {
                request.Method = new HttpMethod(methodOverride);
                qs.Remove("_method");
            }

            uri.Query = qs.ToString();
            request.RequestUri = new Uri(uri.ToString());
            var httpMessageHandler = new HttpClientHandler
            {
                Proxy = new WebProxy("http://localhost:8888".Replace("localhost", Environment.MachineName)),
                UseProxy = true
            };

            using (var httpClient = new HttpClient(httpMessageHandler))
            {
                var responseMessage =
                    await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

                responseMessage.Headers.TransferEncodingChunked = null; //throws an error on calls to WebApi results
                if (responseMessage.StatusCode == HttpStatusCode.NotModified) return responseMessage;
                if (request.Method == HttpMethod.Head) responseMessage.Content = null;
                if (responseMessage.Content != null)
                {
                    try
                    {
                        if (request.Method == HttpMethod.Post)
                        {
                            if (responseMessage.StatusCode == HttpStatusCode.Created
                                || responseMessage.StatusCode == HttpStatusCode.Accepted)
                            {
                                if (responseMessage.Headers.Location != null)
                                {
                                    var redirectMessage = request.CreateResponse(HttpStatusCode.Moved);<commit_msg>Don't use fiddler<commit_after>using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Xml.Linq;
using FormFactory;
using FormFactory.RazorEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OneOf;
using RazorEngine.Text;
using SirenSharp;
using Action = SirenSharp.Action;
using Exception = System.Exception;

namespace SirenToHtmlClientProxy
{
    public class ForwardProxyMessageHandler : DelegatingHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            var userIp = HttpContext.Current.Request.UserHostAddress;
            request.Headers.Add("X-Forwarded-For", userIp);

            IEnumerable<string> destinations;
            if (!request.Headers.TryGetValues("X-Destination", out destinations))
            {
                return request.CreateErrorResponse(HttpStatusCode.BadRequest,
                    "Please add an X-Destination header e.g. firstproxy:80, secondproxy:8080 endwarehost:80");
            }

            if (request.Method == HttpMethod.Get || request.Method == HttpMethod.Trace) request.Content = null;

            var first = new Uri(destinations.First());

            Func<string, string> sanitiseUrls = s => s.Replace(first.ToString(), "/");

            var uri = new UriBuilder(first.ToString().TrimEnd('/') + '/' + request.RequestUri.PathAndQuery.TrimStart('/'));

            uri.Host = first.Host;
            uri.Port = first.Port;
            request.Headers.Clear();
            request.Headers.Remove("X-Destination");
            if (destinations.Skip(1).Any())
                request.Headers.Add("X-Destination", destinations.Skip(1));
            request.Headers.Accept.Clear();
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.siren+json"));

            request.Headers.Host = uri.Host;
            request.Headers.AcceptEncoding.Clear();

            var qs = request.RequestUri.ParseQueryString();

            var methodOverride = qs["_method"];
            
            
            if (methodOverride != null)
            {
                request.Method = new HttpMethod(methodOverride);
                qs.Remove("_method");
            }

            uri.Query = qs.ToString();
            request.RequestUri = new Uri(uri.ToString());
            //var httpMessageHandler = new HttpClientHandler
            //{
            //    Proxy = new WebProxy("http://localhost:8888".Replace("localhost", Environment.MachineName)),
            //    UseProxy = true
            //};

            using (var httpClient = new HttpClient())
            {
                var responseMessage =
                    await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);

                responseMessage.Headers.TransferEncodingChunked = null; //throws an error on calls to WebApi results
                if (responseMessage.StatusCode == HttpStatusCode.NotModified) return responseMessage;
                if (request.Method == HttpMethod.Head) responseMessage.Content = null;
                if (responseMessage.Content != null)
                {
                    try
                    {
                        if (request.Method == HttpMethod.Post)
                        {
                            if (responseMessage.StatusCode == HttpStatusCode.Created
                                || responseMessage.StatusCode == HttpStatusCode.Accepted)
                            {
                                if (responseMessage.Headers.Location != null)
                                {
                                    var redirectMessage = request.CreateResponse(HttpStatusCode.Moved);
{'fraction_non_alphanumeric': 0.09690695934148166, 'fraction_numerical': 0.003492142678972312, 'mean_word_length': 2.013528748590755, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6434454', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1882, 'n_words': 389}