Exemplo n.º 1
0
        WebAnswer GetHtmlAnswer()
        {
            HtmlPageBuilder html;

            if (Request != null)
            {
                html = new HtmlPageBuilder(Request);
            }
            else
            {
                html = new HtmlPageBuilder();
                html.AddHeader(Properties.Resources.CaveWebServerHeaders);
                html.AddFooter($"&nbsp;<hr><address>{Server.ServerVersionString} Server at {NetTools.HostName}<br>&copy {Server.ServerCopyRight}</address>");
                string link = "/";
                html.Breadcrump.Add(new WebLink()
                {
                    Link = link, Text = Server.Title
                });
            }

            foreach (KeyValuePair <string, SerializerTable> t in tables)
            {
                SerializerTable table = t.Value;
                html.StartTable(table.Name, table.Layout);
                foreach (Row row in table.Rows)
                {
                    html.WriteRow(row);
                }
                html.CloseTable();
            }

            return(html.ToAnswer(lastMessage));
        }
Exemplo n.º 2
0
        void ExplainStruct(WebData data, Type t)
        {
            var html = new HtmlPageBuilder(data.Request);

            html.Breadcrump.Add(new WebLink()
            {
                Text = t.FullName
            });
            Bootstrap4 content = html.Content;
            var        layout  = RowLayout.CreateTyped(t);

            content.CardOpen($"<h2>Struct {t.Name}<h2><h4>Table {layout.Name}</h4>{layout.FieldCount} Fields, {t.AssemblyQualifiedName}");

            DocumentHtml(content, documentation.GetType(t), t.ToString());
            content.ListGroupOpen();
            int i = 0;

            foreach (FieldProperties field in layout.Fields)
            {
                XNetDocItem doc = documentation.GetField(t, field.Name.ToString());
                FieldHtml(content, i++, field, doc);
            }
            content.ListGroupClose();
            content.CardClose();
            content.AddHtml("&nbsp;");
            var message = WebMessage.Create("Explain " + t.Name, string.Format("Explain struct {0}", t.Name));

            data.Answer = html.ToAnswer(message);
        }
Exemplo n.º 3
0
        void ExplainEnum(WebData data, Type t)
        {
            var html = new HtmlPageBuilder(data.Request);

            html.Breadcrump.Add(new WebLink()
            {
                Text = t.FullName
            });
            Bootstrap4 content = html.Content;

            content.CardOpenText($"Enum {t.Name}");
            DocumentHtml(content, documentation.GetEnum(t), t.ToString());
            content.ListGroupOpen();
            int i = 0;

            foreach (object value in Enum.GetValues(t))
            {
                XNetDocItem doc = documentation.GetField(t, value.ToString());
                FieldHtml(content, i++, value, doc);
            }
            content.ListGroupClose();
            content.CardClose();
            content.AddHtml("&nbsp;");
            var message = WebMessage.Create("Explain " + t.Name, string.Format("Explain enum {0}", t.Name));

            data.Answer = html.ToAnswer(message);
        }
