[wiki] [sites] Add URL filtering patch for surf || Gregor Best
commit 9314c748abe049b3d216896c972d8ab6db8b3495
Author: Gregor Best <gbe_AT_unobtanium.de>
Date: Tue Oct 14 13:40:39 2014 +0200
Add URL filtering patch for surf
Signed-off-by: Gregor Best <gbe_AT_unobtanium.de>
diff --git a/surf.suckless.org/patches/surf-tip-url-filters.diff b/surf.suckless.org/patches/surf-tip-url-filters.diff
new file mode 100644
index 0000000..b591af2
--- /dev/null
+++ b/surf.suckless.org/patches/surf-tip-url-filters.diff
_AT_@ -0,0 +1,154 @@
+diff --git a/Makefile b/Makefile
+index a9d4d1d..75fb004 100644
+--- a/Makefile
++++ b/Makefile
+_AT_@ -18,7 +18,14 @@ options:
+ _AT_echo CC $<
+ _AT_${CC} -c ${CFLAGS} $<
+
+-${OBJ}: config.h config.mk
++${OBJ}: config.h config.mk filters_compiled
++
++filters_compiled: filters
++ sed -e '/^$$/d' -e 's|\|\\|g' -e 's|$$|",|' -e 's|^|"|' < filters > $_AT_
++
++filters:
++ _AT_echo creating $@ from filters.def
++ _AT_cp filters.def $@
+
+ config.h:
+ _AT_echo creating $@ from config.def.h
+_AT_@ -30,7 +37,7 @@ surf: ${OBJ}
+
+ clean:
+ _AT_echo cleaning
+- _AT_rm -f surf ${OBJ} surf-${VERSION}.tar.gz
++ _AT_rm -f surf ${OBJ} surf-${VERSION}.tar.gz filters_compiled
+
+ dist: clean
+ _AT_echo creating dist tarball
+diff --git a/config.def.h b/config.def.h
+index 80a0feb..3cc5131 100644
+--- a/config.def.h
++++ b/config.def.h
+_AT_@ -21,6 +21,13 @@ static char *cafile = "/etc/ssl/certs/ca-certificates.crt";
+ static char *strictssl = FALSE; /* Refuse untrusted SSL connections */
+ static time_t sessiontime = 3600;
+
++/* Regular expressions to match URLs that should not be loaded */
++char *filter_patterns[] = {
++#include "filters_compiled"
++};
++/* Define this for verbose filtering */
++// #define FILTER_VERBOSE
++
+ /* Webkit default features */
+ static Bool enablescrollbars = TRUE;
+ static Bool enablespatialbrowsing = TRUE;
+diff --git a/filters.def b/filters.def
+new file mode 100644
+index 0000000..77158de
+--- /dev/null
++++ b/filters.def
+_AT_@ -0,0 +1,2 @@
++^eviladvertisments\.com$
++/favicon\.ico$
+diff --git a/surf.c b/surf.c
+index 7c78b4a..8d24197 100644
+--- a/surf.c
++++ b/surf.c
+_AT_@ -20,6 +20,7 @@
+ #include <webkit/webkit.h>
+ #include <glib/gstdio.h>
+ #include <JavaScriptCore/JavaScript.h>
++#include <regex.h>
+ #include <sys/file.h>
+ #include <libgen.h>
+ #include <stdarg.h>
+_AT_@ -27,6 +28,7 @@
+ #include "arg.h"
+
+ char *argv0;
++regex_t *filter_expressions;
+
+ #define LENGTH(x) (sizeof x / sizeof x[0])
+ #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
+_AT_@ -119,6 +121,8 @@ static void destroyclient(Client *c);
+ static void destroywin(GtkWidget* w, Client *c);
+ static void die(const char *errstr, ...);
+ static void eval(Client *c, const Arg *arg);
++static bool filter_init(void);
++static bool filter_request(const gchar *uri);
+ static void find(Client *c, const Arg *arg);
+ static void fullscreen(Client *c, const Arg *arg);
+ static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
+_AT_@ -200,7 +204,7 @@ beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
+ gpointer d) {
+ const gchar *uri = webkit_network_request_get_uri(req);
+
+- if(g_str_has_suffix(uri, "/favicon.ico"))
++ if(filter_request(uri))
+ webkit_network_request_set_uri(req, "about:blank");
+ }
+
+_AT_@ -473,6 +477,49 @@ die(const char *errstr, ...) {
+ exit(EXIT_FAILURE);
+ }
+
++static bool
++filter_init(void) {
++ bool errors = false;
++ char *errorbuf;
++
++ errorbuf = malloc(sizeof(char) * BUFSIZ);
++ filter_expressions = malloc(sizeof(regex_t) * LENGTH(filter_patterns));
++
++ for (off_t idx = 0; idx < LENGTH(filter_patterns); idx++) {
++ char *pat = filter_patterns[idx];
++ int err = regcomp(&filter_expressions[idx], pat,
++ REG_EXTENDED | REG_ICASE | REG_NOSUB);
++ if (err != 0) {
++ /* regerror always ends messages with 0x00 */
++ (void) regerror(err, &filter_expressions[idx], errorbuf, BUFSIZ);
++ fprintf(stderr, "Failed to compile \"%s\": %s
", pat, errorbuf);
++ errors = true;
++ }
++ }
++
++ free(errorbuf);
++ return !errors;
++}
++
++static bool
++filter_request(const gchar *uri) {
++ if (!strcmp(uri, "about:blank"))
++ return false;
++ for (off_t idx = 0; idx < LENGTH(filter_patterns); idx++) {
++ if (regexec(&filter_expressions[idx], uri, 0, NULL, 0) == REG_NOMATCH) {
++ continue;
++ }
++#ifdef FILTER_VERBOSE
++ fprintf(stderr, "filtering \"%s\"
", uri);
++#endif
++ return true;
++ }
++#ifdef FILTER_VERBOSE
++ fprintf(stderr, "not filtering \"%s\"
", uri);
++#endif
++ return false;
++}
++
+ static void
+ find(Client *c, const Arg *arg) {
+ const char *s;
+_AT_@ -1152,6 +1199,10 @@ setup(void) {
+ g_free(new_proxy);
+ usingproxy = 1;
+ }
++
++ if (!filter_init()) {
++ die("Failed to compile one or more filter expressions
");
++ }
+ }
+
+ static void
diff --git a/surf.suckless.org/patches/url-filtering.md b/surf.suckless.org/patches/url-filtering.md
new file mode 100644
index 0000000..6190870
--- /dev/null
+++ b/surf.suckless.org/patches/url-filtering.md
_AT_@ -0,0 +1,25 @@
+URL filtering
+=============
+
+Description
+-----------
+
+This patch adds URL filtering support to surf, for example to remove
+advertisements. The file `filters` contains POSIX regular expressions (see the
+`re_format(7)` manpage). If a HTTP request is about to be made for a URL that
+matches any of the expressions in the file, it is replaced with `about:blank`
+instead. This may lead to slightly broken display on pages that expect e.g.
+images to load correctly. The impact is negligible though.
+
+Example
+-------
+
+An example list of filters looks like this:
+
+ /favicon\.ico$
+ eviladvertismentcompany\.{net,com}/ads
+
+Download
+--------
+
+* [surf-tip-url-filtering.diff](surf-tip-url-filtering.diff) (4176) (20141014)
Received on Tue Oct 14 2014 - 13:40:41 CEST
This archive was generated by hypermail 2.3.0
: Thu Jun 18 2015 - 17:39:41 CEST