Exemplo n.º 1
0
 internal static HttpSysListener CreateDynamicHttpServer(string basePath, out string root, out string baseAddress)
 {
     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 options = new HttpSysOptions();
             options.UrlPrefixes.Add(prefix);
             options.RequestQueueName = prefix.Port; // Convention for use with CreateServerOnExistingQueue
             var listener = new HttpSysListener(options, new LoggerFactory());
             try
             {
                 listener.Start();
                 return(listener);
             }
             catch (HttpSysException ex)
             {
                 listener.Dispose();
                 if (ex.ErrorCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_ALREADY_EXISTS &&
                     ex.ErrorCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SHARING_VIOLATION &&
                     ex.ErrorCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_ACCESS_DENIED)
                 {
                     throw;
                 }
             }
         }
         NextPort = BasePort;
     }
     throw new Exception("Failed to locate a free port.");
 }
Exemplo n.º 2
0
 internal static HttpSysListener CreateDynamicHttpServer(string basePath, out string root, out string baseAddress)
 {
     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 listener = new HttpSysListener(new HttpSysOptions(), new LoggerFactory());
             listener.Options.UrlPrefixes.Add(prefix);
             try
             {
                 listener.Start();
                 return(listener);
             }
             catch (HttpSysException)
             {
                 listener.Dispose();
             }
         }
         NextPort = BasePort;
     }
     throw new Exception("Failed to locate a free port.");
 }
        public async Task Request_MultiplePrefixes(string requestUri, string expectedPathBase, string expectedPath)
        {
            // TODO: We're just doing this to get a dynamic port. This can be removed later when we add support for hot-adding prefixes.
            string root;
            var    server = Utilities.CreateHttpServerReturnRoot("/", out root);

            server.Dispose();
            server = new HttpSysListener(new HttpSysOptions(), new LoggerFactory());
            using (server)
            {
                var uriBuilder = new UriBuilder(root);
                foreach (string path in new[] { "/", "/11", "/2/3", "/2", "/11/2" })
                {
                    server.Options.UrlPrefixes.Add(UrlPrefix.Create(uriBuilder.Scheme, uriBuilder.Host, uriBuilder.Port, path));
                }
                server.Start();

                Task <string> responseTask = SendRequestAsync(root + requestUri);

                var context = await server.AcceptAsync(Utilities.DefaultTimeout);

                var request = context.Request;

                Assert.Equal(expectedPath, request.Path);
                Assert.Equal(expectedPathBase, request.PathBase);

                context.Dispose();

                string response = await responseTask;
                Assert.Equal(string.Empty, response);
            }
        }
Exemplo n.º 4
0
        internal static HttpSysListener CreateServer(string scheme, string host, int port, string path)
        {
            var listener = new HttpSysListener(new HttpSysOptions(), new LoggerFactory());

            listener.Options.UrlPrefixes.Add(UrlPrefix.Create(scheme, host, port, path));
            listener.Start();
            return(listener);
        }
Exemplo n.º 5
0
    internal static HttpSysListener CreateServer(Action <HttpSysOptions> configureOptions)
    {
        var options = new HttpSysOptions();

        configureOptions(options);
        var listener = new HttpSysListener(options, new LoggerFactory());

        listener.Start();
        return(listener);
    }
Exemplo n.º 6
0
        public void Server_RegisterUnavailablePrefix_ThrowsActionableHttpSysException()
        {
            var options = new HttpSysOptions();

            options.UrlPrefixes.Add(UrlPrefix.Create("http", "example.org", "8080", ""));
            var listener = new HttpSysListener(options, new LoggerFactory());

            var exception = Assert.Throws <HttpSysException>(() => listener.Start());

            Assert.Equal((int)UnsafeNclNativeMethods.ErrorCodes.ERROR_ACCESS_DENIED, exception.ErrorCode);
            Assert.Contains($@"netsh http add urlacl url=http://example.org:8080/ user={Environment.UserDomainName}\{Environment.UserName}", exception.Message);
        }
Exemplo n.º 7
0
        internal static HttpSysListener CreateServerOnExistingQueue(AuthenticationSchemes authScheme, bool allowAnonymos, string requestQueueName)
        {
            var options = new HttpSysOptions();

            options.RequestQueueMode              = RequestQueueMode.Attach;
            options.RequestQueueName              = requestQueueName;
            options.Authentication.Schemes        = authScheme;
            options.Authentication.AllowAnonymous = allowAnonymos;
            var listener = new HttpSysListener(options, new LoggerFactory());

            listener.Start();
            return(listener);
        }
Exemplo n.º 8
0
    public void Server_RegisterUnavailablePrefix_ThrowsActionableHttpSysException()
    {
        using var server1 = Utilities.CreateHttpServer(out var address1);

        var options = new HttpSysOptions();

        options.UrlPrefixes.Add(address1);
        using var listener = new HttpSysListener(options, new LoggerFactory());

        var exception = Assert.Throws <HttpSysException>(() => listener.Start());

        Assert.Equal((int)UnsafeNclNativeMethods.ErrorCodes.ERROR_ALREADY_EXISTS, exception.ErrorCode);
        Assert.Contains($"The prefix '{address1}' is already registered.", exception.Message);
    }