Parcourir la source

Load images lazily and reuse HTTP connections

Johannes Zellner il y a 2 semaines
Parent
commit
37c35084cf
4 fichiers modifiés avec 268 ajouts et 72 suppressions
  1. 17 10
      AGENTS.md
  2. 168 35
      browser.c
  3. 81 27
      uhttp.c
  4. 2 0
      uhttp.h

+ 17 - 10
AGENTS.md

@@ -451,8 +451,12 @@ run area), so adding code no longer moves any address.
   `ngui_theme_dark` (dark page, light text, `theme.panel` for `<pre>`);
   `ngui_theme_dark` (dark page, light text, `theme.panel` for `<pre>`);
   a site's own `background-color`/`color` CSS still overrides it.  `browser.c`
   a site's own `background-color`/`color` CSS still overrides it.  `browser.c`
   owns a 960x640 window, the layout/render, URL bar, history, status bar and
   owns a 960x640 window, the layout/render, URL bar, history, status bar and
-  inline `<img>` decoding.
-  Page and image buffers come from `SYS_MEM_ALLOC`; the DOM uses fixed static
+  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
   arenas (2560 nodes, 128 KB attributes, 1792 rules), and page bodies are
   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
   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
   decoded once into a shared 24 MB arena and copied to a display bitmap at
@@ -986,14 +990,17 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     desktop rules, not their mobile layout), and pseudo-classes are never
     desktop rules, not their mobile layout), and pseudo-classes are never
     matched.  `font-size` maps to 1x/2x only, `em`/`rem` both use a 16 px
     matched.  `font-size` maps to 1x/2x only, `em`/`rem` both use a 16 px
     root, and block `width`/`max-width` are not used for centring.  Images
     root, and block `width`/`max-width` are not used for centring.  Images
-    are decoded eagerly into a 24 MB shared arena and downscaled to a 1000 px
-    display bitmap (16 images, 512 KB files / 8 MP decodes); a large
-    image-heavy page can still exhaust the 32 MB heap, and the first `<img>`
-    after a big screenshot can transiently need a new heap growth.  `img_draw`
-    rescales the cached bitmap on every repaint.  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
+    are decoded into a 24 MB shared arena and downscaled to a 1000 px display
+    bitmap (16 images, 512 KB files / 8 MP decodes); a large image-heavy page
+    can still exhaust the 32 MB heap, and the first `<img>` after a big
+    screenshot can transiently need a new heap growth.  `img_draw` rescales
+    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
     page that sets dark text but relies on the browser's default white
     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
     background can be hard to read; pages with their own full background and
     colour rules are unaffected.  TLS is phase A (no certificate
     colour rules are unaffected.  TLS is phase A (no certificate

+ 168 - 35
browser.c

@@ -71,6 +71,23 @@ static struct br_doc doc;
 static struct br_image images[BR_MAX_IMAGES];
 static struct br_image images[BR_MAX_IMAGES];
 static int image_count;
 static int image_count;
 
 
+/* Images are loaded one per main-loop iteration so the parsed page stays
+ * interactive while a slow network/TLS fetch or 4K decode is in progress. */
+static int preload_active;
+static int preload_pending;
+static int preload_wait;
+static unsigned int preload_node;
+static int preload_loaded;
+static int preload_scroll;
+static char post_status_text[BR_STATUS_MAX];
+static int post_status_error;
+static unsigned int post_status_ttl;
+
+/* Content-relative position/height of each <img>, recorded by the measure
+ * pass so image loading can be deferred until it is near the viewport. */
+static unsigned short img_top[BR_MAX_NODES];
+static unsigned short img_h[BR_MAX_NODES];
+
 static unsigned char *heap;
 static unsigned char *heap;
 static unsigned int heap_size = 0;
 static unsigned int heap_size = 0;
 static unsigned int heap_used = 0;
 static unsigned int heap_used = 0;
@@ -329,6 +346,11 @@ static void image_reset(void)
 		images[i].err[0] = '\0';
 		images[i].err[0] = '\0';
 	}
 	}
 	image_count = 0;
 	image_count = 0;
