<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Web.Compilation;

namespace Aquarius.Helpers
{
    public class UserAgentBuilder
    {
        public static string GetSdkComponent()
        {
            return GetAgentComponent(GetSdkAssemblyPath());
        }

        public static string GetApplicationComponent()
        {
            var path = GetExecutingAssemblyPath();

            if (string.IsNullOrEmpty(path))
            {
                // When all else fails, just use the process info to identify the application
                path = Process.GetCurrentProcess().MainModule.FileName;
            }

            return GetAgentComponent(path);
        }

        private static string GetAgentComponent(string path)
        {
            if (string.IsNullOrEmpty(path))
                return string.Empty;

            return $"{Path.GetFileNameWithoutExtension(path)} {FileVersionInfo.GetVersionInfo(path).FileVersion}";
        }

        private static string GetSdkAssemblyPath()
        {
            return GetTrueAssemblyPath(typeof(SdkServiceClient).Assembly);
        }

        private static string GetExecutingAssemblyPath()
        {
            return GetTrueAssemblyPath(GetEntryAssembly());
        }

        private static Assembly GetEntryAssembly()
        {
            var assembly = Assembly.GetEntryAssembly();

            if (assembly != null)
                return assembly;

            // We will get here if the host process is not a .NET assembly, but some unmanaged code (like an IIS app pool)

            try
            {
                // This will identify an ASP.NET entry point
                assembly = BuildManager.GetGlobalAsaxType().BaseType?.Assembly;
            }
            catch (InvalidOperationException)
            {
                // The expected exception when the process is not an ASP.NET application
            }

            return assembly;
        }

        private static string GetTrueAssemblyPath(Assembly assembly)
        {
            if (assembly == null)
                return string.Empty;

            return new Uri(assembly.CodeBase).AbsolutePath;
        }
    }
}
<commit_msg>PF-28 Bugfix for extracting assembly paths containing spaces<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Web.Compilation;

namespace Aquarius.Helpers
{
    public class UserAgentBuilder
    {
        public static string GetSdkComponent()
        {
            return GetAgentComponent(GetSdkAssemblyPath());
        }

        public static string GetApplicationComponent()
        {
            var path = GetExecutingAssemblyPath();

            if (string.IsNullOrEmpty(path))
            {
                // When all else fails, just use the process info to identify the application
                path = Process.GetCurrentProcess().MainModule.FileName;
            }

            return GetAgentComponent(path);
        }

        private static string GetAgentComponent(string path)
        {
            if (string.IsNullOrEmpty(path))
                return string.Empty;

            return $"{Path.GetFileNameWithoutExtension(path)} {FileVersionInfo.GetVersionInfo(path).FileVersion}";
        }

        private static string GetSdkAssemblyPath()
        {
            return GetTrueAssemblyPath(typeof(SdkServiceClient).Assembly);
        }

        private static string GetExecutingAssemblyPath()
        {
            return GetTrueAssemblyPath(GetEntryAssembly());
        }

        private static Assembly GetEntryAssembly()
        {
            var assembly = Assembly.GetEntryAssembly();

            if (assembly != null)
                return assembly;

            // We will get here if the host process is not a .NET assembly, but some unmanaged code (like an IIS app pool)

            try
            {
                // This will identify an ASP.NET entry point
                assembly = BuildManager.GetGlobalAsaxType().BaseType?.Assembly;
            }
            catch (InvalidOperationException)
            {
                // The expected exception when the process is not an ASP.NET application
            }

            return assembly;
        }

        private static string GetTrueAssemblyPath(Assembly assembly)
        {
            if (assembly == null)
                return string.Empty;

            // Lifted from http://stackoverflow.com/questions/864484/getting-the-path-of-the-current-assembly
            var uri = new Uri(assembly.CodeBase);
            return Path.GetFullPath(Uri.UnescapeDataString(uri.AbsolutePath));
        }
    }
}

{'fraction_non_alphanumeric': 0.07179269328802039, 'fraction_numerical': 0.0016992353440951572, 'mean_word_length': 1.8487598306110102, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5387998', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1068, 'n_words': 299}