Exemple #1
0
        internal static IServer CreateDynamicHttpServer(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 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)
                    {
                    }
                }
                NextPort = BasePort;
            }
            throw new Exception("Failed to locate a free port.");
        }
Exemple #2
0
        internal static IHost CreateDynamicHost(string basePath, out string root, out string baseAddress, Action <HttpSysOptions> configureOptions, RequestDelegate app)
        {
            var prefix = UrlPrefix.Create("http", "localhost", "0", basePath);

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

            var host = builder.Build();

            host.Start();

            var options = host.Services.GetRequiredService <IOptions <HttpSysOptions> >();

            prefix      = options.Value.UrlPrefixes.First(); // Has new port
            root        = prefix.Scheme + "://" + prefix.Host + ":" + prefix.Port;
            baseAddress = prefix.ToString();

            return(host);
        }
Exemple #3
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);
        }
Exemple #4
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.");
        }
Exemple #5
0
        public async Task Server_SetConnectionLimitInfinite_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.MaxConnections = -1; // infinite

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

                using (var client1 = await SendHungRequestAsync("GET", address))
                    using (var client2 = await SendHungRequestAsync("GET", address))
                        using (var client3 = await SendHungRequestAsync("GET", address))
                        {
                            // Doesn't max out
                            string responseText = await SendRequestAsync(address);

                            Assert.Equal(string.Empty, responseText);
                        }
            }
        }
Exemple #6
0
        public async Task Server_SetConnectionLimit_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));
            Assert.Null(server.Listener.Options.MaxConnections);
            server.Listener.Options.MaxConnections = 3;

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

                using (var client1 = await SendHungRequestAsync("GET", address))
                    using (var client2 = await SendHungRequestAsync("GET", address))
                    {
                        using (var client3 = await SendHungRequestAsync("GET", address))
                        {
                            // Maxed out, refuses connection and throws
                            await Assert.ThrowsAsync <HttpRequestException>(() => SendRequestAsync(address));
                        }

                        // A connection has been closed, try again.
                        string responseText = await SendRequestAsync(address);

                        Assert.Equal(string.Empty, responseText);
                    }
            }
        }
Exemple #7
0
        internal static IServer CreateServer(string scheme, string host, int port, string path, RequestDelegate app)
        {
            var server = CreatePump();

            server.Features.Get <IServerAddressesFeature>().Addresses.Add(UrlPrefix.Create(scheme, host, port, path).ToString());
            server.StartAsync(new DummyApplication(app), CancellationToken.None).Wait();
            return(server);
        }
Exemple #8
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));
        }
Exemple #9
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());
        }
Exemple #10
0
 internal RequestQueue(string requestQueueName, string urlPrefix, ILogger logger, bool receiver)
     : this(urlGroup : null, requestQueueName, RequestQueueMode.Attach, logger, receiver)
 {
     try
     {
         UrlGroup = new UrlGroup(this, UrlPrefix.Create(urlPrefix));
     }
     catch
     {
         Dispose();
         throw;
     }
 }
Exemple #11
0
        internal static IServer CreateDynamicHttpServer(string basePath, out string root, out string baseAddress, Action <HttpSysOptions> configureOptions, RequestDelegate app)
        {
            var prefix = UrlPrefix.Create("http", "localhost", "0", basePath);

            var server = CreatePump(configureOptions);

            server.Features.Get <IServerAddressesFeature>().Addresses.Add(prefix.ToString());
            server.StartAsync(new DummyApplication(app), CancellationToken.None).Wait();

            prefix      = server.Listener.Options.UrlPrefixes.First(); // Has new port
            root        = prefix.Scheme + "://" + prefix.Host + ":" + prefix.Port;
            baseAddress = prefix.ToString();

            return(server);
        }
Exemple #12
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);
        }
Exemple #13
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);
            }
        }
Exemple #14
0
        public async Task Server_SetHttp503VebosittHittingThrottle_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));
            Assert.Null(server.Listener.Options.MaxConnections);
            server.Listener.Options.MaxConnections   = 3;
            server.Listener.Options.Http503Verbosity = Http503VerbosityLevel.Limited;

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

                using (var client1 = await SendHungRequestAsync("GET", address))
                    using (var client2 = await SendHungRequestAsync("GET", address))
                    {
                        using (var client3 = await SendHungRequestAsync("GET", address))
                        {
                            using (HttpClient client4 = new HttpClient())
                            {
                                // Maxed out, refuses connection should return 503
                                HttpResponseMessage response = await client4.GetAsync(address);

                                Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
                            }
                        }

                        // A connection has been closed, try again.
                        string responseText = await SendRequestAsync(address);

                        Assert.Equal(string.Empty, responseText);
                    }
            }
        }
Exemple #15
0
 public void CreateThrowsForUrlsWithEmptyScheme(string url)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => UrlPrefix.Create(url));
 }
Exemple #16
0
 public void CreateThrowsForUrlsWithoutHost(string url)
 {
     Assert.Throws <ArgumentNullException>(() => UrlPrefix.Create(url));
 }
Exemple #17
0
 public void CreateThrowsForUrlsWithInvalidPorts(string url)
 {
     Assert.Throws <FormatException>(() => UrlPrefix.Create(url));
 }
Exemple #18
0
 public void CreateThrowsForUrlsWithoutSchemeDelimiter(string url)
 {
     Assert.Throws <FormatException>(() => UrlPrefix.Create(url));
 }