コード例 #1
0
        public ControllerDescriptor(Type t, HttpServer server, bool wrapped = false)
        {
            if (server == null || t == null)
            {
                throw new ArgumentNullException("Type and Server parameter cannot be null");
            }
            UseWrap        = wrapped;
            Server         = server;
            ControllerType = t;
            List <MethodInfo> ms = t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
                                   .Where(x => !IgnoreControllerMethodAttribute.HasAttribute(x)).ToList();

            foreach (MethodInfo m in ms)
            {
                CallDescriptor desc = new CallDescriptor(this, m);
                string         name = m.Name.ToLower();
                if (desc.Descriptor != null && !string.IsNullOrEmpty(desc.Descriptor.MethodName))
                {
                    name = desc.Descriptor.MethodName;
                }
                Calls.Add(m.Name.ToLower(), desc);
                if (desc.Descriptor != null && desc.Descriptor.IsDefault && !Calls.ContainsKey(""))
                {
                    Calls.Add("", desc);
                }
            }
        }
コード例 #2
0
 public void Call(IRecursiveCallHandler callBlock, DocRange range)
 {
     if (!Calls.ContainsKey(callBlock))
     {
         Calls.Add(callBlock, new List <DocRange>());
     }
     Calls[callBlock].Add(range);
 }
コード例 #3
0
        public bool ExecuteController(string methodName, HttpRequest request)
        {
            string mn = methodName.ToLower();

            if (Calls.ContainsKey(methodName))
            {
                if (request.IsClosed)
                {
                    return(true);
                }

                BodyType responseType = Server.DefaultResponseType;

                //Header override
                string accept = request.GetHeader("Accept");
                if (!string.IsNullOrEmpty(accept))
                {
                    switch (accept.ToLower())
                    {
                    case "text/json":
                    case "application/json":
                        responseType = BodyType.JSON;
                        break;

                    case "text/xml":
                    case "application/xml":
                        responseType = BodyType.XML;
                        break;

                    default:
                        //Not supported ResponseType
                        break;
                    }
                }

                try
                {
                    CallDescriptor call = Calls[methodName];

                    //Pre-Controller logic
                    request.Server.PreControllerCheck(request, call);
                    if (request.IsClosed)
                    {
                        return(true);
                    }

                    //With descriptor
                    if (call.Descriptor != null)
                    {
                        responseType = call.Descriptor.ResponseType;
                        if (responseType == BodyType.Undefined)
                        {
                            responseType = Server.DefaultResponseType;
                        }

                        if (call.Descriptor.HasParameters)
                        {
                            foreach (string enforced in call.Descriptor.Parameters)
                            {
                                if (!request.Parameters.ContainsKey(enforced))
                                {
                                    throw new ArgumentException($"Missing parameter {enforced}");
                                }
                            }
                        }
                    }

                    object    result = null;
                    Exception ex     = null;

                    try
                    {
                        if (call.TokenRequirements != null)
                        {
                            if (!request.Authenticated || call.TokenRequirements.LevelRequired > request.AuthenticationLevel)
                            {
                                throw new ForbiddenException("You don't have permission to access this API method");
                            }
                            if (call.TokenRequirements.RequestAttributes != null && call.TokenRequirements.RequestAttributes.Length > 0)
                            {
                                foreach (string attr in call.TokenRequirements.RequestAttributes)
                                {
                                    if (!request.Attributes.Contains(attr))
                                    {
                                        throw new ForbiddenException("You don't have permission to access this API method at this moment");
                                    }
                                }
                            }
                        }
                        //Caching
                        if (call.CacheHeader != null)
                        {
                            switch (call.CacheHeader.Cache)
                            {
                            case CachingType.Cache:
                                request.Response.Headers.Add("cache-control", "max-age=" + call.CacheHeader.Validity.ToString());
                                break;

                            case CachingType.Prevent:
                                request.Response.Headers.Add("cache-control", "no-cache, no-store, must-revalidate");
                                request.Response.Headers.Add("Pragma", "no-cache");
                                request.Response.Headers.Add("Expires", "0");
                                break;
                            }
                        }

                        //Parse Parameters
                        object[] paras = GetParameters(call, request);

                        //Handling
                        result = call.Info.Invoke(GetInstance(request), paras.ToArray());
                        if (request.DisableAutoHandling)
                        {
                            return(true);
                        }
                    }
                    catch (TargetInvocationException tx)
                    {
                        ex = tx.InnerException;
                    }
                    catch (Exception x)
                    {
                        ex = x;
                    }

                    if (!request.IsClosed && (result != null || ex != null))
                    {
                        Type resultType;
                        if (ex != null)
                        {
                            resultType = typeof(Exception);
                            result     = new APIWrap(ex);
                            if (!Server.Debug)
                            {
                                ((APIWrap)result).Exception.StackTrace = "";
                            }
                            if (responseType == BodyType.Raw || responseType == BodyType.Razor)
                            {
                                responseType = Server.DefaultResponseType;
                            }
                            HandleResult(Server, request, call, responseType, result, false);
                        }
                        else
                        {
                            HandleResult(Server, request, call, responseType, result, UseWrap);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (responseType == BodyType.Razor)
                    {
                        responseType = Server.DefaultResponseType;
                    }

                    request.Write(new APIWrap(ex), responseType);
                }

                if (!request.IsClosed)
                {
                    request.Close();
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }