Example #1
0
        public HttpResponse Handle(HttpRequest request)
        {
            this.ParseInput(request);
            //TODO check if there is session with given id and execute the method only if user is authorised

            var        method     = this.GetMethod();
            var        controller = this.GetController();
            IInvocable result     =
                (IInvocable)method
                .Invoke(controller, this.methodParams);

            string content = result.Invoke();
            //var response = new HttpResponse()
            //{
            //    ContentAsUTF8 = content,
            //    StatusCode = ResponseStatusCode.Ok
            //};

            //this.ClearRequestParameters();
            //return response;
            var response = new HttpResponse();

            if (!string.IsNullOrEmpty(result.Location))
            {
                response.StatusCode = ResponseStatusCode.Found;
                response.Header.OtherParameters.Add("Location", result.Location);
            }
            else
            {
                response.StatusCode    = ResponseStatusCode.Ok;
                response.ContentAsUTF8 = content;
            }
            return(response);
        }
Example #2
0
        public IHttpResponse Handle(IHttpRequest request)
        {
            if (request.Path == "/favicon.ico")
            {
                return(null);
            }

            this.getParams     = new Dictionary <string, string>(request.UrlParameters);
            this.postParams    = new Dictionary <string, string>(request.FormData);
            this.requestMethod = request.Method.ToString().ToUpper();
            this.PrepareControllerAndActionNames(request.Path);
            var controller = this.GetController();
            var method     = this.GetMethod();

            if (method == null)
            {
                return(new NotFoundResponse());
            }
            this.PrepareMethodParameters(method);

            IInvocable actionResult = (IInvocable)method.Invoke(controller, this.methodParams);

            string content = actionResult.Invoke();

            IHttpResponse response = new ContentResponse(HttpStatusCode.Ok, content);

            return(response);
        }
        public IHttpResponse Handle(IHttpRequest request)
        {
            this.getParameters  = new Dictionary <string, string>(request.UrlParameters);
            this.postParameters = new Dictionary <string, string>(request.FormData);
            this.requestMethod  = request.Method.ToString().ToUpper();

            bool areControllersAndActionValid = this.RetrieveControllerAndActionNames(request);

            if (!areControllersAndActionValid)
            {
                return(new NotFoundResponse());
            }

            MethodInfo methodInfo = this.RetrieveActionForExecution();

            if (methodInfo == null)
            {
                return(new NotFoundResponse());
            }

            this.RetrieveMethodParameters(methodInfo);

            IInvocable actionResult = (IInvocable)methodInfo.Invoke(this.controllerInstance, this.methodParameters);

            string content = actionResult.Invoke();

            IHttpResponse response = new ContentResponse(HttpStatusCode.Ok, content);

            return(response);
        }
Example #4
0
        public IHttpResponse Handle(IHttpRequest request)
        {
            this.request = request;

            this.getParams = request.UrlParameters;

            this.postParams = request.FormData;

            this.requestMethod = request.Method.ToString();

            this.controllerName = GetControllerName(request.Path);

            this.actionName = GetActionName(request.Path);

            MethodInfo method = this.GetMethod();

            if (method == null)
            {
                return(new NotFoundResponse());
            }

            MapMethodParameters(method);

            IInvocable actionResult = (IInvocable)method.Invoke(this.GetController(), this.methodParams);

            string content = actionResult.Invoke();

            IHttpResponse response = new ContentResponse(HttpStatusCode.Ok, content);

            return(response);
        }
Example #5
0
        public void Dispatch()
        {
            IInvocable result
                = (IInvocable)this
                  .GetMethod()
                  .Invoke(this.GetController(), this.methodParams);

            result.Invoke();
        }
Example #6
0
        private HttpResponse CreateHttpResponse()
        {
            var        method       = this.GetMethod();
            IInvocable actionResult = (IInvocable)this.GetMethod()
                                      .Invoke(this.GetController(), this.methodParams);

            string content  = actionResult.Invoke();
            var    response = new HttpResponse()
            {
                StatusCode    = ResponseStatusCode.Ok,
                ContentAsUTF8 = content
            };

            return(response);
        }
