Html string representation. Using this class it safe to mix html and strings. Strings are automatically escaped when combined with html.
示例#1
0
        public override bool Apply(Request request)
        {
            if (request.Flags["block"] == false)
                return false;

            Html status = new Html ();
            Html hr = Html.Format ("<hr/>");
            foreach (Html h in request.GetTriggerHtml ())
                status += h + hr;

            request.Response = new BlockedResponse ("Blocked", status);
            return true;
        }
示例#2
0
        public override Response Status(NameValueCollection httpGet, Request request)
        {
            Html html = new Html ();

            if (httpGet ["return"] != null) {
                request.Response.ReplaceHeader ("Location", httpGet ["return"]);
                request.Response.HttpCode = HttpStatusCode.Redirect;
            }

            if (httpGet ["delete"] != null) {
                int item = int.Parse (httpGet ["delete"]);
                try {
                    listLock.EnterWriteLock ();
                    foreach (RegexFilter rf in filterList.ToArray ()) {
                        if (rf.GetHashCode () == item) {
                            filterList.Remove (rf);
                            foreach (KeyValuePair<string, List<RegexFilter>> pair in hashList) {
                                pair.Value.Remove (rf);
                            }
                        }
                    }
                } finally {
                    listLock.ExitWriteLock ();
                }

                SaveFilters ();
            }

            if (httpGet ["action"] != null) {
                RegexFilter filter = RegexFilter.Parse (httpGet ["pattern"], new Flags (httpGet ["flags"]));
                if (filter != null) {
                    try {
                        listLock.EnterWriteLock ();
                        AddFilter (filter);
                    } finally {
                        listLock.ExitWriteLock ();
                    }
                    SaveFilters ();
                }
            }

            html += Html.Format (@"
                <p>Patterns are in a subset of the AdBlock pattern.</p>
                <p>Example: http://example.com, http://*.example.com, *example.com* or ||example.com</p>
                <table>
                    <tr><th>Pattern</th><th>Flags</th><th></th></th>
                <form action=""?"" method=""get"">
                    <tr>
                        <td><input type=""text"" name=""pattern"" value="""" /></td>
                        <td><input type=""text"" name=""flags"" value=""block"" /></td>
                        <td><input type=""submit"" name=""action"" value=""Add"" /></td>
                    </tr>
                </form>");

            try {
                listLock.EnterReadLock ();

                html += Html.Format ("<h1>Block List</h1>");
                foreach (RegexFilter regex in filterList) {
                    html += Html.Format ("<tr><td>{0}</td><td>{1}</td><td><a href=\"?delete={2}\">delete</a></td></tr>", regex.Pattern, regex.Flags, regex.GetHashCode ());
                }
            } finally {
                listLock.ExitReadLock ();
            }

            html += Html.Format ("</table>");

            return WebUI.ResponseTemplate (ToString (), html);
        }
示例#3
0
 public HtmlData(Html html)
 {
     htmlBuffer = Encoding.UTF8.GetBytes (html.HtmlString);
 }
示例#4
0
        private Html Form(string fromHost, string toHost, string returnUrl)
        {
            Html returnHtml = new Html ();
            if (returnUrl != null)
                returnHtml = Html.Format (@"<input type=""hidden"" name=""return"" value=""{0}"" />", returnUrl);

            return Html.Format (@"
            <form action=""{0}"" method=""get"">
            {1}
            <tr>
            <td><input type=""text"" name=""from"" value=""{2}"" /></td>
            <td><input type=""text"" name=""to"" value=""{3}"" /></td>
            <td>
            <input type=""text"" name=""flags"" value="""" />
            <input type=""submit"" name=""action"" value=""Add custom flags"" />
            <br/>
            <input type=""submit"" name=""action"" value=""Pass"" />
            <input type=""submit"" name=""action"" value=""Fake"" />
            <input type=""submit"" name=""action"" value=""Clean"" />
            <input type=""submit"" name=""action"" value=""Remove"" />
            <input type=""submit"" name=""action"" value=""Block"" />
            </td>
            </tr>
            </form>", Filters.WebUI.FilterUrl (this), returnHtml, fromHost, toHost);
        }
示例#5
0
        public override Response Status(NameValueCollection httpGet, Request request)
        {
            Html html = new Html ();

            if (httpGet ["delete"] != null) {
                int item = int.Parse (httpGet ["delete"]);
                using (listLock.Write) {
                    foreach (RefererPair rp in watchlist.ToArray ()) {
                        if (rp.GetHashCode () == item)
                            watchlist.Remove (rp);
                    }
                }

                SaveFilters ();
            }

            if (httpGet ["clear"] != null) {
                using (listLock.Write) {
                    blocked.Clear ();
                }
            }

            if (httpGet ["action"] != null || httpGet ["flags"] != null) {
                RefererPair p = new RefererPair (httpGet ["from"], httpGet ["to"]);

                p.Flags.Set (httpGet ["flags"]);
                if (httpGet ["action"].Contains (" ") == false)
                    p.Flags.Set (httpGet ["action"]);

                using (listLock.Write) {
                    watchlist.Add (p);

                    foreach (RefererPair bp in blocked.ToArray ()) {
                        if (p.Match (bp))
                            blocked.Remove (bp);
                    }
                }
                SaveFilters ();
            }

            if (httpGet ["return"] != null) {
                Response resp = new Response (HttpStatusCode.Redirect, new Html ());
                resp.ReplaceHeader ("Location", httpGet ["return"]);
                return resp;
            }

            html += Html.Format (@"<h2>Blocked <a href=""?clear=yes"">clear</a></h2>");
            html += Html.Format ("<table><tr><th>From Domain</th><th>To Domain</th><th>Flags</th></tr>");
            html += Form ("", "");
            using (listLock.Read) {
                foreach (RefererPair pair in blocked) {
                    html += Form (pair);
                }
                html += Html.Format ("</table>");

                html += Html.Format ("<h2>Watchlist</h2>");

                html += Html.Format ("<table><tr><th>From Domain</th><th>To Domain</th><th>Flags</th><th>Delete</th></tr>");
                foreach (RefererPair pair in watchlist) {
                    html += Html.Format ("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td><a href=\"?delete={3}\">delete</a></td></tr>", pair.FromHost, pair.ToHost, pair.Flags, pair.GetHashCode ());
                }
                html += Html.Format ("</table>");
            }

            html += Html.Format (@"
            <div>
                <ul>
                    <li><strong>Pass</strong> Allow request to pass through unmodified</li>
                    <li><strong>Fake</strong> Change referer to the root of the target host</li>
                    <li><strong>Clean</strong> Change referer to the root of the source host</li>
                    <li><strong>Remove</strong> Remove the referer header</li>
                    <li><strong>Slow</strong> Do not modify the request but slow down the transfer speed</li>
                    <li><strong>Block</strong> Block the entire request</li>
                </ul>
                <p>From/To: Wildcard(*) allowed in start of domains, applies to subdomains only</p>
                <p>Example: *example.com matches xyz.example.com and example.com but not badexample.com</p>
            </div>");

            return WebUI.ResponseTemplate (ToString (), html);
        }
示例#6
0
        public void Block(string title, Html htmlMessage)
        {
            //Determine content type requested
            //			string cc = null;
            //			while (true) {
            //				if (Accept == null) {
            //					if (Uri.AbsolutePath.EndsWith (".js")) {
            //						cc = "application/x-javascript";
            //						break;
            //					}
            //					if (Uri.AbsolutePath.EndsWith (".css")) {
            //						cc = "text/css";
            //						break;
            //					}
            //					if (Uri.AbsolutePath.EndsWith (".png")) {
            //						cc = "image/png";
            //						break;
            //					}
            //					if (Uri.AbsolutePath.EndsWith (".js")) {
            //						cc = "application/javascript";
            //						break;
            //					}
            //				}
            //				if (Accept.StartsWith ("text/css")) {
            //					cc = "text/css";
            //					break;
            //				}
            //
            //				cc = "text/html";
            //				break;
            //			}

            Response = new BlockedResponse (title, htmlMessage);
        }