Просмотр исходного кода

Keep the browser responsive during slow page loads

Johannes Zellner 2 недель назад
Родитель
Сommit
b151797eca
6 измененных файлов с 418 добавлено и 264 удалено
  1. 18 7
      AGENTS.md
  2. 234 256
      browser.c
  3. 3 0
      browser_html.c
  4. 2 0
      inflate.c
  5. 2 0
      inflate.h
  6. 159 1
      ucmd_curl.c

+ 18 - 7
AGENTS.md

@@ -454,9 +454,13 @@ run area), so adding code no longer moves any address.
   inline `<img>` decoding.  Images are fetched one per main-loop iteration
   (so text and scrolling stay responsive) and only when their measured
   position is within the viewport plus 160 px; deferred images are loaded as
-  the page scrolls.  `uhttp` keep-alive reuses one TCP/TLS connection across a
-  batch of images and reconnects/retries once if the server drops it.  Page
-  and image buffers come from `SYS_MEM_ALLOC`; the DOM uses fixed static
+  the page scrolls.  Network work runs in a headless `curl __fetch` helper
+  process (`ucmd_curl.c`): it writes the raw body and a small metadata file
+  under `/share`, while the browser keeps drawing and scrolling its own
+  window.  The helper asks for identity encoding because inflating multi-MB
+  pages in the browser process stalled the UI; if a server still sends gzip,
+  the browser's inflate has a progress hook that pumps the UI.  Page and
+  image buffers come from `SYS_MEM_ALLOC`; the DOM uses fixed static
   arenas (2560 nodes, 128 KB attributes, 1792 rules), and page bodies are
   capped at 2 MB with CSS sheets up to 832 KB (six sheets).  Images are
   decoded once into a shared 24 MB arena and copied to a display bitmap at
@@ -997,10 +1001,17 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     the cached bitmap on every repaint.  Lazy image loading uses the last
     measured positions; adding an image can reflow the content above it, and
     images below the viewport stay as `[image]` until scrolled into range.
