示例#1
0
        public static Response Model(Request request)
        {
            init();
            var model = request.Variables["model"].Value;
            Type t = modelsTypes[modelNames.IndexOf(model)];
            List<List<String>> table = new List<List<string>>();
            List<PropertyInfo> pis = Feint.FeintORM.FeintORM.GetInstance().getPropertiesFromClass(t).ToList();
            List<PropertyInfo> fis = Feint.FeintORM.FeintORM.GetInstance().getForeignersFromClass(t).ToList();
            var lsh = new List<String>();

            var p = pis[pis.Count - 1];
            pis.RemoveAt(pis.Count - 1);
            pis.Insert(0, p);
            List<String> forms = new List<string>();
            foreach (var pi in pis)
            {
                if (pi.Name != "Id")
                {
                    if (pi.PropertyType == typeof(string))
                    {
                        forms.Add("<tr><td><p><label style\"text-align:right;\">" + pi.Name + ": </label>" + "</td><td><input  type=\"text\" name=\"" + pi.Name + "\"/></td>" + "</p></tr>");
                    }
                    else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(long))
                    {
                        forms.Add("<tr><td><p><label>" + pi.Name + ": </label>" + "</td><td><input type=\"text\" name=\"" + pi.Name + "\"/></td>" + "</p></tr>");
                    } if (pi.PropertyType == typeof(DateTime))
                    {
                        forms.Add("<tr><td>" + pi.Name + ": </td><td><div class=\"input-append date datetime\"><input data-format=\"dd/MM/yyyy hh:mm:ss\" type=\"text\" name=\"" + pi.Name + "\"></input><span class=\"add-on\"><i data-time-icon=\"icon-time\" data-date-icon=\"icon-calendar\"></i></span></div></p></td></tr>");
                    }
                }
                lsh.Add(pi.Name);
            }

            foreach (var f in fis)
            {
                forms.Add("<tr><td><p><label>" + f.Name + " Id: </label>" + "</td><td><input type=\"text\" name=\"" + f.Name + "\"/></td>" + "</p></tr>");
                lsh.Add(f.Name);
            }
            var response = new Response("admin/model.html", new { message = "Hello World!", collumns = lsh, model = model, form = forms });
            return response;
        }
示例#2
0
 public static Response Login(Request request)
 {
     init();
     var response = new Response("admin/login.html", new { usernameName = "username", passwordName = "password", usernameLabel = (usernameLabel == null ? usernameProperty.Name : usernameLabel), passwordLabel = (passwordLabel == null ? passwordProperty.Name : passwordLabel) });
     return response;
 }
示例#3
0
 public static Response Redirect(String url)
 {
     Response r = new Response();
     r.redirectUrl = url;
     return r;
 }
示例#4
0
 public static Response Dashboard(Request request)
 {
     init();
     var response = new Response("admin/dashboard.html", new { message = "Hello World!", models = modelNames });
     return response;
 }
