ucmd_curl.c 13 KB

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