<commit_before>using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Xml;

namespace SpecsFor.Mvc.IIS
{
	internal class IISExpressProcess
	{
		private readonly string _pathToSite;
		private Process _iisProcess;
		private readonly string _applicationHostConfigurationFile;

		/// <summary>
		/// The name of the website to configure for this instance of IIS Express.
		/// </summary>
		private readonly string _webSiteName;

		/// <summary>
		/// Gets the port number in use by this instance of IIS Express.
		/// </summary>
		public int? PortNumber { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [use HTTPS].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [use HTTPS]; otherwise, <c>false</c>.
        /// </value>
        public bool UseHttps { get; set; }

		#region Constructors

		/// <summary>
		/// Creates a new instance.
		/// </summary>
		/// <param name="pathToSite">The full path to the website to host.</param>
		public IISExpressProcess(string pathToSite)
		{
			_pathToSite = pathToSite;
		}

		/// <summary>
		/// Creates a new instance.
		/// </summary>
		/// <param name="pathToSite">The full path to the website to host.</param>
		/// <param name="applicationHostConfigurationFile">The full path to the IIS Express application host configuration file.</param>
		/// <param name="webSiteName">The name of the site in the application host configuration file to configure for this IIS Express process.</param>
		public IISExpressProcess(string pathToSite, string applicationHostConfigurationFile, string webSiteName)
			: this(pathToSite)
		{
			_applicationHostConfigurationFile = applicationHostConfigurationFile;
			_webSiteName = webSiteName;
		}

		#endregion Constructors

		/// <summary>
		/// Starts an IIS Express process that will host the web application.
		/// </summary>
		/// <remarks>
		/// This method finds an open port to configure the site to use and if an application host configuration file
		/// is being used it also configures the site specified when this instance was created with the correct port
		/// and path to the web application.
		/// </remarks>
		public void Start()
		{
			var startInfo = new ProcessStartInfo
								{
									ErrorDialog = false,
									CreateNoWindow = true,
									UseShellExecute = false
								};

			if (PortNumber == null) CaptureAvailablePortNumber();

			// If a configuration file was not provided use the simple IIS Express command line configuration.
			if (string.IsNullOrEmpty(_applicationHostConfigurationFile))
			{
                var xDoc = new XmlDocument();
                string iisConfigPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\iisexpress\config\applicationhost.config";

				// Read the application host configuration file into an XML document.
				using (var hostConfigReader = new StreamReader(iisConfigPath))
				{
					xDoc.Load(hostConfigReader);
				}

                XmlNode sites = xDoc.SelectSingleNode("/configuration/system.applicationHost/sites");
                sites.RemoveAll();

                XmlElement site = xDoc.CreateElement("site");
                site.SetAttribute("name", _webSiteName);
                site.SetAttribute("id", "1");

                XmlElement application = xDoc.CreateElement("application");
                application.SetAttribute("path", "/");
                application.SetAttribute("applicationPool", "Clr4IntegratedAppPool");

                XmlElement virtualDirectory = xDoc.CreateElement("virtualDirectory");
                virtualDirectory.SetAttribute("physicalPath", _pathToSite);<commit_msg>throwing exception when using https but not specifying a port<commit_after>using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Xml;

namespace SpecsFor.Mvc.IIS
{
	internal class IISExpressProcess
	{
		private readonly string _pathToSite;
		private Process _iisProcess;
		private readonly string _applicationHostConfigurationFile;

		/// <summary>
		/// The name of the website to configure for this instance of IIS Express.
		/// </summary>
		private readonly string _webSiteName;

		/// <summary>
		/// Gets the port number in use by this instance of IIS Express.
		/// </summary>
		public int? PortNumber { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [use HTTPS].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [use HTTPS]; otherwise, <c>false</c>.
        /// </value>
        public bool UseHttps { get; set; }

		#region Constructors

		/// <summary>
		/// Creates a new instance.
		/// </summary>
		/// <param name="pathToSite">The full path to the website to host.</param>
		public IISExpressProcess(string pathToSite)
		{
			_pathToSite = pathToSite;
		}

		/// <summary>
		/// Creates a new instance.
		/// </summary>
		/// <param name="pathToSite">The full path to the website to host.</param>
		/// <param name="applicationHostConfigurationFile">The full path to the IIS Express application host configuration file.</param>
		/// <param name="webSiteName">The name of the site in the application host configuration file to configure for this IIS Express process.</param>
		public IISExpressProcess(string pathToSite, string applicationHostConfigurationFile, string webSiteName)
			: this(pathToSite)
		{
			_applicationHostConfigurationFile = applicationHostConfigurationFile;
			_webSiteName = webSiteName;
		}

		#endregion Constructors

		/// <summary>
		/// Starts an IIS Express process that will host the web application.
		/// </summary>
		/// <remarks>
		/// This method finds an open port to configure the site to use and if an application host configuration file
		/// is being used it also configures the site specified when this instance was created with the correct port
		/// and path to the web application.
		/// </remarks>
		public void Start()
		{
			var startInfo = new ProcessStartInfo
								{
									ErrorDialog = false,
									CreateNoWindow = true,
									UseShellExecute = false
								};

            if (PortNumber == null)
            {
                if (UseHttps)
                {
                    throw new ArgumentException("In order to use https you must specify a port that has already been configured for https.");
                }

                CaptureAvailablePortNumber();
            }
            
			// If a configuration file was not provided use the simple IIS Express command line configuration.
			if (string.IsNullOrEmpty(_applicationHostConfigurationFile))
			{
                var xDoc = new XmlDocument();
                string iisConfigPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\iisexpress\config\applicationhost.config";

				// Read the application host configuration file into an XML document.
				using (var hostConfigReader = new StreamReader(iisConfigPath))
				{
					xDoc.Load(hostConfigReader);
				}

                XmlNode sites = xDoc.SelectSingleNode("/configuration/system.applicationHost/sites");
                sites.RemoveAll();

                XmlElement site = xDoc.CreateElement("site");
                site.SetAttribute("name", _webSiteName);
                site.SetAttribute("id", "1");

                XmlElement application = xDoc.CreateElement("application");
                application.SetAttribute("path", "/");
                application.SetAttribute("applicationPool", "Clr4IntegratedAppPool");

                XmlElement virtualDirectory = xDoc.CreateElement("virtualDirectory");
                virtualDirectory.SetAttribute("physicalPath", _pathToSite);
{'fraction_non_alphanumeric': 0.1001031991744066, 'fraction_numerical': 0.0005159958720330237, 'mean_word_length': 2.6954242135367017, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 59, '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': '4024947', 'n_tokens_mistral': 2243, 'n_tokens_neox': 1939, 'n_words': 649}