コード例 #1
0
        public virtual string GetPath(SimpleHttpRequest req)
        {
            var spl0 = req.Header.Split(new char[] { ' ', '?' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
            var url  = spl0[1];

            var spl3 = req.Header.Split(new char[] { '/', ' ', '?' }, StringSplitOptions.RemoveEmptyEntries)
                       .ToArray();

            SimpleHttpContext ctx = new SimpleHttpContext();

            if (req.Header.Contains("?"))
            {
                spl0[2] = Http​Utility.UrlDecode(spl0[2]);
                ctx.ParseQuery(spl0[2]);
            }



            var spl = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

            if (spl.Count() < 1)
            {
                return(null);
            }
            var fr = CleanUrls.FirstOrDefault(z => z.Path == url);

            if (fr != null)
            {
                return(fr.Target);
            }
            return(null);
        }
コード例 #2
0
        public bool Process(SimpleHttpContext ctx, string url)
        {
            var req = ctx.Request;
            var mth = this.GetType().GetMethods();

            foreach (var item in mth)
            {
                var attr1 = item.GetCustomAttributes(typeof(RouterMethodAttribute), true);
                if (attr1 == null || attr1.Count() == 0)
                {
                    continue;
                }
                var ma = attr1[0] as RouterMethodAttribute;
                if (ma.Path == url)
                {
                    item.Invoke(this, new[] { ctx });
                    if (ctx.Redirect)
                    {
                        var           stream = req.Stream;
                        StringBuilder sb     = new StringBuilder();

                        sb.Append("HTTP/1.1 303 See other\r\n");
                        foreach (var hitem in ctx.Response.Headers)
                        {
                            sb.Append($"{hitem.Key}: {hitem.Value}\r\n");
                        }
                        sb.Append("Location: " + ctx.RedirectPath + "\r\n\r\n");
                        //sb.Append("Refresh: 0; url=" + ctx.RedirectPath + "\r\n");


                        byte[] Buffer = Encoding.UTF8.GetBytes(sb.ToString());
                        stream.Write(Buffer, 0, Buffer.Length);
                        stream.Close();
                    }

                    return(true);
                }
            }
            return(false);
        }
コード例 #3
0
        public static string Build(string html, SimpleHttpContext ctx)
        {
            List <HttpParseProcessor> procs  = new List <HttpParseProcessor>();
            StringBuilder             output = new StringBuilder();


            procs.Add(new TemplateParser(output, ctx));

            string accum = "";

            for (int i = 0; i < html.Length; i++)
            {
                accum += html[i];
                while (accum.Length > 10)
                {
                    accum = accum.Remove(0, 1);
                }


                if (procs.Any(z => z.Inside))
                {
                    var fr = procs.First(z => z.Inside);
                    fr.PushSymbol(html[i]);
                    continue;
                }
                foreach (var proc in procs)
                {
                    proc.PushSymbol(html[i]);
                    if (proc.Inside)
                    {
                        break;
                    }
                }

                output.Append(html[i]);
            }

            return(output.ToString());
        }
コード例 #4
0
 public TemplateParser(StringBuilder sb, SimpleHttpContext ct) : base(sb, ct)
 {
     StartTag = "<%%";
     EndTag   = "%%>";
     accum    = new string(' ', Math.Max(StartTag.Length, EndTag.Length));
 }
コード例 #5
0
 public HttpParseProcessor(StringBuilder sb, SimpleHttpContext codeType)
 {
     sbb     = sb;
     Context = codeType;
 }
コード例 #6
0
 public JstlParseProcessor(StringBuilder sb, SimpleHttpContext ct) : base(sb, ct)
 {
     StartTag = "<c:forEach";
     EndTag   = "</c:forEach>";
     accum    = new string(' ', Math.Max(StartTag.Length, EndTag.Length));
 }
コード例 #7
0
        private string DynamicUpdate(string html, SimpleHttpContext ctx)
        {
            html = TemplateParser.Build(html, ctx);
            //search all <%

            bool          inside = false;
            StringBuilder output = new StringBuilder();
            string        insd   = "";

            ctx.Page.Context = ctx;
            ctx.Page.OnPageLoad(ctx);
            if (ctx.Response.OverrideResponse)
            {
                return(ctx.Response.OverrideResponseOutput);
            }
            if (ctx.Redirect)
            {
                return(null);
            }

            /*var plm = ctx.CodeType.GetMethod("OnPageLoad");
             * if (plm != null)
             * {
             *  plm.Invoke(null, new object[] { ctx });
             * }*/
            List <HttpParseProcessor> procs = new List <HttpParseProcessor>();


            procs.Add(new HttpSimpleParser(output, ctx));
            procs.Add(new JstlParseProcessor(output, ctx));

            string accum = "";

            for (int i = 0; i < html.Length; i++)
            {
                accum += html[i];
                while (accum.Length > 10)
                {
                    accum = accum.Remove(0, 1);
                }

                if (accum.Contains("<c:"))
                {
                }

                if (procs.Any(z => z.Inside))
                {
                    var fr = procs.First(z => z.Inside);
                    fr.PushSymbol(html[i]);
                    continue;
                }
                foreach (var proc in procs)
                {
                    proc.PushSymbol(html[i]);
                    if (proc.Inside)
                    {
                        break;
                    }
                }

                output.Append(html[i]);
            }

            return(output.ToString());
        }
コード例 #8
0
        private SimpleHttpContext ProcessRequest(SimpleHttpRequest currentRequest, TcpClient client)
        {
            var stream = currentRequest.Stream;

            var addr = (client.Client.RemoteEndPoint as IPEndPoint).Address;
            var ip   = addr.ToString();

            if (currentRequest.Method == "POST")
            {
                var cc = currentRequest.Raw.FirstOrDefault(z => z.StartsWith("Cookie"));

                if (cc != null)
                {
                    var spl2     = cc.Split(new char[] { ':', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                    var cookieId = spl2[1];
                    currentRequest.Cookie = cookieId;
                }

                /*var g = Guid.NewGuid().ToString().Replace("-", "");
                 * CookieId = g;
                 * string Str = "HTTP/1.1 302 Found\nSet-Cookie: " + g + "\n\n";
                 * byte[] Buffer = Encoding.Default.GetBytes(Str);
                 * stream.Write(Buffer, 0, Buffer.Length);*/
                if (HttpServer.Router != null)
                {
                    try
                    {
                        var spl22 = currentRequest.Header.Split(new char[] { ' ', '?' }, StringSplitOptions.RemoveEmptyEntries)
                                    .ToArray();
                        var tpl = HttpServer.Router.GetData(currentRequest);

                        if (tpl != null)
                        {
                            var    bb   = tpl.Item1;
                            var    mime = tpl.Item2;
                            string Str  = $"HTTP/1.1 200 OK\nContent-type: {mime}\nContent-Length:" +
                                          bb.Length.ToString() + "\n\n";
                            byte[] Buffer = Encoding.UTF8.GetBytes(Str);

                            stream.Write(Buffer, 0, Buffer.Length);
                            stream.Write(bb, 0, bb.Length);
                            stream.Flush();
                            return(null);
                        }
                    }
                    catch (Exception ex)
                    {
                        string err = "<p>" + ex.Message + "</p><p>" + ex.StackTrace.ToString() + "</p>";
                        string Str = "HTTP/1.1 200 OK\nContent-type: text/html\nContent-Length:" +
                                     err.Length.ToString() + "\n\n" + err;

                        byte[] Buffer = Encoding.UTF8.GetBytes(Str);
                        stream.Write(Buffer, 0, Buffer.Length);
                        return(null);
                    }
                }

                var spl = currentRequest.Header.Split(new char[] { '/', ' ', '?' }, StringSplitOptions.RemoveEmptyEntries)
                          .ToArray();
                var splh = currentRequest.Header.Split(new string[] { "POST", "GET", "HTTP", " ", "?" }, StringSplitOptions.RemoveEmptyEntries)
                           .ToArray();


                string path = spl[1];
                path = splh[0];

                Console.WriteLine($"[{ip}] request: " + path);
                HttpServer.Log($"[{ip}] request: " + path);

                var ctx = new SimpleHttpContext();

                if (HtmlGenerator.CodeTypes.ContainsKey(path))
                {
                    ctx.CodeType = HtmlGenerator.CodeTypes[path];
                }
                else
                {
                    ctx.CodeType = HtmlGenerator.DefaultCodeType;
                }
                if (currentRequest.Header.Contains("?"))
                {
                    var query = spl[2];
                    query = Http​Utility.UrlDecode(query);
                    ctx.ParseQuery(query);
                }
                ctx.Page    = Activator.CreateInstance(ctx.CodeType) as HttpPage;
                ctx.Request = currentRequest;
                ctx.IP      = ip;
                ctx.Page.OnPageLoad(ctx);
            }
            else if (currentRequest.Method == "GET")
            {
                var spl = currentRequest.Header.Split(new char[] { '/', ' ', '?' }, StringSplitOptions.RemoveEmptyEntries)
                          .ToArray();
                var splh = currentRequest.Header.Split(new string[] { "GET", "HTTP", " ", "?" }, StringSplitOptions.RemoveEmptyEntries)
                           .ToArray();

                string Html = "<html><body><h1>It works!</h1></body></html>";
                string path = spl[1];
                path = splh[0];

                if (spl[1] == "HTTP")
                {
                    path = "";
                }

                if (string.IsNullOrEmpty(path))
                {
                    path = "Index.htm";
                }

                Console.WriteLine($"[{ip}] request: " + path);
                HttpServer.Log($"[{ip}] request: " + path);

                Logger?.Log("domain dir: " + System.AppDomain.CurrentDomain.BaseDirectory);
                Logger?.Log("path: " + path);
                Logger?.Log("abs path:" + HtmlGenerator.GetAbsolutePath(path));
                Logger?.Log("exist: " + File.Exists(HtmlGenerator.GetAbsolutePath(path)));

                //check router (clean url)

                bool handled = false;
                var  cc      = currentRequest.Raw.FirstOrDefault(z => z.StartsWith("Cookie"));
                if (cc != null)
                {
                    var spl2     = cc.Split(new char[] { ':', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                    var cookieId = spl2[1];
                    currentRequest.Cookie = cookieId;
                }
                if (HttpServer.Router != null)
                {
                    try
                    {
                        //var spl22 = currentRequest.Header.Split(new char[] { ' ', '?' }, StringSplitOptions.RemoveEmptyEntries)
                        //.ToArray();
                        var pth = HttpServer.Router.GetPath(currentRequest);
                        if (pth != null)
                        {
                            path = pth;
                        }
                        else
                        {
                            var tpl = HttpServer.Router.GetData(currentRequest);

                            if (tpl != null)
                            {
                                var    bb   = tpl.Item1;
                                var    mime = tpl.Item2;
                                string Str  = $"HTTP/1.1 200 OK\nContent-type: {mime}; charset=utf-8\nContent-Length:" +
                                              bb.Length.ToString() + "\n\n";
                                byte[] Buffer = Encoding.UTF8.GetBytes(Str);

                                stream.Write(Buffer, 0, Buffer.Length);
                                stream.Write(bb, 0, bb.Length);
                                stream.Flush();
                                return(null);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        string err = "<p>" + ex.Message + "</p><p>" + ex.StackTrace.ToString() + "</p>";
                        string Str = "HTTP/1.1 200 OK\nContent-type: text/html; charset=utf-8\nContent-Length:" +
                                     err.Length.ToString() + "\n\n" + err;

                        byte[] Buffer = Encoding.UTF8.GetBytes(Str);
                        stream.Write(Buffer, 0, Buffer.Length);
                        stream.Flush();
                        return(null);
                    }
                }

                foreach (var item in Mimes)
                {
                    var p1 = (HtmlGenerator.GetAbsolutePath(path));
                    if (path.EndsWith(item.Extension) && File.Exists(p1))
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("HTTP/1.1 200 OK\n");

                        ResourceInfo res = null;
                        if (item.NeverExpirePolicy && UseExpires)
                        {
                            string expDate = (DateTime.Now.AddDays(365)).ToUniversalTime().ToString("r");
                            sb.Append($"Expires: {expDate}\n");
                        }
                        bool gzip = false;
                        if (currentRequest.Raw.Any(z => z.Contains("gzip")))
                        {
                            sb.Append($"Content-Encoding: gzip\n");
                            gzip = true;
                        }
                        sb.Append($"Content-type: {item.Mime}; charset=utf-8\n");

                        var bb = File.ReadAllBytes(HtmlGenerator.GetAbsolutePath(path));
                        if (gzip)
                        {
                            var bres = Zip(bb);

                            sb.Append($"Content-Length:{bres.Length}\n\n");
                            byte[] Buffer = Encoding.UTF8.GetBytes(sb.ToString());
                            stream.Write(Buffer, 0, Buffer.Length);
                            stream.Write(bres, 0, bres.Length);
                        }
                        else
                        {
                            sb.Append($"Content-Length:{bb.Length}\n\n");
                            byte[] Buffer = Encoding.UTF8.GetBytes(sb.ToString());
                            stream.Write(Buffer, 0, Buffer.Length);
                            stream.Write(bb, 0, bb.Length);
                        }



                        stream.Flush();
                        return(null);
                    }
                }


                if (path.EndsWith("htm") && File.Exists(HtmlGenerator.GetAbsolutePath(path)))
                {
                    Stopwatch sw = Stopwatch.StartNew();
                    Html = HtmlGenerator.Get(HtmlGenerator.GetAbsolutePath(path));

                    SimpleHttpContext ctx = new SimpleHttpContext();

                    if (currentRequest.Header.Contains("?"))
                    {
                        var query = spl[2];
                        query = Http​Utility.UrlDecode(query);
                        ctx.ParseQuery(query);
                    }


                    //var cc = currentRequest.Raw.FirstOrDefault(z => z.StartsWith("Cookie"));
                    //if (cc != null)
                    //{
                    //    var spl2 = cc.Split(new char[] { ':', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                    //    var cookieId = spl2[1];
                    //    currentRequest.Cookie = cookieId;
                    //}

                    if (HtmlGenerator.CodeTypes.ContainsKey(path))
                    {
                        ctx.CodeType = HtmlGenerator.CodeTypes[path];
                    }
                    else
                    {
                        ctx.CodeType = HtmlGenerator.DefaultCodeType;
                    }
                    ctx.Page = Activator.CreateInstance(ctx.CodeType) as HttpPage;

                    try
                    {
                        ctx.Request = currentRequest;
                        ctx.IP      = ip;

                        Html = DynamicUpdate(Html, ctx);
                        if (ctx.Redirect)
                        {
                            StringBuilder sb = new StringBuilder();

                            sb.Append("HTTP/1.1 303 See other\r\n");
                            foreach (var hitem in ctx.Response.Headers)
                            {
                                sb.Append($"{hitem.Key}: {hitem.Value}\r\n");
                            }
                            sb.Append("Location: " + ctx.RedirectPath + "\r\n\r\n");
                            //sb.Append("Refresh: 0; url=" + ctx.RedirectPath + "\r\n");


                            byte[] Buffer = Encoding.UTF8.GetBytes(sb.ToString());
                            stream.Write(Buffer, 0, Buffer.Length);
                            stream.Close();
                        }
                        else
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append("HTTP/1.1 200 OK\n");
                            foreach (var hitem in ctx.Response.Headers)
                            {
                                sb.Append($"{hitem.Key}: {hitem.Value}\n");
                            }

                            //var len = Encoding.UTF8.GetBytes(Html).Length;

                            sb.Append("Content-type: text/html; charset=utf-8\n");
                            bool gzip = false;
                            if (currentRequest.Raw.Any(z => z.Contains("gzip")))
                            {
                                sb.Append($"Content-Encoding: gzip\n");
                                gzip = true;
                            }

                            if (gzip)
                            {
                                var bres = Zip(Html);

                                sb.Append($"Content-Length:{bres.Length}\n\n");
                                byte[] Buffer = Encoding.UTF8.GetBytes(sb.ToString());
                                stream.Write(Buffer, 0, Buffer.Length);
                                stream.Write(bres, 0, bres.Length);
                            }
                            else
                            {
                                byte[] Buffer2 = Encoding.UTF8.GetBytes(Html);
                                sb.Append($"Content-Length:{Buffer2.Length}\n\n");
                                byte[] Buffer = Encoding.UTF8.GetBytes(sb.ToString());
                                stream.Write(Buffer, 0, Buffer.Length);
                                stream.Write(Buffer2, 0, Buffer2.Length);
                            }


                            /*sb.Append("Content-Length:" + len.ToString() + "\n\n");
                             *
                             * sb.Append(Html);
                             *
                             * var Str = sb.ToString();
                             *
                             * byte[] Buffer = Encoding.UTF8.GetBytes(Str);
                             *
                             * stream.Write(Buffer, 0, Buffer.Length);*/
                            stream.Flush();
                            stream.Close();
                        }
                        return(ctx);
                    }
                    catch (Exception ex)
                    {
                        string err = "<p>" + ex.Message + "</p><p>" + ex.StackTrace.ToString() + "</p>";
                        string Str = "HTTP/1.1 200 OK\nContent-type: text/html; charset=utf-8\nContent-Length:" +
                                     err.Length.ToString() + "\n\n" + err;

                        byte[] Buffer = Encoding.UTF8.GetBytes(Str);
                        stream.Write(Buffer, 0, Buffer.Length);
                        stream.Flush();
                    }
                    finally
                    {
                        Console.WriteLine($"[{ip}] htm processed within: " + sw.ElapsedMilliseconds + "ms");
                        HttpServer.Log($"[{ip}] htm processed within: " + sw.ElapsedMilliseconds + "ms");
                    }
                }

                if (!handled)
                {
                    if (AllowAccessToUnhandledFiles)
                    {
                        try
                        {
                            if (File.Exists(HtmlGenerator.GetAbsolutePath(path)))
                            {
                                var bb = File.ReadAllBytes(HtmlGenerator.GetAbsolutePath(path));
                                HttpStuff.SendResponse(currentRequest.Stream, bb);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                    else
                    {
                        Console.WriteLine($"[{ip}] {path}: 404 Not Found");
                        StringBuilder sb = new StringBuilder();

                        string err = "<p>404. Not Found</p>";
                        string Str = "HTTP/1.1 404 Not Found\nContent-type: text/html; charset=utf-8\nContent-Length:" +
                                     err.Length.ToString() + "\n\n" + err;

                        byte[] Buffer = Encoding.UTF8.GetBytes(Str);
                        stream.Write(Buffer, 0, Buffer.Length);
                        stream.Flush();
                    }
                }
            }
            return(null);
        }