Răsfoiți Sursa

Speed up bare-hostname loads and redirect hops

Typing cloudron.io cost three TCP connections, two TLS handshakes and
an idle close wait on every redirect hop.

- browser: try a typed bare hostname as https:// first and retry once
  over http:// when the https connection is refused, reset, times out
  or fails the TLS handshake (replacing the history entry).
- uhttp: drop a connection that a redirect leaves without a TLS
  close_notify (tls_abandon), and reuse it for a same-origin redirect
  with a small length-delimited body.
- tcp: tcp_client_close() sends FIN and frees the slot at once instead
  of waiting up to 500 ms for the peer's FIN.
Johannes Zellner 1 săptămână în urmă
părinte
comite
af1ba3a287
8 a modificat fișierele cu 119 adăugiri și 16 ștergeri
  1. 10 1
      AGENTS.md
  2. 41 1
      browser.c
  3. 1 0
      docs/network.html
  4. 1 1
      docs/userland.html
  5. 5 9
      tcp.c
  6. 7 0
      tls.c
  7. 1 0
      tls.h
  8. 53 4
      uhttp.c

+ 10 - 1
AGENTS.md

@@ -559,6 +559,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   (connect/send/recv/close, one connection per process, in-order receive with
   a 4 KB queue per connection so data arriving during a send is never lost,
   one segment of retransmit; `net_pump()` dispatches frames to every client).
+  `tcp_client_close()` sends FIN and frees the slot at once (all sends are
+  synchronous, so nothing is unacknowledged); the peer's late FIN matches no
+  client and is dropped.
   `curl` — the ring-3 command: `http://` or `https://` GET with DNS, ARP,
   a dumped status/headers/body, chunked decoding (redirects are followed,
   up to 5 hops); TLS 1.3 via `tls.c/h` + `tls_sha256.c`, `tls_chacha20.c`,
@@ -878,7 +881,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   layout/render, URL bar, history, status bar and inline `<img>` decoding.
   A dotted input without a scheme that is not a known local file extension
   (`cloudron.io`) is treated as a hostname and skips the local-file stat;
-  origin-level redirects are remembered per process and in `/share/borigins`,
+  it is tried as `https://` first (skipping the usual http -> https redirect
+  connection) and retried once over `http://` when the https connection is
+  refused, reset, times out or fails the TLS handshake (the history entry is
+  replaced, not duplicated).  `uhttp` drops a connection it redirects away
+  from without a TLS close_notify, and keeps it for a same-origin redirect
+  with a small length-delimited body.
+  Origin-level redirects are remembered per process and in `/share/borigins`,
   so later visits to a bare domain go straight to its canonical scheme/host.
   Remote stylesheets are cached under `/share/bcss-*` for the current boot, so
   a reload or a fresh browser launch does not fetch the same CSS sheets again.

+ 41 - 1
browser.c

@@ -1813,6 +1813,9 @@ static int fetch_tty = -2;
 static unsigned int fetch_session;
 static unsigned int fetch_seq;
 static struct fetch_meta fetch_meta;
