/// <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); } }
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; } } }
///<summary> ///</summary> ///<param name="physicalPath"></param> public Server(string physicalPath) : this(CassiniNetworkUtils.GetAvailablePort(32768, 65535, IPAddress.Loopback, false), physicalPath) { }
///<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) { }
/// <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"); }