+	preload_active = 0;
+	preload_pending = 0;
+	preload_wait = 0;
+	preload_node = 0;
+	preload_loaded = 0;
 }
 }
 
 
 /* Decode into the shared arena, then copy/scale the result into a display
 /* Decode into the shared arena, then copy/scale the result into a display
@@ -710,7 +732,6 @@ static int read_url(const char *url, unsigned int cap, unsigned char **out,
 		return 0;
 		return 0;
 	}
 	}
 
 
-	uhttp_init(&http);
 	http.user_agent = "nebos-browser/0.1";
 	http.user_agent = "nebos-browser/0.1";
 	rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
 	rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
 	if (rv != 0) {
 	if (rv != 0) {
@@ -836,7 +857,6 @@ static int fetch_image(const char *url, unsigned int cap,
 		return 0;
 		return 0;
 	}
 	}
 
 
-	uhttp_init(&http);
 	http.user_agent = "nebos-browser/0.1";
 	http.user_agent = "nebos-browser/0.1";
 	rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
 	rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
 	if (rv != 0) {
 	if (rv != 0) {
@@ -938,7 +958,6 @@ static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
 		return 0;
 		return 0;
 	}
 	}
 
 
-	uhttp_init(&http);
 	http.user_agent = "nebos-browser/0.1";
 	http.user_agent = "nebos-browser/0.1";
 	rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
 	rv = uhttp_open(&http, url, UHTTP_GZIP, 0);
 	if (rv != 0) {
 	if (rv != 0) {
@@ -1016,6 +1035,19 @@ static int fetch_css(const char *url, unsigned char **out, unsigned int *outlen,
 	return 0;
 	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 ---- */
 /* ---- page loading ---- */
 
 
 static void redraw_now(void);
 static void redraw_now(void);
@@ -1046,6 +1078,9 @@ static void show_error(const char *title, const char *detail)
 	s_append(page, &n, "</p><p><a href=\"/\">home</a></p></body></html>",
 	s_append(page, &n, "</p><p><a href=\"/\">home</a></p></body></html>",
 		 sizeof(error_page));
 		 sizeof(error_page));
 	br_parse(&doc, page, n);
 	br_parse(&doc, page, n);
+	preload_active = 0;
+	preload_pending = 0;
+	preload_wait = 0;
 	s_copy(page_title, title, sizeof(page_title));
 	s_copy(page_title, title, sizeof(page_title));
 	page_loading = 0;
 	page_loading = 0;
 	set_status(title, 1, 0);
 	set_status(title, 1, 0);
@@ -1108,12 +1143,23 @@ static void load_stylesheets(void)
 	br_reset_styles(&doc);
 	br_reset_styles(&doc);
 }
 }
 
 
