ucmd_curl.c 12 KB

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