Example #7
0
            private static object Invoke(IInvocable method, object instance, object[] args)
            {
                var finalArgs = args.Where(x => !(x is TypeParameter)).ToArray();
                var refArgs   = new Dictionary <int, RefValue>();
                var outArgs   = new Dictionary <int, OutValue>();

                for (int i = 0; i < method.Parameters.Count; i++)
                {
                    if (method.Parameters[i].ParameterType.IsByRef)
                    {
                        var refArg = finalArgs[i] as RefValue;
                        var outArg = finalArgs[i] as OutValue;
                        if (refArg != null)
                        {
                            refArgs[i] = refArg;
                        }
                        else if (outArg != null)
                        {
                            outArgs[i] = outArg;
                        }
                    }
                }

                foreach (var refArg in refArgs)
                {
                    finalArgs[refArg.Key] = refArg.Value.Value;
                }
                foreach (var outArg in outArgs)
                {
                    finalArgs[outArg.Key] = null;
                }

                var result = method.Invoke(instance, finalArgs);

                foreach (var refArg in refArgs)
                {
                    refArg.Value.Value = finalArgs[refArg.Key];
                }
                foreach (var outArg in outArgs)
                {
                    outArg.Value.Value = finalArgs[outArg.Key];
                }

                return(result);
            }
Example #8
0
        public HttpResponse Handle(HttpRequest request)
        {
            this.getParams = WebUtilities.RetrieveGetParameters(request.Url);

            this.postParams = WebUtilities.RetrievePostParameters(request.Content);

            this.requestMethod = request.Method.ToString();

            Tuple <string, string> controllerAndActionNames = this.GetControllerAndActionName(request.Url);

            if (controllerAndActionNames == null)
            {
                return(HttpResponseBuilder.NotFound());
            }

            this.controllerName = controllerAndActionNames.Item1;
            this.actionName     = controllerAndActionNames.Item2;

            MethodInfo method = this.GetControllerMethod(this.controllerName, this.actionName, this.requestMethod);

            if (method == null)
            {
                throw new NotSupportedException("Method not supported!");
            }

            this.SetMethodParameters(method);

            IInvocable actionResult = (IInvocable)this.GetControllerMethod(
                this.controllerName,
                this.actionName,
                this.requestMethod)
                                      .Invoke(
                this.GetController(this.GetControllerTypeName(this.controllerName)),
                this.methodParams);

            string content = actionResult.Invoke();

            HttpResponse response = new HttpResponse()
            {
                ContentAsUTF8 = content
            };

            return(response);
        }
        public HttpResponse Handle(HttpRequest request)
        {
            this.ClearParameters();
            this.request  = request;
            this.response = new HttpResponse();
            this.ParseInput();

            IInvocable result =
                (IInvocable)this.GetMethod()
                .Invoke(this.GetController(), this.methodParams);

            if (string.IsNullOrEmpty(this.response.Header.Location))
            {
                this.response.StatusCode    = ResponseStatusCode.Ok;
                this.response.ContentAsUTF8 = result.Invoke();
            }

            return(this.response);
        }
Example #10
0
        public HttpResponse Handle(HttpRequest request)
        {
            this.ParseInput(request);
            //TODO check if there is session with given id and execute the method only if user is authorised

            var        method     = this.GetMethod();
            var        controller = this.GetController();
            IInvocable result     =
                (IInvocable)method
                .Invoke(controller, this.methodParams);

            string content  = result.Invoke();
            var    response = new HttpResponse()
            {
                ContentAsUTF8 = content,
                StatusCode    = ResponseStatusCode.OK
            };

            this.ClearRequestParameters();
            return(response);
        }
        public HttpResponse Handle(HttpRequest request)
        {
            this.request  = request;
            this.response = new HttpResponse();
            ParseInput();


            MethodInfo method       = this.GetMethod();
            Controller controller   = this.GetController();
            IInvocable actionResult = (IInvocable)method
                                      .Invoke(controller, this.methodParams);

            if (string.IsNullOrEmpty(this.response.Header.Location))
            {
                this.response.StatusCode    = ResponseStatusCode.Ok;
                this.response.ContentAsUTF8 = actionResult.Invoke();
            }

            this.ClearParameters();

            return(response);
        }
