ucmd_curl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. #include "img.h"
  16. #define LINE_MAX_C 320
  17. static void on_connected(void *ctx, int secure)
  18. {
  19. (void)ctx;
  20. if (secure) {
  21. u_puts("curl: connected, TLS handshake\n");
  22. } else {
  23. u_puts("curl: connected, sending request\n");
  24. }
  25. }
  26. static void on_status(void *ctx, int code, const char *line)
  27. {
  28. (void)ctx;
  29. (void)code;
  30. u_puts("curl: ");
  31. u_puts(line);
  32. u_putc('\n');
  33. }
  34. static void on_header(void *ctx, const char *line)
  35. {
  36. (void)ctx;
  37. u_puts(line);
  38. u_putc('\n');
  39. }
  40. static void on_redirect(void *ctx, const char *location)
  41. {
  42. (void)ctx;
  43. u_puts("curl: redirect -> ");
  44. u_puts(location);
  45. u_putc('\n');
  46. }
  47. static const struct uhttp_hooks hooks = {
  48. .connected = on_connected,
  49. .status = on_status,
  50. .header = on_header,
  51. .redirect = on_redirect,
  52. };
  53. /* ---- headless fetch helper for the browser ---------------------------- */
  54. #define FETCH_PATH_MAX 32
  55. #define FETCH_POST_MAX 16384u
  56. #define FETCH_MAX (4u * 1024u * 1024u)
  57. /* Each browser worker gets its own set of /share files; slot -1 keeps the
  58. * historical single-helper names for the one-shot __fetch mode. */
  59. struct fetch_paths {
  60. char req[FETCH_PATH_MAX];
  61. char body[FETCH_PATH_MAX];
  62. char meta[FETCH_PATH_MAX];
  63. char done[FETCH_PATH_MAX];
  64. char post[FETCH_PATH_MAX];
  65. char px[FETCH_PATH_MAX];
  66. };
  67. static struct fetch_paths fp;
  68. static void app_ch(char *d, unsigned int *n, char c, unsigned int cap)
  69. {
  70. if (*n + 1 < cap)
  71. d[(*n)++] = c;
  72. d[*n] = '\0';
  73. }
  74. static void app_str(char *d, unsigned int *n, const char *s, unsigned int cap)
  75. {
  76. while (s && *s)
  77. app_ch(d, n, *s++, cap);
  78. }
  79. static void app_dec(char *d, unsigned int *n, unsigned int v, unsigned int cap)
  80. {
  81. char tmp[12];
  82. unsigned int i = 0, j;
  83. do {
  84. tmp[i++] = (char)('0' + v % 10);
  85. v /= 10;
  86. } while (v && i < sizeof(tmp));
  87. for (j = 0; j < i; j++)
  88. app_ch(d, n, tmp[i - 1 - j], cap);
  89. }
  90. static void fetch_path(char *out, unsigned int cap, const char *kind,
  91. unsigned int session, int slot)
  92. {
  93. unsigned int n = 0;
  94. app_str(out, &n, "/share/bf", cap);
  95. if (slot >= 0) {
  96. app_dec(out, &n, session, cap);
  97. app_ch(out, &n, '_', cap);
  98. app_dec(out, &n, (unsigned int)slot, cap);
  99. }
  100. app_str(out, &n, kind, cap);
  101. }
  102. static void fetch_paths_init(unsigned int session, int slot)
  103. {
  104. fetch_path(fp.req, sizeof(fp.req), "req", session, slot);
  105. fetch_path(fp.body, sizeof(fp.body), "body", session, slot);
  106. fetch_path(fp.meta, sizeof(fp.meta), "meta", session, slot);
  107. fetch_path(fp.done, sizeof(fp.done), "done", session, slot);
  108. fetch_path(fp.post, sizeof(fp.post), "post", session, slot);
  109. fetch_path(fp.px, sizeof(fp.px), "px", session, slot);
  110. }
  111. /* Optional decode in the fetch helper. The browser's UI thread only has
  112. * to copy the finished RGB565 bitmap; the JPEG IDCT/box filter runs in the
  113. * helper process (and several helpers can decode in parallel). */
  114. #define DECODE_ARENA_MAX (24u * 1024u * 1024u)
  115. #define DECODE_PX_MAX (8u * 1024u * 1024u)
  116. struct fetch_px {
  117. unsigned char *data;
  118. unsigned int len, dw, dh, nw, nh, alpha, format;
  119. unsigned int dec_ms;
  120. int err;
  121. };
  122. static void fetch_decode(const unsigned char *data, unsigned int len,
  123. unsigned int max_w, unsigned int max_h,
  124. struct fetch_px *out)
  125. {
  126. struct img_info info;
  127. struct img_options opt = {0};
  128. struct img_mem mem;
  129. struct img_image im;
  130. unsigned char *arena;
  131. unsigned int est, dw, dh, outlen, n;
  132. int r;
  133. memset(out, 0, sizeof(*out));
  134. if (img_sniff(data, len, &info) != 0) {
  135. out->err = 1;
  136. return;
  137. }
  138. est = img_estimate(&info);
  139. if (est == 0xFFFFFFFFu || est > DECODE_ARENA_MAX) {
  140. out->err = 2;
  141. return;
  142. }
  143. arena = (unsigned char *)malloc(est);
  144. if (!arena) {
  145. out->err = 3;
  146. return;
  147. }
  148. img_mem_init(&mem, arena, est);
  149. opt.max_pixels = 8u * 1024u * 1024u;
  150. opt.max_w = max_w;
  151. opt.max_h = max_h;
  152. r = img_decode(data, len, &opt, &mem, &im);
  153. if (r != 0 || !im.w || !im.h) {
  154. out->err = 4;
  155. free(arena);
  156. return;
  157. }
  158. dw = im.w;
  159. dh = im.h;
  160. if (max_w && dw > max_w) {
  161. dh = (unsigned int)((unsigned long long)dh * max_w / dw);
  162. dw = max_w;
  163. }
  164. if (max_h && dh > max_h) {
  165. dw = (unsigned int)((unsigned long long)dw * max_h / dh);
  166. dh = max_h;
  167. }
  168. if (!dw)
  169. dw = 1;
  170. if (!dh)
  171. dh = 1;
  172. if (dw > 4096 || dh > 4096) {
  173. out->err = 5;
  174. free(arena);
  175. return;
  176. }
  177. outlen = dw * dh * 2u + (im.alpha ? dw * dh : 0);
  178. if (outlen > DECODE_PX_MAX) {
  179. out->err = 5;
  180. free(arena);
  181. return;
  182. }
  183. out->data = (unsigned char *)malloc(outlen);
  184. if (!out->data) {
  185. out->err = 6;
  186. free(arena);
  187. return;
  188. }
  189. if (dw == im.w && dh == im.h) {
  190. n = dw * dh;
  191. memcpy(out->data, im.px, n * 2u);
  192. if (im.alpha)
  193. memcpy(out->data + n * 2u, im.alpha, n);
  194. } else {
  195. struct img_blit b;
  196. memset(&b, 0, sizeof(b));
  197. b.dst = (unsigned short *)out->data;
  198. b.dst_w = (int)dw;
  199. b.dst_h = (int)dh;
  200. b.clip_w = (int)dw;
  201. b.clip_h = (int)dh;
  202. b.scale = ((unsigned int)dw << 16) / im.w;
  203. b.dst_a = im.alpha ? out->data + dw * dh * 2u : 0;
  204. b.flags = im.alpha ? IMG_BLIT_SAVE_ALPHA : 0;
  205. img_draw(&im, &b);
  206. }
  207. out->len = outlen;
  208. out->dw = dw;
  209. out->dh = dh;
  210. out->nw = info.width;
  211. out->nh = info.height;
  212. out->alpha = im.alpha ? 1 : 0;
  213. out->format = info.format;
  214. free(arena);
  215. }
  216. static void fetch_fail(unsigned int status, int err, const char *ctype,
  217. const char *enc, const char *final)
  218. {
  219. static char meta[UHTTP_PATH_MAX + UHTTP_HOST_MAX + 256];
  220. unsigned int n = 0;
  221. app_dec(meta, &n, status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  222. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  223. app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  224. app_str(meta, &n, enc ? enc : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  225. app_str(meta, &n, ctype ? ctype : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  226. app_str(meta, &n, final ? final : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  227. app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  228. u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
  229. }
  230. /* One cookie jar for every helper of every browser window, kept in /share
  231. * (RAM, cleared at reboot) so a login session survives helper restarts and
  232. * the parallel workers. Re-read before each request and written back when
  233. * the response changed it; with several helpers the last writer wins. */
  234. #define FETCH_JAR_PATH "/share/bcookies"
  235. static char fetch_jar_path[64] = FETCH_JAR_PATH;
  236. static struct uhttp_jar fetch_jar;
  237. static void jar_load(void)
  238. {
  239. int n = u_syscall3(SYS_READ_FILE, (int)fetch_jar_path,
  240. (int)&fetch_jar, (int)sizeof(fetch_jar));
  241. if (n != (int)sizeof(fetch_jar))
  242. uhttp_jar_init(&fetch_jar);
  243. }
  244. /* The jar as loaded before this request, and a scratch copy of the file. */
  245. static struct uhttp_jar jar_before, jar_file;
  246. static const struct uhttp_cookie *jar_lookup(const struct uhttp_jar *jar,
  247. const struct uhttp_cookie *c)
  248. {
  249. int i;
  250. for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
  251. const struct uhttp_cookie *o = &jar->c[i];
  252. if (o->used && !strcmp(o->host, c->host) &&
  253. !strcmp(o->path, c->path) && !strcmp(o->name, c->name))
  254. return o;
  255. }
  256. return 0;
  257. }
  258. /* Replay one cookie into jar_file as a Set-Cookie line (or delete it). */
  259. static void jar_replay(const struct uhttp_cookie *c, int del)
  260. {
  261. static char line[UHTTP_COOKIE_NAME_MAX + UHTTP_COOKIE_VALUE_MAX +
  262. UHTTP_COOKIE_PATH_MAX + 32];
  263. unsigned int n = 0;
  264. app_str(line, &n, c->name, sizeof(line));
  265. app_ch(line, &n, '=', sizeof(line));
  266. if (!del)
  267. app_str(line, &n, c->value, sizeof(line));
  268. app_str(line, &n, "; Path=", sizeof(line));
  269. app_str(line, &n, c->path, sizeof(line));
  270. if (c->secure)
  271. app_str(line, &n, "; Secure", sizeof(line));
  272. if (del)
  273. app_str(line, &n, "; Max-Age=0", sizeof(line));
  274. (void)uhttp_jar_set(&jar_file, c->host, c->path, line);
  275. }
  276. /* Save this request's cookie changes. Other helpers may have saved while
  277. * the request ran, so the file is re-read and only what this request added,
  278. * changed or deleted is applied on top (writing our whole copy back would
  279. * drop their cookies). */
  280. static void jar_save(void)
  281. {
  282. int i, n;
  283. n = u_syscall3(SYS_READ_FILE, (int)fetch_jar_path, (int)&jar_file,
  284. (int)sizeof(jar_file));
  285. if (n != (int)sizeof(jar_file))
  286. uhttp_jar_init(&jar_file);
  287. for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
  288. const struct uhttp_cookie *c = &fetch_jar.c[i];
  289. const struct uhttp_cookie *o;
  290. if (!c->used)
  291. continue;
  292. o = jar_lookup(&jar_before, c);
  293. if (!o || strcmp(o->value, c->value) || o->secure != c->secure)
  294. jar_replay(c, 0);
  295. }
  296. for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
  297. const struct uhttp_cookie *o = &jar_before.c[i];
  298. if (o->used && !jar_lookup(&fetch_jar, o))
  299. jar_replay(o, 1);
  300. }
  301. u_syscall3(SYS_WRITE_FILE, (int)fetch_jar_path, (int)&jar_file,
  302. (int)sizeof(jar_file));
  303. }
  304. struct fetch_times {
  305. unsigned int open_ms;
  306. unsigned int first_ms;
  307. unsigned int total_ms;
  308. };
  309. static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
  310. int err, unsigned int body_len, unsigned int body_trunc,
  311. const struct fetch_times *t, const struct fetch_px *px)
  312. {
  313. static char meta[UHTTP_PATH_MAX + UHTTP_HOST_MAX + 1024];
  314. unsigned int n = 0;
  315. if (with_seq) {
  316. app_dec(meta, &n, seq, sizeof(meta));
  317. app_ch(meta, &n, '\n', sizeof(meta));
  318. }
  319. app_dec(meta, &n, (unsigned int)h->resp.status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  320. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  321. app_dec(meta, &n, body_trunc, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  322. app_str(meta, &n, h->resp.content_encoding, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  323. app_str(meta, &n, h->resp.content_type, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  324. app_str(meta, &n, h->resp.final_url, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  325. app_dec(meta, &n, body_len, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  326. app_dec(meta, &n, t ? t->open_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  327. app_dec(meta, &n, t ? t->first_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  328. app_dec(meta, &n, t ? t->total_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  329. /* Optional decoded bitmap: dw, dh, native w/h, alpha, px bytes, error,
  330. * format. */
  331. app_dec(meta, &n, px ? px->dw : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  332. app_dec(meta, &n, px ? px->dh : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  333. app_dec(meta, &n, px ? px->nw : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  334. app_dec(meta, &n, px ? px->nh : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  335. app_dec(meta, &n, px ? px->alpha : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  336. app_dec(meta, &n, px ? px->len : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  337. app_dec(meta, &n, px ? (unsigned int)px->err : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  338. app_dec(meta, &n, px ? px->dec_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  339. u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
  340. }
  341. /* Fetch one URL through a possibly reused keep-alive connection. */
  342. static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
  343. const char *method, const char *ctype,
  344. const char *post, unsigned int post_len,
  345. unsigned char *buf, unsigned int *outlen,
  346. unsigned int *outtrunc, int *outerr,
  347. struct fetch_times *times)
  348. {
  349. unsigned int total = 0;
  350. unsigned int t0 = u_ticks_ms(), t1, tfirst = 0;
  351. int rv, trunc = 0, err = 0;
  352. *outlen = 0;
  353. *outtrunc = 0;
  354. *outerr = 0;
  355. if (times) {
  356. times->open_ms = 0;
  357. times->first_ms = 0;
  358. times->total_ms = 0;
  359. }
  360. if (!method)
  361. method = post_len ? "POST" : 0;
  362. if (!ctype || !ctype[0])
  363. ctype = "application/x-www-form-urlencoded";
  364. uhttp_set_method(h, method, post, post_len, ctype);
  365. rv = uhttp_open(h, url, UHTTP_GZIP, 0);
  366. if (rv == UHTTP_ERR_BUSY) {
  367. int tries;
  368. for (tries = 0; tries < 50 && rv == UHTTP_ERR_BUSY; tries++) {
  369. u_sleep_ms(20);
  370. rv = uhttp_open(h, url, UHTTP_GZIP, 0);
  371. }
  372. }
  373. t1 = u_ticks_ms();
  374. if (times) {
  375. times->open_ms = t1 - t0;
  376. times->total_ms = t1 - t0;
  377. }
  378. if (rv != 0) {
  379. *outerr = rv;
  380. return;
  381. }
  382. for (;;) {
  383. rv = uhttp_read(h, buf + total, cap - total,
  384. UHTTP_IDLE_TIMEOUT_MS);
  385. if (rv > 0) {
  386. if (!tfirst)
  387. tfirst = u_ticks_ms();
  388. total += (unsigned int)rv;
  389. if (total >= cap) {
  390. trunc = 1;
  391. break;
  392. }
  393. continue;
  394. }
  395. if (rv < 0)
  396. err = rv;
  397. break;
  398. }
  399. *outlen = total;
  400. *outtrunc = (unsigned int)trunc;
  401. *outerr = err;
  402. if (times) {
  403. times->first_ms = tfirst ? tfirst - t0 : 0;
  404. times->total_ms = u_ticks_ms() - t0;
  405. if (!times->total_ms)
  406. times->total_ms = 1;
  407. }
  408. }
  409. static void fetch_once(void)
  410. {
  411. static struct uhttp h;
  412. static char req[UHTTP_PATH_MAX + UHTTP_HOST_MAX + 256];
  413. struct fetch_times times;
  414. fetch_paths_init(0, -1);
  415. unsigned char *buf;
  416. unsigned int cap = 0;
  417. unsigned int body_len = 0, body_trunc = 0;
  418. unsigned int i;
  419. int rn, err = 0;
  420. rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req,
  421. (int)(sizeof(req) - 1));
  422. if (rn <= 0)
  423. u_exit(1);
  424. req[rn] = '\0';
  425. i = 0;
  426. while (req[i] >= '0' && req[i] <= '9') {
  427. cap = cap * 10 + (unsigned int)(req[i] - '0');
  428. i++;
  429. }
  430. while (req[i] == ' ')
  431. i++;
  432. while (rn > 0 && (req[rn - 1] == '\n' || req[rn - 1] == '\r'))
  433. req[--rn] = '\0';
  434. if (i >= (unsigned int)rn || !req[i]) {
  435. fetch_fail(0, -1, "", "", "");
  436. u_exit(1);
  437. }
  438. if (cap == 0 || cap > FETCH_MAX)
  439. cap = FETCH_MAX;
  440. buf = (unsigned char *)malloc(FETCH_MAX);
  441. if (!buf) {
  442. fetch_fail(0, -1, "", "", "");
  443. u_exit(1);
  444. }
  445. uhttp_init(&h);
  446. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  447. helper_fetch(&h, req + i, cap, 0, 0, 0, 0, buf, &body_len, &body_trunc, &err,
  448. &times);
  449. if (body_len)
  450. u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
  451. (int)body_len);
  452. write_meta(0, 0, &h, err, body_len, body_trunc, &times, 0);
  453. uhttp_close(&h);
  454. free(buf);
  455. u_exit(err ? 1 : 0);
  456. }
  457. /* Long-lived helper: the browser writes a numbered request, we fetch it
  458. * (reusing the same TCP/TLS connection whenever the host is unchanged) and
  459. * publish body + meta + a small done marker. Idle instances exit and
  460. * release the NIC after ten seconds. */
  461. static void fetch_serve(const char *arg)
  462. {
  463. static struct uhttp h;
  464. static char req[UHTTP_PATH_MAX + UHTTP_HOST_MAX + 256];
  465. struct fetch_times times;
  466. unsigned char *buf, *post;
  467. unsigned int last_seq = 0, idle = 0, session = 0;
  468. int slot = -1;
  469. while (arg && *arg == ' ')
  470. arg++;
  471. if (arg && !strncmp(arg, "__serve", 7))
  472. arg += 7;
  473. while (arg && *arg == ' ')
  474. arg++;
  475. if (arg && *arg >= '0' && *arg <= '9') {
  476. while (*arg >= '0' && *arg <= '9')
  477. session = session * 10 + (*arg++ - '0');
  478. while (*arg == ' ')
  479. arg++;
  480. if (*arg >= '0' && *arg <= '9') {
  481. slot = 0;
  482. while (*arg >= '0' && *arg <= '9')
  483. slot = slot * 10 + (*arg++ - '0');
  484. }
  485. }
  486. while (*arg == ' ')
  487. arg++;
  488. if (*arg == '/') {
  489. unsigned int n = 0;
  490. while (*arg && *arg != ' ' && n + 1 < sizeof(fetch_jar_path))
  491. fetch_jar_path[n++] = *arg++;
  492. fetch_jar_path[n] = '\0';
  493. }
  494. fetch_paths_init(session, slot);
  495. buf = (unsigned char *)malloc(FETCH_MAX);
  496. post = (unsigned char *)malloc(FETCH_POST_MAX);
  497. if (!buf || !post)
  498. u_exit(1);
  499. uhttp_init(&h);
  500. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  501. h.keepalive = 1;
  502. h.jar = &fetch_jar;
  503. for (;;) {
  504. unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
  505. unsigned int post_len = 0, jar_changes;
  506. unsigned int dec_w = 0, dec_h = 0;
  507. int decode = 0;
  508. int is_post = 0;
  509. struct fetch_px px;
  510. char method[12], ctype[96];
  511. method[0] = '\0';
  512. ctype[0] = '\0';
  513. char done[16];
  514. unsigned int dn = 0;
  515. unsigned int i = 0;
  516. char *url;
  517. int rn, err = 0;
  518. rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req,
  519. (int)(sizeof(req) - 1));
  520. if (rn <= 0) {
  521. url = 0;
  522. } else {
  523. req[rn] = '\0';
  524. while (req[i] >= '0' && req[i] <= '9') {
  525. seq = seq * 10 + (unsigned int)(req[i] - '0');
  526. i++;
  527. }
  528. while (req[i] == ' ')
  529. i++;
  530. while (req[i] >= '0' && req[i] <= '9') {
  531. cap = cap * 10 + (unsigned int)(req[i] - '0');
  532. i++;
  533. }
  534. while (req[i] == ' ')
  535. i++;
  536. method[0] = '\0';
  537. ctype[0] = '\0';
  538. /* "<seq> <cap> D <max_w> <max_h> <url>": decode the
  539. * response at the display bounds before publishing it. */
  540. if (!strncmp(req + i, "D ", 2)) {
  541. i += 2;
  542. while (req[i] == ' ')
  543. i++;
  544. while (req[i] >= '0' && req[i] <= '9') {
  545. dec_w = dec_w * 10 +
  546. (unsigned int)(req[i] - '0');
  547. i++;
  548. }
  549. while (req[i] == ' ')
  550. i++;
  551. while (req[i] >= '0' && req[i] <= '9') {
  552. dec_h = dec_h * 10 +
  553. (unsigned int)(req[i] - '0');
  554. i++;
  555. }
  556. while (req[i] == ' ')
  557. i++;
  558. decode = 1;
  559. }
  560. /* "X <method> <body length> <content type or -> <url>":
  561. * any method, for the browser's fetch()/XHR. */
  562. if (!decode && !strncmp(req + i, "X ", 2)) {
  563. unsigned int k = 0;
  564. i += 2;
  565. while (req[i] && req[i] != ' ' &&
  566. k + 1 < sizeof(method))
  567. method[k++] = req[i++];
  568. method[k] = '\0';
  569. while (req[i] == ' ')
  570. i++;
  571. while (req[i] >= '0' && req[i] <= '9') {
  572. post_len = post_len * 10 +
  573. (unsigned int)(req[i] - '0');
  574. i++;
  575. }
  576. while (req[i] == ' ')
  577. i++;
  578. k = 0;
  579. while (req[i] && req[i] != ' ' &&
  580. k + 1 < sizeof(ctype))
  581. ctype[k++] = req[i++];
  582. ctype[k] = '\0';
  583. if (!strcmp(ctype, "-"))
  584. ctype[0] = '\0';
  585. while (req[i] == ' ')
  586. i++;
  587. is_post = post_len > 0 || strcmp(method, "GET");
  588. } else if (!strncmp(req + i, "POST ", 5)) {
  589. is_post = 1;
  590. i += 5;
  591. while (req[i] >= '0' && req[i] <= '9') {
  592. post_len = post_len * 10 +
  593. (unsigned int)(req[i] - '0');
  594. i++;
  595. }
  596. while (req[i] == ' ')
  597. i++;
  598. }
  599. while (rn > 0 && (req[rn - 1] == '\n' ||
  600. req[rn - 1] == '\r'))
  601. req[--rn] = '\0';
  602. url = (i < (unsigned int)rn) ? req + i : 0;
  603. }
  604. if (!url || !url[0] || seq == last_seq) {
  605. idle++;
  606. /* 10 ms keeps the next navigation's first request from
  607. * waiting on this poll; 1000 ticks is still ten idle
  608. * seconds before the helper releases the NIC. */
  609. u_sleep_ms(10);
  610. if (idle >= 1000)
  611. break;
  612. continue;
  613. }
  614. idle = 0;
  615. last_seq = seq;
  616. if (cap == 0 || cap > FETCH_MAX)
  617. cap = FETCH_MAX;
  618. if (is_post) {
  619. int pn;
  620. if (post_len > FETCH_POST_MAX)
  621. post_len = FETCH_POST_MAX;
  622. pn = u_syscall3(SYS_READ_FILE, (int)fp.post,
  623. (int)post, (int)FETCH_POST_MAX);
  624. if (pn < 0)
  625. pn = 0;
  626. if ((unsigned int)pn < post_len)
  627. post_len = (unsigned int)pn;
  628. } else {
  629. post_len = 0;
  630. }
  631. jar_load();
  632. memcpy(&jar_before, &fetch_jar, sizeof(jar_before));
  633. jar_changes = fetch_jar.changes;
  634. /* Every request: uhttp_init() after a POST clears h.jar, and a
  635. * jar-less request after the OIDC login POST drops the
  636. * interaction cookie ("authorization request has expired"). */
  637. h.jar = &fetch_jar;
  638. helper_fetch(&h, url, cap, method[0] ? method : 0, ctype,
  639. post_len ? (char *)post : 0, post_len,
  640. buf, &body_len, &body_trunc, &err, &times);
  641. if (fetch_jar.changes != jar_changes)
  642. jar_save();
  643. if (body_len)
  644. u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
  645. (int)body_len);
  646. memset(&px, 0, sizeof(px));
  647. if (decode) {
  648. u_syscall1(SYS_UNLINK, (int)fp.px);
  649. if (body_len && !body_trunc && !err &&
  650. h.resp.status == 200 && !is_post) {
  651. unsigned int td = u_ticks_ms();
  652. fetch_decode(buf, body_len, dec_w, dec_h, &px);
  653. px.dec_ms = u_ticks_ms() - td;
  654. if (!px.err && px.data && px.len)
  655. u_syscall3(SYS_WRITE_FILE, (int)fp.px,
  656. (int)px.data, (int)px.len);
  657. }
  658. }
  659. write_meta(seq, 1, &h, err, body_len, body_trunc, &times, &px);
  660. if (px.data)
  661. free(px.data);
  662. app_dec(done, &dn, seq, sizeof(done));
  663. u_syscall3(SYS_WRITE_FILE, (int)fp.done, (int)done, (int)dn);
  664. if (err || body_trunc || h.peer_close || is_post) {
  665. h.keepalive = 0;
  666. uhttp_close(&h);
  667. if (is_post) {
  668. uhttp_init(&h);
  669. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  670. h.keepalive = 1;
  671. } else {
  672. h.keepalive = 1;
  673. }
  674. }
  675. }
  676. h.keepalive = 0;
  677. uhttp_close(&h);
  678. free(buf);
  679. free(post);
  680. u_exit(0);
  681. }
  682. void _start(const char *arg)
  683. {
  684. static struct uhttp h;
  685. unsigned char buf[512];
  686. unsigned int total = 0, t0, t1 = 0, t2 = 0;
  687. int rv;
  688. if (arg && arg[0] == '_' && arg[1] == '_') {
  689. if (!strncmp(arg, "__fetch", 7)) {
  690. fetch_once();
  691. u_exit(0);
  692. }
  693. if (!strncmp(arg, "__serve", 7)) {
  694. fetch_serve(arg);
  695. u_exit(0);
  696. }
  697. }
  698. if (!arg || !arg[0]) {
  699. u_puts("curl: usage curl <http|https://host[:port]/[path]>\n");
  700. u_exit(1);
  701. }
  702. uhttp_init(&h);
  703. h.user_agent = "nebos-curl/0.1";
  704. u_puts("curl: GET ");
  705. u_puts(arg);
  706. u_putc('\n');
  707. t0 = u_ticks_ms();
  708. rv = uhttp_open(&h, arg, 0, &hooks);
  709. t1 = u_ticks_ms();
  710. if (rv != 0) {
  711. u_puts("curl: ");
  712. u_puts(uhttp_error_string(rv));
  713. u_putc('\n');
  714. uhttp_close(&h);
  715. u_exit(1);
  716. }
  717. if (h.url.secure)
  718. u_puts("curl: WARNING: certificate NOT verified "
  719. "(trust-all mode)\n");
  720. u_putc('\n');
  721. for (;;) {
  722. rv = uhttp_read(&h, buf, sizeof(buf), UHTTP_IDLE_TIMEOUT_MS);
  723. if (rv > 0) {
  724. int i;
  725. if (!t2)
  726. t2 = u_ticks_ms();
  727. for (i = 0; i < rv; i++)
  728. u_putc((char)buf[i]);
  729. total += (unsigned int)rv;
  730. continue;
  731. }
  732. if (rv < 0) {
  733. u_puts("\ncurl: ");
  734. u_puts(uhttp_error_string(rv));
  735. u_puts(" while reading body\n");
  736. }
  737. break;
  738. }
  739. u_putc('\n');
  740. u_puts("curl: ");
  741. u_putdec(total);
  742. u_puts(" bytes received, status ");
  743. u_putdec((unsigned int)h.resp.status);
  744. if (t2) {
  745. u_puts(", ");
  746. u_putdec(t2 - t0);
  747. u_puts(" ms to first byte");
  748. }
  749. u_puts(", ");
  750. u_putdec(u_ticks_ms() - t0);
  751. u_puts(" ms total");
  752. if (!t2 && t1) {
  753. u_puts(" (headers ");
  754. u_putdec(t1 - t0);
  755. u_puts(" ms)");
  756. }
  757. u_putc('\n');
  758. uhttp_close(&h);
  759. u_exit(0);
  760. }