<commit_before>﻿using System;
using System.Text.RegularExpressions;
using Titanium.Web.Proxy.EventArguments;

namespace Titanium.Web.Proxy.Test
{
    public class ProxyTestController
    {
        public int ListeningPort { get; set; }
        public bool EnableSsl { get; set; }
        public bool SetAsSystemProxy { get; set; }

        public void StartProxy()
        {
            ProxyServer.BeforeRequest += OnRequest;
            ProxyServer.BeforeResponse += OnResponse;

            ProxyServer.EnableSsl = EnableSsl;

            ProxyServer.SetAsSystemProxy = SetAsSystemProxy;

            //Exclude Https addresses you don't want to proxy
            //Usefull for clients that use certificate pinning
            //for example dropbox.com
            ProxyServer.ExcludedHttpsHostNameRegex.Add(".dropbox.com");

            ProxyServer.Start();

            ProxyServer.ListeningPort = ProxyServer.ListeningPort;

            Console.WriteLine("Proxy listening on local machine port: {0} ", ProxyServer.ListeningPort);
        }

        public void Stop()
        {
            ProxyServer.BeforeRequest -= OnRequest;
            ProxyServer.BeforeResponse -= OnResponse;

            ProxyServer.Stop();
        }


        //Test On Request, intecept requests
        //Read browser URL send back to proxy by the injection script in OnResponse event
        public void OnRequest(object sender, SessionEventArgs e)
        {
            Console.WriteLine(e.ProxySession.Request.RequestUrl);

            //read request headers
            var requestHeaders = e.ProxySession.Request.RequestHeaders;

            if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
            {
                //Get/Set request body bytes
                byte[] bodyBytes = e.GetRequestBody();
                e.SetRequestBody(bodyBytes);

                //Get/Set request body as string
                string bodyString = e.GetRequestBodyAsString();
                e.SetRequestBodyString(bodyString);

            }

            //To cancel a request with a custom HTML content
            //Filter URL

            if (e.ProxySession.Request.RequestUrl.Contains("google.com"))
            {
                e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
            }
        }

        //Test script injection
        //Insert script to read the Browser URL and send it back to proxy
        public void OnResponse(object sender, SessionEventArgs e)
        {
            ////read response headers
            var responseHeaders = e.ProxySession.Response.ResponseHeaders;


            if (e.ResponseStatusCode == "200")
            {
                if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
                {
                    //Get/Set response body bytes
                    byte[] responseBodyBytes = e.GetResponseBody();
                    e.SetResponseBody(responseBodyBytes);

                    //Get response body as string
                    string responseBody = e.GetResponseBodyAsString();

                    //Modify e.ServerResponse
                    Regex rex = new Regex("</body>", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    string modified = rex.Replace(responseBody, "<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>", 1);

                    //Set modifed response Html Body
                    e.SetResponseBodyString(modified);
                }
            }
        }
    }
}<commit_msg>comment out test<commit_after>﻿using System;
using System.Text.RegularExpressions;
using Titanium.Web.Proxy.EventArguments;

namespace Titanium.Web.Proxy.Test
{
    public class ProxyTestController
    {
        public int ListeningPort { get; set; }
        public bool EnableSsl { get; set; }
        public bool SetAsSystemProxy { get; set; }

        public void StartProxy()
        {
            ProxyServer.BeforeRequest += OnRequest;
            ProxyServer.BeforeResponse += OnResponse;

            ProxyServer.EnableSsl = EnableSsl;

            ProxyServer.SetAsSystemProxy = SetAsSystemProxy;

            //Exclude Https addresses you don't want to proxy
            //Usefull for clients that use certificate pinning
            //for example dropbox.com
            ProxyServer.ExcludedHttpsHostNameRegex.Add(".dropbox.com");

            ProxyServer.Start();

            ProxyServer.ListeningPort = ProxyServer.ListeningPort;

            Console.WriteLine("Proxy listening on local machine port: {0} ", ProxyServer.ListeningPort);
        }

        public void Stop()
        {
            ProxyServer.BeforeRequest -= OnRequest;
            ProxyServer.BeforeResponse -= OnResponse;

            ProxyServer.Stop();
        }


        //Test On Request, intecept requests
        //Read browser URL send back to proxy by the injection script in OnResponse event
        public void OnRequest(object sender, SessionEventArgs e)
        {
            Console.WriteLine(e.ProxySession.Request.RequestUrl);

            ////read request headers
            //var requestHeaders = e.ProxySession.Request.RequestHeaders;

            //if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
            //{
            //    //Get/Set request body bytes
            //    byte[] bodyBytes = e.GetRequestBody();
            //    e.SetRequestBody(bodyBytes);

            //    //Get/Set request body as string
            //    string bodyString = e.GetRequestBodyAsString();
            //    e.SetRequestBodyString(bodyString);

            //}

            ////To cancel a request with a custom HTML content
            ////Filter URL

            //if (e.ProxySession.Request.RequestUrl.Contains("google.com"))
            //{
            //    e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
            //}
        }

        //Test script injection
        //Insert script to read the Browser URL and send it back to proxy
        public void OnResponse(object sender, SessionEventArgs e)
        {
            ////read response headers
            var responseHeaders = e.ProxySession.Response.ResponseHeaders;


            //if (e.ResponseStatusCode == "200")
            //{
            //    if (e.ResponseContentType.Trim().ToLower().Contains("text/html"))
            //    {
            //        //Get/Set response body bytes
            //        byte[] responseBodyBytes = e.GetResponseBody();
            //        e.SetResponseBody(responseBodyBytes);

            //        //Get response body as string
            //        string responseBody = e.GetResponseBodyAsString();

            //        //Modify e.ServerResponse
            //        Regex rex = new Regex("</body>", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline);
            //        string modified = rex.Replace(responseBody, "<script type =\"text/javascript\">alert('Response was modified by this script!');</script></body>", 1);

            //        //Set modifed response Html Body
            //        e.SetResponseBodyString(modified);
            //    }
            //}
        }
    }
}
{'fraction_non_alphanumeric': 0.10112511861190186, 'fraction_numerical': 0.0018977904297139759, 'mean_word_length': 1.772641863960917, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3129412', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1769, 'n_words': 452}