/// <summary>
        /// Deletes the current <see cref="HttpUrlReservation"/>.
        /// </summary>
        /// <exception cref="HttpUrlReservationNotFoundException">If the current <see cref="HttpUrlReservation"/> does not still exist.</exception>
        /// <exception cref="HttpServerException">If an internal error has been occured (use the <see cref="Win32Exception.NativeErrorCode"/> for more information).</exception>
        public void Delete()
        {
            HttpServer.Initialize();

            try
            {
                var configInformation = new HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_SET(this.url, null);

                var result = HttpServerApi.HttpDeleteServiceConfiguration(IntPtr.Zero, HttpServerApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, configInformation, Marshal.SizeOf(configInformation), IntPtr.Zero);

                if (result == Win32Error.ERROR_FILE_NOT_FOUND)
                {
                    throw new HttpUrlReservationNotFoundException(result, string.Format(CultureInfo.CurrentUICulture, HttpServerResources.UrlReservationToDeleteNotFound, this.url));
                }

                if (result != Win32Error.NO_ERROR)
                {
                    throw new HttpServerException(result);
                }
            }
            finally
            {
                HttpServer.Terminate();
            }
        }
        /// <summary>
        /// Update the <see cref="HttpUrlReservationAccessRule"/> of the current <see cref="HttpUrlReservation"/>.
        /// </summary>
        /// <exception cref="HttpUrlReservationNotFoundException">If the current <see cref="HttpUrlReservation"/> does not still exist.</exception>
        /// <exception cref="HttpServerException">If an internal error has been occured (use the <see cref="Win32Exception.NativeErrorCode"/> for more information).</exception>
        private void Update()
        {
            HttpServer.Initialize();

            try
            {
                var ssdl = this.GetSecurityDescriptorSddlForm(AccessControlSections.Access);

                var configInformation = new HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_SET(this.url, ssdl);

                // Delete the URL reservation record
                var result = HttpServerApi.HttpDeleteServiceConfiguration(IntPtr.Zero, HttpServerApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, configInformation, Marshal.SizeOf(configInformation), IntPtr.Zero);

                if (result == Win32Error.ERROR_FILE_NOT_FOUND)
                {
                    throw new HttpUrlReservationNotFoundException(result, string.Format(CultureInfo.CurrentUICulture, HttpServerResources.UrlReservationToUpdateNotFound, this.url));
                }

                // Add the URL reservation record with the new SDDL.
                result = HttpServerApi.HttpSetServiceConfiguration(IntPtr.Zero, HttpServerApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, configInformation, Marshal.SizeOf(configInformation), IntPtr.Zero);

                if (result != Win32Error.NO_ERROR)
                {
                    throw new HttpServerException(result);
                }
            }
            finally
            {
                HttpServer.Terminate();
            }
        }
예제 #3
0
        /// <summary>
        /// Calls the HttpInitialize() function of the HTTP Server API.
        /// </summary>
        internal static void Initialize()
        {
            var version = new HttpServerApi.HTTPAPI_VERSION(1, 0);
            var result  = HttpServerApi.HttpInitialize(version, HttpServerApi.HTTP_INITIALIZE_CONFIG, IntPtr.Zero);

            if (result != Win32Error.NO_ERROR)
            {
                throw new HttpServerException(result);
            }
        }