Exemplo n.º 4
0
        void GetStaticFileListing(WebData data)
        {
            string url     = Uri.UnescapeDataString(data.Request.DecodedUrl).Trim('/');
            string path    = FileSystem.Combine(StaticFilesPath, url);
            var    entries = new List <WebDirectoryEntry>();
            string root    = FileSystem.Combine(path, "..");

            if (FileSystem.IsRelative(root, StaticFilesPath))
            {
                FileSystemInfo info  = new DirectoryInfo(root);
                var            entry = new WebDirectoryEntry()
                {
                    DateTime = info.LastWriteTime,
                    Name     = "..",
                    Type     = WebDirectoryEntryType.Directory,
                    Link     = "/" + url + "/..",
                };
                entries.Add(entry);
            }
            if (FileSystem.IsRelative(path, StaticFilesPath))
            {
                foreach (string dir in Directory.GetDirectories(path))
                {
                    FileSystemInfo info  = new DirectoryInfo(dir);
                    var            entry = new WebDirectoryEntry()
                    {
                        DateTime = info.LastWriteTime,
                        Name     = info.Name,
                        Type     = WebDirectoryEntryType.Directory,
                        Link     = "/" + FileSystem.Combine('/', url, info.Name),
                    };
                    entries.Add(entry);
                }
                foreach (string file in Directory.GetFiles(path))
                {
                    var info  = new FileInfo(file);
                    var entry = new WebDirectoryEntry()
                    {
                        DateTime = info.LastWriteTime,
                        Size     = info.Length,
                        Name     = info.Name,
                        Type     = WebDirectoryEntryType.File,
                        Link     = "/" + FileSystem.Combine('/', url, info.Name),
                    };
                    entries.Add(entry);
                }
            }
            var pb = new HtmlPageBuilder(data.Request);

            pb.Content.CardOpenText($"File Listing:");
            pb.Content.ParagraphText($"{entries.Count} entries");
            pb.Content.TableOpen(new string[] { "Type", "Size", "Name" }, "table-striped table-responsive");
            foreach (WebDirectoryEntry entry in entries)
            {
                pb.Content.TableRowOpen();
                pb.Content.TableHtmlCell(Bootstrap4.GetBadge(entry.Type.ToString(), "badge-default"));
                pb.Content.TableCell(entry.Type == WebDirectoryEntryType.Directory ? string.Empty : entry.Size.FormatBinarySize());
                pb.Content.TableHtmlCell(Bootstrap4.GetLink(entry.Name, entry.Link));
                pb.Content.TableRowClose();
            }
            pb.Content.TableClose();
            pb.Content.CardClose();
            pb.Content.AddHtml("&nbsp;");
            data.Answer = pb.ToAnswer(WebMessage.Create("FileListing", "File listing retrieved."));
        }
Exemplo n.º 5
0
        void ExplainFunction(WebData data, WebServerMethod function)
        {
            var html = new HtmlPageBuilder(data.Request);
            {
                string   path  = string.Empty;
                string[] parts = function.FullPaths.First().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                int      last  = parts.Length - 1;
                for (int n = 0; n < parts.Length; n++)
                {
                    path += "/" + parts[n];
                    html.Breadcrump.Add(new WebLink()
                    {
                        Text = parts[n], Link = (n != last) ? $"/Explain?functions={path}" : $"/Explain?function={path}"
                    });
                }
            }

            Bootstrap4        content  = html.Content;
            WebServerAuthType authType = function.PageAttribute?.AuthType ?? WebServerAuthType.None;
            {
                string link = function.FullPaths.First();
                var    head = new Bootstrap4();
                if (authType != WebServerAuthType.None)
                {
                    // head.DivOpen(Bootstrap4.Item.float_right);
                    head.DivOpen(Bootstrap4.Item.float_right);
                    AddBadges(head, function.PageAttribute);
                    head.DivClose(Bootstrap4.Item.float_right);

                    // head.AddHtml("<br/>");
                }
                head.AddHtml("<h2>");
                head.AddHtml(function.Method.Name.SplitCamelCase().Join("&nbsp;"));
                if (function.Parameters.Length > 0)
                {
                    head.AddHtml(" (");
                    head.AddHtml(function.ParameterString());
                    head.AddHtml(")");
                }
                head.AddHtml("</h2>");

                head.DivOpen(Bootstrap4.Item.float_right);
                head.Link("html", link + ".html", "btn btn-sm btn-outline-primary");
                head.Link("json", link + ".json", "btn btn-sm btn-outline-primary");
                head.Link("xml", link + ".xml", "btn btn-sm btn-outline-primary");
                head.Link("plain", link + ".txt", "btn btn-sm btn-outline-primary");
                head.DivClose(Bootstrap4.Item.float_right);

                head.AddHtml(function.Method.DeclaringType.AssemblyQualifiedName);
                content.CardOpen(head.ToString());
            }
            XNetDocItem doc = documentation.GetMethod(function.Method);

            DocumentHtml(content, doc, function.IsAction ? "Generic action" : function.Method.ToString());
            content.ListGroupOpen();
            int i = 0;

            foreach (ParameterInfo parameter in function.Parameters)
            {
                if (parameter.ParameterType == typeof(WebData))
                {
                    continue;
                }

                ParameterHtml(content, i++, parameter, doc);
            }
            content.ListGroupClose();
            content.CardClose();
            content.AddHtml("&nbsp;");
            var message = WebMessage.Create("Explain " + function.Name, string.Format("Explain function {0}", function));

            data.Answer = html.ToAnswer(message);
        }
