Beispiel #1
0
        private void Add(StaticRoute route)
        {
            if (route == null)
            {
                throw new ArgumentNullException(nameof(route));
            }

            route.Path = route.Path.ToLower();
            if (!route.Path.StartsWith("/"))
            {
                route.Path = "/" + route.Path;
            }
            if (!route.Path.EndsWith("/"))
            {
                route.Path = route.Path + "/";
            }

            if (Exists(route.Method, route.Path))
            {
                return;
            }

            lock (_Lock)
            {
                _Routes.Add(route);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Match a request method and URL to a handler method.
        /// </summary>
        /// <param name="method">The HTTP method.</param>
        /// <param name="path">URL path.</param>
        /// <returns>Method to invoke.</returns>
        public Func <HttpContext, Task> Match(HttpMethod method, string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            path = path.ToLower();
            if (!path.StartsWith("/"))
            {
                path = "/" + path;
            }
            if (!path.EndsWith("/"))
            {
                path = path + "/";
            }

            lock (_Lock)
            {
                StaticRoute curr = _Routes.FirstOrDefault(i => i.Method == method && i.Path == path);
                if (curr == null || curr == default(StaticRoute))
                {
                    return(null);
                }
                else
                {
                    return(curr.Handler);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Check if a static route exists.
        /// </summary>
        /// <param name="method">The HTTP method.</param>
        /// <param name="path">URL path.</param>
        /// <returns>True if exists.</returns>
        public bool Exists(HttpMethod method, string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            path = path.ToLower();
            if (!path.StartsWith("/"))
            {
                path = "/" + path;
            }
            if (!path.EndsWith("/"))
            {
                path = path + "/";
            }

            lock (_Lock)
            {
                StaticRoute curr = _Routes.FirstOrDefault(i => i.Method == method && i.Path == path);
                if (curr == null || curr == default(StaticRoute))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #4
0
        private void Remove(StaticRoute route)
        {
            if (route == null)
            {
                throw new ArgumentNullException(nameof(route));
            }

            lock (_Lock)
            {
                _Routes.Remove(route);
            }

            return;
        }
Beispiel #5
0
        /// <summary>
        /// Add a route.
        /// </summary>
        /// <param name="method">The HTTP method.</param>
        /// <param name="path">URL path, i.e. /path/to/resource.</param>
        /// <param name="handler">Method to invoke.</param>
        public void Add(HttpMethod method, string path, Func <HttpContext, Task> handler)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            StaticRoute r = new StaticRoute(method, path, handler);

            Add(r);
        }
Beispiel #6
0
        /// <summary>
        /// Remove a route.
        /// </summary>
        /// <param name="method">The HTTP method.</param>
        /// <param name="path">URL path.</param>
        public void Remove(HttpMethod method, string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            StaticRoute r = Get(method, path);

            if (r == null || r == default(StaticRoute))
            {
                return;
            }
            else
            {
                lock (_Lock)
                {
                    _Routes.Remove(r);
                }

                return;
            }
        }