+/* UHTTP_ERR_* of the last failed fetch_remote(), 0 when the failure was not
+ * an HTTP-level error (helper died, timeout in the browser, ...). */
+static int fetch_last_error;
 
 static unsigned int meta_uint(char **pp)
 {
@@ -1988,6 +1991,7 @@ static int fetch_worker_submit(struct fetch_worker *w, const char *url,
 	w->seq = fetch_seq;
 	w->ready = 0;
 	w->retried = 0;
+	w->m.error = 0;
 	w->node = node;
 	w->bg = bg;
 	s_copy(w->url, url, sizeof(w->url));
@@ -2192,6 +2196,7 @@ static int fetch_remote(const char *url, unsigned int cap,
 	struct fetch_worker *w = fetch_worker_alloc();
 	int rv;
 
+	fetch_last_error = 0;
 	if (!w) {
 		s_copy(err, "network busy", errcap);
 		return -1;
@@ -2207,6 +2212,7 @@ static int fetch_remote(const char *url, unsigned int cap,
 		return FETCH_ABORT;
 	}
 	if (rv != 0) {
+		fetch_last_error = w->m.error;
 		fetch_worker_release(w);
 		return -1;
 	}
@@ -3123,6 +3129,19 @@ static void preload_step(void)
 	}
 }
 
+/* Set by navigate() when it turned a typed bare host into https://; the next
+ * load_page() consumes it and may retry over http. */
+static int nav_https_upgraded;
+
+/* Errors that mean "https is not served here" rather than "the site is
+ * down": a refused/reset/unanswered port 443 or a failed TLS handshake. */
+static int https_fallback_error(int e)
+{
+	return e == UHTTP_ERR_REFUSED || e == UHTTP_ERR_TIMEOUT ||
+	       e == UHTTP_ERR_TLS || e == UHTTP_ERR_CONNECT ||
+	       e == UHTTP_ERR_RESET;
+}
+
 static void history_push(const char *url)
 {
 	if (!url || !url[0])
@@ -3157,8 +3176,10 @@ static void load_page(const char *url, int local, int push)
 	const char *detail = 0;
 	const char *post = pending_post_len ? pending_post : 0;
 	unsigned int post_len = pending_post_len;
+	int upgraded = nav_https_upgraded;
 	int rv;
 
+	nav_https_upgraded = 0;
 	final[0] = '\0';
 	pending_post_len = 0;
 	cancel_fetch = 0;
@@ -3205,6 +3226,20 @@ static void load_page(const char *url, int local, int push)
 		     &truncated, post, post_len);
 	if (rv == FETCH_ABORT)
 		return;
+	if (rv != 0 && upgraded && !local && starts_with(url, "https://") &&
+	    https_fallback_error(fetch_last_error)) {
+		char http_url[BR_URL_MAX];
+		unsigned int n = 0;
+
+		s_append(http_url, &n, "http://", sizeof(http_url));
+		s_append(http_url, &n, url + 8, sizeof(http_url));
+		/* Replace the https entry instead of adding a second one. */
+		if (push && hist_pos >= 0 && hist_pos < hist_len &&
+		    s_eq(history[hist_pos], url))
+			s_copy(history[hist_pos], http_url, BR_URL_MAX);
+		load_page(http_url, 0, 0);
+		return;
+	}
 	if (rv != 0) {
 		detail = err;
 		show_error("Cannot load this page", detail);
@@ -3486,10 +3521,15 @@ static void navigate(const char *input, int push)
 		}
 	}
 	{
+		/* A typed bare host tries https first: nearly every site
+		 * redirects http -> https anyway, and that hop costs a whole
+		 * extra connection.  load_page() falls back to http when the
+		 * https connection itself fails. */
 		char with_scheme[BR_URL_MAX];
 		unsigned int n = 0;
-		s_append(with_scheme, &n, "http://", sizeof(with_scheme));
+		s_append(with_scheme, &n, "https://", sizeof(with_scheme));
 		s_append(with_scheme, &n, t, sizeof(with_scheme));
+		nav_https_upgraded = 1;
 		load_remote(with_scheme, push);
 	}
 }

+ 1 - 0
docs/network.html

@@ -257,6 +257,7 @@
 		<ul>
 			<li><strong>Demultiplexing.</strong> <code>net_pump()</code> feeds every received frame to <code>tcp_client_input()</code>, which matches the destination IP and port pair and updates that connection's sequence state.</li>
 			<li><strong>MSS / window.</strong> Our MSS is 1460 bytes and the advertised window is 4 KB.</li>
