<commit_before>﻿using System;
using System.Threading;
using System.Net;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

using Oxide.Core.Plugins;

namespace Oxide.Core.Libraries
{
    /// <summary>
    /// The WebRequests library
    /// </summary>
    public class WebRequests : Library
    {
        /// <summary>
        /// Specifies the HTTP request timeout in seconds
        /// </summary>
        public static float Timeout = 30f;
        
        /// <summary>
        /// Represents a single WebRequest instance
        /// </summary>
        public class WebRequest
        {
            /// <summary>
            /// Gets the callback delegate
            /// </summary>
            public Action<int, string> Callback { get; private set; }

            /// <summary>
            /// Overrides the default request timeout
            /// </summary>
            public float Timeout { get; set; }

            /// <summary>
            /// Gets the web request method
            /// </summary>
            public string Method { get; set; }

            /// <summary>
            /// Gets the destination URL
            /// </summary>
            public string URL { get; private set; }
            
            /// <summary>
            /// Gets or sets the request body
            /// </summary>
            public string Body { get; set; }

            /// <summary>
            /// Gets the response code
            /// </summary>
            public int ResponseCode { get; protected set; }

            /// <summary>
            /// Gets the response text
            /// </summary>
            public string ResponseText { get; protected set; }

            /// <summary>
            /// Gets the plugin to which this web request belongs, if any
            /// </summary>
            public Plugin Owner { get; private set; }

            /// <summary>
            /// Gets the web request headers
            /// </summary>
            public Dictionary<string, string> RequestHeaders { get; set; }
            
            private HttpWebRequest request = null;

            /// <summary>
            /// Initializes a new instance of the WebRequest class
            /// </summary>
            /// <param name="url"></param>
            /// <param name="callback"></param>
            /// <param name="owner"></param>
            public WebRequest(string url, Action<int, string> callback, Plugin owner)
            {
                URL = url;
                Callback = callback;
                Owner = owner;
                if (owner != null) owner.OnRemovedFromManager += owner_OnRemovedFromManager;
            }
            
            /// <summary>
            /// Used by the worker thread to start the request
            /// </summary>
            public void Start()
            {   
                try
                {
                    // Create the request
                    request = (HttpWebRequest)System.Net.WebRequest.Create(URL);
                    request.Method = Method;
                    request.Credentials = CredentialCache.DefaultCredentials;
                    request.Proxy = null;
                    request.KeepAlive = false;
                    request.Timeout = (int)Math.Round((Timeout == 0f ? WebRequests.Timeout : Timeout) * 1000f);
                    request.ServicePoint.MaxIdleTime = request.Timeout;

                    if (RequestHeaders != null) request.SetRawHeaders(RequestHeaders);

                    // Optional request body for post requests
                    var data = new byte[0];
                    if (Body != null)
                    {
                        data = Encoding.UTF8.GetBytes(Body);
                        request.ContentLength = data.Length;
                        request.ContentType = "application/x-www-form-urlencoded";
                    }

                    // Perform DNS lookup and connect (blocking)
                    if (data.Length > 0)
                    {
                        request.BeginGetRequestStream(result =>
                        {
                            // Write request body
                            using (var stream = request.EndGetRequestStream(result))
                                stream.Write(data, 0, data.Length);
                            WaitForResponse();
                        }, null);
                    }
                    else
                    {
                        WaitForResponse();
                    }
                }
                catch (Exception ex)
                {
                    ResponseText = ex.Message;
                    Interface.Oxide.LogException(string.Format("Web request produced exception (Url: {0})", URL), ex);
                    if (request != null) request.Abort();
                }
            }

            private void WaitForResponse()
            {
                request.BeginGetResponse(result => {
                    try
                    {
                        using (var response = (HttpWebResponse)request.EndGetResponse(result))
                        {
                            using (var stream = response.GetResponseStream())
                                using (var reader = new StreamReader(stream))
                                    ResponseText = reader.ReadToEnd();
                            ResponseCode = (int)response.StatusCode;
                        }
                    }
                    catch (WebException ex)
                    {
                        ResponseText = ex.Message;
                        var response = ex.Response as HttpWebResponse;
                        if (response != null) ResponseCode = (int)response.StatusCode;
                    }
                    catch (Exception ex)
                    {
                        ResponseText = ex.Message;
                        Interface.Oxide.LogException(string.Format("Web request produced exception (Url: {0})", URL), ex);
                    }
                    OnComplete();
                }, null);
            }

