/* 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, the helper mode and the verbose output hooks. */ #include #include #include "ucmd_lib.h" #include "uhttp.h" #include "uhttp_cookie.h" #include "img.h" #define LINE_MAX_C 320 static void on_connected(void *ctx, int secure) { (void)ctx; if (secure) { u_puts("curl: connected, TLS handshake\n"); } else { u_puts("curl: connected, sending request\n"); } } static void on_status(void *ctx, int code, const char *line) { (void)ctx; (void)code; u_puts("curl: "); u_puts(line); u_putc('\n'); } static void on_header(void *ctx, const char *line) { (void)ctx; u_puts(line); u_putc('\n'); } static void on_redirect(void *ctx, const char *location) { (void)ctx; u_puts("curl: redirect -> "); u_puts(location); u_putc('\n'); } static const struct uhttp_hooks hooks = { .connected = on_connected, .status = on_status, .header = on_header, .redirect = on_redirect, }; /* ---- headless fetch helper for the browser ---------------------------- */ #define FETCH_PATH_MAX 32 #define FETCH_POST_MAX 16384u #define FETCH_MAX (4u * 1024u * 1024u) /* Each browser worker gets its own set of /share files; slot -1 keeps the * historical single-helper names for the one-shot __fetch mode. */ struct fetch_paths { char req[FETCH_PATH_MAX]; char body[FETCH_PATH_MAX]; char meta[FETCH_PATH_MAX]; char done[FETCH_PATH_MAX]; char post[FETCH_PATH_MAX]; char px[FETCH_PATH_MAX]; char prog[FETCH_PATH_MAX]; }; static struct fetch_paths fp; 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_path(char *out, unsigned int cap, const char *kind, unsigned int session, int slot) { unsigned int n = 0; app_str(out, &n, "/share/bf", cap); if (slot >= 0) { app_dec(out, &n, session, cap); app_ch(out, &n, '_', cap); app_dec(out, &n, (unsigned int)slot, cap); } app_str(out, &n, kind, cap); } static void fetch_paths_init(unsigned int session, int slot) { fetch_path(fp.req, sizeof(fp.req), "req", session, slot); fetch_path(fp.body, sizeof(fp.body), "body", session, slot); fetch_path(fp.meta, sizeof(fp.meta), "meta", 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.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 * to copy the finished RGB565 bitmap; the JPEG IDCT/box filter runs in the * helper process (and several helpers can decode in parallel). */ #define DECODE_ARENA_MAX (24u * 1024u * 1024u) #define DECODE_PX_MAX (8u * 1024u * 1024u) struct fetch_px { unsigned char *data; unsigned int len, dw, dh, nw, nh, alpha, format; unsigned int dec_ms; int err; }; static void fetch_decode(const unsigned char *data, unsigned int len, unsigned int max_w, unsigned int max_h, struct fetch_px *out) { struct img_info info; struct img_options opt = {0}; struct img_mem mem; struct img_image im; unsigned char *arena; unsigned int est, dw, dh, outlen, n; int r; memset(out, 0, sizeof(*out)); if (img_sniff(data, len, &info) != 0) { out->err = 1; return; } est = img_estimate(&info); if (est == 0xFFFFFFFFu || est > DECODE_ARENA_MAX) { out->err = 2; return; } arena = (unsigned char *)malloc(est); if (!arena) { out->err = 3; return; } img_mem_init(&mem, arena, est); opt.max_pixels = 8u * 1024u * 1024u; opt.max_w = max_w; opt.max_h = max_h; r = img_decode(data, len, &opt, &mem, &im); if (r != 0 || !im.w || !im.h) { out->err = 4; free(arena); return; } dw = im.w; dh = im.h; if (max_w && dw > max_w) { dh = (unsigned int)((unsigned long long)dh * max_w / dw); dw = max_w; } if (max_h && dh > max_h) { dw = (unsigned int)((unsigned long long)dw * max_h / dh); dh = max_h; } if (!dw) dw = 1; if (!dh) dh = 1; if (dw > 4096 || dh > 4096) { out->err = 5; free(arena); return; } outlen = dw * dh * 2u + (im.alpha ? dw * dh : 0); if (outlen > DECODE_PX_MAX) { out->err = 5; free(arena); return; } out->data = (unsigned char *)malloc(outlen); if (!out->data) { out->err = 6; free(arena); return; } if (dw == im.w && dh == im.h) { n = dw * dh; memcpy(out->data, im.px, n * 2u); if (im.alpha) memcpy(out->data + n * 2u, im.alpha, n); } else { struct img_blit b; memset(&b, 0, sizeof(b)); b.dst = (unsigned short *)out->data; b.dst_w = (int)dw; b.dst_h = (int)dh; b.clip_w = (int)dw; b.clip_h = (int)dh; b.scale = ((unsigned int)dw << 16) / im.w; b.dst_a = im.alpha ? out->data + dw * dh * 2u : 0; b.flags = im.alpha ? IMG_BLIT_SAVE_ALPHA : 0; img_draw(&im, &b); } out->len = outlen; out->dw = dw; out->dh = dh; out->nw = info.width; out->nh = info.height; out->alpha = im.alpha ? 1 : 0; out->format = info.format; free(arena); } static void fetch_fail(unsigned int status, int err, const char *ctype, const char *enc, const char *final) { static char meta[UHTTP_PATH_MAX + UHTTP_HOST_MAX + 256]; 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)fp.meta, (int)meta, (int)n); } /* One cookie jar for every helper of every browser window, kept in /share * (RAM, cleared at reboot) so a login session survives helper restarts and * the parallel workers. Re-read before each request and written back when * the response changed it; with several helpers the last writer wins. */ #define FETCH_JAR_PATH "/share/bcookies" static char fetch_jar_path[64] = FETCH_JAR_PATH; static struct uhttp_jar fetch_jar; static void jar_load(void) { int n = u_syscall3(SYS_READ_FILE, (int)fetch_jar_path, (int)&fetch_jar, (int)sizeof(fetch_jar)); if (n != (int)sizeof(fetch_jar)) uhttp_jar_init(&fetch_jar); } /* The jar as loaded before this request, and a scratch copy of the file. */ static struct uhttp_jar jar_before, jar_file; static const struct uhttp_cookie *jar_lookup(const struct uhttp_jar *jar, const struct uhttp_cookie *c) { int i; for (i = 0; i < UHTTP_COOKIE_MAX; i++) { const struct uhttp_cookie *o = &jar->c[i]; if (o->used && !strcmp(o->host, c->host) && !strcmp(o->path, c->path) && !strcmp(o->name, c->name)) return o; } return 0; } /* Replay one cookie into jar_file as a Set-Cookie line (or delete it). */ static void jar_replay(const struct uhttp_cookie *c, int del) { static char line[UHTTP_COOKIE_NAME_MAX + UHTTP_COOKIE_VALUE_MAX + UHTTP_COOKIE_PATH_MAX + 32]; unsigned int n = 0; app_str(line, &n, c->name, sizeof(line)); app_ch(line, &n, '=', sizeof(line)); if (!del) app_str(line, &n, c->value, sizeof(line)); app_str(line, &n, "; Path=", sizeof(line)); app_str(line, &n, c->path, sizeof(line)); if (c->secure) app_str(line, &n, "; Secure", sizeof(line)); if (del) app_str(line, &n, "; Max-Age=0", sizeof(line)); (void)uhttp_jar_set(&jar_file, c->host, c->path, line); } /* Save this request's cookie changes. Other helpers may have saved while * the request ran, so the file is re-read and only what this request added, * changed or deleted is applied on top (writing our whole copy back would * drop their cookies). */ static void jar_save(void) { int i, n; n = u_syscall3(SYS_READ_FILE, (int)fetch_jar_path, (int)&jar_file, (int)sizeof(jar_file)); if (n != (int)sizeof(jar_file)) uhttp_jar_init(&jar_file); for (i = 0; i < UHTTP_COOKIE_MAX; i++) { const struct uhttp_cookie *c = &fetch_jar.c[i]; const struct uhttp_cookie *o; if (!c->used) continue; o = jar_lookup(&jar_before, c); if (!o || strcmp(o->value, c->value) || o->secure != c->secure) jar_replay(c, 0); } for (i = 0; i < UHTTP_COOKIE_MAX; i++) { const struct uhttp_cookie *o = &jar_before.c[i]; if (o->used && !jar_lookup(&fetch_jar, o)) jar_replay(o, 1); } u_syscall3(SYS_WRITE_FILE, (int)fetch_jar_path, (int)&jar_file, (int)sizeof(jar_file)); } struct fetch_times { unsigned int open_ms; unsigned int first_ms; unsigned int total_ms; }; static void write_meta(unsigned int seq, int with_seq, struct uhttp *h, int err, unsigned int body_len, unsigned int body_trunc, const struct fetch_times *t, const struct fetch_px *px) { static char meta[UHTTP_PATH_MAX + UHTTP_HOST_MAX + 1024]; unsigned int n = 0; if (with_seq) { app_dec(meta, &n, seq, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); } 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, h->resp.content_encoding, 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)); app_dec(meta, &n, t ? t->open_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); app_dec(meta, &n, t ? t->first_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); app_dec(meta, &n, t ? t->total_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); /* Optional decoded bitmap: dw, dh, native w/h, alpha, px bytes, error, * format. */ app_dec(meta, &n, px ? px->dw : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); app_dec(meta, &n, px ? px->dh : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); app_dec(meta, &n, px ? px->nw : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); app_dec(meta, &n, px ? px->nh : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); app_dec(meta, &n, px ? px->alpha : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); app_dec(meta, &n, px ? px->len : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); app_dec(meta, &n, px ? (unsigned int)px->err : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); app_dec(meta, &n, px ? px->dec_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta)); u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n); } /* 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, const char *method, const char *ctype, const char *post, unsigned int post_len, unsigned char *buf, unsigned int *outlen, unsigned int *outtrunc, int *outerr, struct fetch_times *times, unsigned int open_flags, const char *prog, unsigned int prog_seq) { unsigned int total = 0; unsigned int t0 = u_ticks_ms(), t1, tfirst = 0; unsigned int last_prog = 0; int rv, trunc = 0, err = 0; *outlen = 0; *outtrunc = 0; *outerr = 0; if (times) { times->open_ms = 0; times->first_ms = 0; times->total_ms = 0; } if (!method) method = post_len ? "POST" : 0; if (!ctype || !ctype[0]) ctype = "application/x-www-form-urlencoded"; uhttp_set_method(h, method, post, post_len, ctype); rv = uhttp_open(h, url, open_flags, 0); if (rv == UHTTP_ERR_BUSY) { int tries; for (tries = 0; tries < 50 && rv == UHTTP_ERR_BUSY; tries++) { u_sleep_ms(20); rv = uhttp_open(h, url, open_flags, 0); } } t1 = u_ticks_ms(); if (times) { times->open_ms = t1 - t0; times->total_ms = t1 - t0; } if (rv != 0) { *outerr = rv; return; } if (prog && h->resp.have_length) write_progress(prog, prog_seq, 0, h->resp.content_length); last_prog = u_ticks_ms(); for (;;) { 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); if (rv > 0) { if (!tfirst) tfirst = u_ticks_ms(); 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) { trunc = 1; break; } continue; } if (rv < 0) err = rv; break; } if (prog) write_progress(prog, prog_seq, total, h->resp.have_length ? h->resp.content_length : 0); *outlen = total; *outtrunc = (unsigned int)trunc; *outerr = err; if (times) { times->first_ms = tfirst ? tfirst - t0 : 0; times->total_ms = u_ticks_ms() - t0; if (!times->total_ms) times->total_ms = 1; } } static void fetch_once(void) { static struct uhttp h; static char req[UHTTP_PATH_MAX + UHTTP_HOST_MAX + 256]; struct fetch_times times; fetch_paths_init(0, -1); unsigned char *buf; unsigned int cap = 0; unsigned int body_len = 0, body_trunc = 0; unsigned int i; int rn, err = 0; rn = u_syscall3(SYS_READ_FILE, (int)fp.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 *)malloc(FETCH_MAX); if (!buf) { fetch_fail(0, -1, "", "", ""); u_exit(1); } uhttp_init(&h); 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, ×, UHTTP_GZIP, 0, 0); if (body_len) u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf, (int)body_len); write_meta(0, 0, &h, err, body_len, body_trunc, ×, 0); uhttp_close(&h); free(buf); u_exit(err ? 1 : 0); } /* Long-lived helper: the browser writes a numbered request, we fetch it * (reusing the same TCP/TLS connection whenever the host is unchanged) and * publish body + meta + a small done marker. Idle instances exit and * release the NIC after ten seconds. */ static void fetch_serve(const char *arg) { static struct uhttp h; static char req[UHTTP_PATH_MAX + UHTTP_HOST_MAX + 256]; struct fetch_times times; unsigned char *buf, *post; unsigned int last_seq = 0, idle = 0, session = 0; int slot = -1; while (arg && *arg == ' ') arg++; if (arg && !strncmp(arg, "__serve", 7)) arg += 7; while (arg && *arg == ' ') arg++; if (arg && *arg >= '0' && *arg <= '9') { while (*arg >= '0' && *arg <= '9') session = session * 10 + (*arg++ - '0'); while (*arg == ' ') arg++; if (*arg >= '0' && *arg <= '9') { slot = 0; while (*arg >= '0' && *arg <= '9') slot = slot * 10 + (*arg++ - '0'); } } while (*arg == ' ') arg++; if (*arg == '/') { unsigned int n = 0; while (*arg && *arg != ' ' && n + 1 < sizeof(fetch_jar_path)) fetch_jar_path[n++] = *arg++; fetch_jar_path[n] = '\0'; } fetch_paths_init(session, slot); buf = (unsigned char *)malloc(FETCH_MAX); post = (unsigned char *)malloc(FETCH_POST_MAX); if (!buf || !post) u_exit(1); 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.keepalive = 1; h.jar = &fetch_jar; for (;;) { unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0; unsigned int post_len = 0, jar_changes; unsigned int dec_w = 0, dec_h = 0; int decode = 0, download = 0; int is_post = 0; struct fetch_px px; char method[12], ctype[96]; method[0] = '\0'; ctype[0] = '\0'; char done[16]; unsigned int dn = 0; unsigned int i = 0; char *url; int rn, err = 0; rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req, (int)(sizeof(req) - 1)); if (rn <= 0) { url = 0; } else { req[rn] = '\0'; while (req[i] >= '0' && req[i] <= '9') { seq = seq * 10 + (unsigned int)(req[i] - '0'); i++; } while (req[i] == ' ') i++; while (req[i] >= '0' && req[i] <= '9') { cap = cap * 10 + (unsigned int)(req[i] - '0'); i++; } while (req[i] == ' ') i++; method[0] = '\0'; ctype[0] = '\0'; /* " D ": decode the * response at the display bounds before publishing it. */ if (!strncmp(req + i, "D ", 2)) { i += 2; while (req[i] == ' ') i++; while (req[i] >= '0' && req[i] <= '9') { dec_w = dec_w * 10 + (unsigned int)(req[i] - '0'); i++; } while (req[i] == ' ') i++; while (req[i] >= '0' && req[i] <= '9') { dec_h = dec_h * 10 + (unsigned int)(req[i] - '0'); i++; } while (req[i] == ' ') i++; decode = 1; } /* "DL ": 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 ": * any method, for the browser's fetch()/XHR. */ if (!decode && !strncmp(req + i, "X ", 2)) { unsigned int k = 0; i += 2; while (req[i] && req[i] != ' ' && k + 1 < sizeof(method)) method[k++] = req[i++]; method[k] = '\0'; while (req[i] == ' ') i++; while (req[i] >= '0' && req[i] <= '9') { post_len = post_len * 10 + (unsigned int)(req[i] - '0'); i++; } while (req[i] == ' ') i++; k = 0; while (req[i] && req[i] != ' ' && k + 1 < sizeof(ctype)) ctype[k++] = req[i++]; ctype[k] = '\0'; if (!strcmp(ctype, "-")) ctype[0] = '\0'; while (req[i] == ' ') i++; is_post = post_len > 0 || strcmp(method, "GET"); } else if (!strncmp(req + i, "POST ", 5)) { is_post = 1; i += 5; while (req[i] >= '0' && req[i] <= '9') { post_len = post_len * 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'; url = (i < (unsigned int)rn) ? req + i : 0; } if (!url || !url[0] || seq == last_seq) { idle++; /* 10 ms keeps the next navigation's first request from * waiting on this poll; 1000 ticks is still ten idle * seconds before the helper releases the NIC. */ u_sleep_ms(10); if (idle >= 1000) break; continue; } idle = 0; last_seq = seq; if (cap == 0 || cap > FETCH_MAX) cap = FETCH_MAX; if (is_post) { int pn; if (post_len > FETCH_POST_MAX) post_len = FETCH_POST_MAX; pn = u_syscall3(SYS_READ_FILE, (int)fp.post, (int)post, (int)FETCH_POST_MAX); if (pn < 0) pn = 0; if ((unsigned int)pn < post_len) post_len = (unsigned int)pn; } else { post_len = 0; } jar_load(); memcpy(&jar_before, &fetch_jar, sizeof(jar_before)); jar_changes = fetch_jar.changes; /* Every request: uhttp_init() after a POST clears h.jar, and a * jar-less request after the OIDC login POST drops the * interaction cookie ("authorization request has expired"). */ h.jar = &fetch_jar; if (download) u_syscall1(SYS_UNLINK, (int)fp.prog); helper_fetch(&h, url, cap, method[0] ? method : 0, ctype, post_len ? (char *)post : 0, post_len, buf, &body_len, &body_trunc, &err, ×, download ? 0 : UHTTP_GZIP, download ? fp.prog : 0, seq); if (fetch_jar.changes != jar_changes) jar_save(); if (body_len) u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf, (int)body_len); memset(&px, 0, sizeof(px)); if (decode) { u_syscall1(SYS_UNLINK, (int)fp.px); if (body_len && !body_trunc && !err && h.resp.status == 200 && !is_post) { unsigned int td = u_ticks_ms(); fetch_decode(buf, body_len, dec_w, dec_h, &px); px.dec_ms = u_ticks_ms() - td; if (!px.err && px.data && px.len) u_syscall3(SYS_WRITE_FILE, (int)fp.px, (int)px.data, (int)px.len); } } write_meta(seq, 1, &h, err, body_len, body_trunc, ×, &px); if (px.data) free(px.data); app_dec(done, &dn, seq, sizeof(done)); u_syscall3(SYS_WRITE_FILE, (int)fp.done, (int)done, (int)dn); if (err || body_trunc || h.peer_close || is_post) { h.keepalive = 0; uhttp_close(&h); if (is_post) { 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.keepalive = 1; } else { h.keepalive = 1; } } } h.keepalive = 0; uhttp_close(&h); free(buf); free(post); u_exit(0); } void _start(const char *arg) { static struct uhttp h; unsigned char buf[512]; unsigned int total = 0, t0, t1 = 0, t2 = 0; int rv; if (arg && arg[0] == '_' && arg[1] == '_') { if (!strncmp(arg, "__fetch", 7)) { fetch_once(); u_exit(0); } if (!strncmp(arg, "__serve", 7)) { fetch_serve(arg); u_exit(0); } } if (!arg || !arg[0]) { u_puts("curl: usage curl \n"); u_exit(1); } uhttp_init(&h); h.user_agent = "nebos-curl/0.1"; u_puts("curl: GET "); u_puts(arg); u_putc('\n'); t0 = u_ticks_ms(); rv = uhttp_open(&h, arg, 0, &hooks); t1 = u_ticks_ms(); if (rv != 0) { u_puts("curl: "); u_puts(uhttp_error_string(rv)); u_putc('\n'); uhttp_close(&h); u_exit(1); } if (h.url.secure) u_puts("curl: WARNING: certificate NOT verified " "(trust-all mode)\n"); u_putc('\n'); for (;;) { rv = uhttp_read(&h, buf, sizeof(buf), UHTTP_IDLE_TIMEOUT_MS); if (rv > 0) { int i; if (!t2) t2 = u_ticks_ms(); for (i = 0; i < rv; i++) u_putc((char)buf[i]); total += (unsigned int)rv; continue; } if (rv < 0) { u_puts("\ncurl: "); u_puts(uhttp_error_string(rv)); u_puts(" while reading body\n"); } break; } u_putc('\n'); u_puts("curl: "); u_putdec(total); u_puts(" bytes received, status "); u_putdec((unsigned int)h.resp.status); if (t2) { u_puts(", "); u_putdec(t2 - t0); u_puts(" ms to first byte"); } u_puts(", "); u_putdec(u_ticks_ms() - t0); u_puts(" ms total"); if (!t2 && t1) { u_puts(" (headers "); u_putdec(t1 - t0); u_puts(" ms)"); } u_putc('\n'); uhttp_close(&h); u_exit(0); }