+			<li><strong>Close.</strong> <code>tcp_client_close()</code> sends FIN and frees the slot immediately instead of waiting for the peer's FIN; every send is synchronous, so no data is outstanding, and a late FIN matches no client and is dropped.  <code>uhttp</code> also skips the TLS close_notify when a redirect leaves the origin, so a redirect hop costs no extra round trips.</li>
 			<li><strong>Retransmit.</strong> A 500 ms retry timer with a small retry count carries the connection through loss; there is no full congestion-control state machine.</li>
 			<li><strong>Listener.</strong> The server side has one active connection plus three backlog/closing slots. SYN-ACK advertises a zero window while queued; a queued connection may hold 1 KB of early request, and the window opens when <code>tcp_srv_accept()</code> runs.</li>
 			<li><strong>Bursts.</strong> Responses go out in up to four-segment bursts with a cumulative ACK, rather than one segment per ACK.</li>

+ 1 - 1
docs/userland.html

@@ -374,7 +374,7 @@
 			<li><strong>CSS gaps.</strong> Multiple background layers and <code>background-attachment</code> (a non-uniform <code>background-size</code> uses the width scale), 3D/perspective transforms and transformed text/descendants (scale/rotate paint the box and inline images), pseudo-classes/elements, border-radius clipping of descendants, gradients/box-shadow/transitions and <code>color-mix</code>. Flex rows, card grids, tables with rowspan/colspan, inline SVG and CSS variables are supported.</li>
 			<li><strong>Caps.</strong> 2 MB page body, 832 KB CSS, 6144 DOM nodes, 4608 rules, 16 images. Each image gets an <code>img_estimate()</code>-sized decode arena that is freed after the scaled bitmap is made; navigation calls <code>heap_reset()</code> and unmaps the page heap.</li>
 			<li><strong>Fetching.</strong> Up to three worker processes prefetch images in parallel within the viewport plus 160 px; each worker keeps its own TCP/TLS connection and its own request/body/meta files (keyed by browser pid and worker slot, so multiple windows do not collide).  Completed images are decoded as jobs finish.  Page, CSS and script fetches stay ordered.</li>
-			<li><strong>URL entry.</strong> A dotted name without a scheme that is not a known local file extension is treated as a hostname, so <code>cloudron.io</code> does not hit the local-file stat. Origin-level redirects (the common <code>http://host</code> → <code>https://host</code> → <code>https://www.host</code> chain) are cached per process and in <code>/share/borigins</code>; the first visit still follows them, later visits go straight to the canonical origin. Remote stylesheets are cached under <code>/share/bcss-*</code> for the current boot, so a reload or a fresh browser launch skips the CSS downloads.</li>
+			<li><strong>URL entry.</strong> A dotted name without a scheme that is not a known local file extension is treated as a hostname, so <code>cloudron.io</code> does not hit the local-file stat. A typed hostname is tried as <code>https://</code> first, skipping the common <code>http://host</code> → <code>https://host</code> hop; if the https connection is refused, reset, times out or fails the TLS handshake, the browser retries once over <code>http://</code> and replaces the history entry. Origin-level redirects (such as <code>https://host</code> → <code>https://www.host</code>) are cached per process and in <code>/share/borigins</code>; the first visit still follows them, later visits go straight to the canonical origin. Remote stylesheets are cached under <code>/share/bcss-*</code> for the current boot, so a reload or a fresh browser launch skips the CSS downloads.</li>
 			</ul>
 		<p>
 			The browser's default canvas is dark, matching the ngui theme, but an

+ 5 - 9
tcp.c

@@ -632,22 +632,18 @@ void tcp_client_close(unsigned int pid)
 	if (!cl)
 		return;
 	c = &cl->c;
+	/* Send our FIN and free the slot at once.  Every send is synchronous,
+	 * so all our data is already acknowledged; waiting for the peer's FIN
+	 * only cost a round trip (up to 500 ms) per closed connection, which
+	 * every redirect hop and helper teardown paid.  A late FIN/ACK
+	 * matches no client (source ports rotate) and is dropped. */
 	if (c->state == TCP_STATE_ESTABLISHED) {
-		unsigned int deadline = get_ticks_ms() + 500;
 		unsigned int flags;
 
 		flags = net_irq_enter();
 		tcp_send_segment(c, TCP_FIN | TCP_ACK, c->snd_nxt,
 				 c->rcv_nxt, 0, 0, 0);
 		net_irq_exit(flags);
-		c->snd_nxt++;
-		c->state = TCP_STATE_FIN_WAIT;
-		while ((int)(get_ticks_ms() - deadline) < 0) {
-			net_pump(4);
-			if (c->peer_fin || c->state == TCP_STATE_CLOSED)
-				break;
-			process_sleep_ms(1);
-		}
 	}
 	client_free(cl);
 }