-static void preload_images(void)
+static void preload_begin(void)
 {
 {
-	unsigned int i;
-	int loaded = 0;
+	preload_active = 1;
+	preload_pending = 0;
+	preload_wait = 1;
+	preload_node = 0;
+	preload_loaded = 0;
+	preload_scroll = 0;
+}
 
 
-	for (i = 0; i < doc.nnode && loaded < BR_MAX_IMAGES; i++) {
+/* Load at most one image per call.  Images below the current viewport are
+ * left pending until the page is scrolled; this keeps a long article with
+ * many 4K screenshots from stalling on content the user cannot see yet. */
+static void preload_step(void)
+{
+	while (preload_active && preload_node < doc.nnode) {
+		unsigned int i = preload_node;
 		struct br_node *nd = &doc.nodes[i];
 		struct br_node *nd = &doc.nodes[i];
 		const char *src;
 		const char *src;
 		char url[BR_URL_MAX];
 		char url[BR_URL_MAX];
@@ -1122,35 +1168,70 @@ static void preload_images(void)
 		char err[64];
 		char err[64];
 		char msg[64];
 		char msg[64];
 		unsigned int n = 0;
 		unsigned int n = 0;
+		int rv;
 
 
 		if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_IMG ||
 		if (nd->type != BR_NODE_ELEM || nd->tag != BR_TAG_IMG ||
-		    nd->img_index >= 0)
+		    nd->img_index >= 0) {
+			preload_node++;
 			continue;
 			continue;
-		if (ancestor_is(i, BR_TAG_NOSCRIPT))
+		}
+		if (ancestor_is(i, BR_TAG_NOSCRIPT)) {
+			preload_node++;
 			continue;
 			continue;
+		}
 		src = br_src(&doc, i);
 		src = br_src(&doc, i);
 		if (!src[0] || contains_ci(src, ".svg") ||
 		if (!src[0] || contains_ci(src, ".svg") ||
-		    starts_with(src, "data:"))
+		    starts_with(src, "data:")) {
+			preload_node++;
 			continue;
 			continue;
-		if (resolve_link(page_url, src, url, sizeof(url)) != 0)
+		}
+		if (resolve_link(page_url, src, url, sizeof(url)) != 0) {
+			preload_node++;
 			continue;
 			continue;
+		}
+		if (image_count >= BR_MAX_IMAGES) {
+			preload_node = doc.nnode;
+			break;
+		}
+		if (img_top[i] >
+		    (unsigned int)(page_scroll.offset + page_rect.h + 160)) {
+			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);
+			return;
+		}
+		preload_node = i + 1;
+
 		s_append(msg, &n, "Loading images ", sizeof(msg));
 		s_append(msg, &n, "Loading images ", sizeof(msg));
-		s_append_dec(msg, &n, (unsigned int)loaded + 1, sizeof(msg));
+		s_append_dec(msg, &n, (unsigned int)preload_loaded + 1,
+			     sizeof(msg));
 		s_append(msg, &n, "...", sizeof(msg));
 		s_append(msg, &n, "...", sizeof(msg));
-		s_copy(status_text, msg, sizeof(status_text));
-		status_until = 0;
-		status_error = 0;
-		redraw_now();
+		set_status(msg, 0, 0);
+		ngui_invalidate(&ui);
 
 
-		if (fetch_image(url, BR_IMG_FILE_MAX, &data, &len, err,
-				sizeof(err)) != 0)
-			continue;
-		if (decode_image_data(data, len, &images[image_count]) != 0)
-			continue;
-		nd->img_index = (short)image_count;
-		image_count++;
-		loaded++;
+		data = 0;
+		len = 0;
+		rv = fetch_image(url, BR_IMG_FILE_MAX, &data, &len, err,
+				 sizeof(err));
+		if (rv == 0 &&
+		    decode_image_data(data, len, &images[image_count]) == 0) {
+			nd->img_index = (short)image_count;
+			image_count++;
+			preload_loaded++;
+		}
+		ngui_invalidate(&ui);
+		return;
 	}
 	}
+
+	preload_active = 0;
+	preload_pending = 0;
+	browser_net_close();
+	set_status(post_status_text, post_status_error, post_status_ttl);
+	ngui_invalidate(&ui);
 }
 }
 
 
 static void history_push(const char *url)
 static void history_push(const char *url)
@@ -1188,6 +1269,7 @@ static void load_page(const char *url, int local, int push)
 
 
 	final[0] = '\0';
 	final[0] = '\0';
 
 
+	browser_net_open();
 	heap_clear();
 	heap_clear();
 	image_reset();
 	image_reset();
 	page_scroll.offset = 0;
 	page_scroll.offset = 0;
@@ -1238,16 +1320,24 @@ static void load_page(const char *url, int local, int push)
 		s_copy(page_title, doc.title[0] ? doc.title : page_url,
 		s_copy(page_title, doc.title[0] ? doc.title : page_url,
 		       sizeof(page_title));
 		       sizeof(page_title));
 		load_stylesheets();
 		load_stylesheets();
