| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656 |
- /* 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 <stdlib.h>
- #include <string.h>
- #include "ucmd_lib.h"
- #include "uhttp.h"
- #include "uhttp_cookie.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 (2u * 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];
- };
- 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);
- }
- static void fetch_fail(unsigned int status, int err, const char *ctype,
- const char *enc, const char *final)
- {
- char meta[768];
- 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 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)
- {
- char meta[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));
- u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
- }
- /* Fetch one URL through a possibly reused keep-alive connection. */
- 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 total = 0;
- unsigned int t0 = u_ticks_ms(), t1, tfirst = 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, UHTTP_GZIP, 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, UHTTP_GZIP, 0);
- }
- }
- t1 = u_ticks_ms();
- if (times) {
- times->open_ms = t1 - t0;
- times->total_ms = t1 - t0;
- }
- if (rv != 0) {
- *outerr = rv;
- return;
- }
- for (;;) {
- rv = uhttp_read(h, buf + total, cap - total,
- UHTTP_IDLE_TIMEOUT_MS);
- if (rv > 0) {
- if (!tfirst)
- tfirst = u_ticks_ms();
- total += (unsigned int)rv;
- if (total >= cap) {
- trunc = 1;
- break;
- }
- continue;
- }
- if (rv < 0)
- err = rv;
- break;
- }
- *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;
- char req[768];
- 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,
- ×);
- 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, ×);
- 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;
- char req[768];
- 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');
- }
- }
- 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;
- int is_post = 0;
- 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';
- /* "X <method> <body length> <content type or -> <url>":
- * any method, for the browser's fetch()/XHR. */
- if (!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;
- helper_fetch(&h, url, cap, method[0] ? method : 0, ctype,
- post_len ? (char *)post : 0, post_len,
- buf, &body_len, &body_trunc, &err, ×);
- if (fetch_jar.changes != jar_changes)
- jar_save();
- if (body_len)
- u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
- (int)body_len);
- write_meta(seq, 1, &h, err, body_len, body_trunc, ×);
- 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 <http|https://host[:port]/[path]>\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);
- }
|