            private void OnComplete()
            {<commit_msg>Added safety abort after web requests<commit_after>﻿using System;
using System.Threading;
using System.Net;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

using Oxide.Core.Plugins;

namespace Oxide.Core.Libraries
{
    /// <summary>
    /// The WebRequests library
    /// </summary>
    public class WebRequests : Library
    {
        /// <summary>
        /// Specifies the HTTP request timeout in seconds
        /// </summary>
        public static float Timeout = 30f;
        
        /// <summary>
        /// Represents a single WebRequest instance
        /// </summary>
        public class WebRequest
        {
            /// <summary>
            /// Gets the callback delegate
            /// </summary>
            public Action<int, string> Callback { get; private set; }

            /// <summary>
            /// Overrides the default request timeout
            /// </summary>
            public float Timeout { get; set; }

            /// <summary>
            /// Gets the web request method
            /// </summary>
            public string Method { get; set; }

            /// <summary>
            /// Gets the destination URL
            /// </summary>
            public string URL { get; private set; }
            
            /// <summary>
            /// Gets or sets the request body
            /// </summary>
            public string Body { get; set; }

            /// <summary>
            /// Gets the response code
            /// </summary>
            public int ResponseCode { get; protected set; }

            /// <summary>
            /// Gets the response text
            /// </summary>
            public string ResponseText { get; protected set; }

            /// <summary>
            /// Gets the plugin to which this web request belongs, if any
            /// </summary>
            public Plugin Owner { get; private set; }

            /// <summary>
            /// Gets the web request headers
            /// </summary>
            public Dictionary<string, string> RequestHeaders { get; set; }
            
            private HttpWebRequest request = null;

            /// <summary>
            /// Initializes a new instance of the WebRequest class
            /// </summary>
            /// <param name="url"></param>
            /// <param name="callback"></param>
            /// <param name="owner"></param>
            public WebRequest(string url, Action<int, string> callback, Plugin owner)
            {
                URL = url;
                Callback = callback;
                Owner = owner;
                if (owner != null) owner.OnRemovedFromManager += owner_OnRemovedFromManager;
            }
            
            /// <summary>
            /// Used by the worker thread to start the request
            /// </summary>
            public void Start()
            {   
                try
                {
                    // Create the request
                    request = (HttpWebRequest)System.Net.WebRequest.Create(URL);
                    request.Method = Method;
                    request.Credentials = CredentialCache.DefaultCredentials;
                    request.Proxy = null;
                    request.KeepAlive = false;
                    request.Timeout = (int)Math.Round((Timeout == 0f ? WebRequests.Timeout : Timeout) * 1000f);
                    request.ServicePoint.MaxIdleTime = request.Timeout;

                    if (RequestHeaders != null) request.SetRawHeaders(RequestHeaders);

                    // Optional request body for post requests
                    var data = new byte[0];
                    if (Body != null)
                    {
                        data = Encoding.UTF8.GetBytes(Body);
                        request.ContentLength = data.Length;
                        request.ContentType = "application/x-www-form-urlencoded";
                    }

                    // Perform DNS lookup and connect (blocking)
                    if (data.Length > 0)
                    {
                        request.BeginGetRequestStream(result =>
                        {
                            // Write request body
                            using (var stream = request.EndGetRequestStream(result))
                                stream.Write(data, 0, data.Length);
                            WaitForResponse();
                        }, null);
                    }
                    else
                    {
                        WaitForResponse();
                    }
                }
                catch (Exception ex)
                {
                    ResponseText = ex.Message;
                    Interface.Oxide.LogException(string.Format("Web request produced exception (Url: {0})", URL), ex);
                    if (request != null) request.Abort();
                }
            }

            private void WaitForResponse()
            {
                request.BeginGetResponse(result => {
                    try
                    {
                        using (var response = (HttpWebResponse)request.EndGetResponse(result))
                        {
                            using (var stream = response.GetResponseStream())
                                using (var reader = new StreamReader(stream))
                                    ResponseText = reader.ReadToEnd();
                            ResponseCode = (int)response.StatusCode;
                        }
                    }
                    catch (WebException ex)
                    {
                        ResponseText = ex.Message;
                        var response = ex.Response as HttpWebResponse;
                        if (response != null) ResponseCode = (int)response.StatusCode;
                    }
                    catch (Exception ex)
                    {
                        ResponseText = ex.Message;
                        Interface.Oxide.LogException(string.Format("Web request produced exception (Url: {0})", URL), ex);
                    }
                    if (request != null) request.Abort();
                    OnComplete();
                }, null);
            }

            private void OnComplete()
            {
{'fraction_non_alphanumeric': 0.09446672012830794, 'fraction_numerical': 0.0020850040096230954, 'mean_word_length': 1.0444262295081967, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '5610283', 'n_tokens_mistral': 2774, 'n_tokens_neox': 2594, 'n_words': 735}