-		preload_images();
-	}
-	if (truncated)
-		set_status("Page truncated at the buffer limit", 0, 6000);
-	else if (doc.truncated)
-		set_status("Page truncated by the parser", 0, 6000);
-	else if (starts_with(page_url, "https://"))
-		set_status("HTTPS: certificate NOT verified", 0, 6000);
-	else
-		set_status("", 0, 0);
+	}
+	post_status_text[0] = '\0';
+	post_status_error = 0;
+	post_status_ttl = 0;
+	if (truncated) {
+		s_copy(post_status_text, "Page truncated at the buffer limit",
+		       sizeof(post_status_text));
+		post_status_ttl = 6000;
+	} else if (doc.truncated) {
+		s_copy(post_status_text, "Page truncated by the parser",
+		       sizeof(post_status_text));
+		post_status_ttl = 6000;
+	} else if (starts_with(page_url, "https://")) {
+		s_copy(post_status_text, "HTTPS: certificate NOT verified",
+		       sizeof(post_status_text));
+		post_status_ttl = 6000;
+	}
+	preload_begin();
 	page_loading = 0;
 	page_loading = 0;
 	s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
 	s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
 	url_in.len = s_len(url_in.text);
 	url_in.len = s_len(url_in.text);
@@ -1259,6 +1349,7 @@ static void load_page(const char *url, int local, int push)
 
 
 static void load_home(int push)
 static void load_home(int push)
 {
 {
+	browser_net_close();
 	heap_clear();
 	heap_clear();
 	image_reset();
 	image_reset();
 	page_scroll.offset = 0;
 	page_scroll.offset = 0;
@@ -1305,6 +1396,9 @@ static void load_home(int push)
 		br_reset_styles(&doc);
 		br_reset_styles(&doc);
 		s_copy(page_title, doc.title, sizeof(page_title));
 		s_copy(page_title, doc.title, sizeof(page_title));
 	}
 	}
+	post_status_text[0] = '\0';
+	post_status_error = 0;
+	post_status_ttl = 0;
 	page_loading = 0;
 	page_loading = 0;
 	set_status("", 0, 0);
 	set_status("", 0, 0);
 	s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
 	s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
@@ -1574,6 +1668,17 @@ static void add_image_run(struct lay *l, unsigned int node,
 		    (l->nruns > 0 || l->line_x > l->left))
 		    (l->nruns > 0 || l->line_x > l->left))
 			flush_line(l);
 			flush_line(l);
 		add_run(l, 0, 0, &gs, href, tmp);
 		add_run(l, 0, 0, &gs, href, tmp);
+		if (!l->draw) {
+			int t = l->line_y < 0 ? 0 : l->line_y;
+			int hh = 16 * gs.scale;
+
+			if (t > 65535)
+				t = 65535;
+			if (hh > 65535)
+				hh = 65535;
+			img_top[node] = (unsigned short)t;
+			img_h[node] = (unsigned short)hh;
+		}
 		return;
 		return;
 	}
 	}
 	maxw = l->right - l->left;
 	maxw = l->right - l->left;
@@ -1638,6 +1743,16 @@ static void add_image_run(struct lay *l, unsigned int node,
 		dh = dw ? dh * maxw / dw : dh;
 		dh = dw ? dh * maxw / dw : dh;
 		dw = maxw;
 		dw = maxw;
 	}
 	}
+	if (!l->draw) {
+		int t = l->line_y < 0 ? 0 : l->line_y;
+
+		if (t > 65535)
+			t = 65535;
+		if (dh > 65535)
+			dh = 65535;
+		img_top[node] = (unsigned short)t;
+		img_h[node] = (unsigned short)dh;
+	}
 	if (l->line_x + dw > l->right && (l->nruns > 0 ||
 	if (l->line_x + dw > l->right && (l->nruns > 0 ||
 					  l->line_x > l->left))
 					  l->line_x > l->left))
 		flush_line(l);
 		flush_line(l);
@@ -2132,8 +2247,11 @@ static int measure_page(int left, int right)
 	struct br_style base = base_style();
 	struct br_style base = base_style();
 	unsigned int i;
 	unsigned int i;
 
 
-	for (i = 0; i < BR_MAX_NODES; i++)
+	for (i = 0; i < BR_MAX_NODES; i++) {
 		block_h[i] = 0;
 		block_h[i] = 0;
+		img_top[i] = 0;
+		img_h[i] = 0;
+	}
 	l->draw = 0;
 	l->draw = 0;
 	l->ui = 0;
 	l->ui = 0;
 	l->left = left;
 	l->left = left;