-    Video/audio elements are placeholders, and pages that depend on
-    `display:grid`/flex or CSS variables (tagesschau's player and ad slots,
-    for example) can show empty bordered boxes or miss colours.  Because the
-    default canvas is dark, a
+    Each network fetch starts a one-shot helper process and opens a fresh
+    TCP/TLS connection (the `uhttp` keep-alive path exists but the browser
+    does not use it across helper jobs), so many tiny resources have extra
+    process/connection overhead.  The helper deliberately requests identity
+    encoding, so a page that is only small when compressed can exceed the
+    2 MB page/832 KB CSS caps.  Video/audio elements are placeholders, and
+    pages that depend on `display:grid`/flex or CSS variables
+    (tagesschau's player and ad slots, for example) can show empty bordered
+    boxes or miss colours.  Bare `&` characters are now consumed by the
+    entity decoder instead of looping forever.  Because the default canvas is
+    dark, a
     page that sets dark text but relies on the browser's default white
     background can be hard to read; pages with their own full background and
     colour rules are unaffected.  TLS is phase A (no certificate

+ 234 - 256
browser.c

@@ -167,7 +167,6 @@ static unsigned short block_h[BR_MAX_NODES];
 static char li_marker[BR_MAX_NODES][6];
 
 /* one connection object; the browser fetches one resource at a time */
-static struct uhttp http;
 
 /* ---- tiny strings ---- */
 
@@ -660,6 +659,20 @@ static int trunc_write(void *ctx, const unsigned char *data, unsigned int len)
 	return 0;
 }
 
+static void worker_wait_ui(void);
+
+static void inflate_ui_pump(void *ctx)
+{
+	static unsigned int last;
+	unsigned int now = u_ticks_ms();
+
+	(void)ctx;
+	if ((int)(now - last) >= 50) {
+		last = now;
+		worker_wait_ui();
+	}
+}
+
 static int inflate_trunc(const unsigned char *src, unsigned int len,
 			 unsigned char *dst, unsigned int cap,
 			 unsigned int *outlen, int gzip, int *truncated)
@@ -681,6 +694,8 @@ static int inflate_trunc(const unsigned char *src, unsigned int len,
 	sink.write = trunc_write;
 	sink.ctx = &m;
 	inflate_init(&image_inflate_ws, &in, &sink);
+	image_inflate_ws.progress = inflate_ui_pump;
+	image_inflate_ws.progress_ctx = 0;
 	r = gzip ? inflate_gzip(&image_inflate_ws, &got) :
 		   inflate_zlib(&image_inflate_ws, &got);
 	*outlen = m.pos;
@@ -690,6 +705,189 @@ static int inflate_trunc(const unsigned char *src, unsigned int len,
 	return 0;
 }
 
+/* ---- headless fetch helper ------------------------------------------- *
+ *
+ * Network I/O runs in a separate ring-3 process (`curl __fetch`): it writes
+ * the raw response and a small metadata file under /share.  While it works,
+ * the browser keeps drawing and scrolling its own window, so a slow DNS/TLS
+ * handshake or a multi-megabyte body never freezes the UI.
+ */
+
+#define FETCH_REQ  "/share/bfreq"
+#define FETCH_BODY "/share/bfbody"
+#define FETCH_META "/share/bfmeta"
+
+struct fetch_meta {
+	int status;
+	int error;
+	int trunc;
+	unsigned int len;
+	char encoding[16];
+	char ctype[64];
+	char final[BR_URL_MAX];
+};
+
+static void draw(unsigned int *action);
+static int fetch_helper_tty = -2;
+static struct fetch_meta fetch_meta;
+
+static unsigned int meta_uint(char **pp)
+{
+	char *p = *pp;
+	unsigned int v = 0;
+
+	while (*p >= '0' && *p <= '9') {
+		v = v * 10 + (unsigned int)(*p - '0');
+		p++;
+	}
+	while (*p && *p != '\n')
+		p++;
+	if (*p == '\n')
+		p++;
+	*pp = p;
+	return v;
+}
+
+static void meta_str(char **pp, char *out, unsigned int cap)
+{
+	char *p = *pp;
+	unsigned int n = 0;
+
+	while (*p && *p != '\n' && n + 1 < cap)
+		out[n++] = *p++;
+	out[n] = '\0';
+	while (*p && *p != '\n')
+		p++;
+	if (*p == '\n')
+		p++;
+	*pp = p;
+}
+
+static void worker_wait_ui(void)
+{
+	unsigned int action = 0;
+
+	if (!ngui_begin(&ui))
+		return;
+	draw(&action);
+	if (!ngui_focus(&ui) && ngui_scroll_keys(&ui, &page_scroll))
+		ngui_invalidate(&ui);
+	ngui_end(&ui);
+}
+
+static int worker_fetch(const char *url, unsigned int cap,
+			struct fetch_meta *m, char *err, unsigned int errcap)
+{
+	static char req[512], meta[1024];
+	char *p;
+	unsigned int n = 0, deadline;
+	int rn, pid;
+	struct sys_stat st;
+
+	if (u_stat("/share", &st) != 0 || !st.is_dir ||
+	    u_stat("/ramfs/bin/curl", &st) != 0) {
+		s_copy(err, "network helper unavailable", errcap);
+		return -1;
+	}
+	s_append_dec(req, &n, cap, sizeof(req));
+	s_append(req, &n, " ", sizeof(req));
+	s_append(req, &n, url, sizeof(req));
+	s_append(req, &n, "\n", sizeof(req));
+	u_syscall1(SYS_UNLINK, (int)FETCH_META);
+	if (u_syscall3(SYS_WRITE_FILE, (int)FETCH_REQ, (int)req, (int)n) < 0) {
+		s_copy(err, "cannot queue network request", errcap);
+		return -1;
+	}
+	if (fetch_helper_tty == -2) {
+		int t = u_tty_create();
+
+		fetch_helper_tty = t > 0 ? t : 0;
+	}
+	pid = u_spawn("/ramfs/bin/curl", "__fetch", fetch_helper_tty, "/");
+	if (pid <= 0) {
+		s_copy(err, "cannot start network helper", errcap);
+		return -1;
+	}
+	deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS + 5000;
+	while (u_alive(pid)) {
+		worker_wait_ui();
+		if ((int)(u_ticks_ms() - deadline) >= 0) {
+			u_kill(pid);
+			s_copy(err, "network request timed out", errcap);
+			return -1;
+		}
+		u_sleep_ms(20);
+	}
+	rn = u_syscall3(SYS_READ_FILE, (int)FETCH_META, (int)meta,
+			(int)(sizeof(meta) - 1));
+	if (rn <= 0) {
+		s_copy(err, "network helper failed", errcap);
+		return -1;
+	}
+	meta[rn] = '\0';
+	p = meta;
+	m->status = (int)meta_uint(&p);
+	m->error = (int)meta_uint(&p);
+	m->trunc = (int)meta_uint(&p);
+	meta_str(&p, m->encoding, sizeof(m->encoding));
+	meta_str(&p, m->ctype, sizeof(m->ctype));
+	meta_str(&p, m->final, sizeof(m->final));
+	m->len = meta_uint(&p);
+	if (m->error) {
+		s_copy(err, uhttp_error_string(m->error), errcap);
+		return -1;
+	}
+	return 0;
+}
+
+static int fetch_remote(const char *url, unsigned int cap,
+			unsigned char *plain, unsigned int plaincap,
+			unsigned char *comp, unsigned int compcap,
+			struct fetch_meta *m, int *truncated,
+			char *err, unsigned int errcap)
+{
+	int rv, zr, was_trunc;
+
+	if (worker_fetch(url, cap, m, err, errcap) != 0)
+		return -1;
+	if (contains_ci(m->encoding, "gzip") ||
+	    contains_ci(m->encoding, "deflate")) {
+		unsigned int comp_len = 0, plen = 0;
+
+		rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)comp,
+				(int)compcap);
+		if (rv < 0) {
+			s_copy(err, "empty response", errcap);
+			return -1;
+		}
+		comp_len = (unsigned int)rv;
+		if (comp_len < m->len) {
+			s_copy(err, "compressed response is too large", errcap);
+			return -1;
+		}
+		if (contains_ci(m->encoding, "deflate"))
+			zr = inflate_trunc(comp, comp_len, plain, plaincap,
+					   &plen, 0, &was_trunc);
+		else
+			zr = inflate_trunc(comp, comp_len, plain, plaincap,
+					   &plen, 1, &was_trunc);
+		if (zr != 0) {
+			s_copy(err, "compressed response is corrupt", errcap);
+			return -1;
+		}
+		*truncated = m->trunc || was_trunc;
+		return (int)plen;
+	}
+	rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)plain,
+			(int)plaincap);
+	if (rv < 0) {
+		s_copy(err, "empty response", errcap);
+		return -1;
+	}
+	*truncated = m->trunc || ((unsigned int)rv < m->len);
+	return rv;
+}
+
 /* ---- HTTP / file fetch ---- */
 
 static int read_url(const char *url, unsigned int cap, unsigned char **out,
@@ -697,8 +895,9 @@ static int read_url(const char *url, unsigned int cap, unsigned char **out,
 		    unsigned int ctcap, char *final, unsigned int finalcap,
 		    char *err, unsigned int errcap, int *truncated)
 {
-	unsigned char *buf = 0;
-	unsigned int total = 0;
+	unsigned char *buf;
+	unsigned int len;
+	int tr = 0;
 	int rv;
 
 	*out = 0;
@@ -732,102 +931,28 @@ static int read_url(const char *url, unsigned int cap, unsigned char **out,
 		return 0;
 	}
 
-	http.user_agent = "nebos-browser/0.1";
-	rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
-	if (rv != 0) {
-		s_copy(err, uhttp_error_string(rv), errcap);
-		return -1;
-	}
-	if (ctype)
-		s_copy(ctype, http.resp.content_type, ctcap);
-	if (final && http.resp.final_url[0])
-		s_copy(final, http.resp.final_url, finalcap);
-	*is_local = 0;
-
-	if (http.resp.gzip ||
-	    contains_ci(http.resp.content_encoding, "gzip") ||
-	    contains_ci(http.resp.content_encoding, "deflate")) {
-		unsigned char *comp = image_io_buf;
-		unsigned int climit = cap;
-		unsigned char *plain;
-		unsigned int plen = 0;
-		int zr, was_trunc;
-
-		if (climit > BR_PAGE_COMP_MAX)
-			climit = BR_PAGE_COMP_MAX;
-		total = 0;
-		for (;;) {
-			rv = uhttp_read(&http, comp + total, climit - total,
-					UHTTP_IDLE_TIMEOUT_MS);
-			if (rv > 0) {
-				total += (unsigned int)rv;
-				if (total >= climit) {
-					*truncated = 1;
-					break;
-				}
-				continue;
-			}
-			if (rv < 0) {
-				s_copy(err, uhttp_error_string(rv), errcap);
-				uhttp_close(&http);
-				return -1;
-			}
-			break;
-		}
-		uhttp_close(&http);
-		plain = (unsigned char *)heap_alloc(cap, 16);
-		if (!plain) {
-			s_copy(err, "out of memory", errcap);
-			return -1;
-		}
-		if (contains_ci(http.resp.content_encoding, "deflate"))
-			zr = inflate_trunc(comp, total, plain, cap, &plen, 0,
-					   &was_trunc);
-		else
-			zr = inflate_trunc(comp, total, plain, cap, &plen, 1,
-					   &was_trunc);
-		if (zr != 0) {
-			s_copy(err, "compressed page is corrupt", errcap);
-			return -1;
-		}
-		if (was_trunc)
-			*truncated = 1;
-		*out = plain;
-		*outlen = plen;
-		return 0;
-	}
-
 	buf = (unsigned char *)heap_alloc(cap, 16);
 	if (!buf) {
 		s_copy(err, "out of memory", errcap);
-		uhttp_close(&http);
 		return -1;
 	}
-	for (;;) {
-		rv = uhttp_read(&http, buf + total, cap - total,
-				UHTTP_IDLE_TIMEOUT_MS);
-		if (rv > 0) {
-			total += (unsigned int)rv;
-			if (total >= cap) {
-				*truncated = 1;
-				break;
-			}
-			continue;
-		}
-		if (rv < 0) {
-			s_copy(err, uhttp_error_string(rv), errcap);
-			uhttp_close(&http);
-			return -1;
-		}
-		break;
-	}
-	uhttp_close(&http);
-	if (total == 0) {
+	len = (unsigned int)fetch_remote(url, cap, buf, cap,
+					 image_io_buf, BR_PAGE_COMP_MAX,
+					 &fetch_meta, &tr, err, errcap);
+	if ((int)len < 0)
+		return -1;
+	if (len == 0) {
 		s_copy(err, "empty response", errcap);
 		return -1;
 	}
+	if (ctype)
+		s_copy(ctype, fetch_meta.ctype, ctcap);
+	if (final && fetch_meta.final[0])
+		s_copy(final, fetch_meta.final, finalcap);
+	*is_local = 0;
 	*out = buf;
-	*outlen = total;
+	*outlen = len;
+	*truncated = tr;
 	return 0;
 }
 
@@ -838,7 +963,8 @@ static int fetch_image(const char *url, unsigned int cap,
 		       unsigned char **out, unsigned int *outlen,
 		       char *err, unsigned int errcap)
 {
-	unsigned int total = 0;
+	unsigned int len;
+	int tr = 0;
 	int rv;
 
 	*out = 0;
@@ -856,92 +982,29 @@ static int fetch_image(const char *url, unsigned int cap,
 		*outlen = (unsigned int)rv;
 		return 0;
 	}
-
-	http.user_agent = "nebos-browser/0.1";
-	rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
-	if (rv != 0) {
-		s_copy(err, uhttp_error_string(rv), errcap);
+	len = (unsigned int)fetch_remote(url, cap, image_file_buf, cap,
+					 image_comp_buf, BR_IMG_FILE_MAX,
+					 &fetch_meta, &tr, err, errcap);
+	if ((int)len < 0)
 		return -1;
-	}
-	if (http.resp.gzip ||
-	    contains_ci(http.resp.content_encoding, "gzip") ||
-	    contains_ci(http.resp.content_encoding, "deflate")) {
-		unsigned int plen = 0;
-		int zr;
-
-		total = 0;
-		for (;;) {
-			rv = uhttp_read(&http, image_comp_buf + total,
-					BR_IMG_FILE_MAX - total,
-					UHTTP_IDLE_TIMEOUT_MS);
-			if (rv > 0) {
-				total += (unsigned int)rv;
-				if (total >= BR_IMG_FILE_MAX)
-					break;
-				continue;
-			}
-			if (rv < 0) {
-				s_copy(err, uhttp_error_string(rv), errcap);
-				uhttp_close(&http);
-				return -1;
-			}
-			break;
-		}
-		uhttp_close(&http);
-		if (!total) {
-			s_copy(err, "empty image", errcap);
-			return -1;
-		}
-		if (contains_ci(http.resp.content_encoding, "deflate"))
-			zr = inflate_zlib_mem(image_comp_buf, total,
-					      image_file_buf, cap, &plen,
-					      &image_inflate_ws);
-		else
-			zr = inflate_gzip_mem(image_comp_buf, total,
-					      image_file_buf, cap, &plen,
-					      &image_inflate_ws);
-		if (zr != 0) {
-			s_copy(err, "compressed image is corrupt", errcap);
-			return -1;
-		}
-		*out = image_file_buf;
-		*outlen = plen;
-		return 0;
-	}
-
-	for (;;) {
-		rv = uhttp_read(&http, image_file_buf + total, cap - total,
-				UHTTP_IDLE_TIMEOUT_MS);
-		if (rv > 0) {
-			total += (unsigned int)rv;
-			if (total >= cap)
-				break;
-			continue;
-		}
-		if (rv < 0) {
-			s_copy(err, uhttp_error_string(rv), errcap);
-			uhttp_close(&http);
-			return -1;
-		}
-		break;
-	}
-	uhttp_close(&http);
-	if (!total) {
+	if (len == 0) {
 		s_copy(err, "empty image", errcap);
 		return -1;
 	}
 	*out = image_file_buf;
-	*outlen = total;
+	*outlen = len;
 	return 0;
 }
 
+
 /* Read one stylesheet into the shared static buffer.  CSS is parsed
  * immediately, so it does not need the page heap; this leaves the heap for
  * the 2 MB page body, the decode arena and the scaled image bitmaps. */
 static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
 		     char *err, unsigned int errcap)
 {
-	unsigned int total = 0;
+	unsigned int len;
+	int tr = 0;
 	int rv;
 
 	*out = 0;
@@ -957,99 +1020,22 @@ static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
 		*outlen = (unsigned int)rv;
 		return 0;
 	}
-
-	http.user_agent = "nebos-browser/0.1";
-	rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
-	if (rv != 0) {
-		s_copy(err, uhttp_error_string(rv), errcap);
+	len = (unsigned int)fetch_remote(url, BR_CSS_MAX, image_io_buf,
+					 BR_CSS_MAX,
+					 image_io_buf + BR_CSS_MAX,
+					 BR_CSS_COMP_MAX, &fetch_meta, &tr, err,
+					 errcap);
+	if ((int)len < 0)
 		return -1;
-	}
-	if (http.resp.gzip ||
-	    contains_ci(http.resp.content_encoding, "gzip") ||
-	    contains_ci(http.resp.content_encoding, "deflate")) {
-		unsigned char *comp = image_io_buf + BR_CSS_MAX;
-		unsigned int plen = 0;
-		int zr, was_trunc;
-
-		total = 0;
-		for (;;) {
-			rv = uhttp_read(&http, comp + total,
-					BR_CSS_COMP_MAX - total,
-					UHTTP_IDLE_TIMEOUT_MS);
-			if (rv > 0) {
-				total += (unsigned int)rv;
-				if (total >= BR_CSS_COMP_MAX)
-					break;
-				continue;
-			}
-			if (rv < 0) {
-				s_copy(err, uhttp_error_string(rv), errcap);
-				uhttp_close(&http);
-				return -1;
-			}
-			break;
-		}
-		uhttp_close(&http);
-		if (!total) {
-			s_copy(err, "empty stylesheet", errcap);
-			return -1;
-		}
-		if (contains_ci(http.resp.content_encoding, "deflate"))
-			zr = inflate_trunc(comp, total, image_io_buf,
-					   BR_CSS_MAX, &plen, 0, &was_trunc);
-		else
-			zr = inflate_trunc(comp, total, image_io_buf,
-					   BR_CSS_MAX, &plen, 1, &was_trunc);
-		if (zr != 0) {
-			s_copy(err, "stylesheet is corrupt", errcap);
-			return -1;
-		}
-		*out = image_io_buf;
-		*outlen = plen;
-		return 0;
-	}
-
-	for (;;) {
-		rv = uhttp_read(&http, image_io_buf + total,
-				BR_CSS_MAX - total, UHTTP_IDLE_TIMEOUT_MS);
-		if (rv > 0) {
-			total += (unsigned int)rv;
-			if (total >= BR_CSS_MAX)
-				break;
-			continue;
-		}
-		if (rv < 0) {
-			s_copy(err, uhttp_error_string(rv), errcap);
-			uhttp_close(&http);
-			return -1;
-		}
-		break;
-	}
-	uhttp_close(&http);
-	if (!total) {
+	if (len == 0) {
 		s_copy(err, "empty stylesheet", errcap);
 		return -1;
 	}
 	*out = image_io_buf;
-	*outlen = total;
+	*outlen = len;
 	return 0;
 }
 
-static void browser_net_open(void)
-{
-	http.keepalive = 0;
-	uhttp_close(&http);
-	http.keepalive = 1;
-}
-
-static void browser_net_close(void)
-{
-	http.keepalive = 0;
-	uhttp_close(&http);
-}
-
-/* ---- page loading ---- */
-
 static void redraw_now(void);
 
 static char error_page[1024];
@@ -1198,7 +1184,6 @@ static void preload_step(void)
 			preload_active = 0;
 			preload_pending = 1;
 			preload_scroll = page_scroll.offset;
-			browser_net_close();
 			set_status(post_status_text, post_status_error,
 				   post_status_ttl);
 			ngui_invalidate(&ui);
@@ -1229,7 +1214,6 @@ static void preload_step(void)
 
 	preload_active = 0;
 	preload_pending = 0;
-	browser_net_close();
 	set_status(post_status_text, post_status_error, post_status_ttl);
 	ngui_invalidate(&ui);
 }
@@ -1269,7 +1253,6 @@ static void load_page(const char *url, int local, int push)
 
 	final[0] = '\0';
 
-	browser_net_open();
 	heap_clear();
 	image_reset();
 	page_scroll.offset = 0;
@@ -1349,7 +1332,6 @@ static void load_page(const char *url, int local, int push)
 
 static void load_home(int push)
 {
-	browser_net_close();
 	heap_clear();
 	image_reset();
 	page_scroll.offset = 0;
@@ -2551,8 +2533,6 @@ void _start(const char *arg)
 	br_set_theme(ui.theme.text, ui.theme.bg, ui.theme.panel,
 		     ui.theme.accent);
 	br_set_viewport(BR_VIEWPORT_W);
-	uhttp_init(&http);
-	http.user_agent = "nebos-browser/0.1";
 	if (u_getcwd(cwd, sizeof(cwd)) != 0 || !cwd[0])
 		s_copy(cwd, "/", sizeof(cwd));
 	heap_clear();
@@ -2578,7 +2558,6 @@ void _start(const char *arg)
 			preload_scroll = page_scroll.offset;
 			preload_pending = 0;
 			preload_active = 1;
-			browser_net_open();
 		}
 		click_url[0] = '\0';
 		now = u_ticks_ms();
@@ -2630,7 +2609,6 @@ void _start(const char *arg)
 			break;
 		u_sleep_ms(10);
 	}
