| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- /* 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"
- #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_REQ "/share/bfreq"
- #define FETCH_BODY "/share/bfbody"
- #define FETCH_META "/share/bfmeta"
- #define FETCH_DONE "/share/bfdone"
- #define FETCH_POST "/share/bfpost"
- #define FETCH_POST_MAX 4096u
- #define FETCH_MAX (2u * 1024u * 1024u)
- static unsigned int c_len(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- 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_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)FETCH_META, (int)meta, (int)n);
- }
- static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
- int err, unsigned int body_len, unsigned int body_trunc)
- {
- 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));
- u_syscall3(SYS_WRITE_FILE, (int)FETCH_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 *post, unsigned int post_len,
- unsigned char *buf, unsigned int *outlen,
- unsigned int *outtrunc, int *outerr)
- {
- unsigned int total = 0;
- int rv, trunc = 0, err = 0;
- *outlen = 0;
- *outtrunc = 0;
- *outerr = 0;
- uhttp_set_method(h, post_len ? "POST" : 0, post, post_len,
- "application/x-www-form-urlencoded");
- 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);
- }
- }
- if (rv != 0) {
- *outerr = rv;
- return;
- }
- for (;;) {
- rv = uhttp_read(h, buf + total, cap - total,
- UHTTP_IDLE_TIMEOUT_MS);
- if (rv > 0) {
- 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;
- }
- static void fetch_once(void)
- {
- static struct uhttp h;
- char req[768];
- 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)FETCH_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, buf, &body_len, &body_trunc, &err);
- if (body_len)
- u_syscall3(SYS_WRITE_FILE, (int)FETCH_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(void)
- {
- static struct uhttp h;
- char req[768];
- unsigned char *buf, *post;
- unsigned int last_seq = 0, idle = 0;
- 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;
- for (;;) {
- unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
- unsigned int post_len = 0;
- int is_post = 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)FETCH_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++;
- 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++;
- u_sleep_ms(50);
- if (idle >= 200)
- 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)FETCH_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;
- }
- helper_fetch(&h, url, cap, is_post ? (char *)post : 0, post_len,
- buf, &body_len, &body_trunc, &err);
- if (body_len)
- u_syscall3(SYS_WRITE_FILE, (int)FETCH_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)FETCH_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;
- int rv;
- if (arg && c_len(arg) == 7 && arg[0] == '_' && arg[1] == '_') {
- if (arg[2] == 'f')
- fetch_once();
- else if (arg[2] == 's')
- fetch_serve();
- }
- 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');
- rv = uhttp_open(&h, arg, 0, &hooks);
- 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;
- 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);
- u_putc('\n');
- uhttp_close(&h);
- u_exit(0);
- }
|