Example #1
0
        public void PathBaseIsNotNormalized()
        {
            var urlPrefix = UrlPrefix.Create("http://localhost:8080/p\u0041\u030Athbase");

            Assert.False(urlPrefix.Path.IsNormalized(NormalizationForm.FormC));
            Assert.Equal("/p\u0041\u030Athbase/", urlPrefix.Path);
        }
Example #2
0
        internal static IWebHost CreateDynamicHost(string basePath, out string root, out string baseAddress, Action <HttpSysOptions> configureOptions, RequestDelegate app)
        {
            lock (PortLock)
            {
                while (NextPort < MaxPort)
                {
                    var port   = NextPort++;
                    var prefix = UrlPrefix.Create("http", "localhost", port, basePath);
                    root        = prefix.Scheme + "://" + prefix.Host + ":" + prefix.Port;
                    baseAddress = prefix.ToString();

                    var builder = new WebHostBuilder()
                                  .UseHttpSys(options =>
                    {
                        options.UrlPrefixes.Add(prefix);
                        configureOptions(options);
                    })
                                  .Configure(appBuilder => appBuilder.Run(app));

                    var host = builder.Build();


                    try
                    {
                        host.Start();
                        return(host);
                    }
                    catch (HttpSysException)
                    {
                    }
                }
                NextPort = BasePort;
            }
            throw new Exception("Failed to locate a free port.");
        }
Example #3
0
        internal static IServer CreateDynamicHttpsServer(string basePath, out string root, out string baseAddress, Action <HttpSysOptions> configureOptions, RequestDelegate app)
        {
            lock (PortLock)
            {
                while (NextHttpsPort < MaxHttpsPort)
                {
                    var port   = NextHttpsPort++;
                    var prefix = UrlPrefix.Create("https", "localhost", port, basePath);
                    root        = prefix.Scheme + "://" + prefix.Host + ":" + prefix.Port;
                    baseAddress = prefix.ToString();

                    var server = CreatePump();
                    server.Features.Get <IServerAddressesFeature>().Addresses.Add(baseAddress);
                    configureOptions(server.Listener.Options);
                    try
                    {
                        server.StartAsync(new DummyApplication(app), CancellationToken.None).Wait();
                        return(server);
                    }
                    catch (HttpSysException)
                    {
                    }
                }
                NextHttpsPort = BaseHttpsPort;
            }
            throw new Exception("Failed to locate a free port.");
        }
Example #4
0
        /// <summary>
        /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa364698(v=vs.85).aspx
        /// </summary>
        /// <param name="scheme">http or https. Will be normalized to lower case.</param>
        /// <param name="host">+, *, IPv4, [IPv6], or a dns name. Http.Sys does not permit punycode (xn--), use Unicode instead.</param>
        /// <param name="port">If empty, the default port for the given scheme will be used (80 or 443).</param>
        /// <param name="path">Should start and end with a '/', though a missing trailing slash will be added. This value must be un-escaped.</param>
        public static UrlPrefix Create(string scheme, string host, string port, string path)
        {
            int?portValue = null;

            if (!string.IsNullOrWhiteSpace(port))
            {
                portValue = int.Parse(port, NumberStyles.None, CultureInfo.InvariantCulture);
            }

            return(UrlPrefix.Create(scheme, host, portValue, path));
        }
Example #5
0
        public void UrlsAreParsedCorrectly(string url, string scheme, string host, string port, string pathBase, string toString)
        {
            var urlPrefix = UrlPrefix.Create(url);

            Assert.Equal(scheme, urlPrefix.Scheme);
            Assert.Equal(host, urlPrefix.Host);
            Assert.Equal(port, urlPrefix.Port);
            Assert.Equal(pathBase, urlPrefix.Path);

            Assert.Equal(toString ?? url, urlPrefix.ToString());
        }
Example #6
0
        private IServer CreateServer(out string root, RequestDelegate app)
        {
            // TODO: We're just doing this to get a dynamic port. This can be removed later when we add support for hot-adding prefixes.
            var dynamicServer = Utilities.CreateHttpServerReturnRoot("/", out root, app);

            dynamicServer.Dispose();
            var rootUri = new Uri(root);
            var server  = Utilities.CreatePump();

            foreach (string path in new[] { "/", "/11", "/2/3", "/2", "/11/2" })
            {
                server.Listener.Options.UrlPrefixes.Add(UrlPrefix.Create(rootUri.Scheme, rootUri.Host, rootUri.Port, path));
            }

            server.StartAsync(new DummyApplication(app), CancellationToken.None).Wait();
            return(server);
        }
Example #7
0
        public async Task Server_SetQueueLimit_Success()
        {
            // This is just to get a dynamic port
            string address;

            using (Utilities.CreateHttpServer(out address, httpContext => Task.FromResult(0))) { }

            var server = Utilities.CreatePump();

            server.Listener.Options.UrlPrefixes.Add(UrlPrefix.Create(address));
            server.Listener.Options.RequestQueueLimit = 1001;

            using (server)
            {
                await server.StartAsync(new DummyApplication(), CancellationToken.None);

                string response = await SendRequestAsync(address);

                Assert.Equal(string.Empty, response);
            }
        }
Example #8
0
 public void CreateThrowsForUrlsWithInvalidPorts(string url)
 {
     Assert.Throws <FormatException>(() => UrlPrefix.Create(url));
 }
Example #9
0
 public void CreateThrowsForUrlsWithoutHost(string url)
 {
     Assert.Throws <ArgumentNullException>(() => UrlPrefix.Create(url));
 }
Example #10
0
 public void CreateThrowsForUrlsWithEmptyScheme(string url)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => UrlPrefix.Create(url));
 }
Example #11
0
 public void CreateThrowsForUrlsWithoutSchemeDelimiter(string url)
 {
     Assert.Throws <FormatException>(() => UrlPrefix.Create(url));
 }