Example #12
0
        public HttpResponse Handle(HttpRequest request)
        {
            string url   = WebUtility.UrlDecode(request.Url);
            string query = string.Empty;

            if (url.Contains('?'))
            {
                query = url.Split('?')[1];
            }

            var controllerActionParams = url.Split('?');
            var controllerAction       = controllerActionParams[0].Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            controllerActionParams = query.Split('&');

            if (controllerActionParams.Length >= 1)
            {
                foreach (var getPar in controllerActionParams)
                {
                    if (getPar.Contains('='))
                    {
                        var keyValue = getPar.Split('=');
                        getParams.Add(keyValue[0], keyValue[1]);
                    }
                }
            }

            string contentParam = request.Content;

            if (contentParam != null)
            {
                contentParam = WebUtility.UrlDecode(contentParam);
                var postRequestParams = contentParam.Split('&');

                foreach (var postPar in postRequestParams)
                {
                    var parametersRest = postPar.Split('=');
                    postParams.Add(parametersRest[0], parametersRest[1]);
                }
            }

            this.requestMethod = request.Method.ToString();

            string controllerNameFormat = controllerAction[0].First().ToString().ToUpper() + String.Join("", controllerAction[0].Skip(1)) + "Controller";

            this.controllerName = controllerNameFormat;

            string actionNameFormat = controllerAction[1].First().ToString().ToUpper() + String.Join("", controllerAction[1].Skip(1));

            this.actionName = actionNameFormat;

            MethodInfo method = this.GetMethod();

            if (method == null)
            {
                throw new NotSupportedException("No such method");
            }

            IEnumerable <ParameterInfo> parameters = method.GetParameters();

            this.methodParams = new object[parameters.Count()];

            int index = 0;

            foreach (ParameterInfo param in parameters)
            {
                if (param.ParameterType.IsPrimitive)
                {
                    object value = this.getParams[param.Name];
                    this.methodParams[index] = Convert.ChangeType(value, param.ParameterType);
                    index++;
                }
                else
                {
                    Type   bindingModelType = param.ParameterType;
                    object bindingModel     = Activator.CreateInstance(bindingModelType);
                    IEnumerable <PropertyInfo> properties = bindingModelType.GetProperties();

                    foreach (PropertyInfo property in properties)
                    {
                        property.SetValue(bindingModel, Convert.ChangeType(postParams[property.Name], property.PropertyType));
                    }

                    this.methodParams[index] = Convert.ChangeType(bindingModel, bindingModelType);
                    index++;
                }
            }

            IInvocable actionResult = (IInvocable)this.GetMethod()
                                      .Invoke(this.GetController(), this.methodParams);

            string content  = actionResult.Invoke();
            var    response = new HttpResponse()
            {
                StatusCode    = ResponseStatusCode.Ok,
                ContentAsUTF8 = content
            };

            this.ClearParamDictionaries();
            return(response);
        }
        public IHttpResponse Handle(IHttpRequest request)
        {
            var getTokens =
                request
                .UrlParameters;

            foreach (var item in getTokens)
            {
                string key   = item.Key;
                string value = item.Value;

                this.getParams[key] = value;
            }

            var postTokens = request.FormData;

            foreach (var item in postTokens)
            {
                string key   = item.Key;
                string value = item.Value;

                this.postParams[key] = value;
            }

            this.requestMethod = request.Method.ToString().ToUpper();

            string getControllerAndActionTokens =
                request
                .Url
                .Split(new string[] { "?" }, StringSplitOptions.RemoveEmptyEntries)[0];

            if (getControllerAndActionTokens.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries).Length < 2)
            {
                return(new NotFoundResponse());
            }

            string controllerName = getControllerAndActionTokens
                                    .Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries)[0] + "Controller";

            controllerName      = controllerName.First().ToString().ToUpper() + controllerName.Substring(1);
            this.controllerName = controllerName;


            string actionName = getControllerAndActionTokens
                                .Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries).Last();

            actionName      = actionName.First().ToString().ToUpper() + actionName.Substring(1);
            this.actionName = actionName;


            if (actionName == controllerName)
            {
                return(new NotFoundResponse());
            }

            MethodInfo method = this.GetMethod();

            if (method == null)
            {
                return(new NotFoundResponse());
            }

            IEnumerable <ParameterInfo> parameters = method.GetParameters();

            this.methodParams = new object[parameters.Count()];

            int index = 0;

            foreach (ParameterInfo param in parameters)
            {
                if (param.ParameterType.IsPrimitive ||
                    param.ParameterType == typeof(string))
                {
                    object value = this.getParams[param.Name];
                    this.methodParams[index] = Convert.ChangeType(
                        value,
                        param.ParameterType
                        );

                    index++;
                }
                else
                {
                    Type bindingModelType = param.ParameterType;

                    object bindingModel =
                        Activator.CreateInstance(bindingModelType);

                    IEnumerable <PropertyInfo> properties
                        = bindingModelType.GetProperties();

                    foreach (PropertyInfo property in properties)
                    {
                        property.SetValue(bindingModel,
                                          Convert.ChangeType(
                                              postParams[property.Name],
                                              property.PropertyType
                                              )
                                          );
                    }

                    this.methodParams[index] = Convert.ChangeType(
                        bindingModel,
                        bindingModelType
                        );
                    index++;
                }
            }

            IInvocable actionResult = (IInvocable)this.GetMethod()
                                      .Invoke(this.GetController(), this.methodParams);

            string content = actionResult.Invoke();

            IHttpResponse response =
                new ContentResponse(HttpStatusCode.Ok, content);

            return(response);
        }
