|
|
@@ -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);
|
|
|
}
|
|
|
}
|