+ 7 - 0
tls.c

@@ -909,3 +909,10 @@ void tls_close(void)
 		send_record(21, cn, 2, 0);
 	phase = 0;                      /* no stale epoch on a later connection */
 }
+
+/* Forget the session without sending close_notify (the connection is being
+ * dropped anyway, e.g. after a redirect), so the close costs no round trip. */
+void tls_abandon(void)
+{
+	phase = 0;
+}

+ 1 - 0
tls.h

@@ -19,5 +19,6 @@ int tls_recv(void *buf, unsigned int max, unsigned int timeout_ms);
 
 /* Best-effort close_notify (then the caller closes the TCP connection). */
 void tls_close(void);
+void tls_abandon(void);
 
 #endif

+ 53 - 4
uhttp.c

@@ -385,6 +385,39 @@ static void transport_close(struct uhttp *h)
 	h->connected = 0;
 }
 
+/* Drop the connection without a TLS close_notify: used when a redirect
+ * leaves this origin, so the hop pays no extra round trip. */
+static void transport_abandon(struct uhttp *h)
+{
+	if (!h->connected)
+		return;
+	if (h->url.secure)
+		tls_abandon();
+	u_syscall0(SYS_TCP_CLOSE);
+	h->connected = 0;
+}
+
+static int reader_getc(struct uhttp *h, unsigned int timeout_ms);
+
+/* Discard a small, length-delimited redirect body so the next request can
+ * reuse the connection.  1 when the connection is still usable. */
+static int drain_small_body(struct uhttp *h)
+{
+	unsigned int left;
+
+	if (h->resp.chunked || !h->resp.have_length ||
+	    h->resp.content_length > 4096)
+		return 0;
+	left = h->resp.content_length;
+	while (left) {
+		if (reader_getc(h, UHTTP_IDLE_TIMEOUT_MS) < 0)
+			return 0;
+		left--;
+	}
+	/* Bytes past the body would belong to nothing we asked for. */
+	return h->pos >= h->len;
+}
+
 /* Connect to h->url (TCP + TLS).  Keeps the NIC acquired. */
 static int http_connect(struct uhttp *h)
 {
@@ -741,10 +774,26 @@ int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
 				rv = UHTTP_ERR_TOOMANY;
 				goto fail;
 			}
-			transport_close(h);
-			if (url_apply_location(&h->url, location) != 0) {
-				rv = UHTTP_ERR_REDIRECT;
-				goto fail;
+			{
+				struct uhttp_url next = h->url;
+
+				if (url_apply_location(&next, location) != 0) {
+					transport_abandon(h);
+					rv = UHTTP_ERR_REDIRECT;
+					goto fail;
+				}
+				/* A same-origin redirect (/ -> /en/) keeps the
+				 * TCP/TLS connection; any other one drops it
+				 * without the close handshake. */
+				if (h->keepalive && !h->peer_close &&
+				    host_same(next.host, h->url.host) &&
+				    next.port == h->url.port &&
+				    next.secure == h->url.secure &&
+				    drain_small_body(h))
+					reuse = 1;
+				else
+					transport_abandon(h);
+				h->url = next;
 			}
 			/* A redirected POST becomes a GET unless the
 			 * server explicitly asks to repeat the request. */