ucmd_curl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. /* The jar as loaded before this request, and a scratch copy of the file. */
  136. static struct uhttp_jar jar_before, jar_file;
  137. static const struct uhttp_cookie *jar_lookup(const struct uhttp_jar *jar,
  138. const struct uhttp_cookie *c)
  139. {
  140. int i;
  141. for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
  142. const struct uhttp_cookie *o = &jar->c[i];
  143. if (o->used && !strcmp(o->host, c->host) &&
  144. !strcmp(o->path, c->path) && !strcmp(o->name, c->name))
  145. return o;
  146. }
  147. return 0;
  148. }
  149. /* Replay one cookie into jar_file as a Set-Cookie line (or delete it). */
  150. static void jar_replay(const struct uhttp_cookie *c, int del)
  151. {
  152. static char line[UHTTP_COOKIE_NAME_MAX + UHTTP_COOKIE_VALUE_MAX +
  153. UHTTP_COOKIE_PATH_MAX + 32];
  154. unsigned int n = 0;
  155. app_str(line, &n, c->name, sizeof(line));
  156. app_ch(line, &n, '=', sizeof(line));
  157. if (!del)
  158. app_str(line, &n, c->value, sizeof(line));
  159. app_str(line, &n, "; Path=", sizeof(line));
  160. app_str(line, &n, c->path, sizeof(line));
  161. if (c->secure)
  162. app_str(line, &n, "; Secure", sizeof(line));
  163. if (del)
  164. app_str(line, &n, "; Max-Age=0", sizeof(line));
  165. (void)uhttp_jar_set(&jar_file, c->host, c->path, line);
  166. }
  167. /* Save this request's cookie changes. Other helpers may have saved while
  168. * the request ran, so the file is re-read and only what this request added,
  169. * changed or deleted is applied on top (writing our whole copy back would
  170. * drop their cookies). */
  171. static void jar_save(void)
  172. {
  173. int i, n;
  174. n = u_syscall3(SYS_READ_FILE, (int)FETCH_JAR_PATH, (int)&jar_file,
  175. (int)sizeof(jar_file));
  176. if (n != (int)sizeof(jar_file))
  177. uhttp_jar_init(&jar_file);
  178. for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
  179. const struct uhttp_cookie *c = &fetch_jar.c[i];
  180. const struct uhttp_cookie *o;
  181. if (!c->used)
  182. continue;
  183. o = jar_lookup(&jar_before, c);
  184. if (!o || strcmp(o->value, c->value) || o->secure != c->secure)
  185. jar_replay(c, 0);
  186. }
  187. for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
  188. const struct uhttp_cookie *o = &jar_before.c[i];
  189. if (o->used && !jar_lookup(&fetch_jar, o))
  190. jar_replay(o, 1);
  191. }
  192. u_syscall3(SYS_WRITE_FILE, (int)FETCH_JAR_PATH, (int)&jar_file,
  193. (int)sizeof(jar_file));
  194. }
  195. struct fetch_times {
  196. unsigned int open_ms;
  197. unsigned int first_ms;
  198. unsigned int total_ms;
  199. };
  200. static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
  201. int err, unsigned int body_len, unsigned int body_trunc,
  202. const struct fetch_times *t)
  203. {
  204. char meta[1024];
  205. unsigned int n = 0;
  206. if (with_seq) {
  207. app_dec(meta, &n, seq, sizeof(meta));
  208. app_ch(meta, &n, '\n', sizeof(meta));
  209. }
  210. app_dec(meta, &n, (unsigned int)h->resp.status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  211. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  212. app_dec(meta, &n, body_trunc, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  213. app_str(meta, &n, h->resp.content_encoding, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  214. app_str(meta, &n, h->resp.content_type, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  215. app_str(meta, &n, h->resp.final_url, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  216. app_dec(meta, &n, body_len, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  217. app_dec(meta, &n, t ? t->open_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  218. app_dec(meta, &n, t ? t->first_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  219. app_dec(meta, &n, t ? t->total_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  220. u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
  221. }
  222. /* Fetch one URL through a possibly reused keep-alive connection. */
  223. static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
  224. const char *method, const char *ctype,
  225. const char *post, unsigned int post_len,
  226. unsigned char *buf, unsigned int *outlen,
  227. unsigned int *outtrunc, int *outerr,
  228. struct fetch_times *times)
  229. {
  230. unsigned int total = 0;
  231. unsigned int t0 = u_ticks_ms(), t1, tfirst = 0;
  232. int rv, trunc = 0, err = 0;
  233. *outlen = 0;
  234. *outtrunc = 0;
  235. *outerr = 0;
  236. if (times) {
  237. times->open_ms = 0;
  238. times->first_ms = 0;
  239. times->total_ms = 0;
  240. }
  241. if (!method)
  242. method = post_len ? "POST" : 0;
  243. if (!ctype || !ctype[0])
  244. ctype = "application/x-www-form-urlencoded";
  245. uhttp_set_method(h, method, post, post_len, ctype);
  246. rv = uhttp_open(h, url, UHTTP_GZIP, 0);
  247. if (rv == UHTTP_ERR_BUSY) {
  248. int tries;
  249. for (tries = 0; tries < 50 && rv == UHTTP_ERR_BUSY; tries++) {
  250. u_sleep_ms(20);
  251. rv = uhttp_open(h, url, UHTTP_GZIP, 0);
  252. }
  253. }
  254. t1 = u_ticks_ms();
  255. if (times) {
  256. times->open_ms = t1 - t0;
  257. times->total_ms = t1 - t0;
  258. }
  259. if (rv != 0) {
  260. *outerr = rv;
  261. return;
  262. }
  263. for (;;) {
  264. rv = uhttp_read(h, buf + total, cap - total,
  265. UHTTP_IDLE_TIMEOUT_MS);
  266. if (rv > 0) {
  267. if (!tfirst)
  268. tfirst = u_ticks_ms();
  269. total += (unsigned int)rv;
  270. if (total >= cap) {
  271. trunc = 1;
  272. break;
  273. }
  274. continue;
  275. }
  276. if (rv < 0)
  277. err = rv;
  278. break;
  279. }
  280. *outlen = total;
  281. *outtrunc = (unsigned int)trunc;
  282. *outerr = err;
  283. if (times) {
  284. times->first_ms = tfirst ? tfirst - t0 : 0;
  285. times->total_ms = u_ticks_ms() - t0;
  286. if (!times->total_ms)
  287. times->total_ms = 1;
  288. }
  289. }
  290. static void fetch_once(void)
  291. {
  292. static struct uhttp h;
  293. char req[768];
  294. struct fetch_times times;
  295. fetch_paths_init(0, -1);
  296. unsigned char *buf;
  297. unsigned int cap = 0;
  298. unsigned int body_len = 0, body_trunc = 0;
  299. unsigned int i;
  300. int rn, err = 0;
  301. rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req,
  302. (int)(sizeof(req) - 1));
  303. if (rn <= 0)
  304. u_exit(1);
  305. req[rn] = '\0';
  306. i = 0;
  307. while (req[i] >= '0' && req[i] <= '9') {
  308. cap = cap * 10 + (unsigned int)(req[i] - '0');
  309. i++;
  310. }
  311. while (req[i] == ' ')
  312. i++;
  313. while (rn > 0 && (req[rn - 1] == '\n' || req[rn - 1] == '\r'))
  314. req[--rn] = '\0';
  315. if (i >= (unsigned int)rn || !req[i]) {
  316. fetch_fail(0, -1, "", "", "");
  317. u_exit(1);
  318. }
  319. if (cap == 0 || cap > FETCH_MAX)
  320. cap = FETCH_MAX;
  321. buf = (unsigned char *)malloc(FETCH_MAX);
  322. if (!buf) {
  323. fetch_fail(0, -1, "", "", "");
  324. u_exit(1);
  325. }
  326. uhttp_init(&h);
  327. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  328. helper_fetch(&h, req + i, cap, 0, 0, 0, 0, buf, &body_len, &body_trunc, &err,
  329. &times);
  330. if (body_len)
  331. u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
  332. (int)body_len);
  333. write_meta(0, 0, &h, err, body_len, body_trunc, &times);
  334. uhttp_close(&h);
  335. free(buf);
  336. u_exit(err ? 1 : 0);
  337. }
  338. /* Long-lived helper: the browser writes a numbered request, we fetch it
  339. * (reusing the same TCP/TLS connection whenever the host is unchanged) and
  340. * publish body + meta + a small done marker. Idle instances exit and
  341. * release the NIC after ten seconds. */
  342. static void fetch_serve(const char *arg)
  343. {
  344. static struct uhttp h;
  345. char req[768];
  346. struct fetch_times times;
  347. unsigned char *buf, *post;
  348. unsigned int last_seq = 0, idle = 0, session = 0;
  349. int slot = -1;
  350. while (arg && *arg == ' ')
  351. arg++;
  352. if (arg && !strncmp(arg, "__serve", 7))
  353. arg += 7;
  354. while (arg && *arg == ' ')
  355. arg++;
  356. if (arg && *arg >= '0' && *arg <= '9') {
  357. while (*arg >= '0' && *arg <= '9')
  358. session = session * 10 + (*arg++ - '0');
  359. while (*arg == ' ')
  360. arg++;
  361. if (*arg >= '0' && *arg <= '9') {
  362. slot = 0;
  363. while (*arg >= '0' && *arg <= '9')
  364. slot = slot * 10 + (*arg++ - '0');
  365. }
  366. }
  367. fetch_paths_init(session, slot);
  368. buf = (unsigned char *)malloc(FETCH_MAX);
  369. post = (unsigned char *)malloc(FETCH_POST_MAX);
  370. if (!buf || !post)
  371. u_exit(1);
  372. uhttp_init(&h);
  373. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  374. h.keepalive = 1;
  375. h.jar = &fetch_jar;
  376. for (;;) {
  377. unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
  378. unsigned int post_len = 0, jar_changes;
  379. int is_post = 0;
  380. char method[12], ctype[96];
  381. method[0] = '\0';
  382. ctype[0] = '\0';
  383. char done[16];
  384. unsigned int dn = 0;
  385. unsigned int i = 0;
  386. char *url;
  387. int rn, err = 0;
  388. rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req,
  389. (int)(sizeof(req) - 1));
  390. if (rn <= 0) {
  391. url = 0;
  392. } else {
  393. req[rn] = '\0';
  394. while (req[i] >= '0' && req[i] <= '9') {
  395. seq = seq * 10 + (unsigned int)(req[i] - '0');
  396. i++;
  397. }
  398. while (req[i] == ' ')
  399. i++;
  400. while (req[i] >= '0' && req[i] <= '9') {
  401. cap = cap * 10 + (unsigned int)(req[i] - '0');
  402. i++;
  403. }
  404. while (req[i] == ' ')
  405. i++;
  406. method[0] = '\0';
  407. ctype[0] = '\0';
  408. /* "X <method> <body length> <content type or -> <url>":
  409. * any method, for the browser's fetch()/XHR. */
  410. if (!strncmp(req + i, "X ", 2)) {
  411. unsigned int k = 0;
  412. i += 2;
  413. while (req[i] && req[i] != ' ' &&
  414. k + 1 < sizeof(method))
  415. method[k++] = req[i++];
  416. method[k] = '\0';
  417. while (req[i] == ' ')
  418. i++;
  419. while (req[i] >= '0' && req[i] <= '9') {
  420. post_len = post_len * 10 +
  421. (unsigned int)(req[i] - '0');
  422. i++;
  423. }
  424. while (req[i] == ' ')
  425. i++;
  426. k = 0;
  427. while (req[i] && req[i] != ' ' &&
  428. k + 1 < sizeof(ctype))
  429. ctype[k++] = req[i++];
  430. ctype[k] = '\0';
  431. if (!strcmp(ctype, "-"))
  432. ctype[0] = '\0';
  433. while (req[i] == ' ')
  434. i++;
  435. is_post = post_len > 0 || strcmp(method, "GET");
  436. } else if (!strncmp(req + i, "POST ", 5)) {
  437. is_post = 1;
  438. i += 5;
  439. while (req[i] >= '0' && req[i] <= '9') {
  440. post_len = post_len * 10 +
  441. (unsigned int)(req[i] - '0');
  442. i++;
  443. }
  444. while (req[i] == ' ')
  445. i++;
  446. }
  447. while (rn > 0 && (req[rn - 1] == '\n' ||
  448. req[rn - 1] == '\r'))
  449. req[--rn] = '\0';
  450. url = (i < (unsigned int)rn) ? req + i : 0;
  451. }
  452. if (!url || !url[0] || seq == last_seq) {
  453. idle++;
  454. /* 10 ms keeps the next navigation's first request from
  455. * waiting on this poll; 1000 ticks is still ten idle
  456. * seconds before the helper releases the NIC. */
  457. u_sleep_ms(10);
  458. if (idle >= 1000)
  459. break;
  460. continue;
  461. }
  462. idle = 0;
  463. last_seq = seq;
  464. if (cap == 0 || cap > FETCH_MAX)
  465. cap = FETCH_MAX;
  466. if (is_post) {
  467. int pn;
  468. if (post_len > FETCH_POST_MAX)
  469. post_len = FETCH_POST_MAX;
  470. pn = u_syscall3(SYS_READ_FILE, (int)fp.post,
  471. (int)post, (int)FETCH_POST_MAX);
  472. if (pn < 0)
  473. pn = 0;
  474. if ((unsigned int)pn < post_len)
  475. post_len = (unsigned int)pn;
  476. } else {
  477. post_len = 0;
  478. }
  479. jar_load();
  480. memcpy(&jar_before, &fetch_jar, sizeof(jar_before));
  481. jar_changes = fetch_jar.changes;
  482. /* Every request: uhttp_init() after a POST clears h.jar, and a
  483. * jar-less request after the OIDC login POST drops the
  484. * interaction cookie ("authorization request has expired"). */
  485. h.jar = &fetch_jar;
  486. helper_fetch(&h, url, cap, method[0] ? method : 0, ctype,
  487. post_len ? (char *)post : 0, post_len,
  488. buf, &body_len, &body_trunc, &err, &times);
  489. if (fetch_jar.changes != jar_changes)
  490. jar_save();
  491. if (body_len)
  492. u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
  493. (int)body_len);
  494. write_meta(seq, 1, &h, err, body_len, body_trunc, &times);
  495. app_dec(done, &dn, seq, sizeof(done));
  496. u_syscall3(SYS_WRITE_FILE, (int)fp.done, (int)done, (int)dn);
  497. if (err || body_trunc || h.peer_close || is_post) {
  498. h.keepalive = 0;
  499. uhttp_close(&h);
  500. if (is_post) {
  501. uhttp_init(&h);
  502. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  503. h.keepalive = 1;
  504. } else {
  505. h.keepalive = 1;
  506. }
  507. }
  508. }
  509. h.keepalive = 0;
  510. uhttp_close(&h);
  511. free(buf);
  512. free(post);
  513. u_exit(0);
  514. }
  515. void _start(const char *arg)
  516. {
  517. static struct uhttp h;
  518. unsigned char buf[512];
  519. unsigned int total = 0, t0, t1 = 0, t2 = 0;
  520. int rv;
  521. if (arg && arg[0] == '_' && arg[1] == '_') {
  522. if (!strncmp(arg, "__fetch", 7)) {
  523. fetch_once();
  524. u_exit(0);
  525. }
  526. if (!strncmp(arg, "__serve", 7)) {
  527. fetch_serve(arg);
  528. u_exit(0);
  529. }
  530. }
  531. if (!arg || !arg[0]) {
  532. u_puts("curl: usage curl <http|https://host[:port]/[path]>\n");
  533. u_exit(1);
  534. }
  535. uhttp_init(&h);
  536. h.user_agent = "nebos-curl/0.1";
  537. u_puts("curl: GET ");
  538. u_puts(arg);
  539. u_putc('\n');
  540. t0 = u_ticks_ms();
  541. rv = uhttp_open(&h, arg, 0, &hooks);
  542. t1 = u_ticks_ms();
  543. if (rv != 0) {
  544. u_puts("curl: ");
  545. u_puts(uhttp_error_string(rv));
  546. u_putc('\n');
  547. uhttp_close(&h);
  548. u_exit(1);
  549. }
  550. if (h.url.secure)
  551. u_puts("curl: WARNING: certificate NOT verified "
  552. "(trust-all mode)\n");
  553. u_putc('\n');
  554. for (;;) {
  555. rv = uhttp_read(&h, buf, sizeof(buf), UHTTP_IDLE_TIMEOUT_MS);
  556. if (rv > 0) {
  557. int i;
  558. if (!t2)
  559. t2 = u_ticks_ms();
  560. for (i = 0; i < rv; i++)
  561. u_putc((char)buf[i]);
  562. total += (unsigned int)rv;
  563. continue;
  564. }
  565. if (rv < 0) {
  566. u_puts("\ncurl: ");
  567. u_puts(uhttp_error_string(rv));
  568. u_puts(" while reading body\n");
  569. }
  570. break;
  571. }
  572. u_putc('\n');
  573. u_puts("curl: ");
  574. u_putdec(total);
  575. u_puts(" bytes received, status ");
  576. u_putdec((unsigned int)h.resp.status);
  577. if (t2) {
  578. u_puts(", ");
  579. u_putdec(t2 - t0);
  580. u_puts(" ms to first byte");
  581. }
  582. u_puts(", ");
  583. u_putdec(u_ticks_ms() - t0);
  584. u_puts(" ms total");
  585. if (!t2 && t1) {
  586. u_puts(" (headers ");
  587. u_putdec(t1 - t0);
  588. u_puts(" ms)");
  589. }
  590. u_putc('\n');
  591. uhttp_close(&h);
  592. u_exit(0);
  593. }