@@ -2433,6 +2551,8 @@ void _start(const char *arg)
 	br_set_theme(ui.theme.text, ui.theme.bg, ui.theme.panel,
 	br_set_theme(ui.theme.text, ui.theme.bg, ui.theme.panel,
 		     ui.theme.accent);
 		     ui.theme.accent);
 	br_set_viewport(BR_VIEWPORT_W);
 	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])
 	if (u_getcwd(cwd, sizeof(cwd)) != 0 || !cwd[0])
 		s_copy(cwd, "/", sizeof(cwd));
 		s_copy(cwd, "/", sizeof(cwd));
 	heap_clear();
 	heap_clear();
@@ -2448,6 +2568,18 @@ void _start(const char *arg)
 		unsigned int action = 0;
 		unsigned int action = 0;
 		unsigned int now;
 		unsigned int now;
 
 
+		if (preload_active) {
+			if (preload_wait)
+				preload_wait = 0;
+			else
+				preload_step();
+		} else if (preload_pending &&
+			   page_scroll.offset != preload_scroll) {
+			preload_scroll = page_scroll.offset;
+			preload_pending = 0;
+			preload_active = 1;
+			browser_net_open();
+		}
 		click_url[0] = '\0';
 		click_url[0] = '\0';
 		now = u_ticks_ms();
 		now = u_ticks_ms();
 		if (status_until && (int)(now - status_until) >= 0) {
 		if (status_until && (int)(now - status_until) >= 0) {
@@ -2498,6 +2630,7 @@ void _start(const char *arg)
 			break;
 			break;
 		u_sleep_ms(10);
 		u_sleep_ms(10);
 	}
 	}
+	browser_net_close();
 	u_win_close();
 	u_win_close();
 	u_exit(0);
 	u_exit(0);
 }
 }

+ 81 - 27
uhttp.c

@@ -529,6 +529,7 @@ static int read_response_head(struct uhttp *h, char *location, int *have_loc)
 	h->resp.gzip = 0;
 	h->resp.gzip = 0;
 	h->resp.content_type[0] = '\0';
 	h->resp.content_type[0] = '\0';
 	h->resp.content_encoding[0] = '\0';
 	h->resp.content_encoding[0] = '\0';
+	h->peer_close = 0;
 	h->chunk_state = 0;
 	h->chunk_state = 0;
 	h->chunk_left = 0;
 	h->chunk_left = 0;
 	h->body_read = 0;
 	h->body_read = 0;
@@ -574,6 +575,9 @@ static int read_response_head(struct uhttp *h, char *location, int *have_loc)
 		} else if (hdr_match(line, "location")) {
 		} else if (hdr_match(line, "location")) {
 			copy_after_colon(line, location, UHTTP_LOC_MAX);
 			copy_after_colon(line, location, UHTTP_LOC_MAX);
 			*have_loc = location[0] != '\0';
 			*have_loc = location[0] != '\0';
+		} else if (hdr_match(line, "connection") &&
+			   contains_ci(line, "close")) {
+			h->peer_close = 1;
 		}
 		}
 	}
 	}
 	return 0;
 	return 0;
@@ -598,7 +602,10 @@ static int send_request(struct uhttp *h, unsigned int flags)
 	n = req_app(req, n, sizeof(req), "\r\nAccept: */*\r\n");
 	n = req_app(req, n, sizeof(req), "\r\nAccept: */*\r\n");
 	if (flags & UHTTP_GZIP)
 	if (flags & UHTTP_GZIP)
 		n = req_app(req, n, sizeof(req), "Accept-Encoding: gzip\r\n");
 		n = req_app(req, n, sizeof(req), "Accept-Encoding: gzip\r\n");
-	n = req_app(req, n, sizeof(req), "Connection: close\r\n\r\n");
+	n = req_app(req, n, sizeof(req),
+		      (h->keepalive && !h->peer_close) ?
+		      "Connection: keep-alive\r\n\r\n" :
+		      "Connection: close\r\n\r\n");
 
 
 	if ((int)n > u_syscall0(SYS_TCP_PEER_MSS))
 	if ((int)n > u_syscall0(SYS_TCP_PEER_MSS))
 		return UHTTP_ERR_TOOLONG;
 		return UHTTP_ERR_TOOLONG;