Example #14
0
        public HttpResponse Handle(HttpRequest request)
        {
            this.RetrieveRequestParams(request);

            this.requestMethod = request.Method.ToString();

            string[] urlComponents = request.Url.Split('/');

            if (urlComponents.Length >= 2)
            {
                this.controllerName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(urlComponents[1]) + "Controller";

                this.actionName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(urlComponents[2].Split('?')[0]);
            }

            MethodInfo method = GetMethod();

            if (method == null)
            {
                throw new NotSupportedException("No such method!");
            }

            //////////////////////////////////////////

            IEnumerable <ParameterInfo> parameters = method.GetParameters();

            this.methodParams = new object[parameters.Count()];

            int index = 0;

            foreach (ParameterInfo param in parameters)
            {
                if (param.ParameterType.IsPrimitive)
                {
                    object value = this.getParams[param.Name];

                    this.methodParams[index] = Convert.ChangeType(value, param.ParameterType);
                    index++;
                }
                else
                {
                    Type   bindingModelType = param.ParameterType;
                    object bindingModel     = Activator.CreateInstance(bindingModelType);

                    IEnumerable <PropertyInfo> properties = bindingModelType.GetProperties();

                    foreach (PropertyInfo property in properties)
                    {
                        property.SetValue(bindingModel, Convert.ChangeType(postParams[property.Name], property.PropertyType));
                    }
                    this.methodParams[index] = Convert.ChangeType(bindingModel, bindingModelType);
                    index++;
                }
            }

            IInvocable actionResult = (IInvocable)this.GetMethod().Invoke(this.GetController(), this.methodParams);

            string content = actionResult.Invoke();

            var response = new HttpResponse
            {
                StatusCode    = ResponseStatusCode.Ok,
                ContentAsUTF8 = content
            };

            return(response);

            //////////////////////////////////////////
        }
