Ejemplo n.º 1
0
        protected virtual object ProcessRequest(object sender, RequestEventArgs e)
        {
            try
            {
                var uri = e.Request.Uri.AbsolutePath;
                uri = uri.TrimEnd('/');

                foreach (var com in commands)
                {
                    var verbs = new RestVerbs();
                    if (com.HasVerbs)
                    {
                        var match = Regex.Match(uri, com.UriVerbMatch);
                        if (!match.Success)
                        {
                            continue;
                        }
                        if ((match.Groups.Count - 1) != com.UriVerbs.Length)
                        {
                            continue;
                        }

                        for (int i = 0; i < com.UriVerbs.Length; i++)
                        {
                            verbs.Add(com.UriVerbs[i], match.Groups[i + 1].Value);
                        }
                    }
                    else if (com.UriTemplate.ToLower() != uri.ToLower())
                    {
                        continue;
                    }

                    var obj = ExecuteCommand(com, verbs, e.Request.Parameters);
                    if (obj != null)
                    {
                        return(obj);
                    }
                }
            }
            catch (Exception exception)
            {
                return(new Dictionary <string, string>
                {
                    { "status", "500" },
                    { "error", "Internal server error." },
                    { "errormsg", exception.Message },
                    { "stacktrace", exception.StackTrace },
                });
            }
            return(new Dictionary <string, string>
            {
                { "status", "404" },
                { "error", "Specified API endpoint doesn't exist. Refer to the documentation for a list of valid endpoints." }
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to process a request received by the <see cref="HttpListener"/>
        /// </summary>
        /// <param name="sender">Sender of the request</param>
        /// <param name="e">RequestEventArgs received</param>
        /// <returns>A <see cref="RestObject"/> describing the state of the request</returns>
        protected virtual object ProcessRequest(object sender, RequestEventArgs e)
        {
            try
            {
                var uri = e.Request.Uri.AbsolutePath;
                uri = uri.TrimEnd('/');
                string upgrade = null;

                if (redirects.ContainsKey(uri))
                {
                    upgrade = redirects[uri].Item2;
                    uri     = redirects[uri].Item1;
                }

                foreach (var com in commands)
                {
                    var verbs = new RestVerbs();
                    if (com.HasVerbs)
                    {
                        var match = Regex.Match(uri, com.UriVerbMatch);
                        if (!match.Success)
                        {
                            continue;
                        }
                        if ((match.Groups.Count - 1) != com.UriVerbs.Length)
                        {
                            continue;
                        }

                        for (int i = 0; i < com.UriVerbs.Length; i++)
                        {
                            verbs.Add(com.UriVerbs[i], match.Groups[i + 1].Value);
                        }
                    }
                    else if (com.UriTemplate.ToLower() != uri.ToLower())
                    {
                        continue;
                    }

                    var obj = ExecuteCommand(com, verbs, e.Request.Parameters, e.Request, e.Context);
                    if (obj != null)
                    {
                        if (!string.IsNullOrWhiteSpace(upgrade) && obj is RestObject)
                        {
                            if (!(obj as RestObject).ContainsKey("upgrade"))
                            {
                                (obj as RestObject).Add("upgrade", upgrade);
                            }
                        }

                        return(obj);
                    }
                }
            }
            catch (Exception exception)
            {
                return(new RestObject("500")
                {
                    { "error", "Internal server error." },
                    { "errormsg", exception.Message },
                    { "stacktrace", exception.StackTrace },
                });
            }
            return(new RestObject("404")
            {
                { "error", "Specified API endpoint doesn't exist. Refer to the documentation for a list of valid endpoints." }
            });
        }