<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace epvpapi.Connection
{
    public class GuestSession
    {
        /// <summary>
        /// Container containing the cookies used in the current session
        /// </summary>
        public CookieContainer Cookies { get; protected set; }

        /// <summary>
        /// Proxy for issuing requests when behind a proxy
        /// </summary>
        public WebProxy Proxy { get; set; }

        /// <summary>
        /// If set to true, the proxy will be used for all requests
        /// </summary>
        public bool UseProxy { get; set; }

        
        public GuestSession(WebProxy proxy):
            this()
        {
            UseProxy = true;
            Proxy = proxy;
        }

        public GuestSession()
        {
            Cookies = new CookieContainer();
        }

        /// <summary> Performs a HTTP GET request </summary>
        /// <param name="url"> Location to request </param>
        /// <param name="headers"> HTTP Headers that are transmitted with the request </param>
        /// <returns> Server <c>Response</c> to the sent request </returns>
        internal Response Get(string url, List<HttpHeader> headers)
        {
            try
            {
                var handler = new HttpClientHandler
                {
                    UseCookies = true,
                    CookieContainer = Cookies,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };

                if (UseProxy)
                {
                    handler.UseProxy = true;
                    handler.Proxy = Proxy;
                }

                var client = new HttpClient(handler);

                foreach (var header in headers)
                    client.DefaultRequestHeaders.Add(header.Name, header.Value);

                var response = client.GetAsync(url);
                if (!response.Result.IsSuccessStatusCode && response.Result.StatusCode != HttpStatusCode.SeeOther)
                    throw new RequestFailedException("Request failed, Server returned " + response.Result.StatusCode);

                return new Response(response.Result);
            }
            catch (CookieException exception)
            {
                throw new RequestFailedException("The Session could not be resolved", exception);
            }
        }

        /// <summary> Performs a HTTP GET request </summary>
        /// <param name="url"> Location to request </param>
        /// <returns> Server <c>Response</c> to the sent request </returns>
        internal Response Get(string url)
        {
            return Get(url, HttpHeader.CommonHeaders);
        }

        /// <summary> Performs a HTTP POST request </summary>
        /// <param name="url"> Location where to post the data </param>
        /// <param name="content"> Contents to post </param>
        /// <param name="headers"> HTTP Headers that are transmitted with the request </param>
        /// <returns> Server <c>Response</c> to the sent request </returns>
        internal Response Post(string url, IEnumerable<KeyValuePair<string, string>> content, List<HttpHeader> headers)
        {
            try
            {
                var targetUrl = new Uri(url);

                var handler = new HttpClientHandler()
                {
                    UseCookies = true,
                    CookieContainer = Cookies,
                    AllowAutoRedirect = true,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };

                if (UseProxy)
                {
                    handler.UseProxy = true;
                    handler.Proxy = Proxy;
                }

                var client = new HttpClient(handler);
                foreach(var header in headers)
                    client.DefaultRequestHeaders.Add(header.Name, header.Value);

                var encodedContent = new FormUrlEncodedContent(content);
                encodedContent.Headers.ContentType.MediaType = "application/x-www-form-urlencoded";
                encodedContent.Headers.ContentType.CharSet = "UTF-8";

                var response = client.PostAsync(targetUrl, encodedContent);
                if (!response.Result.IsSuccessStatusCode && response.Result.StatusCode != HttpStatusCode.SeeOther && response.Result.StatusCode != HttpStatusCode.Redirect)
                    throw new RequestFailedException("Request failed, Server returned " + response.Result.StatusCode);

                return new Response(response.Result);
            }
            catch (CookieException exception)
            {
                throw new RequestFailedException("The Session could not be resolved", exception);
            }
        }

        internal Response Post(string url, IEnumerable<KeyValuePair<string, string>> content)
        {
            return Post(url, content, HttpHeader.DefaultPostHeaders);
        }

        /// <summary>
        /// Performs a Multipart POST request
        /// </summary>
        /// <param name="url"> Location where to post the data </param>
        /// <param name="content"> Contents to post </param>
        /// <returns> Server <c>Response</c> to the sent request  </returns>
        internal Response PostMultipartFormData(Uri url, MultipartFormDataContent content)
        {
            var handler = new HttpClientHandler()
            {
                UseCookies = true,
                CookieContainer = Cookies,
                AllowAutoRedirect = true,
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };

            if (UseProxy)
            {
                handler.UseProxy = true;
                handler.Proxy = Proxy;
            }

            var client = new HttpClient(handler);

            var response = client.PostAsync(url, content);
            if (!response.Result.IsSuccessStatusCode) throw new RequestFailedException("Server returned " + response.Result.StatusCode);
            return new Response(response.Result);
        }
    }
}<commit_msg>fixing bug where some disposables were not disposed correctly, which was causing bugs by requesting huge amount of requests<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace epvpapi.Connection
{
    public class GuestSession
    {
        /// <summary>
        /// Container containing the cookies used in the current session
        /// </summary>
        public CookieContainer Cookies { get; protected set; }

        /// <summary>
        /// Proxy for issuing requests when behind a proxy
        /// </summary>
        public WebProxy Proxy { get; set; }

        /// <summary>
        /// If set to true, the proxy will be used for all requests
        /// </summary>
        public bool UseProxy { get; set; }

        public GuestSession(WebProxy proxy) :
            this()
        {
            UseProxy = true;
            Proxy = proxy;
        }

        public GuestSession()
        {
            Cookies = new CookieContainer();
        }

        /// <summary> Performs a HTTP GET request </summary>
        /// <param name="url"> Location to request </param>
        /// <param name="headers"> HTTP Headers that are transmitted with the request </param>
        /// <returns> Server <c>Response</c> to the sent request </returns>
        internal Response Get(string url, List<HttpHeader> headers)
        {
            try
            {
                using (var handler = new HttpClientHandler()
                {
                    UseCookies = true,
                    CookieContainer = Cookies,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                })
                {
                    if (UseProxy)
                    {
                        handler.UseProxy = true;
                        handler.Proxy = Proxy;
                    }

                    using (var client = new HttpClient(handler))
                    {
                        foreach (var header in headers)
                            client.DefaultRequestHeaders.Add(header.Name, header.Value);

                        var response = client.GetAsync(url);
                        if (!response.Result.IsSuccessStatusCode && response.Result.StatusCode != HttpStatusCode.SeeOther)
                            throw new RequestFailedException("Request failed, Server returned " + response.Result.StatusCode);

                        return new Response(response.Result);
                    }
                }

            }
            catch (CookieException exception)
            {
                throw new RequestFailedException("The Session could not be resolved", exception);
            }
        }

        /// <summary> Performs a HTTP GET request </summary>
        /// <param name="url"> Location to request </param>
        /// <returns> Server <c>Response</c> to the sent request </returns>
        internal Response Get(string url)
        {
            return Get(url, HttpHeader.CommonHeaders);
        }

        /// <summary> Performs a HTTP POST request </summary>
        /// <param name="url"> Location where to post the data </param>
        /// <param name="content"> Contents to post </param>
        /// <param name="headers"> HTTP Headers that are transmitted with the request </param>
        /// <returns> Server <c>Response</c> to the sent request </returns>
        internal Response Post(string url, IEnumerable<KeyValuePair<string, string>> content, List<HttpHeader> headers)
        {
            try
            {
                var targetUrl = new Uri(url);

                using (var handler = new HttpClientHandler()
                {
                    UseCookies = true,
                    CookieContainer = Cookies,
                    AllowAutoRedirect = true,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                })
                {
                    if (UseProxy)
                    {
                        handler.UseProxy = true;
                        handler.Proxy = Proxy;
                    }

                    using (var client = new HttpClient(handler))
                    {
                        foreach (var header in headers)
                            client.DefaultRequestHeaders.Add(header.Name, header.Value);

                        var encodedContent = new FormUrlEncodedContent(content);
                        encodedContent.Headers.ContentType.MediaType = "application/x-www-form-urlencoded";
                        encodedContent.Headers.ContentType.CharSet = "UTF-8";

                        var response = client.PostAsync(targetUrl, encodedContent);
                        if (!response.Result.IsSuccessStatusCode && response.Result.StatusCode != HttpStatusCode.SeeOther && response.Result.StatusCode != HttpStatusCode.Redirect)
                            throw new RequestFailedException("Request failed, Server returned " + response.Result.StatusCode);

                        return new Response(response.Result);
                    }
                }
            }
            catch (CookieException exception)
            {
                throw new RequestFailedException("The Session could not be resolved", exception);
            }
        }

        internal Response Post(string url, IEnumerable<KeyValuePair<string, string>> content)
        {
            return Post(url, content, HttpHeader.DefaultPostHeaders);
        }

        /// <summary>
        /// Performs a Multipart POST request
        /// </summary>
        /// <param name="url"> Location where to post the data </param>
        /// <param name="content"> Contents to post </param>
        /// <returns> Server <c>Response</c> to the sent request  </returns>
        internal Response PostMultipartFormData(Uri url, MultipartFormDataContent content)
        {
            using (var handler = new HttpClientHandler()
            {
                UseCookies = true,
                CookieContainer = Cookies,
                AllowAutoRedirect = true,
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            })
            {
                if (UseProxy)
                {
                    handler.UseProxy = true;
                    handler.Proxy = Proxy;
                }

                using (var client = new HttpClient(handler))
                {
                    var response = client.PostAsync(url, content);
                    if (!response.Result.IsSuccessStatusCode)
                        throw new RequestFailedException("Server returned " + response.Result.StatusCode);

                    return new Response(response.Result);
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08867328519855595, 'fraction_numerical': 0.00015042117930204573, 'mean_word_length': 1.5764386746754504, 'pattern_counts': {'":': 0, '<': 107, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6256343', 'n_tokens_mistral': 3111, 'n_tokens_neox': 2928, 'n_words': 863}