[hackers] [surf][PATCH] support blob download, fix download referer

From: nzl <uruabi_AT_gmail.com>
Date: Thu, 03 Jan 2019 20:02:55 +0000

* external downloaders can't handle blob urls
* multiple webviews share one webcontext, the actuall client initiated
  a download might not be the one passed to downloadstarted. we can get
  the source webview from WebKitDownload.
* added new config variable downdir for saving blobs to, could also pass it
  as an argument to external downloaders, and cd to it before download.
---
 surf.c | 58 ++++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 44 insertions(+), 14 deletions(-)
diff --git a/surf.c b/surf.c
index d48fbc9..efec8c0 100644
--- a/surf.c
+++ b/surf.c
_AT_@ -205,10 +205,10 @@ static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
 static void decideresource(WebKitPolicyDecision *d, Client *c);
 static void insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e,
                             Client *c);
-static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
-                            Client *c);
-static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
-static void download(Client *c, WebKitURIResponse *r);
+static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d, gpointer unused);
+static void responsereceived(WebKitDownload *d, GParamSpec *ps, gpointer unused);
+static void decidedownloadpath(WebKitDownload *d, gchar *name, gpointer unused);
+static void download(const char *uri, const char *referer);
 static void webprocessterminated(WebKitWebView *v,
                                  WebKitWebProcessTerminationReason r,
                                  Client *c);
_AT_@ -340,6 +340,7 @@ setup(void)
 	scriptfile = buildfile(scriptfile);
 	cachedir   = buildpath(cachedir);
 	certdir    = buildpath(certdir);
+	downdir    = buildpath(downdir);
 
 	gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
 
_AT_@ -1708,8 +1709,7 @@ decideresource(WebKitPolicyDecision *d, Client *c)
 	if (webkit_response_policy_decision_is_mime_type_supported(r)) {
 		webkit_policy_decision_use(d);
 	} else {
-		webkit_policy_decision_ignore(d);
-		download(c, res);
+		webkit_policy_decision_download(d);
 	}
 }
 
_AT_@ -1720,24 +1720,54 @@ insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, Client *c)
 }
 
 void
-downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
+downloadstarted(WebKitWebContext *wc, WebKitDownload *d, gpointer unused)
 {
 	g_signal_connect(G_OBJECT(d), "notify::response",
-	                 G_CALLBACK(responsereceived), c);
+	                 G_CALLBACK(responsereceived), NULL);
 }
 
 void
-responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
+responsereceived(WebKitDownload *d, GParamSpec *ps, gpointer unused)
 {
-	download(c, webkit_download_get_response(d));
-	webkit_download_cancel(d);
+	WebKitURIResponse *r;
+	const char *uri, *referer;
+
+	r = webkit_download_get_response(d);
+	uri = webkit_uri_response_get_uri(r);
+	if (g_str_has_prefix(uri, "blob:")) {
+		g_signal_connect(G_OBJECT(d), "decide-destination",
+				 G_CALLBACK(decidedownloadpath), NULL);
+	} else {
+		referer = webkit_web_view_get_uri(webkit_download_get_web_view(d));
+		if (!referer)
+			referer = "";
+		download(uri, referer);
+		webkit_download_cancel(d);
+	}
 }
 
 void
-download(Client *c, WebKitURIResponse *r)
+decidedownloadpath(WebKitDownload *d, gchar *name, gpointer unused)
 {
-	Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
-	spawn(c, &a);
+	gchar *uri, *path;
+
+	path = g_build_filename(downdir, name, NULL);
+	while (g_file_test(path, G_FILE_TEST_EXISTS)) {
+		name = path;
+		path = g_strdup_printf("%s.", path);
+		g_free(name);
+	}
+	uri = g_filename_to_uri(path, NULL, NULL);
+	g_free(path);
+	webkit_download_set_destination(d, uri);
+	g_free(uri);
+}
+
+void
+download(const char *uri, const char *referer)
+{
+	Arg a = (Arg)DOWNLOAD(uri, referer);
+	spawn(NULL, &a);
 }
 
 void
-- 
2.19.1
Received on Thu Jan 03 2019 - 21:02:55 CET

This archive was generated by hypermail 2.3.0 : Thu Jan 03 2019 - 21:12:21 CET