示例#5
0
        private void listenerCallback(IAsyncResult result)
        {
            HttpListener listener = (HttpListener)result.AsyncState;
            HttpListenerContext context = listener.EndGetContext(result);
            HttpListenerRequest request = context.Request;
            HttpListenerResponse response = context.Response;
            try
            {

                Request req = new Request(request.Url.LocalPath.ToString());
                Response res = null;

                if (req.Url.StartsWith("/" + FeintSDK.Settings.StaticFolder))
                {

                    if (req.Url.Contains("./") || req.Url.ToString().Contains(".."))
                        res = null;
                    else
                    {
                        try
                        {
                            FileStream fs = new FileStream("FeintSite/" + req.Url.Substring(1), FileMode.Open, FileAccess.Read);
                            byte[] buffer = new byte[fs.Length];
                            fs.Read(buffer, 0, (int)fs.Length);
                            res = new FeintSDK.Response(buffer);
                            fs.Close();
                            string ext = req.Url.Substring(req.Url.LastIndexOf("."), req.Url.Length - req.Url.LastIndexOf("."));
                            response.Headers.Add("Content-Type", GetMimeType(ext) + "; charset=utf-8");

                        }
                        catch
                        {

                        }
                    }
                }
                else
                {
                    req.Method = request.HttpMethod;
                    req.Session = new Session();
                    string text = null;
                    using (var reader = new StreamReader(request.InputStream,
                                             request.ContentEncoding))
                    {
                        text = reader.ReadToEnd();
                    }
                    if (text.Length > 0)
                    {

                        if (request.ContentType.StartsWith("application/json"))
                        {
                            req.FormData = JsonConvert.DeserializeObject<Dictionary<string, string>>(text);
                        }
                        else if (!request.ContentType.StartsWith("multipart/form-data"))
                        {
                            req.FormData = new Dictionary<string, string>(parseContent(text));
                        }
                        else
                        {
                            req.FormData = new Dictionary<string, string>();
                            string[] postTab = text.Replace("\r", "").Split('\n');
                            String postName = "";
                            for (int i = 0; i < postTab.Length; i++)
                            {
                                if (postTab[i].StartsWith("------"))
                                    continue;
                                if (postTab[i].StartsWith("Content-Disposition: form-data;"))
                                {

                                    postName = HttpUtility.UrlDecode(postTab[i].Substring(38, postTab[i].LastIndexOf("\"") - 38)).Trim();
                                    continue;
                                }
                                if (postTab[i].Length > 0)
                                {
                                    req.FormData.Add(postName, HttpUtility.UrlDecode(postTab[i]).Trim());
                                }

                            }
                        }
                    }
                    FeintSDK.RequestMethod actualMethod = FeintSDK.RequestMethod.POST;
                    switch (request.HttpMethod)
                    {
                        case "GET":
                            actualMethod = FeintSDK.RequestMethod.GET;
                            break;
                        case "POST":
                            actualMethod = FeintSDK.RequestMethod.POST;
                            break;
                        case "PUT":
                            actualMethod = FeintSDK.RequestMethod.PUT;
                            break;
                        case "DELETE":
                            actualMethod = FeintSDK.RequestMethod.DELETE;
                            break;
                    }
                    if (!req.Url.EndsWith("/"))
                        req.Url += "/";
                    for (int i = 0; i < FeintSDK.Settings.Urls.Count; i++)
                    {
                        var match = Regex.Match(req.Url.ToString(), FeintSDK.Settings.Urls[i].UrlMatch);

                        if (match.Success && (FeintSDK.Settings.Urls[i].Method == FeintSDK.RequestMethod.ALL || actualMethod == FeintSDK.Settings.Urls[i].Method))
                        {
                            setNonPublicSetProperty(req, req.GetType(), "Variables", match.Groups);

                            setSesion(request, response, req);
                            MethodInfo mi = FeintSDK.Settings.Urls[i].View.GetMethodInfo();
                            FeintSDK.AOPAttribute[] aops = (FeintSDK.AOPAttribute[])mi.GetCustomAttributes(typeof(FeintSDK.AOPAttribute), true);
                            res = null;
                            foreach (var aop in aops)
                            {
                                res = aop.PreRequest(req);
                            }
                            if (res == null)
                            {
                                res = FeintSDK.Settings.Urls[i].View(req);
                                if (res.MimeType != null)
                                {
                                    response.Headers.Add("Content-Type", res.MimeType + "; charset=utf-8");
                                }
                            }
                            var redirect = res.GetType().GetField("redirectUrl",
                             BindingFlags.NonPublic |
                             BindingFlags.Instance);
                            foreach (var aop in aops)
                            {
                                aop.PostRequest(req);
                            }
                            String url = (String)redirect.GetValue(res);
                            if (url != null)
                            {
                                //response.SeeOtherRedirect(url);
                                response.Redirect(url);

                                 response.Close();
                                return;
                            }
                            break;
                        }
                    }
                }
                if (res != null)
                {

                    //response.ContentLength64 = res.Data.Length;
                    //System.IO.Stream output = response.OutputStream;
                    //output.Write(res.Data, 0, res.Data.Length);
                    //output.Close();
                    response.StatusCode = res.Status;
                    response.ContentLength64 = res.Data.Length;
                    System.IO.Stream output = response.OutputStream;
                    output.Write(res.Data, 0, res.Data.Length);
                    output.Close();
                }
                else
                {
                    string responseString = "<HTML><BODY>404!!!</BODY></HTML>";
                    byte[] buffer = System.Text.Encoding.GetEncoding(1252).GetBytes(responseString);
                    response.ContentLength64 = buffer.Length;
                    System.IO.Stream output = response.OutputStream;
                    output.Write(buffer, 0, buffer.Length);
                    output.Close();
                }

            }
            catch (IOException e)
            {
                Log.D(e);
                // Console.WriteLine(e);
                string responseString = "<HTML><BODY>Im n00b- internal server erron- 503!!!</BODY></HTML>";
                byte[] buffer = System.Text.Encoding.GetEncoding(1252).GetBytes(responseString);
                response.ContentLength64 = buffer.Length;
                System.IO.Stream output = response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();
            }
        }