-	browser_net_close();
 	u_win_close();
 	u_exit(0);
 }

+ 3 - 0
browser_html.c

@@ -222,7 +222,10 @@ static void decode_entity(const char *s, unsigned int len, unsigned int *pos,
 	while (i < len && s[i] != ';' && i - start < 12)
 		i++;
 	if (i >= len || s[i] != ';') {
+		/* Not an entity: emit the '&' and advance past it.  Leaving
+		 * *pos unchanged here used to spin forever on bare '&'. */
 		emit(out, cap, n, '&');
+		*pos = start;
 		return;
 	}
 	if (s[start] == '#') {

+ 2 - 0
inflate.c

@@ -119,6 +119,8 @@ static int flush_out(struct inflate *z)
 	if (r)
 		return -1;
 	z->pending = 0;
+	if (z->progress)
+		z->progress(z->progress_ctx);
 	return 0;
 }
 

+ 2 - 0
inflate.h

@@ -34,6 +34,8 @@ struct inflate_huff {
 struct inflate {
 	struct inflate_src src;
 	struct inflate_sink sink;
+	void (*progress)(void *ctx);    /* optional UI/service pump */
+	void *progress_ctx;
 
 	unsigned int bitbuf;
 	int bitcnt;

+ 159 - 1
ucmd_curl.c

@@ -1,8 +1,12 @@
 /* curl: ring-3 command.  Fetch one http:// or https:// URL with the shared
  * uhttp client and dump the status line, headers and body to the terminal.
  *
+ * It also has a headless `__fetch` mode used by the browser: read
+ * /share/bfreq, fetch it, and write the body plus /share/bfmeta for the GUI
+ * process.  That keeps DNS/TLS/body waits out of the browser's UI loop.
+ *
  * The TLS and HTTP code lives in uhttp.c (shared with the browser); this file
- * only keeps the CLI and the verbose output hooks. */
+ * only keeps the CLI, the helper mode and the verbose output hooks. */
 
 #include "ucmd_lib.h"
 #include "uhttp.h"
@@ -50,6 +54,155 @@ static const struct uhttp_hooks hooks = {
 	.redirect = on_redirect,
 };
 
+
+/* ---- headless fetch helper for the browser ---------------------------- */
+
+#define FETCH_REQ   "/share/bfreq"
+#define FETCH_BODY  "/share/bfbody"
+#define FETCH_META  "/share/bfmeta"
+#define FETCH_MAX   (2u * 1024u * 1024u)
+
+static unsigned int c_len(const char *s)
+{
+	unsigned int n = 0;
+
+	while (s && s[n])
+		n++;
+	return n;
+}
+
+static void app_ch(char *d, unsigned int *n, char c, unsigned int cap)
+{
+	if (*n + 1 < cap)
+		d[(*n)++] = c;
+	d[*n] = '\0';
+}
+
+static void app_str(char *d, unsigned int *n, const char *s, unsigned int cap)
+{
+	while (s && *s)
+		app_ch(d, n, *s++, cap);
+}
+
+static void app_dec(char *d, unsigned int *n, unsigned int v, unsigned int cap)
+{
+	char tmp[12];
+	unsigned int i = 0, j;
+
+	do {
+		tmp[i++] = (char)('0' + v % 10);
+		v /= 10;
+	} while (v && i < sizeof(tmp));
+	for (j = 0; j < i; j++)
+		app_ch(d, n, tmp[i - 1 - j], cap);
+}
+
+static void fetch_fail(unsigned int status, int err, const char *ctype,
+		       const char *enc, const char *final)
+{
+	char meta[768];
+	unsigned int n = 0;
+
+	app_dec(meta, &n, status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_str(meta, &n, enc ? enc : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_str(meta, &n, ctype ? ctype : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_str(meta, &n, final ? final : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	u_syscall3(SYS_WRITE_FILE, (int)FETCH_META, (int)meta, (int)n);
+}
+
+static void fetch_once(void)
+{
+	static struct uhttp h;
+	char req[512];
+	char meta[768];
+	unsigned char *buf, *body;
+	unsigned int cap = 0, total = 0, n = 0;
+	unsigned int body_len = 0, body_trunc = 0;
+	unsigned int i, trunc = 0;
+	int rn, rv, err = 0;
+	const char *enc_out;
+
+	rn = u_syscall3(SYS_READ_FILE, (int)FETCH_REQ, (int)req,
+			(int)(sizeof(req) - 1));
+	if (rn <= 0)
+		u_exit(1);
+	req[rn] = '\0';
+	i = 0;
+	while (req[i] >= '0' && req[i] <= '9') {
+		cap = cap * 10 + (unsigned int)(req[i] - '0');
+		i++;
+	}
+	while (req[i] == ' ')
+		i++;
+	while (rn > 0 && (req[rn - 1] == '\n' || req[rn - 1] == '\r'))
+		req[--rn] = '\0';
+	if (i >= (unsigned int)rn || !req[i]) {
+		fetch_fail(0, -1, "", "", "");
+		u_exit(1);
+	}
+	if (cap == 0 || cap > FETCH_MAX)
+		cap = FETCH_MAX;
+
+	buf = (unsigned char *)u_mem_alloc(FETCH_MAX);
+	if (!buf) {
+		fetch_fail(0, -1, "", "", "");
+		u_exit(1);
+	}
+
+	uhttp_init(&h);
+	h.user_agent = "nebos-browser-fetch/0.1";
+	rv = uhttp_open(&h, req + i, 0, 0);
+	if (rv != 0) {
+		err = rv;
+		fetch_fail(0, err, h.resp.content_type,
+			   h.resp.content_encoding, h.resp.final_url);
+		uhttp_close(&h);
+		u_mem_free();
+		u_exit(1);
+	}
+
+	for (;;) {
+		rv = uhttp_read(&h, buf + total, cap - total,
+				UHTTP_IDLE_TIMEOUT_MS);
+		if (rv > 0) {
+			total += (unsigned int)rv;
+			if (total >= cap) {
+				trunc = 1;
+				break;
+			}
+			continue;
+		}
+		if (rv < 0) {
+			err = rv;
+		}
+		break;
+	}
+
+	body = buf;
+	body_len = total;
+	body_trunc = trunc;
+	enc_out = h.resp.content_encoding;
+	if (body_len)
+		u_syscall3(SYS_WRITE_FILE, (int)FETCH_BODY, (int)body,
+			   (int)body_len);
+
+	app_dec(meta, &n, (unsigned int)h.resp.status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, body_trunc, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_str(meta, &n, enc_out, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_str(meta, &n, h.resp.content_type, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_str(meta, &n, h.resp.final_url, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, body_len, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	u_syscall3(SYS_WRITE_FILE, (int)FETCH_META, (int)meta, (int)n);
+
+	uhttp_close(&h);
+	u_mem_free();
+	u_exit(err ? 1 : 0);
+}
+
 void _start(const char *arg)
 {
 	static struct uhttp h;
@@ -57,6 +210,11 @@ void _start(const char *arg)
 	unsigned int total = 0;
 	int rv;
 
+	if (arg && c_len(arg) == 7 && arg[0] == '_' &&
+	    arg[1] == '_' && arg[2] == 'f' && arg[3] == 'e' &&
+	    arg[4] == 't' && arg[5] == 'c' && arg[6] == 'h') {
+		fetch_once();
+	}
 	if (!arg || !arg[0]) {
 		u_puts("curl: usage curl <http|https://host[:port]/[path]>\n");
 		u_exit(1);