ucmd_curl.c 24 KB

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