Hi folks,

I'm wondering whether surf should open local html files (file://*.html) as web 
pages instead of calling the download fn on them (which is the current behavior 
as of d068a3878). Could be useful for viewing e.g. local HTML documentation.

Attached is the patch that worked for me.

One possible drawback in this patch is that I'm making a guess based only on 
URI scheme (file://) and the extension (.htm/.html). It would be more correct 
to determine the actual MIME type of the file to open, but I barely know 
webkit2 api. I tried calling webkit_uri_response_get_mime_type on local HTML 
but it returns "application/x-zerosize" not "text/html".

Thanks
Oleg
>From d093e80e8b4795037713d85bf331e2be33ac49df Mon Sep 17 00:00:00 2001
From: Oleg Kostyuchenko <[email protected]>
Date: Thu, 20 Jun 2019 14:42:39 +0300
Subject: [PATCH] open local html files instead of downloading them

---
 surf.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/surf.c b/surf.c
index 2b54e3c..80b2c27 100644
--- a/surf.c
+++ b/surf.c
@@ -1687,6 +1687,24 @@ decideresource(WebKitPolicyDecision *d, Client *c)
 		return;
 	}
 
+	if (g_str_has_prefix(uri, "file://")) {
+		/* chop off the query/fragment part (RFC 3986) if any */
+		const gchar *uri_end = g_strstr_len(uri, -1, "?");
+
+		if (!uri_end)
+			uri_end = g_strstr_len(uri, -1, "#");
+
+		if (!uri_end)
+			uri_end = uri + strlen(uri);
+
+		if (!strncmp(uri_end - 5, ".html", 5) ||
+			!strncmp(uri_end - 4, ".htm", 4)) {
+			/* if this is an html file, open it locally */
+			webkit_policy_decision_use(d);
+			return;
+		}
+	}
+
 	if (!g_str_has_prefix(uri, "http://";)
 	    && !g_str_has_prefix(uri, "https://";)
 	    && !g_str_has_prefix(uri, "about:")
-- 
2.20.1

Reply via email to