Example #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.");
 }
Example #2
0
    public MessagePump(IOptions <HttpSysOptions> options, ILoggerFactory loggerFactory, IAuthenticationSchemeProvider authentication)
    {
        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }
        if (loggerFactory == null)
        {
            throw new ArgumentNullException(nameof(loggerFactory));
        }
        _options = options.Value;
        Listener = new HttpSysListener(_options, loggerFactory);
        _logger  = loggerFactory.CreateLogger <MessagePump>();

        if (_options.Authentication.Schemes != AuthenticationSchemes.None)
        {
            authentication.AddScheme(new AuthenticationScheme(HttpSysDefaults.AuthenticationScheme, displayName: _options.Authentication.AuthenticationDisplayName, handlerType: typeof(AuthenticationHandler)));
        }

        Features         = new FeatureCollection();
        _serverAddresses = new ServerAddressesFeature();
        Features.Set <IServerAddressesFeature>(_serverAddresses);

        if (HttpApi.SupportsDelegation)
        {
            Features.Set <IServerDelegationFeature>(this);
        }

        _maxAccepts = _options.MaxAccepts;
    }
Example #3
0
    internal static MessagePump CreatePump(Action <HttpSysOptions> configureOptions, ILoggerFactory loggerFactory = null)
    {
        var options = new HttpSysOptions();

        configureOptions(options);
        return(new MessagePump(Options.Create(options), loggerFactory ?? new LoggerFactory(), new AuthenticationSchemeProvider(Options.Create(new AuthenticationOptions()))));
    }
    protected override string ConfigureServer(HttpSysOptions options, string baseServerAddress)
    {
        options.RequestQueueMode = RequestQueueMode.CreateOrAttach;
        var basePrefix = UrlPrefix.Create(baseServerAddress);
        var prefix     = UrlPrefix.Create(basePrefix.Scheme, basePrefix.Host, basePrefix.Port, "/server");

        options.UrlPrefixes.Add(prefix);
        return(prefix.ToString());
    }
Example #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);
    }
Example #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);
        }
Example #7
0
    public HttpSysListener(HttpSysOptions options, ILoggerFactory loggerFactory)
    {
        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }
        if (loggerFactory == null)
        {
            throw new ArgumentNullException(nameof(loggerFactory));
        }

        if (!HttpApi.Supported)
        {
            throw new PlatformNotSupportedException();
        }

        Debug.Assert(HttpApi.ApiVersion == HttpApiTypes.HTTP_API_VERSION.Version20, "Invalid Http api version");

        Options = options;

        Logger = loggerFactory.CreateLogger <HttpSysListener>();

        _state        = State.Stopped;
        _internalLock = new object();

        // V2 initialization sequence:
        // 1. Create server session
        // 2. Create url group
        // 3. Create request queue
        // 4. Add urls to url group - Done in Start()
        // 5. Attach request queue to url group - Done in Start()

        try
        {
            _serverSession = new ServerSession();

            _requestQueue = new RequestQueue(options.RequestQueueName, options.RequestQueueMode, Logger);

            _urlGroup = new UrlGroup(_serverSession, _requestQueue, Logger);

            _disconnectListener = new DisconnectListener(_requestQueue, Logger);
        }
        catch (Exception exception)
        {
            // If Url group or request queue creation failed, close server session before throwing.
            _requestQueue?.Dispose();
            _urlGroup?.Dispose();
            _serverSession?.Dispose();
            Log.HttpSysListenerCtorError(Logger, exception);
            throw;
        }
    }
Example #8
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);
        }
Example #9
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);
    }
Example #10
0
        internal static void SetHttpSysOptions(WebHostBuilderContext context, HttpSysOptions options)
        {
            var hostConfiguration = context.Configuration.GetSection(WebApiConfiguration.SectionName)
                                    .Get <WebApiConfiguration>();

            options.Authentication.Schemes        = AuthenticationSchemes.NTLM | AuthenticationSchemes.Negotiate;
            options.Authentication.AllowAnonymous = true;

            options.UrlPrefixes.Add($"http://*:{hostConfiguration.PortNumber}");

            if (hostConfiguration.HttpsPort.HasValue)
            {
                options.UrlPrefixes.Add($"https://*:{hostConfiguration.HttpsPort}");
            }
        }
 protected override string ConfigureServer(HttpSysOptions options, string baseServerAddress)
 {
     options.RequestQueueMode = RequestQueueMode.CreateOrAttach;
     return(baseServerAddress);
 }
 protected abstract string ConfigureServer(HttpSysOptions options, string baseServerAddress);