ucmd_curl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /* curl: ring-3 command. Fetch one http:// or https:// URL with the shared
  2. * uhttp client and dump the status line, headers and body to the terminal.
  3. *
  4. * It also has a headless `__fetch` mode used by the browser: read
  5. * /share/bfreq, fetch it, and write the body plus /share/bfmeta for the GUI
  6. * process. That keeps DNS/TLS/body waits out of the browser's UI loop.
  7. *
  8. * The TLS and HTTP code lives in uhttp.c (shared with the browser); this file
  9. * only keeps the CLI, the helper mode and the verbose output hooks. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "ucmd_lib.h"
  13. #include "uhttp.h"
  14. #include "uhttp_cookie.h"
  15. #define LINE_MAX_C 320
  16. static void on_connected(void *ctx, int secure)
  17. {
  18. (void)ctx;
  19. if (secure) {
  20. u_puts("curl: connected, TLS handshake\n");
  21. } else {
  22. u_puts("curl: connected, sending request\n");
  23. }
  24. }
  25. static void on_status(void *ctx, int code, const char *line)
  26. {
  27. (void)ctx;
  28. (void)code;
  29. u_puts("curl: ");
  30. u_puts(line);
  31. u_putc('\n');
  32. }
  33. static void on_header(void *ctx, const char *line)
  34. {
  35. (void)ctx;
  36. u_puts(line);
  37. u_putc('\n');
  38. }
  39. static void on_redirect(void *ctx, const char *location)
  40. {
  41. (void)ctx;
  42. u_puts("curl: redirect -> ");
  43. u_puts(location);
  44. u_putc('\n');
  45. }
  46. static const struct uhttp_hooks hooks = {
  47. .connected = on_connected,
  48. .status = on_status,
  49. .header = on_header,
  50. .redirect = on_redirect,
  51. };
  52. /* ---- headless fetch helper for the browser ---------------------------- */
  53. #define FETCH_PATH_MAX 32
  54. #define FETCH_POST_MAX 16384u
  55. #define FETCH_MAX (2u * 1024u * 1024u)
  56. /* Each browser worker gets its own set of /share files; slot -1 keeps the
  57. * historical single-helper names for the one-shot __fetch mode. */
  58. struct fetch_paths {
  59. char req[FETCH_PATH_MAX];
  60. char body[FETCH_PATH_MAX];
  61. char meta[FETCH_PATH_MAX];
  62. char done[FETCH_PATH_MAX];
  63. char post[FETCH_PATH_MAX];
  64. };
  65. static struct fetch_paths fp;
  66. static void app_ch(char *d, unsigned int *n, char c, unsigned int cap)
  67. {
  68. if (*n + 1 < cap)
  69. d[(*n)++] = c;
  70. d[*n] = '\0';
  71. }
  72. static void app_str(char *d, unsigned int *n, const char *s, unsigned int cap)
  73. {
  74. while (s && *s)
  75. app_ch(d, n, *s++, cap);
  76. }
  77. static void app_dec(char *d, unsigned int *n, unsigned int v, unsigned int cap)
  78. {
  79. char tmp[12];
  80. unsigned int i = 0, j;
  81. do {
  82. tmp[i++] = (char)('0' + v % 10);
  83. v /= 10;
  84. } while (v && i < sizeof(tmp));
  85. for (j = 0; j < i; j++)
  86. app_ch(d, n, tmp[i - 1 - j], cap);
  87. }
  88. static void fetch_path(char *out, unsigned int cap, const char *kind,
  89. unsigned int session, int slot)
  90. {
  91. unsigned int n = 0;
  92. app_str(out, &n, "/share/bf", cap);
  93. if (slot >= 0) {
  94. app_dec(out, &n, session, cap);
  95. app_ch(out, &n, '_', cap);
  96. app_dec(out, &n, (unsigned int)slot, cap);
  97. }
  98. app_str(out, &n, kind, cap);
  99. }
  100. static void fetch_paths_init(unsigned int session, int slot)
  101. {
  102. fetch_path(fp.req, sizeof(fp.req), "req", session, slot);
  103. fetch_path(fp.body, sizeof(fp.body), "body", session, slot);
  104. fetch_path(fp.meta, sizeof(fp.meta), "meta", session, slot);
  105. fetch_path(fp.done, sizeof(fp.done), "done", session, slot);
  106. fetch_path(fp.post, sizeof(fp.post), "post", session, slot);
  107. }
  108. static void fetch_fail(unsigned int status, int err, const char *ctype,
  109. const char *enc, const char *final)
  110. {
  111. char meta[768];
  112. unsigned int n = 0;
  113. app_dec(meta, &n, status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  114. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  115. app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  116. app_str(meta, &n, enc ? enc : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  117. app_str(meta, &n, ctype ? ctype : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  118. app_str(meta, &n, final ? final : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  119. app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  120. u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
  121. }
  122. /* One cookie jar for every helper of every browser window, kept in /share
  123. * (RAM, cleared at reboot) so a login session survives helper restarts and
  124. * the parallel workers. Re-read before each request and written back when
  125. * the response changed it; with several helpers the last writer wins. */
  126. #define FETCH_JAR_PATH "/share/bcookies"
  127. static struct uhttp_jar fetch_jar;
  128. static void jar_load(void)
  129. {
  130. int n = u_syscall3(SYS_READ_FILE, (int)FETCH_JAR_PATH,
  131. (int)&fetch_jar, (int)sizeof(fetch_jar));
  132. if (n != (int)sizeof(fetch_jar))
  133. uhttp_jar_init(&fetch_jar);
  134. }
  135. static void jar_save(void)
  136. {
  137. u_syscall3(SYS_WRITE_FILE, (int)FETCH_JAR_PATH, (int)&fetch_jar,
  138. (int)sizeof(fetch_jar));
  139. }
  140. struct fetch_times {
  141. unsigned int open_ms;
  142. unsigned int first_ms;
  143. unsigned int total_ms;
  144. };
  145. static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
  146. int err, unsigned int body_len, unsigned int body_trunc,
  147. const struct fetch_times *t)
  148. {
  149. char meta[1024];
  150. unsigned int n = 0;
  151. if (with_seq) {
  152. app_dec(meta, &n, seq, sizeof(meta));
  153. app_ch(meta, &n, '\n', sizeof(meta));
  154. }
  155. app_dec(meta, &n, (unsigned int)h->resp.status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  156. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  157. app_dec(meta, &n, body_trunc, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  158. app_str(meta, &n, h->resp.content_encoding, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  159. app_str(meta, &n, h->resp.content_type, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  160. app_str(meta, &n, h->resp.final_url, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  161. app_dec(meta, &n, body_len, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  162. app_dec(meta, &n, t ? t->open_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  163. app_dec(meta, &n, t ? t->first_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  164. app_dec(meta, &n, t ? t->total_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  165. u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
  166. }
  167. /* Fetch one URL through a possibly reused keep-alive connection. */
  168. static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
  169. const char *method, const char *ctype,
  170. const char *post, unsigned int post_len,
  171. unsigned char *buf, unsigned int *outlen,
  172. unsigned int *outtrunc, int *outerr,
  173. struct fetch_times *times)
  174. {
  175. unsigned int total = 0;
  176. unsigned int t0 = u_ticks_ms(), t1, tfirst = 0;
  177. int rv, trunc = 0, err = 0;
  178. *outlen = 0;
  179. *outtrunc = 0;
  180. *outerr = 0;
  181. if (times) {
  182. times->open_ms = 0;
  183. times->first_ms = 0;
  184. times->total_ms = 0;
  185. }
  186. if (!method)
  187. method = post_len ? "POST" : 0;
  188. if (!ctype || !ctype[0])
  189. ctype = "application/x-www-form-urlencoded";
  190. uhttp_set_method(h, method, post, post_len, ctype);
  191. rv = uhttp_open(h, url, UHTTP_GZIP, 0);
  192. if (rv == UHTTP_ERR_BUSY) {
  193. int tries;
  194. for (tries = 0; tries < 50 && rv == UHTTP_ERR_BUSY; tries++) {
  195. u_sleep_ms(20);
  196. rv = uhttp_open(h, url, UHTTP_GZIP, 0);
  197. }
  198. }
  199. t1 = u_ticks_ms();
  200. if (times) {
  201. times->open_ms = t1 - t0;
  202. times->total_ms = t1 - t0;
  203. }
  204. if (rv != 0) {
  205. *outerr = rv;
  206. return;
  207. }
  208. for (;;) {
  209. rv = uhttp_read(h, buf + total, cap - total,
  210. UHTTP_IDLE_TIMEOUT_MS);
  211. if (rv > 0) {
  212. if (!tfirst)
  213. tfirst = u_ticks_ms();
  214. total += (unsigned int)rv;
  215. if (total >= cap) {
  216. trunc = 1;
  217. break;
  218. }
  219. continue;
  220. }
  221. if (rv < 0)
  222. err = rv;
  223. break;
  224. }
  225. *outlen = total;
  226. *outtrunc = (unsigned int)trunc;
  227. *outerr = err;
  228. if (times) {
  229. times->first_ms = tfirst ? tfirst - t0 : 0;
  230. times->total_ms = u_ticks_ms() - t0;
  231. if (!times->total_ms)
  232. times->total_ms = 1;
  233. }
  234. }
  235. static void fetch_once(void)
  236. {
  237. static struct uhttp h;
  238. char req[768];
  239. struct fetch_times times;
  240. fetch_paths_init(0, -1);
  241. unsigned char *buf;
  242. unsigned int cap = 0;
  243. unsigned int body_len = 0, body_trunc = 0;
  244. unsigned int i;
  245. int rn, err = 0;
  246. rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req,
  247. (int)(sizeof(req) - 1));
  248. if (rn <= 0)
  249. u_exit(1);
  250. req[rn] = '\0';
  251. i = 0;
  252. while (req[i] >= '0' && req[i] <= '9') {
  253. cap = cap * 10 + (unsigned int)(req[i] - '0');
  254. i++;
  255. }
  256. while (req[i] == ' ')
  257. i++;
  258. while (rn > 0 && (req[rn - 1] == '\n' || req[rn - 1] == '\r'))
  259. req[--rn] = '\0';
  260. if (i >= (unsigned int)rn || !req[i]) {
  261. fetch_fail(0, -1, "", "", "");
  262. u_exit(1);
  263. }
  264. if (cap == 0 || cap > FETCH_MAX)
  265. cap = FETCH_MAX;
  266. buf = (unsigned char *)malloc(FETCH_MAX);
  267. if (!buf) {
  268. fetch_fail(0, -1, "", "", "");
  269. u_exit(1);
  270. }
  271. uhttp_init(&h);
  272. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  273. helper_fetch(&h, req + i, cap, 0, 0, 0, 0, buf, &body_len, &body_trunc, &err,
  274. &times);
  275. if (body_len)
  276. u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
  277. (int)body_len);
  278. write_meta(0, 0, &h, err, body_len, body_trunc, &times);
  279. uhttp_close(&h);
  280. free(buf);
  281. u_exit(err ? 1 : 0);
  282. }
  283. /* Long-lived helper: the browser writes a numbered request, we fetch it
  284. * (reusing the same TCP/TLS connection whenever the host is unchanged) and
  285. * publish body + meta + a small done marker. Idle instances exit and
  286. * release the NIC after ten seconds. */
  287. static void fetch_serve(const char *arg)
  288. {
  289. static struct uhttp h;
  290. char req[768];
  291. struct fetch_times times;
  292. unsigned char *buf, *post;
  293. unsigned int last_seq = 0, idle = 0, session = 0;
  294. int slot = -1;
  295. while (arg && *arg == ' ')
  296. arg++;
  297. if (arg && !strncmp(arg, "__serve", 7))
  298. arg += 7;
  299. while (arg && *arg == ' ')
  300. arg++;
  301. if (arg && *arg >= '0' && *arg <= '9') {
  302. while (*arg >= '0' && *arg <= '9')
  303. session = session * 10 + (*arg++ - '0');
  304. while (*arg == ' ')
  305. arg++;
  306. if (*arg >= '0' && *arg <= '9') {
  307. slot = 0;
  308. while (*arg >= '0' && *arg <= '9')
  309. slot = slot * 10 + (*arg++ - '0');
  310. }
  311. }
  312. fetch_paths_init(session, slot);
  313. buf = (unsigned char *)malloc(FETCH_MAX);
  314. post = (unsigned char *)malloc(FETCH_POST_MAX);
  315. if (!buf || !post)
  316. u_exit(1);
  317. uhttp_init(&h);
  318. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  319. h.keepalive = 1;
  320. h.jar = &fetch_jar;
  321. for (;;) {
  322. unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
  323. unsigned int post_len = 0, jar_changes;
  324. int is_post = 0;
  325. char method[12], ctype[96];
  326. method[0] = '\0';
  327. ctype[0] = '\0';
  328. char done[16];
  329. unsigned int dn = 0;
  330. unsigned int i = 0;
  331. char *url;
  332. int rn, err = 0;
  333. rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req,
  334. (int)(sizeof(req) - 1));
  335. if (rn <= 0) {
  336. url = 0;
  337. } else {
  338. req[rn] = '\0';
  339. while (req[i] >= '0' && req[i] <= '9') {
  340. seq = seq * 10 + (unsigned int)(req[i] - '0');
  341. i++;
  342. }
  343. while (req[i] == ' ')
  344. i++;
  345. while (req[i] >= '0' && req[i] <= '9') {
  346. cap = cap * 10 + (unsigned int)(req[i] - '0');
  347. i++;
  348. }
  349. while (req[i] == ' ')
  350. i++;
  351. method[0] = '\0';
  352. ctype[0] = '\0';
  353. /* "X <method> <body length> <content type or -> <url>":
  354. * any method, for the browser's fetch()/XHR. */
  355. if (!strncmp(req + i, "X ", 2)) {
  356. unsigned int k = 0;
  357. i += 2;
  358. while (req[i] && req[i] != ' ' &&
  359. k + 1 < sizeof(method))
  360. method[k++] = req[i++];
  361. method[k] = '\0';
  362. while (req[i] == ' ')
  363. i++;
  364. while (req[i] >= '0' && req[i] <= '9') {
  365. post_len = post_len * 10 +
  366. (unsigned int)(req[i] - '0');
  367. i++;
  368. }
  369. while (req[i] == ' ')
  370. i++;
  371. k = 0;
  372. while (req[i] && req[i] != ' ' &&
  373. k + 1 < sizeof(ctype))
  374. ctype[k++] = req[i++];
  375. ctype[k] = '\0';
  376. if (!strcmp(ctype, "-"))
  377. ctype[0] = '\0';
  378. while (req[i] == ' ')
  379. i++;
  380. is_post = post_len > 0 || strcmp(method, "GET");
  381. } else if (!strncmp(req + i, "POST ", 5)) {
  382. is_post = 1;
  383. i += 5;
  384. while (req[i] >= '0' && req[i] <= '9') {
  385. post_len = post_len * 10 +
  386. (unsigned int)(req[i] - '0');
  387. i++;
  388. }
  389. while (req[i] == ' ')
  390. i++;
  391. }
  392. while (rn > 0 && (req[rn - 1] == '\n' ||
  393. req[rn - 1] == '\r'))
  394. req[--rn] = '\0';
  395. url = (i < (unsigned int)rn) ? req + i : 0;
  396. }
  397. if (!url || !url[0] || seq == last_seq) {
  398. idle++;
  399. /* 10 ms keeps the next navigation's first request from
  400. * waiting on this poll; 1000 ticks is still ten idle
  401. * seconds before the helper releases the NIC. */
  402. u_sleep_ms(10);
  403. if (idle >= 1000)
  404. break;
  405. continue;
  406. }
  407. idle = 0;
  408. last_seq = seq;
  409. if (cap == 0 || cap > FETCH_MAX)
  410. cap = FETCH_MAX;
  411. if (is_post) {
  412. int pn;
  413. if (post_len > FETCH_POST_MAX)
  414. post_len = FETCH_POST_MAX;
  415. pn = u_syscall3(SYS_READ_FILE, (int)fp.post,
  416. (int)post, (int)FETCH_POST_MAX);
  417. if (pn < 0)
  418. pn = 0;
  419. if ((unsigned int)pn < post_len)
  420. post_len = (unsigned int)pn;
  421. } else {
  422. post_len = 0;
  423. }
  424. jar_load();
  425. jar_changes = fetch_jar.changes;
  426. helper_fetch(&h, url, cap, method[0] ? method : 0, ctype,
  427. post_len ? (char *)post : 0, post_len,
  428. buf, &body_len, &body_trunc, &err, &times);
  429. if (fetch_jar.changes != jar_changes)
  430. jar_save();
  431. if (body_len)
  432. u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
  433. (int)body_len);
  434. write_meta(seq, 1, &h, err, body_len, body_trunc, &times);
  435. app_dec(done, &dn, seq, sizeof(done));
  436. u_syscall3(SYS_WRITE_FILE, (int)fp.done, (int)done, (int)dn);
  437. if (err || body_trunc || h.peer_close || is_post) {
  438. h.keepalive = 0;
  439. uhttp_close(&h);
  440. if (is_post) {
  441. uhttp_init(&h);
  442. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  443. h.keepalive = 1;
  444. } else {
  445. h.keepalive = 1;
  446. }
  447. }
  448. }
  449. h.keepalive = 0;
  450. uhttp_close(&h);
  451. free(buf);
  452. free(post);
  453. u_exit(0);
  454. }
  455. void _start(const char *arg)
  456. {
  457. static struct uhttp h;
  458. unsigned char buf[512];
  459. unsigned int total = 0, t0, t1 = 0, t2 = 0;
  460. int rv;
  461. if (arg && arg[0] == '_' && arg[1] == '_') {
  462. if (!strncmp(arg, "__fetch", 7)) {
  463. fetch_once();
  464. u_exit(0);
  465. }
  466. if (!strncmp(arg, "__serve", 7)) {
  467. fetch_serve(arg);
  468. u_exit(0);
  469. }
  470. }
  471. if (!arg || !arg[0]) {
  472. u_puts("curl: usage curl <http|https://host[:port]/[path]>\n");
  473. u_exit(1);
  474. }
  475. uhttp_init(&h);
  476. h.user_agent = "nebos-curl/0.1";
  477. u_puts("curl: GET ");
  478. u_puts(arg);
  479. u_putc('\n');
  480. t0 = u_ticks_ms();
  481. rv = uhttp_open(&h, arg, 0, &hooks);
  482. t1 = u_ticks_ms();
  483. if (rv != 0) {
  484. u_puts("curl: ");
  485. u_puts(uhttp_error_string(rv));
  486. u_putc('\n');
  487. uhttp_close(&h);
  488. u_exit(1);
  489. }
  490. if (h.url.secure)
  491. u_puts("curl: WARNING: certificate NOT verified "
  492. "(trust-all mode)\n");
  493. u_putc('\n');
  494. for (;;) {
  495. rv = uhttp_read(&h, buf, sizeof(buf), UHTTP_IDLE_TIMEOUT_MS);
  496. if (rv > 0) {
  497. int i;
  498. if (!t2)
  499. t2 = u_ticks_ms();
  500. for (i = 0; i < rv; i++)
  501. u_putc((char)buf[i]);
  502. total += (unsigned int)rv;
  503. continue;
  504. }
  505. if (rv < 0) {
  506. u_puts("\ncurl: ");
  507. u_puts(uhttp_error_string(rv));
  508. u_puts(" while reading body\n");
  509. }
  510. break;
  511. }
  512. u_putc('\n');
  513. u_puts("curl: ");
  514. u_putdec(total);
  515. u_puts(" bytes received, status ");
  516. u_putdec((unsigned int)h.resp.status);
  517. if (t2) {
  518. u_puts(", ");
  519. u_putdec(t2 - t0);
  520. u_puts(" ms to first byte");
  521. }
  522. u_puts(", ");
  523. u_putdec(u_ticks_ms() - t0);
  524. u_puts(" ms total");
  525. if (!t2 && t1) {
  526. u_puts(" (headers ");
  527. u_putdec(t1 - t0);
  528. u_puts(" ms)");
  529. }
  530. u_putc('\n');
  531. uhttp_close(&h);
  532. u_exit(0);
  533. }