Example #15
0
        public HttpResponse Handle(HttpRequest request)
        {
            this.requestParameters = new Dictionary <string, string>();
            string requestMethod = null;

            if (request.Method == RequestMethod.GET)
            {
                this.requestParameters = DecodeParams(request.Url);
            }
            else
            {
                this.requestParameters = DecodeParams(request.Content);
            }

            this.requestMethodType = request.Method.ToString();
            this.MapUrlToControllerAndAction(request.Url);

            MethodInfo method = this.GetMethod();

            if (method == null)
            {
                throw new NotSupportedException("Action is not supported");
            }
            IEnumerable <ParameterInfo> parameters = method.GetParameters();

            this.methodParameters = new object[parameters.Count()];
            int index = 0;

            foreach (ParameterInfo pamfo in parameters)

            {
                if (pamfo.ParameterType.IsPrimitive)
                {
                    object value = this.requestParameters[pamfo.Name];
                    this.methodParameters[index] = Convert.ChangeType(value, pamfo.ParameterType);
                    index++;
                }
                else if (pamfo.ParameterType == typeof(HttpRequest))
                {
                    this.methodParameters[index] = pamfo;
                    index++;
                }
                else if (pamfo.ParameterType == typeof(HttpSession))
                {
                    this.methodParameters[index] = request.HttpSession;
                    index++;
                }
                else
                {
                    Type   bindingModelType = pamfo.ParameterType;
                    object bindingModel     = Activator.CreateInstance(bindingModelType);
                    foreach (var requestParam in this.requestParameters)
                    {
                        PropertyInfo pinfo =
                            bindingModelType.GetProperties()
                            .FirstOrDefault(p => p.Name.ToLower() == requestParam.Key.ToLower());
                        if (pinfo != null)
                        {
                            pinfo.SetValue(bindingModel, Convert.ChangeType(this.requestParameters[pinfo.Name], pinfo.PropertyType));
                        }
                    }
                    this.methodParameters[index] = Convert.ChangeType(bindingModel, bindingModelType);
                    index++;
                }
            }
            Controller controller = this.GetController();

            IInvocable actionResult = (IInvocable)method.Invoke(controller, this.methodParameters);
            string     content      = actionResult.Invoke();
            var        response     = new HttpResponse();

            if (!string.IsNullOrEmpty(actionResult.Location))
            {
                response.StatusCode = ResponseStatusCode.Found;
                response.Header.OtherParameters.Add("Location", actionResult.Location);
            }
            else
            {
                response.StatusCode    = ResponseStatusCode.Ok;
                response.ContentAsUTF8 = content;
            }
            return(response);
        }
 /// <summary>
 /// Test if invoking the SampleInvocable instance works.
 /// </summary>
 /// <param name="sampleInvocable">Can be real local instance or remote
 /// proxy.</param>
 private static void InvokeSampleInvokable(IInvocable sampleInvocable)
 {
     Assert.Equal("FOO", sampleInvocable.Invoke(new[] { "Foo" }));
 }
Example #17
0
        public IHttpResponse Handle(IHttpRequest request)
        {
            foreach (var urlParam in request.UrlParameters)
            {
                this.getParams[urlParam.Key] = urlParam.Value;
            }

            foreach (var postParam in request.FormData)
            {
                this.postParams[postParam.Key] = postParam.Value;
            }

            this.requestMethod = request.Method.ToString().ToUpper();

            var urlTokens = request.Url.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (urlTokens.Length != 2)
            {
                return(new NotFoundResponse());
            }
            if (request.Url.Contains("?"))
            {
                int ind = urlTokens[urlTokens.Length - 1].IndexOf("?");

                urlTokens[urlTokens.Length - 1] = urlTokens[urlTokens.Length - 1].Substring(0, ind);
            }

            string controllerName = urlTokens[0].Substring(0, 1).ToUpper() + urlTokens[0].Substring(1).ToLower() + MvcContext.Get.ControllerSuffix;
            string actionName     = urlTokens[1].Substring(0, 1).ToUpper() + urlTokens[1].Substring(1).ToLower();

            this.controllerName = controllerName;
            this.actionName     = actionName;

            MethodInfo method = this.GetMethod();

            if (method == null)
            {
                return(new NotFoundResponse());
            }

            IEnumerable <ParameterInfo> parameters = method.GetParameters();

            this.methodParams = new object[parameters.Count()];

            int index = 0;

            foreach (ParameterInfo param in parameters)
            {
                if (param.ParameterType.IsPrimitive || param.ParameterType == typeof(string))
                {
                    object value = this.getParams[param.Name];
                    this.methodParams[index] = Convert.ChangeType(value, param.ParameterType);
                    index++;
                }
                else
                {
                    Type   bindingModelType = param.ParameterType;
                    object bindingModel     = Activator.CreateInstance(bindingModelType);

                    IEnumerable <PropertyInfo> properties = bindingModelType.GetProperties();

                    foreach (PropertyInfo property in properties)
                    {
                        property.SetValue(
                            bindingModel, Convert.ChangeType(
                                postParams[property.Name],
                                property.PropertyType
                                )
                            );
                    }

                    this.methodParams[index] = Convert.ChangeType(
                        bindingModel,
                        bindingModelType);
                    index++;
                }
            }

            IInvocable actionResult = (IInvocable)this.GetMethod()
                                      .Invoke(this.GetController(), this.methodParams);

            string content = actionResult.Invoke();

            IHttpResponse response = new ContentResponse(HttpStatusCode.Ok, content);

            return(response);
        }