Holds information about a single hub.
Inheritance: Descriptor
 /// <summary>
 /// Builds a dictionary of all possible methods on a given hub.
 /// Single entry contains a collection of available overloads for a given method name (key).
 /// This dictionary is being cached afterwards.
 /// </summary>
 /// <param name="hub">Hub to build cache for</param>
 /// <returns>Dictionary of available methods</returns>
 private static IDictionary<string, IEnumerable<MethodDescriptor>> BuildMethodCacheFor(HubDescriptor hub)
 {
     return ReflectionHelper.GetExportedHubMethods(hub.HubType)
         .GroupBy(GetMethodName, StringComparer.OrdinalIgnoreCase)
         .ToDictionary(group => group.Key,
                       group => group.Select(oload => GetMethodDescriptor(group.Key, hub, oload)),
                       StringComparer.OrdinalIgnoreCase);
 }
        public IHub Create(HubDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

            if (descriptor.HubType == null)
            {
                return null;
            }

            return ActivatorUtilities.CreateInstance(_serviceProvider, descriptor.HubType) as IHub;
        }
        /// <summary>
        /// Determines whether client is authorized to connect to <see cref="IHub"/>.
        /// </summary>
        /// <param name="hubDescriptor">Description of the hub client is attempting to connect to.</param>
        /// <param name="request">The (re)connect request from the client.</param>
        /// <returns>true if the caller is authorized to connect to the hub; otherwise, false.</returns>
        public virtual bool AuthorizeHubConnection(HubDescriptor hubDescriptor, HttpRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            // If RequireOutgoing is explicitly set to false, authorize all connections.
            if (_requireOutgoing.HasValue && !_requireOutgoing.Value)
            {
                return true;
            }

            return UserAuthorized(request.HttpContext.User);
        }
 private static MethodDescriptor GetMethodDescriptor(string methodName, HubDescriptor hub, MethodInfo methodInfo)
 {
     Type progressReportingType;
     var parameters = ExtractProgressParameter(methodInfo.GetParameters(), out progressReportingType);
     
     return new MethodDescriptor
     {
         ReturnType = methodInfo.ReturnType,
         Name = methodName,
         NameSpecified = (GetMethodAttributeName(methodInfo) != null),
         Invoker = new HubMethodDispatcher(methodInfo).Execute,
         Hub = hub,
         Attributes = methodInfo.GetCustomAttributes(typeof(Attribute), inherit: true).Cast<Attribute>(),
         ProgressReportingType = progressReportingType,
         Parameters = parameters
         .Select(p => new ParameterDescriptor
         {
             Name = p.Name,
             ParameterType = p.ParameterType,
         })
         .ToList()
     };
 }
 public NullMethodDescriptor(HubDescriptor descriptor, string methodName, IEnumerable<MethodDescriptor> availableMethods)
 {
     _methodName = methodName;
     _availableMethods = availableMethods;
     Hub = descriptor;
 }
 /// <summary>
 /// This method is called before the AuthorizeConnect components of any modules added later to the <see cref="IHubPipeline"/>
 /// are executed. If this returns false, then those later-added modules will not run and the client will not be allowed
 /// to subscribe to client-side invocations of methods belonging to the hub defined by the <see cref="HubDescriptor"/>.
 /// </summary>
 /// <param name="hubDescriptor">A description of the hub the client is trying to subscribe to.</param>
 /// <param name="request">The connect request made by the client trying to subscribe to the hub.</param>
 /// <returns>true, if the client is authorized to connect to the hub, false otherwise.</returns>
 protected virtual bool OnBeforeAuthorizeConnect(HubDescriptor hubDescriptor, HttpRequest request)
 {
     return true;
 }
 /// <summary>
 /// Retrieves an existing dictionary of all available methods for a given hub from cache.
 /// If cache entry does not exist - it is created automatically by BuildMethodCacheFor.
 /// </summary>
 /// <param name="hub"></param>
 /// <returns></returns>
 private IDictionary<string, IEnumerable<MethodDescriptor>> FetchMethodsFor(HubDescriptor hub)
 {
     return _methods.GetOrAdd(
         hub.Name,
         key => BuildMethodCacheFor(hub));
 }
        private static string BuildHubExecutableMethodCacheKey(HubDescriptor hub, string method, IList<IJsonValue> parameters)
        {
            string normalizedParameterCountKeyPart;

            if (parameters != null)
            {
                normalizedParameterCountKeyPart = parameters.Count.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                // NOTE: we normalize a null parameter array to be the same as an empty (i.e. Length == 0) parameter array
                normalizedParameterCountKeyPart = "0";
            }

            // NOTE: we always normalize to all uppercase since method names are case insensitive and could theoretically come in diff. variations per call
            string normalizedMethodName = method.ToUpperInvariant();

            string methodKey = hub.Name + "::" + normalizedMethodName + "(" + normalizedParameterCountKeyPart + ")";

            return methodKey;
        }
 public IEnumerable<MethodDescriptor> GetMethods(HubDescriptor hub)
 {
     return FetchMethodsFor(hub)
         .SelectMany(kv => kv.Value)
         .ToList();
 }
        /// <summary>
        /// Searches the specified <paramref name="hub">Hub</paramref> for the specified <paramref name="method"/>.
        /// </summary>
        /// <remarks>
        /// In the case that there are multiple overloads of the specified <paramref name="method"/>, the <paramref name="parameters">parameter set</paramref> helps determine exactly which instance of the overload should be resolved. 
        /// If there are multiple overloads found with the same number of matching parameters, none of the methods will be returned because it is not possible to determine which overload of the method was intended to be resolved.
        /// </remarks>
        /// <param name="hub">Hub to search for the specified <paramref name="method"/> on.</param>
        /// <param name="method">The method name to search for.</param>
        /// <param name="descriptor">If successful, the <see cref="MethodDescriptor"/> that was resolved.</param>
        /// <param name="parameters">The set of parameters that will be used to help locate a specific overload of the specified <paramref name="method"/>.</param>
        /// <returns>True if the method matching the name/parameter set is found on the hub, otherwise false.</returns>
        public bool TryGetMethod(HubDescriptor hub, string method, out MethodDescriptor descriptor, IList<IJsonValue> parameters)
        {
            string hubMethodKey = BuildHubExecutableMethodCacheKey(hub, method, parameters);

            if (!_executableMethods.TryGetValue(hubMethodKey, out descriptor))
            {
                IEnumerable<MethodDescriptor> overloads;

                if (FetchMethodsFor(hub).TryGetValue(method, out overloads))
                {
                    var matches = overloads.Where(o => o.Matches(parameters)).ToList();

                    // If only one match is found, that is the "executable" version, otherwise none of the methods can be returned because we don't know which one was actually being targeted
                    descriptor = matches.Count == 1 ? matches[0] : null;
                }
                else
                {
                    descriptor = null;
                }

                // If an executable method was found, cache it for future lookups (NOTE: we don't cache null instances because it could be a surface area for DoS attack by supplying random method names to flood the cache)
                if (descriptor != null)
                {
                    _executableMethods.TryAdd(hubMethodKey, descriptor);
                }
            }

            return descriptor != null;
        }
 public bool TryGetHub(string hubName, out HubDescriptor descriptor)
 {
     return _hubs.Value.TryGetValue(hubName, out descriptor);
 }
 public IList <string> RejoiningGroups(HubDescriptor hubDescriptor, HttpRequest request, IList <string> groups)
 {
     return(Pipeline.RejoiningGroups(hubDescriptor, request, groups));
 }
 public bool AuthorizeConnect(HubDescriptor hubDescriptor, HttpRequest request)
 {
     return(Pipeline.AuthorizeConnect(hubDescriptor, request));
 }
Example #14
0
 /// <summary>
 /// This method is called before the AuthorizeConnect components of any modules added later to the <see cref="IHubPipeline"/>
 /// are executed. If this returns false, then those later-added modules will not run and the client will not be allowed
 /// to subscribe to client-side invocations of methods belonging to the hub defined by the <see cref="HubDescriptor"/>.
 /// </summary>
 /// <param name="hubDescriptor">A description of the hub the client is trying to subscribe to.</param>
 /// <param name="request">The connect request made by the client trying to subscribe to the hub.</param>
 /// <returns>true, if the client is authorized to connect to the hub, false otherwise.</returns>
 protected virtual bool OnBeforeAuthorizeConnect(HubDescriptor hubDescriptor, HttpRequest request)
 {
     return(true);
 }