|
@@ -75,6 +75,7 @@ struct fetch_paths {
|
|
|
char done[FETCH_PATH_MAX];
|
|
char done[FETCH_PATH_MAX];
|
|
|
char post[FETCH_PATH_MAX];
|
|
char post[FETCH_PATH_MAX];
|
|
|
char px[FETCH_PATH_MAX];
|
|
char px[FETCH_PATH_MAX];
|
|
|
|
|
+ char prog[FETCH_PATH_MAX];
|
|
|
};
|
|
};
|
|
|
static struct fetch_paths fp;
|
|
static struct fetch_paths fp;
|
|
|
|
|
|
|
@@ -126,6 +127,7 @@ static void fetch_paths_init(unsigned int session, int slot)
|
|
|
fetch_path(fp.done, sizeof(fp.done), "done", session, slot);
|
|
fetch_path(fp.done, sizeof(fp.done), "done", session, slot);
|
|
|
fetch_path(fp.post, sizeof(fp.post), "post", session, slot);
|
|
fetch_path(fp.post, sizeof(fp.post), "post", session, slot);
|
|
|
fetch_path(fp.px, sizeof(fp.px), "px", session, slot);
|
|
fetch_path(fp.px, sizeof(fp.px), "px", session, slot);
|
|
|
|
|
+ fetch_path(fp.prog, sizeof(fp.prog), "prog", session, slot);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Optional decode in the fetch helper. The browser's UI thread only has
|
|
/* Optional decode in the fetch helper. The browser's UI thread only has
|
|
@@ -381,16 +383,37 @@ static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
|
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/* Fetch one URL through a possibly reused keep-alive connection. */
|
|
|
|
|
|
|
+/* Progress for the browser's download manager: the helper writes the bytes
|
|
|
|
|
+ * received so far while the body is still arriving. `total` is 0 when the
|
|
|
|
|
+ * server sent no Content-Length. */
|
|
|
|
|
+static void write_progress(const char *path, unsigned int seq,
|
|
|
|
|
+ unsigned int got, unsigned int total)
|
|
|
|
|
+{
|
|
|
|
|
+ static char buf[80];
|
|
|
|
|
+ unsigned int n = 0;
|
|
|
|
|
+
|
|
|
|
|
+ app_dec(buf, &n, seq, sizeof(buf));
|
|
|
|
|
+ app_ch(buf, &n, '\n', sizeof(buf));
|
|
|
|
|
+ app_dec(buf, &n, got, sizeof(buf));
|
|
|
|
|
+ app_ch(buf, &n, '\n', sizeof(buf));
|
|
|
|
|
+ app_dec(buf, &n, total, sizeof(buf));
|
|
|
|
|
+ app_ch(buf, &n, '\n', sizeof(buf));
|
|
|
|
|
+ u_syscall3(SYS_WRITE_FILE, (int)path, (int)buf, (int)n);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Fetch one URL through a possibly reused keep-alive connection. `prog`
|
|
|
|
|
+ * (with `prog_seq`) turns on progress publishing for a download. */
|
|
|
static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
|
|
static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
|
|
|
const char *method, const char *ctype,
|
|
const char *method, const char *ctype,
|
|
|
const char *post, unsigned int post_len,
|
|
const char *post, unsigned int post_len,
|
|
|
unsigned char *buf, unsigned int *outlen,
|
|
unsigned char *buf, unsigned int *outlen,
|
|
|
unsigned int *outtrunc, int *outerr,
|
|
unsigned int *outtrunc, int *outerr,
|
|
|
- struct fetch_times *times)
|
|
|
|
|
|
|
+ struct fetch_times *times, unsigned int open_flags,
|
|
|
|
|
+ const char *prog, unsigned int prog_seq)
|
|
|
{
|
|
{
|
|
|
unsigned int total = 0;
|
|
unsigned int total = 0;
|
|
|
unsigned int t0 = u_ticks_ms(), t1, tfirst = 0;
|
|
unsigned int t0 = u_ticks_ms(), t1, tfirst = 0;
|
|
|
|
|
+ unsigned int last_prog = 0;
|
|
|
int rv, trunc = 0, err = 0;
|
|
int rv, trunc = 0, err = 0;
|
|
|
|
|
|
|
|
*outlen = 0;
|
|
*outlen = 0;
|
|
@@ -406,13 +429,13 @@ static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
|
|
|
if (!ctype || !ctype[0])
|
|
if (!ctype || !ctype[0])
|
|
|
ctype = "application/x-www-form-urlencoded";
|
|
ctype = "application/x-www-form-urlencoded";
|
|
|
uhttp_set_method(h, method, post, post_len, ctype);
|
|
uhttp_set_method(h, method, post, post_len, ctype);
|
|
|
- rv = uhttp_open(h, url, UHTTP_GZIP, 0);
|
|
|
|
|
|
|
+ rv = uhttp_open(h, url, open_flags, 0);
|
|
|
if (rv == UHTTP_ERR_BUSY) {
|
|
if (rv == UHTTP_ERR_BUSY) {
|
|
|
int tries;
|
|
int tries;
|
|
|
|
|
|
|
|
for (tries = 0; tries < 50 && rv == UHTTP_ERR_BUSY; tries++) {
|
|
for (tries = 0; tries < 50 && rv == UHTTP_ERR_BUSY; tries++) {
|
|
|
u_sleep_ms(20);
|
|
u_sleep_ms(20);
|
|
|
- rv = uhttp_open(h, url, UHTTP_GZIP, 0);
|
|
|
|
|
|
|
+ rv = uhttp_open(h, url, open_flags, 0);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
t1 = u_ticks_ms();
|
|
t1 = u_ticks_ms();
|
|
@@ -424,13 +447,29 @@ static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
|
|
|
*outerr = rv;
|
|
*outerr = rv;
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+ if (prog && h->resp.have_length)
|
|
|
|
|
+ write_progress(prog, prog_seq, 0, h->resp.content_length);
|
|
|
|
|
+ last_prog = u_ticks_ms();
|
|
|
for (;;) {
|
|
for (;;) {
|
|
|
- rv = uhttp_read(h, buf + total, cap - total,
|
|
|
|
|
|
|
+ unsigned int want = cap - total;
|
|
|
|
|
+
|
|
|
|
|
+ /* A download reads in small slices so the progress file
|
|
|
|
|
+ * changes while the body is still arriving; uhttp_read()
|
|
|
|
|
+ * fills its whole buffer otherwise. */
|
|
|
|
|
+ if (prog && want > 16u * 1024u)
|
|
|
|
|
+ want = 16u * 1024u;
|
|
|
|
|
+ rv = uhttp_read(h, buf + total, want,
|
|
|
UHTTP_IDLE_TIMEOUT_MS);
|
|
UHTTP_IDLE_TIMEOUT_MS);
|
|
|
if (rv > 0) {
|
|
if (rv > 0) {
|
|
|
if (!tfirst)
|
|
if (!tfirst)
|
|
|
tfirst = u_ticks_ms();
|
|
tfirst = u_ticks_ms();
|
|
|
total += (unsigned int)rv;
|
|
total += (unsigned int)rv;
|
|
|
|
|
+ if (prog && (unsigned int)(u_ticks_ms() - last_prog) >= 100) {
|
|
|
|
|
+ last_prog = u_ticks_ms();
|
|
|
|
|
+ write_progress(prog, prog_seq, total,
|
|
|
|
|
+ h->resp.have_length ?
|
|
|
|
|
+ h->resp.content_length : 0);
|
|
|
|
|
+ }
|
|
|
if (total >= cap) {
|
|
if (total >= cap) {
|
|
|
trunc = 1;
|
|
trunc = 1;
|
|
|
break;
|
|
break;
|
|
@@ -441,6 +480,9 @@ static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
|
|
|
err = rv;
|
|
err = rv;
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
+ if (prog)
|
|
|
|
|
+ write_progress(prog, prog_seq, total,
|
|
|
|
|
+ h->resp.have_length ? h->resp.content_length : 0);
|
|
|
*outlen = total;
|
|
*outlen = total;
|
|
|
*outtrunc = (unsigned int)trunc;
|
|
*outtrunc = (unsigned int)trunc;
|
|
|
*outerr = err;
|
|
*outerr = err;
|
|
@@ -494,8 +536,8 @@ static void fetch_once(void)
|
|
|
|
|
|
|
|
uhttp_init(&h);
|
|
uhttp_init(&h);
|
|
|
h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
|
|
h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
|
|
|
- helper_fetch(&h, req + i, cap, 0, 0, 0, 0, buf, &body_len, &body_trunc, &err,
|
|
|
|
|
- ×);
|
|
|
|
|
|
|
+ helper_fetch(&h, req + i, cap, 0, 0, 0, 0, buf, &body_len,
|
|
|
|
|
+ &body_trunc, &err, ×, UHTTP_GZIP, 0, 0);
|
|
|
if (body_len)
|
|
if (body_len)
|
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
|
|
|
(int)body_len);
|
|
(int)body_len);
|
|
@@ -560,7 +602,7 @@ static void fetch_serve(const char *arg)
|
|
|
unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
|
|
unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
|
|
|
unsigned int post_len = 0, jar_changes;
|
|
unsigned int post_len = 0, jar_changes;
|
|
|
unsigned int dec_w = 0, dec_h = 0;
|
|
unsigned int dec_w = 0, dec_h = 0;
|
|
|
- int decode = 0;
|
|
|
|
|
|
|
+ int decode = 0, download = 0;
|
|
|
int is_post = 0;
|
|
int is_post = 0;
|
|
|
struct fetch_px px;
|
|
struct fetch_px px;
|
|
|
char method[12], ctype[96];
|
|
char method[12], ctype[96];
|
|
@@ -615,6 +657,12 @@ static void fetch_serve(const char *arg)
|
|
|
i++;
|
|
i++;
|
|
|
decode = 1;
|
|
decode = 1;
|
|
|
}
|
|
}
|
|
|
|
|
+ /* "DL <url>": a browser download. No gzip and a progress
|
|
|
|
|
+ * file while the body arrives. */
|
|
|
|
|
+ if (!decode && !strncmp(req + i, "DL ", 3)) {
|
|
|
|
|
+ download = 1;
|
|
|
|
|
+ i += 3;
|
|
|
|
|
+ }
|
|
|
/* "X <method> <body length> <content type or -> <url>":
|
|
/* "X <method> <body length> <content type or -> <url>":
|
|
|
* any method, for the browser's fetch()/XHR. */
|
|
* any method, for the browser's fetch()/XHR. */
|
|
|
if (!decode && !strncmp(req + i, "X ", 2)) {
|
|
if (!decode && !strncmp(req + i, "X ", 2)) {
|
|
@@ -696,9 +744,13 @@ static void fetch_serve(const char *arg)
|
|
|
* jar-less request after the OIDC login POST drops the
|
|
* jar-less request after the OIDC login POST drops the
|
|
|
* interaction cookie ("authorization request has expired"). */
|
|
* interaction cookie ("authorization request has expired"). */
|
|
|
h.jar = &fetch_jar;
|
|
h.jar = &fetch_jar;
|
|
|
|
|
+ if (download)
|
|
|
|
|
+ u_syscall1(SYS_UNLINK, (int)fp.prog);
|
|
|
helper_fetch(&h, url, cap, method[0] ? method : 0, ctype,
|
|
helper_fetch(&h, url, cap, method[0] ? method : 0, ctype,
|
|
|
post_len ? (char *)post : 0, post_len,
|
|
post_len ? (char *)post : 0, post_len,
|
|
|
- buf, &body_len, &body_trunc, &err, ×);
|
|
|
|
|
|
|
+ buf, &body_len, &body_trunc, &err, ×,
|
|
|
|
|
+ download ? 0 : UHTTP_GZIP,
|
|
|
|
|
+ download ? fp.prog : 0, seq);
|
|
|
if (fetch_jar.changes != jar_changes)
|
|
if (fetch_jar.changes != jar_changes)
|
|
|
jar_save();
|
|
jar_save();
|
|
|
if (body_len)
|
|
if (body_len)
|