示例#1
0
        public static IAppBuilder UseWa(this IAppBuilder builder, IWaConfig config)
        {
            builder.Use((context, next) =>
            {
                if (config.SendStaticContent(context))
                {
                    return(Task.Delay(0));
                }

                try
                {
                    var match = config.Match(context);
                    if (!match.Success)
                    {
                        return(next());
                    }
                    return(config.Run(context, match));
                }
                catch (Exception e)
                {
                    Console.Write(e);
                    return(next());
                }
            });
            return(builder);
        }
示例#2
0
 public void Setup(IOwinContext context, IWaConfig config)
 {
     this.context  = context;
     this.request  = context.Request;
     this.response = context.Response;
     this.config   = config;
 }
示例#3
0
 public BareApiRoute(string pattern, MethodInfo methodInfo, IWaConfig config)
     : base(pattern, methodInfo, config)
 {
 }
示例#4
0
        public AbstractRoute(string pattern, MethodInfo methodInfo, IWaConfig config)
        {
            this.config = config;
            method      = methodInfo;

            foreach (var part in pattern.Split(new[] { '/' }))
            {
                if (part.IndexOf('{') == 0)
                {
                    if (part[part.Length - 1] != '}')
                    {
                        throw new Exception("part must end with }");
                    }
                    var  pair = part.Substring(1, part.Length - 2).Split(new[] { ':' });
                    Part newPart;

                    /*if (pair.Length == 2)
                     * {
                     *  switch (pair[1])
                     *  {
                     *      case "int":
                     *          newPart = new IntPart();
                     *          break;
                     *      case "long":
                     *          newPart = new LongPart();
                     *          break;
                     *      default:
                     *          throw new Exception("unsupported type");
                     *  }
                     * }
                     * else
                     * {
                     *  newPart = new StringPart();
                     * }
                     * newPart.pattern = pair[0];
                     * if (newPart.pattern[newPart.pattern.Length -1 ] == '?')
                     * {
                     *  newPart.pattern = newPart.pattern.Substring(0, newPart.pattern.Length - 1);
                     *  newPart.optional = true;
                     * }*/
                    string partName = pair[0];
                    bool   optional = false;
                    if (partName[partName.Length - 1] == '?')
                    {
                        partName = partName.Substring(0, partName.Length - 1);
                        optional = true;
                    }

                    var parameter = methodInfo.GetParameters().SingleOrDefault(p => p.Name == partName);
                    if (parameter == null)
                    {
                        throw new Exception(string.Format("method {0} parameter {1} cannot be found (from pattern {2})", methodInfo.Name, partName, pattern));
                    }
                    if (parameter.ParameterType.IsEnum)
                    {
                        newPart = new EnumPart(parameter.ParameterType);
                    }
                    else if (parameter.ParameterType == typeof(string))
                    {
                        newPart = new StringPart();
                    }
                    else if (parameter.ParameterType == typeof(long))
                    {
                        newPart = new LongPart();
                    }
                    else if (parameter.ParameterType == typeof(int))
                    {
                        newPart = new IntPart();
                    }
                    else
                    {
                        throw new Exception("Unsupported uri part parameter type " + parameter.ParameterType.Name);
                    }
                    newPart.pattern  = partName;
                    newPart.optional = optional;
                    newPart.order    = parameter.Position;
                    parts.Add(newPart);
                }
                else if (part.Length > 0)
                {
                    parts.Add(new Part {
                        pattern = part
                    });
                }
            }

            body = method.GetParameters().Where(p => p.GetCustomAttribute <FromBodyAttribute>() != null).SingleOrDefault();

            foreach (var parameter in method.GetParameters().Where(p => p.GetCustomAttribute <FromUriAttribute>() != null))
            {
                if (query == null)
                {
                    query = new Dictionary <string, Part>();
                }
                if (parameter.ParameterType == typeof(int))
                {
                    query[parameter.Name] = new IntPart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType == typeof(long))
                {
                    query[parameter.Name] = new LongPart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType == typeof(string))
                {
                    query[parameter.Name] = new StringPart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType == typeof(bool))
                {
                    query[parameter.Name] = new BoolPart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType == typeof(double))
                {
                    query[parameter.Name] = new DoublePart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType == typeof(DateTime))
                {
                    query[parameter.Name] = new DatePart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType.IsEnum)
                {
                    query[parameter.Name] = new EnumPart(parameter.ParameterType)
                    {
                        order = parameter.Position
                    }
                }
                ;
                else
                {
                    throw new Exception("Parameter type " + parameter.ParameterType + " unsupported in " + method.DeclaringType.FullName + "." + method.Name);
                }

                if (parameter.HasDefaultValue)
                {
                    query[parameter.Name].optional     = true;
                    query[parameter.Name].defaultValue = parameter.DefaultValue;
                }
            }

            var unmappedParameter = method.GetParameters().FirstOrDefault(p => p.GetCustomAttribute <FromBodyAttribute>() == null && p.GetCustomAttribute <FromUriAttribute>() == null &&
                                                                          !parts.Any(x => x.order == p.Position));

            if (unmappedParameter != null)
            {
                throw new Exception("Parameter " + unmappedParameter.Name + " has not been mapped in " + method.Name + " from " + method.DeclaringType.Name);
            }

            numberOfMandatoryParts = parts.Count(p => !p.optional);
        }
 public AsyncHttpActionResultApiRoute(string pattern, MethodInfo methodInfo, IWaConfig config)
     : base(pattern, methodInfo, config)
 {
 }