Ejemplo n.º 1
0
        //protected override void OnShown(EventArgs e)
        //{
        //    if (_automated)
        //    {
        //        WindowState = FormWindowState.Minimized;
        //        Hide();
        //    }
        //}

        #endregion

        #region Private Methods


        private void InitializeUI()
        {
            ButtonStart.Text           = SR.GetString(SR.WebdevStart);
            toolStripStatusLabel1.Text = SR.GetString(SR.WebdevAspNetVersion, CommonExtensions.GetAspVersion());


            // if sqlite is missing then just silently enable in-memory logging,
            // hide the enable logging item and enable view log item



            ShowLogButton.Enabled = false;



            toolStripStatusLabel1.Text = SR.GetString(SR.WebdevAspNetVersion, CommonExtensions.GetAspVersion());

            List <IPAddress> localAddresses = new List <IPAddress>(CassiniNetworkUtils.GetLocalAddresses());

            localAddresses.Insert(0, System.Net.IPAddress.IPv6Loopback);
            localAddresses.Insert(0, System.Net.IPAddress.Loopback);

            IPSpecificTextBox.Items.AddRange(localAddresses.Select(i => i.ToString()).ToArray());
            if (IPSpecificTextBox.Items.Count > 0)
            {
                IPSpecificTextBox.SelectedIndex = 0;
                IPSpecificTextBox.Text          = IPSpecificTextBox.Items[0].ToString();
            }

            InvokeSetRunState(RunState.Idle);
            if (_server == null)
            {
                ShowMainForm();
            }
            else
            {
                _automated        = true;
                _server.TimedOut += OnTimedOut;
                IPMode            = IPMode.Specific;
                PortMode          = PortMode.Specific;

                UpdateUIFromServer();


                InvokeSetRunState(RunState.Running);
                base.Text        = SR.GetString(SR.WebdevNameWithPort, _server.Port);
                TrayIcon.Visible = true;
                DisplayTrayTip();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Will start specified application on loopback
        /// </summary>
        /// <param name="applicationPath">Physical path to application.</param>
        /// <param name="port">Port to listen on.</param>
        /// <param name="virtualPath">Optional. defaults to "/"</param>
        /// <param name="hostName">Optional. Is used to construct RootUrl. Defaults to "localhost"</param>
        public void StartServer(string applicationPath, int port, string virtualPath, string hostName)
        {
            // WebHost.Server will not run on any other IP
            IPAddress ipAddress = IPAddress.Loopback;

            if (!CassiniNetworkUtils.IsPortAvailable(ipAddress, port))
            {
                throw new Exception(string.Format("Port {0} is in use.", port));
            }

            applicationPath = Path.GetFullPath(applicationPath);

            virtualPath = String.Format("/{0}/", (virtualPath ?? string.Empty).Trim('/')).Replace("//", "/");
            hostName    = string.IsNullOrEmpty(hostName) ? "localhost" : hostName;

            StartServer(applicationPath, ipAddress, port, virtualPath, hostName);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// </summary>
        internal void Validate()
        {
            if (string.IsNullOrEmpty(ApplicationPath))
            {
                throw new CassiniException(SR.ErrApplicationPathIsNull, ErrorField.ApplicationPath);
            }

            try
            {
                ApplicationPath = Path.GetFullPath(ApplicationPath);
            }
            catch
            {
            }
            if (!Directory.Exists(ApplicationPath))
            {
                throw new CassiniException(SR.WebdevDirNotExist, ErrorField.ApplicationPath);
            }

            ApplicationPath = ApplicationPath.Trim('\"').TrimEnd('\\');


            if (!string.IsNullOrEmpty(VirtualPath))
            {
                VirtualPath = VirtualPath.Trim('\"');
                VirtualPath = VirtualPath.Trim('/');
                VirtualPath = "/" + VirtualPath;
            }
            else
            {
                VirtualPath = "/";
            }


            if (!VirtualPath.StartsWith("/"))
            {
                VirtualPath = "/" + VirtualPath;
            }


            if (AddHost && string.IsNullOrEmpty(HostName))
            {
                throw new CassiniException(SR.ErrInvalidHostname, ErrorField.HostName);
            }


            IPAddress = ParseIP(IPMode, IPv6, IPAddress).ToString();


            if (VisualStudio) // then STOP HERE.
            {
                // It is fortunate that in order to provide api parity with WebDev
                // we do not need to port scan. Visual Studio balks and refuses to
                // attach if we monkey around and open ports.
                Port     = Port == 0 ? 80 : Port;
                PortMode = PortMode.Specific;
                return;
            }


            switch (PortMode)
            {
            case PortMode.FirstAvailable:

                if (PortRangeStart < 1)
                {
                    throw new CassiniException(SR.ErrInvalidPortRangeValue, ErrorField.PortRangeStart);
                }

                if (PortRangeEnd < 1)
                {
                    throw new CassiniException(SR.ErrInvalidPortRangeValue, ErrorField.PortRangeEnd);
                }

                if (PortRangeStart > PortRangeEnd)
                {
                    throw new CassiniException(SR.ErrPortRangeEndMustBeEqualOrGreaterThanPortRangeSta,
                                               ErrorField.PortRange);
                }
                Port = CassiniNetworkUtils.GetAvailablePort(PortRangeStart, PortRangeEnd,
                                                            System.Net.IPAddress.Parse(IPAddress), true);

                if (Port == 0)
                {
                    throw new CassiniException(SR.ErrNoAvailablePortFound, ErrorField.PortRange);
                }

                break;

            case PortMode.Specific:

                if ((Port < 1) || (Port > 0xffff))
                {
                    throw new CassiniException(SR.ErrPortOutOfRange, ErrorField.Port);
                }


                // start waiting....
                //TODO: design this hack away.... why am I waiting in a validation method?
                int now = Environment.TickCount;

                // wait until either 1) the specified port is available or 2) the specified amount of time has passed
                while (Environment.TickCount < now + WaitForPort &&
                       CassiniNetworkUtils.GetAvailablePort(Port, Port, System.Net.IPAddress.Parse(IPAddress), true) !=
                       Port)
                {
                    Thread.Sleep(100);
                }

                // is the port available?
                if (CassiniNetworkUtils.GetAvailablePort(Port, Port, System.Net.IPAddress.Parse(IPAddress), true) !=
                    Port)
                {
                    throw new CassiniException(SR.ErrPortIsInUse, ErrorField.Port);
                }


                break;

            default:

                throw new CassiniException(SR.ErrInvalidPortMode, ErrorField.None);
            }
        }
Ejemplo n.º 4
0
        private static void Main(string[] cmdLine)
        {
            CommandLineArguments args = new CommandLineArguments();


            if (!CommandLineParser.ParseArgumentsWithUsage(cmdLine, args))
            {
                Environment.Exit(-1);
            }
            else
            {
                switch (args.RunMode)
                {
                case RunMode.Server:
                    IPAddress ip = IPAddress.Loopback;
                    try
                    {
                        args.Validate();

                        ip = CommandLineArguments.ParseIP(args.IPMode, args.IPv6, args.IPAddress);
                        int port = args.PortMode == PortMode.FirstAvailable ?
                                   CassiniNetworkUtils.GetAvailablePort(args.PortRangeStart, args.PortRangeEnd, ip, true) :
                                   args.Port;

                        if (args.AddHost)
                        {
                            HostsFile.AddHostEntry(ip.ToString(), args.HostName);
                        }

                        using (var server =
                                   new Server(port, args.VirtualPath, args.ApplicationPath,
                                              ip, args.HostName, args.TimeOut))
                        {
                            server.Start();
                            Console.WriteLine("started: {0}\r\nPress Enter key to exit....", server.RootUrl);
                            Console.ReadLine();
                            server.ShutDown();
                        }
                    }
                    catch (CassiniException ex)
                    {
                        Console.WriteLine("error:{0} {1}",
                                          ex.Field == ErrorField.None
                                                  ? ex.GetType().Name
                                                  : ex.Field.ToString(), ex.Message);
                    }
                    catch (Exception ex2)
                    {
                        Console.WriteLine("error:{0}", ex2.Message);
                        Console.WriteLine(CommandLineParser.ArgumentsUsage(typeof(CommandLineArguments)));
                    }
                    finally
                    {
                        if (args.AddHost)
                        {
                            HostsFile.RemoveHostEntry(ip.ToString(), args.HostName);
                        }
                    }
                    break;

                case RunMode.Hostsfile:
                    SetHostsFile(args);
                    break;
                }
            }
        }
Ejemplo n.º 5
0
 ///<summary>
 ///</summary>
 ///<param name="physicalPath"></param>
 ///<param name="requireAuthentication"></param>
 public Server(string physicalPath, bool requireAuthentication)
     : this(
         CassiniNetworkUtils.GetAvailablePort(32768, 65535, IPAddress.Loopback, false), "/", physicalPath,
         requireAuthentication)
 {
 }
Ejemplo n.º 6
0
 ///<summary>
 ///</summary>
 ///<param name="physicalPath"></param>
 public Server(string physicalPath)
     : this(CassiniNetworkUtils.GetAvailablePort(32768, 65535, IPAddress.Loopback, false), physicalPath)
 {
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Will start specified application as "localhost" on loopback and first available port in the range 8000-10000 with vpath "/"
 /// </summary>
 /// <param name="applicationPath">Physical path to application.</param>
 public void StartServer(string applicationPath)
 {
     StartServer(applicationPath, CassiniNetworkUtils.GetAvailablePort(8000, 10000, IPAddress.Loopback, true), "/", "localhost");
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Combine the RootUrl of the running web application with the relative url specified.
 /// </summary>
 /// <param name="relativeUrl"></param>
 /// <returns></returns>
 public string NormalizeUrl(string relativeUrl)
 {
     return(CassiniNetworkUtils.NormalizeUrl(RootUrl, relativeUrl));
 }