コード例 #1
0
        protected async override Task RunAsync(HttpApiContext context)
        {
            string prefix = context.RoutePrefix;

            string url = context.Request.RawUrl.Substring(prefix.Length);
            if (url.StartsWith("/"))
            {
                url = url.Substring(1);
            }
            string[] tokens = url.Split('/');

            string methodName = tokens[0];

            var method = this.GetType().GetMethod(methodName);
            List<object> plist = new List<object>();
            foreach (var p in method.GetParameters())
            {
                plist.Add(Load(context,p));
            }

            var to = method.Invoke(this, plist.ToArray());

            var tobj = to as Task;
            if (tobj != null)
            {
                await tobj;
                var p = tobj.GetType().GetProperty("Result");
                context.Result = p.GetValue(tobj);
            }
            else { 
                context.Result = to;
            }
        }
コード例 #2
0
        public async Task<HttpApiResponse> ExecuteAsync(HttpApiRequest request)
        {
            try
            {
                var url = request.RawUrl;
                var context = new HttpApiContext { 
                    Request = request
                };

                var r = RegisteredRoutes.FirstOrDefault(x => url.StartsWith(x.RoutePrefix, StringComparison.OrdinalIgnoreCase));
                if (r == null)
                {
                    using (var c = new HttpProxyController())
                    {
                        await c.ExecuteAsync(context);
                        return context.Response;
                    }
                }

                using (BaseHttpApiController c = Activator.CreateInstance(r.Controller) as BaseHttpApiController)
                {
                    context.RoutePrefix = r.RoutePrefix;
                    await c.ExecuteAsync(context);
                    return context.Response;
                }
            }
            catch (Exception ex)
            {
                return HttpApiResponse.FromException(ex);
            }
        }
コード例 #3
0
        protected override async Task RunAsync(HttpApiContext context)
        {
            using (WebRequestHandler handler = new WebRequestHandler())
            {
                handler.UseCookies = false;
                handler.AllowAutoRedirect = false;
                handler.ServerCertificateValidationCallback = delegate { return true; };
                //handler.ClientCertificateOptions = ClientCertificateOption.Automatic;
                handler.AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip;
                using (HttpClient client = new HttpClient(handler))
                {
                    HttpRequestMessage request = Create(context.Request);

                    var response = await client.SendAsync(request);


                    var data = await response.Content.ReadAsByteArrayAsync();

                    context.Response = SetResponse(response, data);
                    var ct = response.Content.Headers.ContentType;
                    if (ct != null)
                    {
                        context.Response.ContentType = ct.ToString();
                    }

                    //context.Response.ContentType = response.Content.Headers.ContentType.ToString();
                }
            }
        }
コード例 #4
0
 protected void PostProcess(HttpApiContext context)
 {
     if (context.Response == null)
     {
         context.Response = context.Result as HttpApiResponse;
         if (context.Response == null)
         {
             context.Response = HttpApiResponse.ToJson(context.Result);
         }
     }
 }
コード例 #5
0
        private object Load(HttpApiContext context, ParameterInfo p)
        {
            string key = p.Name;
            var source = ModelSource.FormOrQuery;
            var ms = p.GetCustomAttribute<ModelSourceAttribute>();
            if (ms != null)
            {
                source = ms.ModelSource;
                if (!string.IsNullOrWhiteSpace(ms.Name))
                {
                    key = ms.Name;
                }
            }

            var pt = p.ParameterType;

            object value = LoadValue(context, source, key, pt) ?? p.DefaultValue;
            if (value == null)
            {
                
                if (pt.IsValueType)
                    return Activator.CreateInstance(pt);
                if (pt == typeof(string))
                    return value;
                if (pt.GetGenericTypeDefinition() == typeof(Nullable<>))
                    return value;
            }

            /// load model from form..
            if (source == ModelSource.FormOrQuery)
            {

            }
            else
            {
                var model = context.Request.Form["formModel"];
                return JsonConvert.DeserializeObject(model, pt);
            }

            return value;
            
        }
コード例 #6
0
 private object LoadValue(HttpApiContext context, ModelSource source, string key, Type type)
 {
     object v = null;
     switch (source)
     {
         case ModelSource.Header:
             return context.Request.Headers[key];
             
         case ModelSource.FormOrQuery:
             v = context.Request.Form[key];
             if (v == null)
             {
                 v = context.Request.QueryString[key];
             }
             return v;
         case ModelSource.FormJson:
             return JsonConvert.DeserializeObject(context.Request.TextContent, type);
         case ModelSource.QueryString:
             return context.Request.QueryString[key];
         case ModelSource.QueryStringJson:
             v = context.Request.QueryString[key];
             if (v != null)
             {
                 v = JsonConvert.DeserializeObject((string)v, type);
             }
             return v;
         case ModelSource.FormModelJson:
             break;
         default:
             break;
     }
     return null;
 }
コード例 #7
0
 protected virtual void Init(HttpApiContext context)
 {
     
 }
コード例 #8
0
 protected abstract Task RunAsync(HttpApiContext context);
コード例 #9
0
 public async Task ExecuteAsync(HttpApiContext context)
 {
     Init(context);
     await RunAsync(context);
     PostProcess(context);
 }