Exemplo n.º 6
0
        void ExplainFunctionList(WebData data)
        {
            var html = new HtmlPageBuilder(data.Request);

            IEnumerable <KeyValuePair <string, WebServerMethod> > paths = data.Server.RegisteredPaths;

            if (data.Request.Parameters.TryGetValue("functions", out string functions))
            {
                string   path  = string.Empty;
                string[] parts = functions.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                for (int n = 0; n < parts.Length; n++)
                {
                    path += "/" + parts[n];
                    html.Breadcrump.Add(new WebLink()
                    {
                        Text = parts[n], Link = $"/Explain?functions={path}"
                    });
                }
                paths = paths.Where(p => p.Key.StartsWith(functions));
            }

            Bootstrap4 content = html.Content;

            content.ListGroupOpen();
            int i = 0;

            ILookup <WebServerMethod, string> lookup = paths.ToLookup(p => p.Value, p => p.Key);

            foreach (IGrouping <WebServerMethod, string> item in lookup)
            {
                WebServerMethod function = item.Key;

                // if (item.Key == "/") continue;
                content.ListGroupItemOpen(i++ % 2 == 0 ? " list-group-item-info" : null);
                XNetDocItem doc = documentation.GetMethod(function.Method);

                content.AddHtml("<div style=\"width:100%\">");
                content.DivOpen(Bootstrap4.Item.row);
                WebServerAuthType authType = function.PageAttribute?.AuthType ?? WebServerAuthType.None;
                if (authType != WebServerAuthType.None)
                {
                    content.DivOpen(Bootstrap4.Item.col, "col-12 col-sm-auto flex-sm-last");
                    AddBadges(content, function.PageAttribute);
                    content.DivClose(Bootstrap4.Item.col);
                }
                content.DivOpen(Bootstrap4.Item.col);
                content.AddHtml("<h4>");
                content.AddHtml(function.Method.Name.SplitCamelCase().Join(" "));
                if (function.Parameters.Length > 0)
                {
                    content.AddHtml(" (");
                    content.AddHtml(function.ParameterString());
                    content.AddHtml(")");
                }
                content.AddHtml("</h4>");
                content.DivClose(Bootstrap4.Item.col);
                content.DivClose(Bootstrap4.Item.row);

                foreach (string path in item)
                {
                    content.DivOpen(Bootstrap4.Item.row);
                    content.DivOpen(Bootstrap4.Item.col);
                    content.Link(path, $"Explain?function={path}");
                    content.DivClose(Bootstrap4.Item.col);

                    content.DivOpen(Bootstrap4.Item.col, "col-12 col-sm-auto");
                    content.Link("html", path + ".html", "btn btn-sm btn-outline-primary");
                    content.Link("json", path + ".json", "btn btn-sm btn-outline-primary");
                    content.Link("xml", path + ".xml", "btn btn-sm btn-outline-primary");
                    content.Link("plain", path + ".txt", "btn btn-sm btn-outline-primary");
                    content.DivClose(Bootstrap4.Item.col);
                    content.DivClose(Bootstrap4.Item.row);
                }

                if (doc?.Summary != null)
                {
                    content.DivOpen(Bootstrap4.Item.row);
                    content.DivOpen(Bootstrap4.Item.col);
                    content.AddHtml("<strong>Description:</strong>");
                    int cdata = doc.Summary.IndexOf("<![CDATA[");
                    if (cdata > -1)
                    {
                        content.Text(doc.Summary.Substring(0, cdata));
                        content.AddHtml("<br/><code>");
                        string code     = doc.Summary.Substring(cdata + 9);
                        int    cdataEnd = code.IndexOf("]]>");
                        content.AddHtml(code.Substring(0, cdata));
                        content.AddHtml("</code>");
                        content.Text(doc.Summary.Substring(cdata + cdataEnd + 9 + 3));
                    }
                    else
                    {
                        content.Text(doc.Summary);
                    }
                    content.DivClose(Bootstrap4.Item.col);
                    content.DivClose(Bootstrap4.Item.row);
                }
                content.AddHtml("</div>");
                content.ListGroupItemClose();
            }
            content.ListGroupClose();
            content.AddHtml("&nbsp;");
            var message = WebMessage.Create("Explain", functions == null ? "Explain functions." : $"Explain {functions} functions.");

            data.Answer = html.ToAnswer(message);
        }