예제 #4
0
        /// <summary>
        /// Register an URL reservation in the Windows HTTP Server.
        /// </summary>
        /// <param name="url">URL to register.</param>
        /// <param name="rule">Rule to apply to the URL to register.</param>
        /// <returns>An instance of <see cref="HttpUrlReservation"/> which allows to manage the URL reservation.</returns>
        public static HttpUrlReservation RegisterUrlReservation(string url, HttpUrlReservationAccessRule rule)
        {
            Contracts.IsNotNull(url, nameof(url));
            Contracts.IsNotNull(rule, nameof(rule));

            Initialize();

            try
            {
                // Create the ACL access
                var acl = new DiscretionaryAcl(false, false, 1);
                acl.AddAccess(rule.AccessControlType, (SecurityIdentifier)rule.IdentityReference, (int)rule.HttpUrlReservationAccessRights, rule.InheritanceFlags, rule.PropagationFlags);

                // Create the security descriptor and convert it to SDDL.
                var securityDescriptor = new CommonSecurityDescriptor(false, false, ControlFlags.DiscretionaryAclPresent, (SecurityIdentifier)rule.IdentityReference, null, null, acl);
                var ssdl = securityDescriptor.GetSddlForm(AccessControlSections.Access);

                var configInformation = new HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_SET(url, ssdl);

                var result = HttpServerApi.HttpSetServiceConfiguration(IntPtr.Zero, HttpServerApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, configInformation, Marshal.SizeOf(configInformation), IntPtr.Zero);

                if (result == Win32Error.ERROR_ALREADY_EXISTS)
                {
                    throw new HttpUrlReservationAlreadyExistsException(result, string.Format(CultureInfo.CurrentUICulture, HttpServerResources.UrlReservationAlreadyExists, url));
                }

                if (result != Win32Error.NO_ERROR)
                {
                    throw new HttpServerException(result);
                }

                return(new HttpUrlReservation(url, new[] { rule }));
            }
            finally
            {
                Terminate();
            }
        }
예제 #5
0
        /// <summary>
        /// Gets the URL reservations registered in the Windows HTTP server.
        /// </summary>
        /// <returns>List of URL reservations registered in the Windows HTTP server.</returns>
        public static IReadOnlyList <HttpUrlReservation> GetUrlReservations()
        {
            Initialize();

            try
            {
                var urlReservations = new List <HttpUrlReservation>();
                var record          = 0u;

                while (true)
                {
                    // Create the HTTP_SERVICE_CONFIG_URLACL_QUERY structure to query all the URL reservations
                    HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_QUERY inputConfigInfoQuery = new HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_QUERY
                    {
                        QueryDesc = HttpServerApi.HTTP_SERVICE_CONFIG_QUERY_TYPE.HttpServiceConfigQueryNext,
                        Token     = record,
                    };

                    // Create an unmanaged memory which will be use to retrieve the output of the HttpQueryServiceConfiguration() call.
                    using (UnmanagedMemoryBlock pOutputConfigInfo = new UnmanagedMemoryBlock())
                    {
                        int returnLength;

                        var result = HttpServerApi.HttpQueryServiceConfiguration(
                            IntPtr.Zero,
                            HttpServerApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo,
                            inputConfigInfoQuery,
                            Marshal.SizeOf(inputConfigInfoQuery),
                            pOutputConfigInfo,
                            pOutputConfigInfo.Length,
                            out returnLength,
                            IntPtr.Zero);

                        // If the API returns ERROR_INSUFFICIENT_BUFFER, increase the pOutputConfigInfo memory block
                        // and call the HttpQueryServiceConfiguration() function again.
                        if (result == Win32Error.ERROR_INSUFFICIENT_BUFFER)
                        {
                            pOutputConfigInfo.Extends(returnLength);

                            result = HttpServerApi.HttpQueryServiceConfiguration(
                                IntPtr.Zero,
                                HttpServerApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo,
                                inputConfigInfoQuery,
                                Marshal.SizeOf(inputConfigInfoQuery),
                                pOutputConfigInfo,
                                pOutputConfigInfo.Length,
                                out returnLength,
                                IntPtr.Zero);
                        }
                        else if (result == Win32Error.ERROR_NO_MORE_ITEMS)
                        {
                            // Exit the loop (there is no more record)
                            break;
                        }

                        // If the HttpQueryServiceConfiguration() function does not return NO_ERROR, throw a Win32Exception.
                        if (result != Win32Error.NO_ERROR)
                        {
                            throw new Win32Exception((int)result);
                        }

                        var urlAcl = pOutputConfigInfo.Unwrap <HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_SET>();

                        urlReservations.Add(ConvertUrlAclToUrlReservation(urlAcl));
                    }

                    record++;
                }

                return(urlReservations);
            }
            finally
            {
                Initialize();
            }
        }
예제 #6
0
 /// <summary>
 /// Calls the HttpTerminate function of the HTTP Server API.
 /// </summary>
 internal static void Terminate()
 {
     HttpServerApi.HttpTerminate(HttpServerApi.HTTP_INITIALIZE_CONFIG, IntPtr.Zero);
 }