@@ -617,56 +624,93 @@ void uhttp_init(struct uhttp *h)
 	h->user_agent = "nebos-uhttp/0.1";
 	h->user_agent = "nebos-uhttp/0.1";
 }
 }
 
 
+static int host_same(const char *a, const char *b)
+{
+	while (*a && *b && *a == *b) {
+		a++;
+		b++;
+	}
+	return *a == *b;
+}
+
 int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
 int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
 	       const struct uhttp_hooks *hooks)
 	       const struct uhttp_hooks *hooks)
 {
 {
 	char location[UHTTP_LOC_MAX];
 	char location[UHTTP_LOC_MAX];
+	char old_host[UHTTP_HOST_MAX];
+	unsigned int old_port = 0;
+	int old_secure = 0;
 	unsigned int hops = 0;
 	unsigned int hops = 0;
+	int reuse = 0;
 	int rv;
 	int rv;
 
 
 	h->hooks = hooks;
 	h->hooks = hooks;
 	if (!h->user_agent)
 	if (!h->user_agent)
 		h->user_agent = "nebos-uhttp/0.1";
 		h->user_agent = "nebos-uhttp/0.1";
+	if (h->keepalive && h->connected) {
+		s_copy(old_host, h->url.host, sizeof(old_host));
+		old_port = h->url.port;
+		old_secure = h->url.secure;
+		reuse = 1;
+	}
 	h->len = h->pos = 0;
 	h->len = h->pos = 0;
 	h->eof = 0;
 	h->eof = 0;
 	h->error = 0;
 	h->error = 0;
-	h->connected = 0;
-	h->owns_nic = 0;
+	h->peer_close = 0;
 
 
 	if (uhttp_parse_url(url, &h->url) != 0)
 	if (uhttp_parse_url(url, &h->url) != 0)
 		return UHTTP_ERR_URL;
 		return UHTTP_ERR_URL;
 	url_build(&h->url, h->resp.final_url, sizeof(h->resp.final_url));
 	url_build(&h->url, h->resp.final_url, sizeof(h->resp.final_url));
 
 
-	{
-		struct sys_netinfo ni;
-
-		if (u_syscall1(SYS_NET_INFO, (int)&ni) != 1)
-			return UHTTP_ERR_NIC;
-		if (u_syscall0(SYS_NET_CONFIGURED) != 1)
-			return UHTTP_ERR_IP;
-		if (u_syscall0(SYS_NET_ACQUIRE) != 0)
-			return UHTTP_ERR_BUSY;
-		h->owns_nic = 1;
+	if (reuse && host_same(old_host, h->url.host) &&
+	    old_port == h->url.port && old_secure == h->url.secure) {
+		/* Keep the existing TCP/TLS connection. */
+	} else {
+		reuse = 0;
+		if (h->connected)
+			transport_close(h);
+		if (!h->owns_nic) {
+			struct sys_netinfo ni;
+
+			if (u_syscall1(SYS_NET_INFO, (int)&ni) != 1)
+				return UHTTP_ERR_NIC;
+			if (u_syscall0(SYS_NET_CONFIGURED) != 1)
+				return UHTTP_ERR_IP;
+			if (u_syscall0(SYS_NET_ACQUIRE) != 0)
+				return UHTTP_ERR_BUSY;
+			h->owns_nic = 1;
+		}
 	}
 	}
 
 
 	h->total_deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS;
 	h->total_deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS;
 
 
 	for (;;) {
 	for (;;) {
 		int have_loc;
 		int have_loc;
-
-		rv = http_connect(h);
-		if (rv != 0)
-			goto fail;
-		if (h->hooks && h->hooks->connected)
-			h->hooks->connected(h->hooks->ctx, h->url.secure);
-
-		rv = send_request(h, flags);
-		if (rv != 0)
-			goto fail;
-
-		rv = read_response_head(h, location, &have_loc);
-		if (rv != 0)
-			goto fail;
+		int used_reuse = reuse;
+
+		reuse = 0;
+		for (;;) {
+			if (!used_reuse) {
+				rv = http_connect(h);
+				if (rv != 0)
+					goto fail;
+				if (h->hooks && h->hooks->connected)
+					h->hooks->connected(h->hooks->ctx,
+							    h->url.secure);
+			}
+			rv = send_request(h, flags);
+			if (rv == 0)
+				rv = read_response_head(h, location,
+							&have_loc);
+			if (rv == 0)
+				break;
+			if (!used_reuse)
+				goto fail;
+			/* A kept-alive socket may have been closed by the
+			 * server; reconnect and retry once. */
+			used_reuse = 0;
+			transport_close(h);
+		}
 
 
 		if (h->resp.status >= 300 && h->resp.status < 400 && have_loc) {
 		if (h->resp.status >= 300 && h->resp.status < 400 && have_loc) {
 			if (h->hooks && h->hooks->redirect)
 			if (h->hooks && h->hooks->redirect)
@@ -690,6 +734,7 @@ int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
 	return 0;
 	return 0;
 
 
 fail:
 fail:
+	h->keepalive = 0;
 	uhttp_close(h);
 	uhttp_close(h);
 	return rv;
 	return rv;
 }
 }
@@ -725,6 +770,8 @@ int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
 							 timeout_ms);
 							 timeout_ms);
 
 
 				if (hl < 0) {
 				if (hl < 0) {
+					if (h->eof)
+						h->peer_close = 1;
 					if (n)
 					if (n)
 						break;
 						break;
 					return h->error ? h->error : 0;
 					return h->error ? h->error : 0;
@@ -757,6 +804,8 @@ int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
 			} else if (h->chunk_state == 1) {
 			} else if (h->chunk_state == 1) {
 				c = reader_getc(h, timeout_ms);
 				c = reader_getc(h, timeout_ms);
 				if (c < 0) {
 				if (c < 0) {
+					if (h->eof)
+						h->peer_close = 1;
 					if (n)
 					if (n)
 						break;
 						break;
 					return h->error ? h->error : 0;
 					return h->error ? h->error : 0;
@@ -776,6 +825,7 @@ int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
 
 
 				if (hl <= 0) {
 				if (hl <= 0) {
 					h->eof = 1;
 					h->eof = 1;
+					h->peer_close = 1;
 					break;
 					break;
 				}
 				}
 			}
 			}
@@ -787,6 +837,8 @@ int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
 			}
 			}
 			c = reader_getc(h, timeout_ms);
 			c = reader_getc(h, timeout_ms);
 			if (c < 0) {
 			if (c < 0) {
+				if (h->eof)
+					h->peer_close = 1;
 				if (n)
 				if (n)
 					break;
 					break;
 				return h->error ? h->error : 0;
 				return h->error ? h->error : 0;
@@ -800,6 +852,8 @@ int uhttp_read(struct uhttp *h, void *vbuf, unsigned int max,
 
 
 void uhttp_close(struct uhttp *h)
 void uhttp_close(struct uhttp *h)
 {
 {
+	if (h->keepalive && h->connected && !h->peer_close && !h->error)
+		return;
 	transport_close(h);
 	transport_close(h);
 	if (h->owns_nic) {
 	if (h->owns_nic) {
 		u_syscall0(SYS_NET_RELEASE);
 		u_syscall0(SYS_NET_RELEASE);

+ 2 - 0
uhttp.h

@@ -83,6 +83,8 @@ struct uhttp {
 	int error;                      /* UHTTP_ERR_* once a read failed */
 	int error;                      /* UHTTP_ERR_* once a read failed */
 	int connected;
 	int connected;
 	int owns_nic;
 	int owns_nic;
+	int keepalive;                  /* reuse the TCP/TLS connection */
+	int peer_close;                 /* response asked to close it */
 
 
 	/* chunked transfer state */
 	/* chunked transfer state */
 	int chunk_state;                /* 0 size line, 1 data, 2 crlf, 3 trailers */
 	int chunk_state;                /* 0 size line, 1 data, 2 crlf, 3 trailers */