ucmd_curl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
  122. int err, unsigned int body_len, unsigned int body_trunc)
  123. {
  124. char meta[1024];
  125. unsigned int n = 0;
  126. if (with_seq) {
  127. app_dec(meta, &n, seq, sizeof(meta));
  128. app_ch(meta, &n, '\n', sizeof(meta));
  129. }
  130. app_dec(meta, &n, (unsigned int)h->resp.status, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  131. app_dec(meta, &n, (unsigned int)err, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  132. app_dec(meta, &n, body_trunc, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  133. app_str(meta, &n, h->resp.content_encoding, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  134. app_str(meta, &n, h->resp.content_type, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  135. app_str(meta, &n, h->resp.final_url, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  136. app_dec(meta, &n, body_len, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
  137. u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
  138. }
  139. /* Fetch one URL through a possibly reused keep-alive connection. */
  140. static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
  141. const char *post, unsigned int post_len,
  142. unsigned char *buf, unsigned int *outlen,
  143. unsigned int *outtrunc, int *outerr)
  144. {
  145. unsigned int total = 0;
  146. int rv, trunc = 0, err = 0;
  147. *outlen = 0;
  148. *outtrunc = 0;
  149. *outerr = 0;
  150. uhttp_set_method(h, post_len ? "POST" : 0, post, post_len,
  151. "application/x-www-form-urlencoded");
  152. rv = uhttp_open(h, url, UHTTP_GZIP, 0);
  153. if (rv == UHTTP_ERR_BUSY) {
  154. int tries;
  155. for (tries = 0; tries < 50 && rv == UHTTP_ERR_BUSY; tries++) {
  156. u_sleep_ms(20);
  157. rv = uhttp_open(h, url, UHTTP_GZIP, 0);
  158. }
  159. }
  160. if (rv != 0) {
  161. *outerr = rv;
  162. return;
  163. }
  164. for (;;) {
  165. rv = uhttp_read(h, buf + total, cap - total,
  166. UHTTP_IDLE_TIMEOUT_MS);
  167. if (rv > 0) {
  168. total += (unsigned int)rv;
  169. if (total >= cap) {
  170. trunc = 1;
  171. break;
  172. }
  173. continue;
  174. }
  175. if (rv < 0)
  176. err = rv;
  177. break;
  178. }
  179. *outlen = total;
  180. *outtrunc = (unsigned int)trunc;
  181. *outerr = err;
  182. }
  183. static void fetch_once(void)
  184. {
  185. static struct uhttp h;
  186. char req[768];
  187. fetch_paths_init(0, -1);
  188. unsigned char *buf;
  189. unsigned int cap = 0;
  190. unsigned int body_len = 0, body_trunc = 0;
  191. unsigned int i;
  192. int rn, err = 0;
  193. rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req,
  194. (int)(sizeof(req) - 1));
  195. if (rn <= 0)
  196. u_exit(1);
  197. req[rn] = '\0';
  198. i = 0;
  199. while (req[i] >= '0' && req[i] <= '9') {
  200. cap = cap * 10 + (unsigned int)(req[i] - '0');
  201. i++;
  202. }
  203. while (req[i] == ' ')
  204. i++;
  205. while (rn > 0 && (req[rn - 1] == '\n' || req[rn - 1] == '\r'))
  206. req[--rn] = '\0';
  207. if (i >= (unsigned int)rn || !req[i]) {
  208. fetch_fail(0, -1, "", "", "");
  209. u_exit(1);
  210. }
  211. if (cap == 0 || cap > FETCH_MAX)
  212. cap = FETCH_MAX;
  213. buf = (unsigned char *)malloc(FETCH_MAX);
  214. if (!buf) {
  215. fetch_fail(0, -1, "", "", "");
  216. u_exit(1);
  217. }
  218. uhttp_init(&h);
  219. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  220. helper_fetch(&h, req + i, cap, 0, 0, buf, &body_len, &body_trunc, &err);
  221. if (body_len)
  222. u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
  223. (int)body_len);
  224. write_meta(0, 0, &h, err, body_len, body_trunc);
  225. uhttp_close(&h);
  226. free(buf);
  227. u_exit(err ? 1 : 0);
  228. }
  229. /* Long-lived helper: the browser writes a numbered request, we fetch it
  230. * (reusing the same TCP/TLS connection whenever the host is unchanged) and
  231. * publish body + meta + a small done marker. Idle instances exit and
  232. * release the NIC after ten seconds. */
  233. static void fetch_serve(const char *arg)
  234. {
  235. static struct uhttp h;
  236. char req[768];
  237. unsigned char *buf, *post;
  238. unsigned int last_seq = 0, idle = 0, session = 0;
  239. int slot = -1;
  240. while (arg && *arg == ' ')
  241. arg++;
  242. if (arg && !strncmp(arg, "__serve", 7))
  243. arg += 7;
  244. while (arg && *arg == ' ')
  245. arg++;
  246. if (arg && *arg >= '0' && *arg <= '9') {
  247. while (*arg >= '0' && *arg <= '9')
  248. session = session * 10 + (*arg++ - '0');
  249. while (*arg == ' ')
  250. arg++;
  251. if (*arg >= '0' && *arg <= '9') {
  252. slot = 0;
  253. while (*arg >= '0' && *arg <= '9')
  254. slot = slot * 10 + (*arg++ - '0');
  255. }
  256. }
  257. fetch_paths_init(session, slot);
  258. buf = (unsigned char *)malloc(FETCH_MAX);
  259. post = (unsigned char *)malloc(FETCH_POST_MAX);
  260. if (!buf || !post)
  261. u_exit(1);
  262. uhttp_init(&h);
  263. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  264. h.keepalive = 1;
  265. for (;;) {
  266. unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
  267. unsigned int post_len = 0;
  268. int is_post = 0;
  269. char done[16];
  270. unsigned int dn = 0;
  271. unsigned int i = 0;
  272. char *url;
  273. int rn, err = 0;
  274. rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req,
  275. (int)(sizeof(req) - 1));
  276. if (rn <= 0) {
  277. url = 0;
  278. } else {
  279. req[rn] = '\0';
  280. while (req[i] >= '0' && req[i] <= '9') {
  281. seq = seq * 10 + (unsigned int)(req[i] - '0');
  282. i++;
  283. }
  284. while (req[i] == ' ')
  285. i++;
  286. while (req[i] >= '0' && req[i] <= '9') {
  287. cap = cap * 10 + (unsigned int)(req[i] - '0');
  288. i++;
  289. }
  290. while (req[i] == ' ')
  291. i++;
  292. if (!strncmp(req + i, "POST ", 5)) {
  293. is_post = 1;
  294. i += 5;
  295. while (req[i] >= '0' && req[i] <= '9') {
  296. post_len = post_len * 10 +
  297. (unsigned int)(req[i] - '0');
  298. i++;
  299. }
  300. while (req[i] == ' ')
  301. i++;
  302. }
  303. while (rn > 0 && (req[rn - 1] == '\n' ||
  304. req[rn - 1] == '\r'))
  305. req[--rn] = '\0';
  306. url = (i < (unsigned int)rn) ? req + i : 0;
  307. }
  308. if (!url || !url[0] || seq == last_seq) {
  309. idle++;
  310. u_sleep_ms(50);
  311. if (idle >= 200)
  312. break;
  313. continue;
  314. }
  315. idle = 0;
  316. last_seq = seq;
  317. if (cap == 0 || cap > FETCH_MAX)
  318. cap = FETCH_MAX;
  319. if (is_post) {
  320. int pn;
  321. if (post_len > FETCH_POST_MAX)
  322. post_len = FETCH_POST_MAX;
  323. pn = u_syscall3(SYS_READ_FILE, (int)fp.post,
  324. (int)post, (int)FETCH_POST_MAX);
  325. if (pn < 0)
  326. pn = 0;
  327. if ((unsigned int)pn < post_len)
  328. post_len = (unsigned int)pn;
  329. } else {
  330. post_len = 0;
  331. }
  332. helper_fetch(&h, url, cap, is_post ? (char *)post : 0, post_len,
  333. buf, &body_len, &body_trunc, &err);
  334. if (body_len)
  335. u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
  336. (int)body_len);
  337. write_meta(seq, 1, &h, err, body_len, body_trunc);
  338. app_dec(done, &dn, seq, sizeof(done));
  339. u_syscall3(SYS_WRITE_FILE, (int)fp.done, (int)done, (int)dn);
  340. if (err || body_trunc || h.peer_close || is_post) {
  341. h.keepalive = 0;
  342. uhttp_close(&h);
  343. if (is_post) {
  344. uhttp_init(&h);
  345. h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
  346. h.keepalive = 1;
  347. } else {
  348. h.keepalive = 1;
  349. }
  350. }
  351. }
  352. h.keepalive = 0;
  353. uhttp_close(&h);
  354. free(buf);
  355. free(post);
  356. u_exit(0);
  357. }
  358. void _start(const char *arg)
  359. {
  360. static struct uhttp h;
  361. unsigned char buf[512];
  362. unsigned int total = 0;
  363. int rv;
  364. if (arg && arg[0] == '_' && arg[1] == '_') {
  365. if (!strncmp(arg, "__fetch", 7)) {
  366. fetch_once();
  367. u_exit(0);
  368. }
  369. if (!strncmp(arg, "__serve", 7)) {
  370. fetch_serve(arg);
  371. u_exit(0);
  372. }
  373. }
  374. if (!arg || !arg[0]) {
  375. u_puts("curl: usage curl <http|https://host[:port]/[path]>\n");
  376. u_exit(1);
  377. }
  378. uhttp_init(&h);
  379. h.user_agent = "nebos-curl/0.1";
  380. u_puts("curl: GET ");
  381. u_puts(arg);
  382. u_putc('\n');
  383. rv = uhttp_open(&h, arg, 0, &hooks);
  384. if (rv != 0) {
  385. u_puts("curl: ");
  386. u_puts(uhttp_error_string(rv));
  387. u_putc('\n');
  388. uhttp_close(&h);
  389. u_exit(1);
  390. }
  391. if (h.url.secure)
  392. u_puts("curl: WARNING: certificate NOT verified "
  393. "(trust-all mode)\n");
  394. u_putc('\n');
  395. for (;;) {
  396. rv = uhttp_read(&h, buf, sizeof(buf), UHTTP_IDLE_TIMEOUT_MS);
  397. if (rv > 0) {
  398. int i;
  399. for (i = 0; i < rv; i++)
  400. u_putc((char)buf[i]);
  401. total += (unsigned int)rv;
  402. continue;
  403. }
  404. if (rv < 0) {
  405. u_puts("\ncurl: ");
  406. u_puts(uhttp_error_string(rv));
  407. u_puts(" while reading body\n");
  408. }
  409. break;
  410. }
  411. u_putc('\n');
  412. u_puts("curl: ");
  413. u_putdec(total);
  414. u_puts(" bytes received, status ");
  415. u_putdec((unsigned int)h.resp.status);
  416. u_putc('\n');
  417. uhttp_close(&h);
  418. u_exit(0);
  419. }